TensorFlow:将float64张量转换为float32
我正在尝试使用:train =
optimizer.minimize(loss)但是标准优化器无法使用tf.float64
。因此,我想截断loss
从fromtf.float64
到only
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.float64
到tf.float32
使用tf.cast()
OP:
loss = tf.cast(loss, tf.float32)
更长的答案是,这不能解决优化器的所有问题。(缺少对支持tf.float64
的已知问题。)优化器要求
您要优化的所有对象也必须具有type tf.float32
。
以上是 TensorFlow:将float64张量转换为float32 的全部内容, 来源链接: utcz.com/qa/404227.html