2017-06-15 250 views

回答

0

这段代码有效。它只是再次解码你的行的第一个2个值(但它假定你的所有行都有相同的格式:长度为2和整数的2个列表)。

line = tf.Variable('1,2-3,4-0') 
line = tf.decode_csv(line, record_defaults=[['1'], ['1'], [1]], field_delim='-') 

# Let us decode the first and the second values of the line 
x = tf.decode_csv(line[0], record_defaults=[[1], [1]], field_delim=',') 
y = tf.decode_csv(line[1], record_defaults=[[1], [1]], field_delim=',') 

# Let us run the previous ops 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    print(sess.run(x))  # print [1, 2] 
    print(sess.run(y))  # print [3, 4] 
    print(sess.run(line[2])) # print 0 

编辑:根据评论,

你可以尝试tf.string_split

def decode(line): 
    line = tf.decode_csv(line, record_defaults=[['1'], ['1'], [1]], field_delim='-') 

    x = tf.string_split(line[:1], ",").values 
    y = tf.string_split(line[1:2], ",").values 

    x = tf.string_to_number(x, tf.int32) 
    y = tf.string_to_number(y, tf.int32) 

    return x, y, line[2] 

line = tf.Variable('12-3,4-0') 
x, y, z = decode(line) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    print(sess.run(x)) # print [12] 
    print(sess.run(y)) # print [3 4] 
    print(sess.run(z)) # print 0 

EDIT2:简体decode FUNC。

+0

前2列不固定长度,可能是'1,2-3,4-5'或'1-2,3-1' – danche

+0

谢谢,它的工作原理。使用'x = tf.string_split(line [:1],“,”)'可以获取SparseTensor中的一列值。但是不够直接〜 – danche

+0

它在我的终端工作良好。但是当我运行包含上面代码的脚本python文件时,python会崩溃...... @ Nicolas – danche

相关问题