您必须使用dtype float为占位符张量'Placeholder_1'提供值

时间:2022-03-04 21:24:38
import tensorflow as tf

# H(x) = Wx + b

W = tf.Variable(tf.random_normal([1],name='weight'))
b = tf.Variable(tf.random_normal([1],name='bias'))

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

hypothesis = X * W + b

cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

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

Weights = []

for step in range(100):
    sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]})
    if step % 99 ==0:
        print(step, sess.run(cost), sess.run(W), sess.run(b))

This is the code I have. When I enter x_col[0] in Python Shell I get array([ 3., 5., 73., 33.], dtype=float32) and for y_col[0], I get array([ 3., 5., 73., 33.]).

这是我的代码。当我在Python Shell中输入x_col [0]时,我得到数组([3.,5.,73。,33。],dtype = float32),对于y_col [0],我得到数组([3.,5, 73。,33。])。

So I believe the code should work giving cost of 0 and W of 1 and 0 for b. But this error comes up. I don't know how I can fix this problem

所以我认为代码应该有效,给b的成本为0,W为1和0。但是这个错误出现了。我不知道如何解决这个问题

For your information, for sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]}) I get [960446.13, array([ 76.92639923, 127.70278168, 1854.09997559, 838.57220459], dtype=float32), None].

为了您的信息,sess.run([费用,假设,火车],feed_dict = {X:x_col [0],Y:y_col [0]})我得到[960446.13,数组([76.92639923,127.70278168,1854.09997559,838.57220459] ],dtype = float32),无]。

3 个解决方案

#1


1  

In your print statement

在你的打印声明中

print(step, sess.run(cost), sess.run(W), sess.run(b))

you are using sess.run(cost), but cost is dependent upon X and Y, whose values you should provide as they are placeholders. So, you'll need to provide that in feed_dict as

您正在使用sess.run(cost),但成本取决于X和Y,您应该提供其值,因为它们是占位符。所以,你需要在feed_dict中提供它

print(step, sess.run(cost, feed_dict={X: some_x_value, Y: some_y_value}), sess.run(W), sess.run(b))

#2


0  

In TensorFlow you define a computational graph that is executed with the sess.run() statement. As part of that graph the cost operation is defined by placeholder X and Y. To compute the cost you have to feed a value for X and Y.

在TensorFlow中,您定义了一个使用sess.run()语句执行的计算图。作为该图的一部分,成本操作由占位符X和Y定义。要计算成本,您必须为X和Y提供值。

In your print statement you call sess.run(cost) without feeding X and Y. That is the reason for the error.

在你的print语句中,你调用sess.run(cost)而不给X和Y.这就是错误的原因。

But you already executed the graph. Just store the resulting values:

但是你已经执行了图表。只需存储结果值:

C, H, _ = sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]})

and print results for cost C and hypothesis H

并打印成本C和假设H的结果

#3


0  

@layog's answer is right. Just want to show you the code you should use:

@ layog的回答是对的。只是想向您展示您应该使用的代码:

for step in range(100):
  cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], 
                                       feed_dict={X:x_col[0],Y:y_col[0]})
  if step % 99 ==0:
    print(step, cost_val, W_val, b_val)

It's more efficient to run the training op and compute tensor values in one shot (note that you don't have to specify hypothesis). If you want to explicitly compute any tensor, you'll have to pass the placeholders too:

运行训练操作并一次计算张量值更有效(请注意,您不必指定假设)。如果要显式计算任何张量,则还必须传递占位符:

sess.run(cost, feed_dict={X:x_col[0],Y:y_col[0]})

#1


1  

In your print statement

在你的打印声明中

print(step, sess.run(cost), sess.run(W), sess.run(b))

you are using sess.run(cost), but cost is dependent upon X and Y, whose values you should provide as they are placeholders. So, you'll need to provide that in feed_dict as

您正在使用sess.run(cost),但成本取决于X和Y,您应该提供其值,因为它们是占位符。所以,你需要在feed_dict中提供它

print(step, sess.run(cost, feed_dict={X: some_x_value, Y: some_y_value}), sess.run(W), sess.run(b))

#2


0  

In TensorFlow you define a computational graph that is executed with the sess.run() statement. As part of that graph the cost operation is defined by placeholder X and Y. To compute the cost you have to feed a value for X and Y.

在TensorFlow中,您定义了一个使用sess.run()语句执行的计算图。作为该图的一部分,成本操作由占位符X和Y定义。要计算成本,您必须为X和Y提供值。

In your print statement you call sess.run(cost) without feeding X and Y. That is the reason for the error.

在你的print语句中,你调用sess.run(cost)而不给X和Y.这就是错误的原因。

But you already executed the graph. Just store the resulting values:

但是你已经执行了图表。只需存储结果值:

C, H, _ = sess.run([cost,hypothesis,train], feed_dict={X:x_col[0],Y:y_col[0]})

and print results for cost C and hypothesis H

并打印成本C和假设H的结果

#3


0  

@layog's answer is right. Just want to show you the code you should use:

@ layog的回答是对的。只是想向您展示您应该使用的代码:

for step in range(100):
  cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], 
                                       feed_dict={X:x_col[0],Y:y_col[0]})
  if step % 99 ==0:
    print(step, cost_val, W_val, b_val)

It's more efficient to run the training op and compute tensor values in one shot (note that you don't have to specify hypothesis). If you want to explicitly compute any tensor, you'll have to pass the placeholders too:

运行训练操作并一次计算张量值更有效(请注意,您不必指定假设)。如果要显式计算任何张量,则还必须传递占位符:

sess.run(cost, feed_dict={X:x_col[0],Y:y_col[0]})