Skip to content

Instantly share code, notes, and snippets.

@danielwatson6
Created July 31, 2018 08:43
Show Gist options
  • Save danielwatson6/574d611b7cc8cf6fadb22448966d8b17 to your computer and use it in GitHub Desktop.
Save danielwatson6/574d611b7cc8cf6fadb22448966d8b17 to your computer and use it in GitHub Desktop.
def split_by_delimiter(ts, delimiter):
"""Split a numeric tensor similarly to python's `str.split` method."""
ts_str = tf.reshape(tf.reduce_join(tf.as_string(ts), separator=' '), [-1])
ts_slices = tf.string_split(
tf.string_split(ts_str, delimiter=str(delimiter)).values)
result = tf.SparseTensor(
ts_slices.indices,
tf.string_to_number(ts_slices.values, out_type=ts.dtype),
ts_slices.dense_shape)
return tf.sparse_tensor_to_dense(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment