Vik*_*ram 1 python machine-learning tensorflow
尝试掌握TensorFlow的文档。
下面的程序导致“不兼容的类型转换错误”
import tensorflow as tf
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = 1.0
linear_model = W * x + b
#tf.to_float(linear_model, name='ToFloat')
# Global initialization is must
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(linear_model, {x:[1,2,3,4]}))
Run Code Online (Sandbox Code Playgroud)
上面的程序导致此错误
文件“ v-prog3-variables.py”,第7行,linear_model = W * x + b ..。。.. ValueError:请求为类型为'int32_ref'的变量键入类型为'float32'的不兼容类型转换
我试图通过将'linear_model'变量定义为float(linear_model = 1.0)或tf.to_float(linear_model = W * x + b)来解决问题
但是什么都没有
我是TensorFlow新手,请帮帮我。提前致谢。
我能够通过将其重铸到float32来使其运行。您已阅读该库的源代码吗?是的...我不问问题
import tensorflow as tf
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + tf.cast(b, tf.float32)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(linear_model, {x:[1,2,3,4]}))
Run Code Online (Sandbox Code Playgroud)
您需要为该类型使用命名参数,例如tf.Variable([.3], dtype=tf.float32)
的签名tf.Variable会__init__(self, initial_value=None, trainable=True,...导致常见错误。
然后该__init__方法将从您的输入推断类型:
[.3]将给出tf.float32, 和[-3]将给出tf.int32
您在将它们相乘时得到的错误的前导。如果您想坚持使用该tf.float32类型,您也可以用作[-3.]的初始值b。
| 归档时间: |
|
| 查看次数: |
3195 次 |
| 最近记录: |