vendredi 1 mars 2019

Am I using pre-trained variables on testing set?

I'm quite new to deeplearning and this is almost my first code. My question is, I am getting almost 100 percent training accuracy, but my testing accuracy is only about 0.12. I tried using regularization because I thought that overfitting was the problem. However, this may not be the problem because after I added some dropouts and regularization, the testing accuracy dropped..lol.. Am I not using the variables that I trained when I test my testing accuracy ? Thanks a lot.

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)

Aucun commentaire:

Enregistrer un commentaire