Tensorflow无法正确将字符串(float)转换为Int

时间:2020-07-07 14:23:52

标签: python-3.x string tensorflow floating-point

例如,我有这个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”包含小数吗?

1 个答案:

答案 0 :(得分:0)

我的猜测是,当您要求直接转换为int时,您指定的字符串数被视为浮点数。您可以尝试: