- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 auto-mpg.data。我遇到了这个问题:
回溯(最近一次调用最后一次): 文件“C:/PythonProjects/test5.py”,第 60 行,位于 _, c = sess.run([optimizer, loss], feed_dict={X: xTrain, Y: yTrain}) # 获取损失值 文件“C:\Python3\lib\site-packages\tensorflow\python\client\session.py”,第 929 行,运行中 运行元数据指针) 文件“C:\Python3\lib\site-packages\tensorflow\python\client\session.py”,第 1128 行,在 _run 中 str(subfeed_t.get_shape())))
ValueError:无法为张量“Placeholder_1:0”提供形状为 (352, 1) 的值,其形状为“(?,)”
我尝试为 Xtrain、Xtest、Ytrain、Ytest 创建单独的占位符,但我相信这也不是正确的方法。
如何对测试/训练数据使用相同的 X 和 Y 占位符?
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
filename = "auto-mpg.data"
column_names = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'year', 'origin', 'name']
df = pd.read_csv(filename, delim_whitespace=True, header=None, na_values = "?", names=column_names)
df = df.drop('name', axis=1)
df = df.dropna()
# Ont-hot encoding for category data
origin = df.pop('origin')
df['USA'] = (origin == 1)*1.0
df['Europe'] = (origin == 2)*1.0
df['Japan'] = (origin == 3)*1.0
df = df.drop(['year'], axis=1)
x_data = df.drop('mpg', axis=1)
y_data = df[['mpg']] # Continuous target variable : mpg
print(df.shape)
# Test/Train split of 90%/10%
xTrain, xTest, yTrain, yTest = train_test_split(x_data, y_data, test_size=0.1, random_state=0)
print(xTrain.shape) # 352x8
print(xTest.shape) # 40x8
print(yTrain.shape) # 352x1
print(yTest.shape) #40x1
def LinearRegression():
y_pred = tf.add(tf.matmul(X, W), b)
loss = tf.reduce_mean(tf.square(y_pred - Y))
return loss
# Xtrain = tf.placeholder(tf.float32, [352,8])
# Ytrain = tf.placeholder(tf.float32, [352,1])
# Xtest = tf.placeholder(tf.float32, [40,8])
# Ytest = tf.placeholder(tf.float32, [40,1])
numFeatures = xTrain.shape[1]
X = tf.placeholder(tf.float32, [None,numFeatures])
Y = tf.placeholder(tf.float32, [None])
W = tf.get_variable(name='Weight', dtype=tf.float32, shape=([8,1]), initializer=tf.zeros_initializer())
b = tf.get_variable(name='Bias', dtype=tf.float32, shape=([1]), initializer=tf.zeros_initializer())
loss=LinearRegression()
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0000001).minimize(loss)
epochs = 1000
display_step = 100 # Display every 10s output
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for e in range(epochs):
_, c = sess.run([optimizer, loss], feed_dict={X: xTrain, Y: yTrain}) # Get loss value
if (e + 1) % display_step == 0:
print('Epoch #:', '%d' % (e + 1), 'Loss =', '{:.9f}'.format(c), 'W =', sess.run(W), 'b =', sess.run(b))
print("Training completed...")
training_cost = sess.run(loss, feed_dict={X: xTrain, Y: yTrain})
weight = sess.run(W)
bias = sess.run(b)
print("Training cost=", training_cost, '; ' "W =", weight, '; ' "b =", bias)
print("Testing result...")
test_loss = LinearRegression() # Same function as above
testing_cost = sess.run(test_loss, feed_dict={X: xTest, Y: yTest})
print("Testing cost:", testing_cost)
print("Absolute mean square loss difference:", abs(training_cost - testing_cost))
fitted_prediction = sess.run(W) * xTest + sess.run(b)
print('fitted_prediction = ',fitted_prediction)
最佳答案
看起来你的 yTrain 是排名 2 ( [352,1] ),但你试图用它提供的占位符是一个标量。尝试将 Y 更改为
Y = tf.placeholder(tf.float32, [None,1])
关于python - TensorFlow多元线性回归占位符和矩阵乘法误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398313/
我有一个任务,必须求解线性方程组 Ax =B,其中 A 是 10000 量级的稀疏矩阵。我正在使用 csparse 来求解它。在我的初始实现中,出于演示目的,A 是 3*3 阶单位矩阵,B ={1,2
我正在尝试训练随机森林模型,但出现以下错误。我需要对分类模型使用不同的设置来解决 RMSE 问题吗?我尝试将“好”转换为一个因素,但这引发了一个新错误。 错误: Error in train.defa
当使用 %Lf 打印时,一个大的 double 值被改变以下组合的值给出正确的结果小数点前9位/小数点后6位例如用 %Lf 打印的小数点前 9 位的值输入:3435537287.32输出:343553
我正在尝试实现高度为 100% 的重复垂直背景。遗憾的是,每当我试图通过仅添加纯文本来扩展页面的高度时,具有 100% 高度和背景重复的背景就会被切掉。我只想让背景重复。这是屏幕截图。 http://
所以我有: t = [0.0, 3.0, 5.0, 7.2, 10.0, 13.0, 15.0, 20.0, 25.0, 30.0, 35.0] U = [12.5, 10.0, 7.6, 6.0,
我使用了 Nister 的 5 点法来计算基本矩阵。使用 RANSAC 和 Sampson 错误阈值进一步改进了异常值拒绝。我随机选择 5 个点集,估计基本矩阵并评估匹配向量的 Sampson 误差。
为分类问题运行 gbm 函数时。我收到以下错误: Error in res[flag, ] 0.5,1,0) table(pred,df$Group) pred 0 1 0 98
我使用 Angular ngTagsInput,我的列表如下: [{text: "4353453"}, {text: "453453"}, {text: "4534534"}, {text: "53
我正在尝试 Angular 问题,并且坚持理解错误,这意味着如果我无法理解错误,我将无法前进,例如 zone.js:654 Unhandled Promise rejection: Failed to
我有一个关于在线性混合模型上运行事后测试的问题: 我正在 lme4 中运行一个线性混合模型,分为 3 组,每组 5 条蛇,每组采用不同的通气率 (Vent),在不同的位置进行测量时间点 (Time),
我正在尝试运行逻辑回归并不断收到“NA”错误。问题是它说有 NA 的列没有 NA,全是 0 或 1。我的代码如下: #V1=race, V2=momcounts of breast cancer, V
我在输出之间得到随机 NaN。 Random Temp:61.816288952756864 'F Random Temp:NaN 'F Random Temp:NaN 'F Random Temp:
我正在尝试通过取下平板框架来减少我的大炮天文图像,这很有效。但它使所有值都非常低(所以几乎是黑色图片),这就是为什么我也想将其乘以平均值。然而这给了我一个错误。 (虽然没有乘法它也能工作。) 有人知道
我正在使用 adaboost 构建一个模型,并尝试让 roc 图发挥作用。这是我的代码: ens=fitensemble(X,y,'AdaBoostM1',100,'Tree'); [ytest, s
当尝试使用 Protractor 和 Angular Testing 模态窗口上的可见按钮时,我收到以下错误: UnknownError: unknown error: Element is not
我正在尝试使用命令通过 Yeoman 构建 Angular “哟有 Angular ” 一切正常,直到我到达 tmp 目录: npm ERR! Error: EACCES, mkdir '/home/
我在使用 OpenCV 计算立体声对的校正时遇到一些问题:stereoCalibrate 返回高均方根误差,我得到了错误的校正对。我尝试了我的整改程序和 opencv 提供的 stereo_calib
我在 Mac (OS X 10.9) 上安装了 Yeoman,并且正在尝试运行 yo angular。 我收到以下错误: path.js:384 throw new TypeError('Ar
我有运行循环的线程。我需要该循环每 5 毫秒运行一次(1 毫秒错误)。我知道 Sleep() 函数并不精确。 你有什么建议吗? 更新。我不能用其他方式做到这一点。在循环结束时,我需要某种 sleep
我一直在试验 FFT 算法。我使用 NAudio 以及来自互联网的 FFT 算法的工作代码。根据我对性能的观察,生成的音调不准确。 我将 MIDI(从 GuitarPro 生成)转换为 WAV 文件(
我是一名优秀的程序员,十分优秀!