我有以下错误。
无法为Tensor' Placeholder_31:0'提供形状值(525,3),其形状为'(?,2)'
这是我的代码:
data=pd.read_csv('/Users/kselvan/Desktop/cancer.csv',names=["A","B","C","D","E","F","G","H","I","J","K"])
s=np.asarray([1,0,0])
ve=np.asarray([0,1,0])
data['K'] = data['K'].map({2: s, 4: ve})
data=data.iloc[np.random.permutation(len(data))]
data=data.reset_index(drop=True)
#training data
x_input=data.loc[0:524,["A","B","C","D","E","F","G","H","I","J"]]
temp=data['K']
y_input=temp[0:525]
#test data
x_test=data.loc[525:698,["A","B","C","D","E","F","G","H","I","J"]]
y_test=temp[525:699]
#placeholders and variables. input has 4 features and output has 3 classes
x=tf.placeholder(tf.float32,shape=[None,10])
y_=tf.placeholder(tf.float32,shape=[None,2])
print(x.shape)
print(y.shape)
#weight and bias
W=tf.Variable(tf.zeros([10,2]))
b=tf.Variable(tf.zeros([2]))
# model
#softmax function for multiclass classification
y = tf.nn.softmax(tf.matmul(x, W) + b)
#loss function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
#optimiser -
train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
#calculating accuracy of our model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#session parameters
sess = tf.InteractiveSession()
#initialising variables
init = tf.global_variables_initializer()
sess.run(init)
#number of interations
epoch=2000
for step in range(epoch):
_, c=sess.run([train_step,cross_entropy], feed_dict={x: x_input, y_:[t for t in y_input.as_matrix()]})
if step%500==0 :
print(c)
因为我是张流的新手,所以猫弄清楚这是什么错误。任何人都可以帮我整理一下吗?
答案 0 :(得分:1)
您正在使用下一行声明占位符(?,2):
y_=tf.placeholder(tf.float32,shape=[None,2])
但是问题是3类的分类。所以你应该将你的y_,W和b改为:
y_=tf.placeholder(tf.float32,shape=[None,3])
W=tf.Variable(tf.zeros([10,3]))
b=tf.Variable(tf.zeros([3]))
答案 1 :(得分:0)
实际上,你定义了你的体重和bais的错误形状,所以根据你的网络架构改变重量和bais的尺寸,它应该是这样的
y = tf.placeholder(tf.float32,shape=[None,number_of_classes])
w = tf.Variable(tf.zeros([input_tensor_shape,output_tensor_shape]))
b = tf.Variable(tf.zeros([number_of_classes]))