我有一个用于预测CIFAR10类的CNN,我已经从MNIST数据集的工作解决方案中进行了改编。
该模型是 image-> conv-> pool-> conv-> pool-> FC-> dropout-> FC-> class_prediction ,我假设比完全连接的网络(我在做CS231 course时达到约50%)要好得多。
我收到了一些奇怪的结果,其中准确性和损失似乎并非真正相互关联:training accuracy and loss graph
如图所示,损失不断下降,这是一件好事,但即使在训练集上,它似乎也不会真正影响准确性。
很抱歉在这里全部转储,但在下面我已经提取了我认为相关的所有代码。如果我可以添加任何进一步的信息,请告诉我。
with tf.name_scope('Model'):
# Construct model
pred = conv_net(x, weights, biases, keep_prob)
with tf.name_scope('Loss'):
# Define loss function and optimizer
net = tf.nn.softmax_cross_entropy_with_logits(pred, y_true, name='Softmax_Loss')
cost = tf.reduce_mean(net)
with tf.name_scope('Adam_Descent'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, name='Adam_Descent').minimize(cost)
# Evaluate the model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y_true, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
#
# Code omitted
#
with tf.Session() as sess:
sess.run(init)
step = 1
# Train for a while
while step * batch_size < training_iters:
batch_x, batch_y = get_rand_train_batch(batch_size, 10000)
# Run optimization
_, summary = sess.run([optimizer, merged_summary_op], feed_dict={x: batch_x,
y_true: batch_y,
keep_prob: 0.75})
if step * index % display_step == 0:
# Calculate loss and accuracy
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y_true: batch_y,
keep_prob: 1})
print_blue("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:2.3f}%".format(acc*100))
summary_writer.add_summary(summary, step * batch_size)
step += 1
# Calculate test accuracy
print("\nTesting Accuracy:", \
sess.run(accuracy, feed_dict={x: X_test[:1000],
y_true: y_test[:1000],
keep_prob: 1.}))