我正在尝试使用this教程为多个变量实现线性回归模型。我尝试使用sklearn的train_test_split()
方法将其替换为自己的数据集。
import numpy as np
import tensorflow as tf
import pandas as pd
df = pd.read_csv('airfoil_self_noise.csv',sep=',')
from sklearn.model_selection import train_test_split
X_true, X_test, y_true, y_test = train_test_split(df.iloc[:,:-1].values,df.iloc[:,-1].values,test_size = 0.2, random_state=0)
n_features = np.shape(X_true)[1]
m_examples = np.shape(X_true)[0]
# Placeholder that is fed input data.
X_in = tf.placeholder(tf.float32, [None, n_features], "X_in")
# The model: we assume y = X_in * w + b
w = tf.Variable(tf.random_normal((n_features, 1)), name="w")
b = tf.Variable(tf.constant(0.1, shape=[]), name="b")
h = tf.add(tf.matmul(X_in, w), b, name="h")
# Placeholder that is fed observed results.
y_in = tf.placeholder(tf.float32,[1,None], "y_in")
# The loss function: we are minimizing square root of mean
loss_op = tf.reduce_mean(tf.square(tf.subtract(y_in, h)), name="loss")
train_op = tf.train.GradientDescentOptimizer(0.3).minimize(loss_op)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(1000):
sess.run(train_op, feed_dict={
X_in: X_true,
y_in: y_true
})
w_computed = sess.run(w)
b_computed = sess.run(b)
print ("w computed [%s]" % ', '.join(['%.5f' % x for x in w_computed.flatten()]))
print ("w actual [%s]" % ', '.join(['%.5f' % x for x in w_true.flatten()]))
print ("b computed %.3f" % b_computed)
print ("b actual %.3f" % b_true[0])
我似乎遇到的问题是送入y_in的numpy数组的形状。
Traceback (most recent call last):
File "Airfoil_Test_TF.py", line 32, in <module>
y_in: y_true
File ".../anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File ".../anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1128, in _run
str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1202,) for Tensor 'y_in:0', which has shape '(1, ?)'
我试图修改y_in的占位符的尺寸,但是它没有任何作用。本教程最初使用尺寸[None,1]
定义了占位符,而不是采用这种方式,但是我找不到找到将y_true
转换为形状(,1202)的方法,因为一个维数组无法被换成numpy。
有什么建议吗?
谢谢!
答案 0 :(得分:1)
您可以致电
y_true = y_true.reshape((1,-1))
这应该可以解决问题。
简短说明:
在numpy中,形状(10,)
只是一维向量。
形状(10, 1)
代表一个明确的行向量。
形状(1, 10)
代表列向量。