【问题标题】:How can I transform a string to a list in tensorflow?如何将字符串转换为张量流中的列表?
【发布时间】:2019-12-11 09:56:50
【问题描述】:

我在一行 csv 中有一些内容为 "1,2,'["Action","Childre"]'" 我想解码用户 tensorflow 以读取第一个入口作为 int 和最后一个作为张量。

CSV_COLUMNS=["movieId","userId","genre"]

LABEL_COLUMN="genre"

columns = tf.decode_csv('2,3,\"[\'Action,Children\']\"',record_defaults= [0.0,0.0,""])

features = dict(zip(CSV_COLUMNS, columns))

label = features.pop(LABEL_COLUMN)

with tf.Session() as sess:
    sess.run(features)
    sess.run(label)

结果是 {'movieId':2.0,'userId':3.0} b"['hola']"

但我希望最后的结果显示 数组(["hola"])

有什么想法吗?

【问题讨论】:

    标签: string list tensorflow


    【解决方案1】:

    您可以使用tf.strings.substr 从字符串中去除[''],然后使用tf.string_split 将字符串拆分为一个数组。 请注意,输出是SparseTensor

    label = tf.strings.substr(label, 2, tf.strings.length(label) - 4)
    splitted = tf.string_split(tf.expand_dims(label, 0), ',')
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 2017-11-20
      • 2021-01-04
      • 2023-03-04
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多