gpt4 book ai didi

python - 我无法找出为什么编译器报告错误?

转载 作者:行者123 更新时间:2023-12-01 02:51:43 25 4
gpt4 key购买 nike

import tensorflow as tf 
import numpy as np

W = tf.Variable([0,3], dtype = tf.float32)
b = tf.Variable([-0.3], dtype = tf.float32)

x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)

loss = tf.reduce_sum(tf.square(linear_model - y))

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
sess.run(train, {x: x_train, y: y_train})

curr_W, curr_b, curr_loss = sess.run([W,b,loss], {x: [1,2,3,4], y: [0,-1,-2,-3]})
print("w: %s, b: %s, loss: %s", curr_W, curr_b, curr_loss)

错误:InvalidArgumentError(请参阅上面的回溯):不兼容的形状:[2] 与 [4] [[节点: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](变量/读取, _recv_Placeholder_0)]]

最佳答案

我猜问题就出在这里而不是 W 中的这个,因为您使用的是 float32

W = tf.Variable([0,3], dtype = tf.float32)
b = tf.Variable([-0.3], dtype = tf.float32)

将会

W = tf.Variable([0.3], dtype = tf.float32)
b = tf.Variable([-0.3], dtype = tf.float32)

关于python - 我无法找出为什么编译器报告错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689762/

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