初学神经网络,搭建了最简单的网络,代码如下:
import tensorflow as tf
import numpy as np
BATCH_SIZE = 8
seed = 23455
rng = np.random.RandomState(seed)
X = rng.rand(32,2)
Y = [int(x0+x1<1) for (x0,x1) in X]
print("X:\n",X)
print("Y:\n",Y)
x = tf.placeholder(tf.float32,shape=[None,2])
y_ = tf.placeholder(tf.float32,shape=[None,1])
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
loss = tf.reduce_mean(tf.square(y-y_))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print("w1:\n",sess.run(w1))
print("w2:\n",sess.run(w2))
STEP = 3000
for i in range(STEP):
start = (i*BATCH_SIZE)%32
end = start+BATCH_SIZE
sess.run(train_step,feed_dict={x:X[start:end],y_:Y[start:end]})
if i % 100 == 0 :
total_loss = sess.run(loss,feed_dict={x:X,y_:Y})
print("After %d train,loss is %f "%(i,total_loss))
print("\n")
print("w1:\n",sess.run(w1))
print("w2:\n",sess.run(w2))
结果报错
ValueError: Cannot feed value of shape (8,) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
按照错误结果显示一直以为是张量不匹配的问题,从维度(shape)入手,发现shape没有任何问题呀,最终错误出在了:
将:
Y = [int(x0+x1<1) for (x0,x1) in X]
改为:
Y = [[int(x0+x1<1)] for (x0,x1) in X]
问题就解决了,个人理解:Y是数据集中的标签,该行代码是给X贴标签,int(x0+x1)<1是给判断语句,应该括号单独括起来。
(注:但是为什么不带括号会显示张良不匹配的问题)