例如,我有这个string
张量:
s = tf.constant('154.0', dtype=tf.string)
我想将其转换为tf.int32
张量:
tf.strings.to_number(s, out_type=tf.int32)
但是我遇到了以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: StringToNumberOp could not correctly convert string: 154.0 [Op:StringToNumber]
为什么会这样?是因为字符串“ 154.0”包含小数吗?
答案 0 :(得分:0)
我的猜测是,当您要求直接转换为int时,您指定的字符串数被视为浮点数。您可以尝试:
out_type
的情况下进行自动转换,然后使用tf.cast https://www.tensorflow.org/api_docs/python/tf/cast