gpt4 book ai didi

python - TensorFlow:将 float64 张量转换为 float32

转载 作者:太空狗 更新时间:2023-10-29 17:00:02 24 4
gpt4 key购买 nike

我正在尝试使用:train = optimizer.minimize(loss) 但标准优化器不适用于 tf.float64。因此,我想将我的 losstf.float64 截断为仅 tf.float32

Traceback (most recent call last):
File "q4.py", line 85, in <module>
train = optimizer.minimize(loss)
File "/Library/Python/2.7/site-packages/tensorflow/python/training/optimizer.py", line 190, in minimize
colocate_gradients_with_ops=colocate_gradients_with_ops)
File "/Library/Python/2.7/site-packages/tensorflow/python/training/optimizer.py", line 229, in compute_gradients
self._assert_valid_dtypes([loss])
File "/Library/Python/2.7/site-packages/tensorflow/python/training/optimizer.py", line 354, in _assert_valid_dtypes
dtype, t.name, [v for v in valid_dtypes]))
ValueError: Invalid type tf.float64 for Add_1:0, expected: [tf.float32].

最佳答案

简短的回答是,您可以使用 tf.cast() 将张量从 tf.float64 转换为 tf.float32操作:

loss = tf.cast(loss, tf.float32)

较长的答案是,这不会解决优化器的所有问题。 (缺少对 tf.float64 的支持是一个 known issue。)优化器要求您提供的所有 tf.Variable 对象正在尝试优化的还必须具有类型 tf.float32

关于python - TensorFlow:将 float64 张量转换为 float32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com