我在使用Tensorflow创建一个机器学习模型(MNIST数据集)。模型训练得很完美,但在进行预测时出现了错误。问题是我无法识别错误所在,我并没有使用任何导致错误的字符串或字节对象。另外,请告诉我我在创建神经网络的过程中是否走在正确的方向上。
这是关于MNIST
数据集的。
代码:
import numpy as npimport pandas as pdimport tensorflow as tfimport sysinput_data_train=pd.read_csv('fashion-mnist_train.csv')y_train=input_data_train['label']x_train=input_data_train.drop(columns=['label'])y_train=np.array(y_train)y_train=y_train.reshape(-1,1)from sklearn.preprocessing import OneHotEncoderenc=OneHotEncoder(sparse=False)y_train=enc.fit_transform(y_train)tf.reset_default_graph()sess=tf.InteractiveSession()'''since we will be using tensor flow as framework for the creation of the neural network''''''since it is a convention that the input layer is designed on the a placeholder and the perpetual hidden layers on the variable'''input_layer_x=tf.placeholder(tf.float32,shape=(None,784))input_layer_y=tf.placeholder(tf.float32,shape=(None,10))w1=tf.get_variable('wq',[784,50])b1=tf.get_variable('bq',[50])'''generating the outputs'''output_1=tf.nn.relu(tf.add(tf.matmul(input_layer_x,w1),b1))'''now we will move towards the second layer'''w2=tf.get_variable('wa',[50,25])b2=tf.get_variable('ba',[25])output_2=tf.nn.relu(tf.add(tf.matmul(output_1,w2),b2))'''now we will be moving towards out output layer'''w3=tf.get_variable('wz',[25,10])b3=tf.get_variable('bz',[10])output_3=tf.add(tf.matmul(output_2,w3),b3)'''we have now defined all our layers and output layer''''''moving towards using the cost function''' cost= tf.reduce_mean(tf.nn.softmax_cross_entropy_with _logits_v2(logits=output_ 3,labels=input_layer_y)) opt=tf.train.AdamOptimizer().minimize(cost) batch_size=10000 epochs=5 init=tf.global_variables_initializer() ''' this helped to initialise all the values ''' sess.run(init) i=0 j=0 while i<epochs: j=0 while j<60000: start=j*batch_size batch_x=x_train[start:start +batch_size] batch_y=y_train[start:start +batch_size] a,res= sess.run([opt,cost],feed_dict={input_layer_x:batch_x,input_layer_y:batch_y}) j=j+1i=i+1print(i)'''now since out model is trained we will be testing this with respect to our inputs(training)'''data_test=pd.read_csv('fashion-mnist_test.csv')y_test=input_data_train['label']x_test=input_data_train.drop(columns=['label'])y_test=np.array(y_test)y_test=y_test.reshape(-1,1)y_test=enc.fit_transform(y_test)pred=tf.equal(tf.math.argmax(output_3,1),tf.math.argmax(input_layer_y),1)accuracy=tf.reduce_mean(tf.cast(pred,tf.float32))sess.run(accuracy,feed_dict={X:x_test,Y:y_test})''' now we will be looking towards the accuracy '''sess.close()
回答:
你的代码中有一些错误。按照以下步骤可以消除任何错误:
- 将
pred = tf.equal(tf.math.argmax(output_3,1),tf.math.argmax(input_layer_y),1)
替换为pred = tf.equal(tf.math.argmax(output_3, 1), tf.math.argmax(input_layer_y, 1))
- 以上代码中没有名为
X
和Y
的占位符。因此,将sess.run(accuracy,feed_dict={X:x_test,Y:y_test})
替换为sess.run(accuracy,feed_dict={input_layer_x:x_test,input_layer_y:y_test})
,因为input_layer_x
和input_layer_y
是上面使用的有效占位符。 -
对于测试标签,不应使用
fit_transform
方法,而应使用已拟合的enc
进行transform
操作,如下所示:y_test=enc.transform(y_test)