我对深度学习非常陌生,这几乎是我的第一个代码。 我的问题是,我的培训准确性几乎达到100%,但我的测试准确性仅为0.12。 我尝试使用正则化,因为我认为过拟合是问题所在。但是,这可能不是问题,因为在添加了一些辍学和正则化之后,测试准确性下降了..大声笑。 在测试测试准确性时,我是否不使用我训练的变量? 非常感谢。
x = tf.placeholder(tf.float32, shape = [None, 128, 18, 1])
y = tf.placeholder(tf.float32, shape = [None, 6])
is_train = tf.placeholder(tf.bool, name = 'is_train')
keep_prob = tf.placeholder(tf.float32)
y_pred, logits = CNN_model_2(x, 6)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = logits))
regularization_losses = tf.losses.get_regularization_losses()
loss = tf.add_n([loss] + regularization_losses)
optimizer = tf.train.AdamOptimizer(learning_rate = 0.001).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()
save_dir = 'D:\AMD\Only_oneoverten_sec\Log'
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch = next_batch(50, training_features, training_labels)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict = {x: batch[0], y: batch[1], keep_prob: 1.0})
loss_print = loss.eval(feed_dict = {x: batch[0], y: batch[1], keep_prob: 1.0})
print("Epoch: %d, Training Set Accuracy: %f, Loss: %f" % (i, train_accuracy, loss_print))
sess.run(optimizer, feed_dict = {x: batch[0], y: batch[1], keep_prob:0.8})
saver.save(sess, os.path.join(save_dir, 'CNN_model'), global_step = 1000)
test_accuracy = 0
for i in range(20):
test_batch = next_batch(20, testing_features, testing_labels)
test_accuracy = test_accuracy + accuracy.eval(feed_dict = {x: test_batch[0], y: test_batch[1], keep_prob: 1.0})
test_accuracy = test_accuracy / 20
print("Test accuracy: %f" %test_accuracy)
答案 0 :(得分:0)
使用训练有素的变量可以,但是您的算法在这些标签中只是错误的
for i in range(20):
test_batch = next_batch(20, testing_features, testing_labels)
test_accuracy = test_accuracy + accuracy.eval(feed_dict = {x: test_batch[0], y: test_batch[1], keep_prob: 1.0})
test_accuracy = test_accuracy / 20
更改如下:
test_accuracy =0
for i in range(20):
test_batch = next_batch(20, testing_features, testing_labels)
acc = accuracy.eval(feed_dict = {x: test_batch[0], y: test_batch[1], keep_prob: 1.0})
test_accuracy += acc / 20
或:
for i in range(20):
test_batch = next_batch(20, testing_features, testing_labels)
test_accuracy = test_accuracy + accuracy.eval(feed_dict = {x: test_batch[0], y: test_batch[1], keep_prob: 1.0})
test_accuracy = test_accuracy / 20