0
请记住,我对tf非常陌生。Tensorflow Shape Error(最新版本)多元线性回归
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
np.set_printoptions(suppress=True)
boston = load_boston()
m = boston.data.shape[0] - 1
bt_unfixed = np.transpose(boston.data)
bt = np.insert(bt_unfixed, 0, 1)
Y = tf.placeholder(tf.float64)
X = tf.placeholder(tf.float64, [None, 13])
print X.shape
W = tf.Variable(np.transpose(np.array([0.00,0,0,0,0,0,0,0,0,0,0,0,0]).flatten()), name='weights')
b = tf.Variable(0.5, name='bias')
hypothesis = tf.add(tf.matmul(X, W), tf.cast(b, tf.float64))
loss = tf.reduce_sum(tf.square(hypothesis - Y))/(2 * m)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train_op = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for i in range(0, 1200):
for (x, y) in zip(boston.data, boston.target.data):
sess.run(train_op, feed_dict={X:x, Y:y})
print "Done!\n"
print "Running test...\n"
t = sess.run(cost, feed_dict={X:boston.data[504], Y:boston.target.data[504]})
print "loss =" + str(t) + "Real value" + str(boston.target.data[504]) + "Pred " +str(sess.run(hypothesis, feed_dict={X:boston.data[504]}))
请随时纠正我的代码中的任何其他错误!
当我定义假设时会引发错误,我需要转置我的向量以将它们相乘,但它不起作用。
Traceback (most recent call last):
File "multihouse.py", line 20, in <module>
hypothesis = tf.add(tf.matmul(X, W), tf.cast(b, tf.float64))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1398, in matmul
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1348, in _mat_mul
transpose_b=transpose_b, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2382, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1783, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn
raise ValueError(err.message)
ValueError: Shape must be rank 2 but is rank 1