|
| 1 | +import os |
| 2 | +import time |
| 3 | +import load_data |
| 4 | +import torch |
| 5 | +import torch.nn.functional as F |
| 6 | +from torch.autograd import Variable |
| 7 | +import torch.optim as optim |
| 8 | +import numpy as np |
| 9 | +from LSTM import LSTMClassifier |
1 | 10 |
|
| 11 | +TEXT, vocab_size, word_embeddings, train_iter, valid_iter, test_iter = load_data.load_dataset() |
| 12 | + |
| 13 | +def clip_gradient(model, clip_value): |
| 14 | + params = list(filter(lambda p: p.grad is not None, model.parameters())) |
| 15 | + for p in params: |
| 16 | + p.grad.data.clamp_(-clip_value, clip_value) |
| 17 | + |
| 18 | +def train_model(model, train_iter, epoch): |
| 19 | + total_epoch_loss = 0 |
| 20 | + total_epoch_acc = 0 |
| 21 | + model.cuda() |
| 22 | + optim = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters())) |
| 23 | + steps = 0 |
| 24 | + model.train() |
| 25 | + for idx, batch in enumerate(train_iter): |
| 26 | + text = batch.text[0] |
| 27 | + target = batch.label |
| 28 | + target = torch.autograd.Variable(target).long() |
| 29 | + if torch.cuda.is_available(): |
| 30 | + text = text.cuda() |
| 31 | + target = target.cuda() |
| 32 | + if (text.size()[0] is not 32):# One of the batch returned by BucketIterator has length different than 32. |
| 33 | + continue |
| 34 | + optim.zero_grad() |
| 35 | + prediction = model(text) |
| 36 | + loss = loss_fn(prediction, target) |
| 37 | + num_corrects = (torch.max(prediction, 1)[1].view(target.size()).data == target.data).float().sum() |
| 38 | + acc = 100.0 * num_corrects/len(batch) |
| 39 | + loss.backward() |
| 40 | + clip_gradient(model, 1e-1) |
| 41 | + optim.step() |
| 42 | + steps += 1 |
| 43 | + |
| 44 | + if steps % 100 == 0: |
| 45 | + print (f'Epoch: {epoch+1}, Idx: {idx+1}, Training Loss: {loss.item():.4f}, Training Accuracy: {acc.item(): .2f}%') |
| 46 | + |
| 47 | + total_epoch_loss += loss.item() |
| 48 | + total_epoch_acc += acc.item() |
| 49 | + |
| 50 | + return total_epoch_loss/len(train_iter), total_epoch_acc/len(train_iter) |
| 51 | + |
| 52 | +def eval_model(model, val_iter): |
| 53 | + total_epoch_loss = 0 |
| 54 | + total_epoch_acc = 0 |
| 55 | + model.eval() |
| 56 | + with torch.no_grad(): |
| 57 | + for idx, batch in enumerate(val_iter): |
| 58 | + text = batch.text[0] |
| 59 | + if (text.size()[0] is not 32): |
| 60 | + continue |
| 61 | + target = batch.label |
| 62 | + target = torch.autograd.Variable(target).long() |
| 63 | + if torch.cuda.is_available(): |
| 64 | + text = text.cuda() |
| 65 | + target = target.cuda() |
| 66 | + prediction = model(text) |
| 67 | + loss = loss_fn(prediction, target) |
| 68 | + num_corrects = (torch.max(prediction, 1)[1].view(target.size()).data == target.data).sum() |
| 69 | + acc = 100.0 * num_corrects/len(batch) |
| 70 | + total_epoch_loss += loss.item() |
| 71 | + total_epoch_acc += acc.item() |
| 72 | + |
| 73 | + return total_epoch_loss/len(val_iter), total_epoch_acc/len(val_iter) |
| 74 | + |
| 75 | + |
| 76 | +learning_rate = 2e-5 |
| 77 | +batch_size = 32 |
| 78 | +output_size = 2 |
| 79 | +hidden_size = 256 |
| 80 | +embedding_length = 300 |
| 81 | + |
| 82 | +model = LSTMClassifier(batch_size, output_size, hidden_size, vocab_size, embedding_length, word_embeddings) |
| 83 | +loss_fn = F.cross_entropy |
| 84 | + |
| 85 | +for epoch in range(10): |
| 86 | + train_loss, train_acc = train_model(model, train_iter, epoch) |
| 87 | + val_loss, val_acc = eval_model(model, valid_iter) |
| 88 | + |
| 89 | + print(f'Epoch: {epoch+1:02}, Train Loss: {train_loss:.3f}, Train Acc: {train_acc:.2f}%, Val. Loss: {val_loss:3f}, Val. Acc: {val_acc:.2f}%') |
| 90 | + |
| 91 | +test_loss, test_acc = eval_model(model, test_iter) |
| 92 | +print(f'Test Loss: {test_loss:.3f}, Test Acc: {test_acc:.2f}%') |
| 93 | + |
| 94 | +''' Let us now predict the sentiment on a single sentence just for the testing purpose. ''' |
| 95 | +test_sen1 = "This is one of the best creation of Nolan. I can say, it's his magnum opus. Loved the soundtrack and especially those creative dialogues." |
| 96 | +test_sen2 = "Ohh, such a ridiculous movie. Not gonna recommend it to anyone. Complete waste of time and money." |
| 97 | + |
| 98 | +test_sen1 = TEXT.preprocess(test_sen1) |
| 99 | +test_sen1 = [[TEXT.vocab.stoi[x] for x in test_sen1]] |
| 100 | + |
| 101 | +test_sen2 = TEXT.preprocess(test_sen2) |
| 102 | +test_sen2 = [[TEXT.vocab.stoi[x] for x in test_sen2]] |
| 103 | + |
| 104 | +test_sen = np.asarray(test_sen1) |
| 105 | +test_sen = torch.LongTensor(test_sen) |
| 106 | +test_tensor = Variable(test_sen, volatile=True) |
| 107 | +test_tensor = test_tensor.cuda() |
| 108 | +model.eval() |
| 109 | +output = model(test_tensor, 1) |
| 110 | +out = F.softmax(output, 1) |
| 111 | +if (torch.argmax(out[0]) == 1): |
| 112 | + print ("Sentiment: Positive") |
| 113 | +else: |
| 114 | + print ("Sentiment: Negative") |
0 commit comments