test_wrong = T.neq(T.argmax(test_prediction, axis=1), T.argmax(target_var, axis=1))
# Compile a second function computing the validation loss and accuracy:
# val_fn = theano.function([input_var, target_var, labeled_var], [loss2*lr[1], test_acc], allow_input_downcast=True)
val_fn = theano.function([input_var, target_var, labeled_var],
                         [test_loss,
                          losses_ratio[0] * test_loss1.mean(),
                          losses_ratio[1] * test_loss2.mean(),
                          losses_ratio[2] * l2_penalties.mean(),
                          classification, test_acc, test_wrong],
                         allow_input_downcast=True, on_unused_input='ignore')

# ----------------------------RUN-----------------------------------#
MODE = input('"TEST" OR "TRAIN"?\n')
if MODE == 'TEST':
    # load saved best model
    io.read_model_data([unsupervised_graph, supervised_graph], BEST_MODEL_PATH)
    run_test(val_fn, teX, teY)
elif MODE == 'TRAIN':
    # if last model exists, load last model:
    best_validation_acc = 0
    best_validation_err = float('inf')
    if os.path.isfile(LAST_MODEL_PATH):
        choice = input(
            'PREVIOUS MODEL FOUND, CONTINUE TRAINING OR OVERRIDE OR END? (ANSWER: "CONTINUE", "OVERRIDE", "END")\n')
        if choice == 'CONTINUE':
            best_validation_err, best_validation_acc, old_lr = io.read_model_data(
                [unsupervised_graph, supervised_graph], LAST_MODEL_PATH)
            sgd_lr.set_value(old_lr)
        elif choice == 'OVERRIDE':
            best_validation_acc = 0
            best_validation_err = float('inf')
                  dtype=theano.config.floatX)
# Compile a second function computing the validation loss and accuracy:
#val_fn = theano.function([input_var, target_var, labeled_var], [loss2*lr[1], test_acc], allow_input_downcast=True)
val_fn = theano.function([input_var, target_var, labeled_var],
                         [test_loss,
                          lr[0]*test_loss1.mean(),
                          lr[1]*test_loss2.mean(),
                          lr[2]*l2_penalties.mean(),
                          test_acc], allow_input_downcast=True,
                         on_unused_input='ignore')

# ----------------------------RUN-----------------------------------#
MODE = input('"TEST" OR "TRAIN"?\n')
if MODE == 'TEST':
    # load saved best model
    io.read_model_data([unsupervised_graph, supervised_graph], BEST_MODEL_PATH)
    run_test(val_fn, teX, teY)
elif MODE == 'TRAIN':
    # if last model exists, load last model:
    best_validation_acc = 0
    if os.path.isfile(LAST_MODEL_PATH + '.' + io.PARAM_EXTENSION):
        choice = input(
            'PREVIOUS MODEL FOUND, CONTINUE TRAINING OR OVERRIDE OR END? (ANSWER: "CONTINUE", "OVERRIDE", "END")\n')
        if choice == 'CONTINUE':
            best_validation_acc = io.read_model_data([unsupervised_graph, supervised_graph], BEST_MODEL_PATH)
        elif choice == 'OVERRIDE':
            best_validation_acc = 0
        else:
            sys.exit('Terminated by user choice.')
    print 'Training...'
    # number of epochs to train