예제 #1
0
model_path = "trained_models/128_hidden/run2/best_model_1fc.pt"
model.load_state_dict(torch.load(model_path))
model.eval()
print("Loaded trained model weights successfully!")

#######################################
################## 2. #################
#######################################
os.makedirs("activations", exist_ok=True)
for i, (data, true_labels) in enumerate(testing_dataloader):

    data = data.type(torch.FloatTensor)
    true_labels = true_labels.type(torch.LongTensor)

    stg_activations, tp_activations, ifg_activations, word_predictions = model.out(
        data)

    torch.save(stg_activations, 'activations/stg_{}.pt'.format(i))
    torch.save(tp_activations, 'activations/tp_{}.pt'.format(i))
    torch.save(ifg_activations, 'activations/ifg_{}.pt'.format(i))
    '''
    print(stg_activations.shape) => torch.Size([2130, 3, 120])
    print(tp_activations.shape) => torch.Size([30, 71, 128])
    print(ifg_activations.shape) => torch.Size([30, 71, 128])
    sys.exit(0)
    '''

    print(stg_activations.shape)
    print(tp_activations.shape)
    print(ifg_activations.shape)
best_acc = 0
training_loss = []
test_accuracy = []
for epoch in range(epochs):
    total_loss = 0
    '''
    START EVALUATION PROCEDURE (Start evaluation already before training for comparison)
    '''
    with torch.no_grad():
        correct = 0
        total = 0
        for i, (data, true_labels) in enumerate(testing_dataloader):
            data = data.type(torch.FloatTensor)
            true_labels = true_labels.type(torch.LongTensor)

            output_conv, output_lstm1, output_lstm1, predictions = model.out(
                data)

            total += true_labels.size(0)
            correct += (predictions.max(1)[1] == true_labels).sum().item()
            accuracy = 100 * correct / total

        print('Accuracy of the network on the evaluation dataset: %d %%' %
              (100 * correct / total))

        if accuracy > best_acc:
            best_acc == accuracy
            # SAVE MODEL
            print("SAVING MODEL")
            torch.save(model.state_dict(), "trained_models/best_model.pt")

    test_accuracy.append(accuracy)