我使用Tensorflow为MNIST数据集实现了一个简单的模型。
以下是模型:
X = tf.placeholder(tf.float32, [None, 784])
Y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.random_normal(shape=[784, 10], stddev=0.01), name="Weigths")
b = tf.Variable(tf.zeros([1,10]), name="Bias")
Y = tf.nn.softmax(tf.add(tf.matmul(X,W), b))
以下是成本函数的样子:
entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y_, logits=Y)
loss = tf.reduce_mean(entropy)
backprop:
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
这是我的火车循环:
for epoch in range(n_epochs):
avg_loss = 0;
n_batches = int(MNIST.train.num_examples/batch_size)
for i in range(n_batches):
X_batch, Y_batch = MNIST.train.next_batch(batch_size)
_, l, summary = sess.run([optimizer, loss, merged_summary], feed_dict={X: X_batch, Y_: Y_batch})
writer.add_summary(summary, pos)
avg_loss = l / n_batches
print('Epoch :', epoch, 'AvgLoss =', avg_loss)
print ("Accuracy:", acc.eval(feed_dict={X: MNIST.test.images, Y_: MNIST.test.labels}))
但我不了解每个时代的平均成本结果:
Epoch : 0 AvgLoss = 0.0028913601962
Epoch : 1 AvgLoss = 0.00283967841755
Epoch : 2 AvgLoss = 0.0028030406345
Epoch : 3 AvgLoss = 0.002759949294
Epoch : 4 AvgLoss = 0.00283429449255
Epoch : 5 AvgLoss = 0.00276749762622
Epoch : 6 AvgLoss = 0.00276815457778
Epoch : 7 AvgLoss = 0.00279549772089
Epoch : 8 AvgLoss = 0.00277937347239
Epoch : 9 AvgLoss = 0.00274000016126
Epoch : 10 AvgLoss = 0.00275734966451
Epoch : 11 AvgLoss = 0.00278236475858
Epoch : 12 AvgLoss = 0.00275594126094
Epoch : 13 AvgLoss = 0.0027651628581
Epoch : 14 AvgLoss = 0.00275661511855
Epoch : 15 AvgLoss = 0.00275890090249
Epoch : 16 AvgLoss = 0.00273716428063
Epoch : 17 AvgLoss = 0.00273372628472
Epoch : 18 AvgLoss = 0.0027502430569
Epoch : 19 AvgLoss = 0.00279064221816
Epoch : 20 AvgLoss = 0.00273178425702
Epoch : 21 AvgLoss = 0.00277335535396
Epoch : 22 AvgLoss = 0.00276518474926
Epoch : 23 AvgLoss = 0.00276605887847
Epoch : 24 AvgLoss = 0.00275481895967
它没有减少每个循环......但它给了我一个很好的准确性:
Accuracy: 0.9295
关于为什么会这样的任何想法?
答案 0 :(得分:2)
<强>损失:强> 要看到损失减少 - 每次迭代(每批)的打印损失或每100次迭代而不是每个时期。通常,它在几个时代达到最小值。
<强>精度:强>
使用两层完全连接的NN或CNN以获得更好的准确性。您可以添加ReLU图层和Dropout以获得更好的性能。
2层完全连接的NN:96-98%的准确度; ConvNet:99%的准确度。
要查看统计信息:http://yann.lecun.com/exdb/mnist/
另外,请尝试他的CNN代码:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/convolutional_network.py
答案 1 :(得分:0)
在TensorFlow 2.0中,现在非常容易
History = model.fit(trainX,trainY,validation_data = (testX,testY),batch_size= 100, epochs = epochs,verbose = 1)
train_loss = History.history['loss']
val_loss = History.history['val_loss']
acc = History.history['accuracy']
val_acc = History.history['val_accuracy']
当您拟合模型时,将其存储在变量中。如上所示,您还可以获得每个时期的损失和准确性以及验证损失和准确性。
您可以相应地绘制图形
损失
plt.plot(History.history['loss'])
plt.plot(History.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(['Train', 'Test'])
plt.show()
准确性
plt.plot(History.history['accuracy'])
plt.plot(History.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.legend(['Train', 'Test'])
plt.show()