如何将列表形式的字符串转换为张量?

2024-09-30 16:33:05 发布

您现在位置:Python中文网/ 问答频道 /正文

当我读到下一个入口的csv文件时 2,3,“[‘Action’,‘Children’]” 我想把第三个entrace读成带两个弦的张量,但是当我用

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

目前的结果是:

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

我如何得到结果:

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

我想把它用作cathegorical_features


Tags: 文件csvstringtfactionfeaturesdecodeshape
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:05

我决定这样做:

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")

相关问题 更多 >