【问题标题】:How can I transform a string with form of a list into a tensor?如何将具有列表形式的字符串转换为张量?
【发布时间】:2019-08-03 00:05:41
【问题描述】:

当我读取带有下一个入口的 csv 文件时 2,3,"['动作','儿童']" 我想将第三个入口读取为带有两个字符串的张量,但是当我使用

tf.decode_csv('2,3,"['Action','Children']" ',[0,0,""])

当前结果是:

b"['Action','Children']"

我怎样才能得到结果:

tf.string ["Action","Children"] shape=(1,)

我想将其用作cathegorical_features

【问题讨论】:

  • 在tensorflow中可以使用tf.compat.as_str_any将字节转为字符串
  • “Action”和“children”的存在暗示了(至少)两个特征。也许您可以澄清您是否尝试单独映射这些(因此每条记录都有 2 个新功能,一个用于操作,一个用于儿童)或者您是否尝试将其映射为“交叉功能” - 一个新列,但每个可能的值组合是您的分类特征的一个独特的可能类别
  • @Stewart 我想将张量b"['Action','Children']" 转换为张量[b'Action',b'Children'],因为我注意到第二个与tf.feature_column.categorical_column_with_vocabulary_list() 配合得很好。

标签: python string list tensorflow categorical-data


【解决方案1】:

我是这样解决的:

CSV_COLUMNS=["Int1","Int2","genre"]
DEFAULTS=[0,0,["Unknown"]]
columns = tf.decode_csv('2,3,"['Action','Children']" ', record_defaults=DEFAULTS)
features = dict(zip(CSV_COLUMNS, columns))
features["genre"]=tf.regex_replace(features["genre"],pattern="\[",rewrite="")
features["genre"]=tf.regex_replace(features["genre"],pattern="\]",rewrite="")
features["genre"]=tf.regex_replace(features["genre"],pattern="\'",rewrite="")           
features["genre"]=tf.strings.split(features["genre"],sep=",",result_type="RaggedTensor")

【讨论】:

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