如何将字符串张量转换为张量浮点列表?

1 casting shape reshape tensorflow tensor

所以,我的代码就像

    parsed_line = tf.decode_csv(line, [[0], [0], [""]])
    print(parsed_line[0])
    del parsed_line[0]
    del parsed_line[0]
    features = parsed_line
    print(parsed_line[0])
Run Code Online (Sandbox Code Playgroud)

那么结果是

    [<tf.Tensor 'DecodeCSV:0' shape=() dtype=int32>, <tf.Tensor 'DecodeCSV:1' shape=() dtype=int32>, <tf.Tensor 'DecodeCSV:2' shape=() dtype=string>]
Run Code Online (Sandbox Code Playgroud)

    [<Tensor("DecodeCSV:2", shape=(), dtype=string)>]
Run Code Online (Sandbox Code Playgroud)

我会给这个解码函数的 csv 是

    1, 0, 0101010010101010101010
Run Code Online (Sandbox Code Playgroud)

我想要这个“0101010010101010101010”

    [0,1,0,1,0,.........] 
Run Code Online (Sandbox Code Playgroud)

在张量流中

    [<Tensor("DecodeCSV:2", shape=(), dtype=string)>]
Run Code Online (Sandbox Code Playgroud)

    [<tf.Tensor 'DecodeCSV:0' shape=() dtype=int32>, <tf.Tensor 'DecodeCSV:1' shape=() dtype=int32>, ............]
Run Code Online (Sandbox Code Playgroud)

你有什么想法吗?

pfm*_*pfm 5

您可以使用tf.string_splitand 以这种方式执行此操作tf.string_to_number

import tensorflow as tf

line = tf.constant("1000111101", shape=(1,))
b = tf.string_split(line, delimiter="").values
c = tf.string_to_number(b, tf.int32)
print(c)
with tf.Session() as sess:
    print(sess.run(c))
Run Code Online (Sandbox Code Playgroud)

[1 0 0 0 1 1 1 1 0 1]