Я пытаюсь реализовать модель линейной регрессии для нескольких переменных, используя этот учебник.Я попытался заменить это своим собственным набором данных, используя метод 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])
Проблема, с которой я столкнулся, заключается в форме массива numpy, переданного в y_in.
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.
Есть предложения?
Спасибо!