ValueError: 无法为形状为(2,)的张量’Placeholder:0’提供值,该张量形状应为'(1, 2)’

我是TensorFlow的新手,正在尝试创建一个线性回归模型。我的代码是

import numpy as npimport tensorflow as tfbias = np.ones((50, 1))trainX = np.arange(0, 10, 0.2).reshape(50, 1)trainY = (3 * trainX + np.random.rand(trainX.shape[0]) * 20 - 10) + 10trainX = np.append(bias, trainX, axis=1)X = tf.placeholder("float", shape=(1, 2))Y = tf.placeholder("float")w = tf.Variable([[0.0, 0.0]], name="weights")model = tf.matmul(X, tf.transpose(w))cost = tf.pow((Y - model), 2)init = tf.global_variables_initializer()train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)with tf.Session() as sess:    sess.run(init)    for i in range(50):        for (x, y) in zip(trainX, trainY):            sess.run(train_op, feed_dict={X: x, Y: y})    print(sess.run(w))

我不知道自己做错了什么。我认为问题出在初始化权重上。我的想法很简单,就是预测两个最能拟合数据的权重常数。

我在Colaboratory笔记本中遇到了这个错误


回答:

这里有几个因素在起作用。我假设你希望trainY的形状是(50,),但由于你在重塑后才添加噪声,广播导致trainX + np.random.rand(trainX.shape[0])的形状变为(50, 50)。如果你将代码的初始部分改为

bias = np.ones(50)trainX = np.arange(0, 10, 0.2)trainY = (3 * trainX + np.random.rand(trainX.shape[0]) * 20 - 10) + 10trainX = np.vstack([bias, trainX]).T

并确保通过

sess.run(train_op, feed_dict={X: x.reshape((1, 2)), Y: y})

正确设置形状,那么你的代码将能够运行。

然而,由于你只处理二维向量的内积,你可以完全避免重塑操作,只需简单地使用tf.tensordot即可:

X = tf.placeholder("float", shape=(2,))Y = tf.placeholder("float")w = tf.Variable([0.0, 0.0], name="weights")model = tf.tensordot(X, w, 1)

最后,请注意,虽然在将样本分成小批次后传递给优化器(通常称为批处理)的方法在大数据集上效果很好,但在你的情况下,你也可以一次性传递整个样本;也就是说,类似于

X = tf.placeholder("float", shape=(50, 2))Y = tf.placeholder("float", shape=(50, 1))w = tf.Variable(tf.zeros([2, 1], "float"), name="weights")model = tf.matmul(X, w)cost = tf.reduce_sum(tf.pow((Y - model), 2))init = tf.global_variables_initializer()train_op = tf.train.GradientDescentOptimizer(0.0001).minimize(cost)with tf.Session() as sess:    sess.run(init)    for i in range(10000):        sess.run(train_op, feed_dict={X: trainX, Y: trainY.reshape((50, 1))})

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注