gpt4 book ai didi

python - Tensorflow程序导致类型转换错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:22 24 4
gpt4 key购买 nike

尝试掌握 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]}))

以上程序导致此错误

File "v-prog3-variables.py", line 7, in linear_model = W * x + b .. .. .. ValueError: Incompatible type conversion requested to type 'float32' for variable of type 'int32_ref'

我试图通过将“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]}))

关于python - Tensorflow程序导致类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com