我正在尝试使用:train = optimizer.minimize(loss)
,但标准优化器不适用于tf.float64
。因此,我想将loss
从tf.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].
答案 0 :(得分:39)
简短的回答是,您可以使用tf.cast()
操作将张量从tf.float64
转换为tf.float32
:
loss = tf.cast(loss, tf.float32)
答案越长,这将无法解决优化器的所有问题。 (缺少对tf.float64
的支持是known issue。)优化程序要求您尝试优化的所有 tf.Variable
对象也必须具有类型tf.float32
。