简体   繁体   English

TensorFlow:将float64张量强制转换为float32

[英]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.

相关问题 Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为 float32 层的 dtype - Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32 Tensor转换请求dtype float32的dtype float64 - Tensor conversion requested dtype float64 for Tensor with dtype float32 警告:tensorflow:Layer my_model 正在将输入张量从 dtype float64 转换为 float32 层的 dtype,这是 TensorFlow 2 中的新行为 - WARNING:tensorflow:Layer my_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2 在Tensorflow中为float32和float64实现新的运算符时出错 - Error implementing a new operator in Tensorflow for float32 and float64 在 tensorflow 数据集中将数据类型从 float32 更改为 float64 - Change datatype from float32 to float64 in a tensorflow dataset 将float32表示形式转换为float64 NumPy Python - float32 representation to float64 NumPy Python astype float32对于整数的float64出错 - Error in astype float32 vs float64 for integer NumPy的铸造FLOAT32到float64 - Numpy casting float32 to float64 float64到float32 Cython错误 - float64 to float32 Cython Error float32 / float64 numpy变量的不同行为 - Different behavior of float32/float64 numpy variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM