Professional Documents
Culture Documents
0.0.1 Implementation of Recurrent Neural Network: #Importing The Required Libraries
0.0.1 Implementation of Recurrent Neural Network: #Importing The Required Libraries
[ ]: import torchtext
tokenizer = get_tokenizer("basic_english")
def build_vocabulary(datasets):
for dataset in datasets:
for _, text in dataset:
yield tokenizer(text)
vocab = build_vocab_from_iterator(build_vocabulary([train_dataset,␣
,→test_dataset]), min_freq=1, specials=["<UNK>"])
vocab.set_default_index(vocab["<UNK>"])
[ ]: len(vocab)
[ ]: 98635
1
[ ]: tokens = tokenizer("Hello how are you?, Welcome to Recureent Neural Network")
indexes = vocab(tokens)
tokens, indexes
[ ]: (['hello',
'how',
'are',
'you',
'?',
',',
'welcome',
'to',
'recureent',
'neural',
'network'],
[12388, 355, 42, 164, 80, 3, 3298, 4, 0, 82087, 200])
[ ]: 0
max_words = 25
def vectorize_batch(batch):
Y, X = list(zip(*batch))
X = [vocab(tokenizer(text)) for text in X]
X = [tokens+([0]* (max_words-len(tokens))) if len(tokens)<max_words else␣
,→tokens[:max_words] for tokens in X] ## Bringing all samples to max_words␣
,→length.
2
test_loader = DataLoader(test_dataset , batch_size=1024,␣
,→collate_fn=vectorize_batch)
[ ]: for X, Y in train_loader:
print(X.shape, Y.shape)
break
embed_len = 50
hidden_dim = 50
n_layers=1
class RNNClassifier(nn.Module):
def __init__(self):
super(RNNClassifier, self).__init__()
self.embedding_layer = nn.Embedding(num_embeddings=len(vocab),␣
,→embedding_dim=embed_len)
return self.linear(output[:,-1])
[ ]: rnn_classifier = RNNClassifier()
rnn_classifier
[ ]: RNNClassifier(
(embedding_layer): Embedding(98635, 50)
(rnn): RNN(50, 50, batch_first=True)
(linear): Linear(in_features=50, out_features=4, bias=True)
)
3
Layer : Embedding(98635, 50)
Parameters :
torch.Size([98635, 50])
out.shape
[ ]: torch.Size([1024, 4])
Y_shuffled.append(Y)
Y_preds.append(preds.argmax(dim=-1))
Y_shuffled = torch.cat(Y_shuffled)
Y_preds = torch.cat(Y_preds)
4
for i in range(1, epochs+1):
losses = []
for X, Y in tqdm(train_loader):
Y_preds = model(X)
loss = loss_fn(Y_preds, Y)
losses.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
epochs = 15
learning_rate = 1e-3
loss_fn = nn.CrossEntropyLoss()
rnn_classifier = RNNClassifier()
optimizer = Adam(rnn_classifier.parameters(), lr=learning_rate)
5
100%|����������| 118/118 [00:20<00:00, 5.63it/s]
Train Loss : 0.554
0%| | 0/118 [00:00<?, ?it/s]
Valid Loss : 0.569
Valid Acc : 0.791
100%|����������| 118/118 [00:20<00:00, 5.73it/s]
Train Loss : 0.478
1%| | 1/118 [00:00<00:22, 5.16it/s]
Valid Loss : 0.507
Valid Acc : 0.821
100%|����������| 118/118 [00:20<00:00, 5.68it/s]
Train Loss : 0.423
1%| | 1/118 [00:00<00:21, 5.48it/s]
Valid Loss : 0.485
Valid Acc : 0.834
100%|����������| 118/118 [00:23<00:00, 5.10it/s]
Train Loss : 0.384
0%| | 0/118 [00:00<?, ?it/s]
Valid Loss : 0.464
Valid Acc : 0.843
100%|����������| 118/118 [00:22<00:00, 5.16it/s]
Train Loss : 0.352
1%| | 1/118 [00:00<00:23, 4.94it/s]
Valid Loss : 0.447
Valid Acc : 0.851
100%|����������| 118/118 [00:26<00:00, 4.52it/s]
Train Loss : 0.325
0%| | 0/118 [00:00<?, ?it/s]
Valid Loss : 0.439
Valid Acc : 0.853
100%|����������| 118/118 [00:23<00:00, 5.09it/s]
Train Loss : 0.302
0%| | 0/118 [00:00<?, ?it/s]
6
Valid Loss : 0.436
Valid Acc : 0.856
100%|����������| 118/118 [00:24<00:00, 4.79it/s]
Train Loss : 0.285
1%| | 1/118 [00:00<00:23, 5.09it/s]
Valid Loss : 0.417
Valid Acc : 0.864
100%|����������| 118/118 [00:21<00:00, 5.50it/s]
Train Loss : 0.265
1%| | 1/118 [00:00<00:23, 5.07it/s]
Valid Loss : 0.426
Valid Acc : 0.862
100%|����������| 118/118 [00:20<00:00, 5.76it/s]
Train Loss : 0.253
0%| | 0/118 [00:00<?, ?it/s]
Valid Loss : 0.413
Valid Acc : 0.864
100%|����������| 118/118 [00:23<00:00, 5.02it/s]
Train Loss : 0.238
0%| | 0/118 [00:00<?, ?it/s]
Valid Loss : 0.408
Valid Acc : 0.870
100%|����������| 118/118 [00:21<00:00, 5.54it/s]
Train Loss : 0.224
Valid Loss : 0.416
Valid Acc : 0.869
7
Y_actual, Y_preds = MakePredictions(rnn_classifier, test_loader)
Classification Report :
precision recall f1-score support
Confusion Matrix :
[[1642 84 109 65]
[ 56 1782 24 38]
[ 96 19 1579 206]
[ 62 35 196 1607]]
normalize=True,
title="Confusion Matrix",
cmap="Purples",
hide_zeros=True,
figsize=(5,5)
);
plt.xticks(rotation=90);
8
9