[英]TensorFlow: cast a float64 tensor to float32
I am trying to use: train = optimizer.minimize(loss)
but the standard optimizers do not work with tf.float64
. 我正在尝试使用:
train = optimizer.minimize(loss)
但标准优化器不能与tf.float64
一起tf.float64
。 Therefore I want to truncate my loss
from tf.float64
to only tf.float32
. 因此,我想将我从
tf.float64
loss
截断为仅限于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].
The short answer is that you can convert a tensor from tf.float64
to tf.float32
using the tf.cast()
op: 简短的回答是你可以使用
tf.cast()
op将张量从tf.float64
转换为tf.float32
:
loss = tf.cast(loss, tf.float32)
The longer answer is that this will not solve all of your problems with the optimizers. 更长的答案是,这不会解决优化器的所有问题。 (The lack of support for
tf.float64
is a known issue .) The optimizers require that all of the tf.Variable
objects that you are trying to optimize must also have type tf.float32
. (缺少对
tf.float64
的支持是一个已知问题 。)优化器要求您尝试优化的所有tf.Variable
对象也必须具有类型tf.float32
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.