tensorflow:根据某些分隔符

时间:2018-04-28 07:14:41

标签: tensorflow

说我有张力:

ts = tf.constant([1,2,3,-1,3,4,5,-1,2])

如何将张量分割为张量列表,并以-1作为分隔符并获取以下内容

tf.constant([1,2,3])
tf.constant([3,4,5])
tf.constant([1,2])

3 个答案:

答案 0 :(得分:1)

您可以将常量转换为字符串,然后将它们作为字符串拆分为您想要的切片,然后您可以将它们转换回数字列表:

这是切片步骤:

import tensorflow as tf

ts = tf.constant([1,2,3,-1,3,4,5,-1,2])
ts_slices = 
    tf.string_split(tf.reshape(tf.reduce_join(tf.as_string(ts)), 
    [-1]), delimiter='-1')
sess = tf.Session()
sess.run(ts_slices.values)

这会给你:

array([b'23', b'345', b'2'], dtype=object)

现在您可以再次转换为整数。

我不确定这是解决这个问题的最佳解决方案,但至少它会解决您的问题。

答案 1 :(得分:0)

部分解决方案,您必须提前指定块数。希望这仍然有帮助。

代码(已测试):

import tensorflow as tf

ts = tf.constant([ 1, 2, 3, -1, 3, 4, 5, -1, 1, 2 ] )
delimiter = tf.constant( [ -1 ] )

left_padded = tf.concat( [ delimiter, ts ], axis = 0 )
padded = tf.concat( [ delimiter, ts, delimiter ], axis = 0 )
begin = tf.squeeze( tf.where( tf.equal( padded, delimiter ) ), axis = -1 ) # get position of -1s
sizes = begin[ 1 : ] - begin[ : -1 ] # get sizes of chunks
chunks = tf.split( left_padded, sizes, num = 3 ) # have to pre-specify number of chunks
for i in xrange( len( chunks ) ):
    chunks[ i ] = chunks[ i ][ 1 : ] # get rid of leading -1s

with tf.Session() as sess:
    print( sess.run( [ chunks ] ) )

输出:

  

[[array([1,2,3],dtype = int32),
  数组([3,4,5],dtype = int32),
  数组([1,2],dtype = int32)]]

答案 2 :(得分:0)

借用Sabine Maennel的回答,这是一个完全包含的解决方案:

import tensorflow as tf

def split_by_delimiter(ts, delimiter):
    """Split a 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=tf.int64),
        ts_slices.dense_shape)

    return tf.sparse_tensor_to_dense(result)