Tensorflow Logistic回归Iris数据集(csv),无法为形状为((?,3))的Tensor'Placeholder_1:0'输入形状(106,)的值

时间:2019-03-07 07:36:37

标签: python tensorflow logistic-regression

我正在尝试对从here获得的虹膜数据集进行逻辑回归,我正在使用tensorflow,并且感觉到它在错误行的feed_dict部分给了我错误。 我尝试根据使用np.reshape()发生的错误来更改x_input和y_input的形状,但这没有帮助。

#training data
x_input=data.loc[0:105, ['SEPAL LENGTH','SEPAL WIDTH','PETAL LENGTH','PETAL WIDTH']]
temp=data['FLOWER']
y_input=temp[0:106]
#test data
x_test=data.loc[106:149, ['SEPAL LENGTH','SEPAL WIDTH','PETAL LENGTH','PETAL WIDTH']]
y_test=temp[106:150]

#placeholders and variables. input has 4 features and output has 3 classes
x=tf.placeholder(tf.float32,shape=[None, 4])
y_=tf.placeholder(tf.float32,shape=[None, 3])
#weight and bias
W=tf.Variable(tf.zeros([4,3]))
b=tf.Variable(tf.zeros([3]))


# X is placeholdre for iris features. We will feed data later on
X = tf.placeholder(tf.float32, [None, 4])
# y is placeholder for iris labels. We will feed data later on
Y = tf.placeholder(tf.float32, [3, None])

w = tf.Variable(tf.zeros([4,3]))
b = tf.Variable(tf.zeros([3]))


# 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.initialize_all_variables()
sess.run(init)
#number of interations
epochs=2000

for step in range(epochs):
           _, c=sess.run([train_step,cross_entropy], feed_dict={x: x_input.as_matrix(), y_:y_input.as_matrix()}) #error line
           if step%500==0:
               print(c)

错误堆栈看起来像这样

ValueError                                Traceback (most recent call last)
<ipython-input-13-aa781a046f65> in <module>
      1 for step in range(epochs):
----> 2            _, c=sess.run([train_step,cross_entropy], feed_dict={x: x_input.as_matrix(), y_:y_input.as_matrix()})
      3            if step%500==0:
      4                print(c)

c:\users\hp\anaconda3\envs\maskrcnn\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    893     try:
    894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
    896       if run_metadata:
    897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

c:\users\hp\anaconda3\envs\maskrcnn\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1102                 'Cannot feed value of shape %r for Tensor %r, '
   1103                 'which has shape %r'
-> 1104                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
   1105           if not self.graph.is_feedable(subfeed_t):
   1106             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (106,) for Tensor 'Placeholder_1:0', which has shape '(?, 3)'

0 个答案:

没有答案