python-3.x - Tensorflow 无法正确将字符串(浮点数)转换为 Int
问题描述
例如我有这个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' 包含小数吗?
解决方案
我的猜测是,您指定的字符串编号被视为浮点数,而您要求直接转换为 int。您可以尝试:
- 传递一个表示 int 的字符串,看看它是否有效(它应该);或者
- 让 tensorflow 在不指定 的情况下进行自动转换
out_type
,然后将输出张量转换为 int32,例如使用 tf.cast https://www.tensorflow.org/api_docs/python/tf/cast
推荐阅读
- spring-cloud-stream - Spring Cloud Stream 主题分区 KStream 读/写
- mysql - Bash 脚本 MySQL 连接和通过变量查询
- sas - 使用 DATE9 格式将数字转换为 SAS 日期
- gremlin - Gremlin:使用顶点变量通过 HTTP 向图形添加边
- angular - NullInjectorError:没有函数()的提供者{}!
- tensorflow - InvalidArgumentError:您必须使用 dtype bool 为占位符张量“Placeholder”提供一个值 ... [[accuracy/accuracy_var/_3191]]
- jquery - 如何创建单选按钮动态分配用户输入的值?
- python-3.x - 无法在 jupyter notebook 中为谷歌云实例导入熊猫
- c# - 从 Asp.net Web Api 控制器获取文件名
- ruby-on-rails - lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require': 无法加载此类文件 -- sass (LoadError)