Assignment 2.4: Text classification via CNN (20 points)

In this assignment you should perform sentiment analysis of the IMDB reviews based on CNN architecture. Read carefully Convolutional Neural Networks for Sentence Classification by Yoon Kim.

In [ ]:
import numpy as np
import torch

import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from torchtext import datasets
from torchtext.data import Field, LabelField
from torchtext.data import Iterator

Preparing Data

In [ ]:
TEXT = Field(sequential=True, lower=True, batch_first=True)
LABEL = LabelField(batch_first=True)
In [ ]:
train, tst = datasets.IMDB.splits(TEXT, LABEL)
trn, vld = train.split()
In [ ]:
# %%time
TEXT.build_vocab(trn)
In [ ]:
LABEL.build_vocab(trn)

Creating the Iterator (2 points)

Define an iterator here

In [ ]:
train_iter, val_iter, test_iter = 
# =============================
#      Write code here
# =============================

Define CNN-based text classification model (8 points)

In [ ]:
class CNN(nn.Module):
    def __init__(self, V, D, kernel_sizes, dropout=0.5):
        super(CNN, self).__init__()
        
        # =============================
        #      Write code here
        # =============================
        
    def forward(self, x):
        
        # =============================
        #      Write code here
        # =============================
        
        return logit
In [ ]:
kernel_sizes = [3,4,5]
vocab_size = len(TEXT.vocab)
dropout = 0.5
dim = 300

model = CNN(vocab_size, dim, kernel_sizes, dropout)
In [ ]:
model.cuda()

The training loop (3 points)

Define the optimization function and the loss functions.

In [ ]:
opt = # your code goes here
loss_func = # your code goes here

Think carefully about the stopping criteria.

In [ ]:
epochs = # your code goes here
In [ ]:
%%time
for epoch in range(1, epochs + 1):
    running_loss = 0.0
    running_corrects = 0
    model.train() 
    for batch in train_iter:         
        
        x = batch.text
        y = batch.label
        
        opt.zero_grad()
        preds = model(x)
        loss = loss_func(preds, y)
        loss.backward()
        opt.step()
        running_loss += loss.item()
        
    epoch_loss = running_loss / len(trn)
    
    val_loss = 0.0
    model.eval()
    correct = 0
    total = 0 
    for batch in val_iter:
        
        x = batch.text
        y = batch.label
        
        preds = model(x)
        loss = loss_func(preds, y)
        val_loss += loss.item()
        
    val_loss /= len(vld)
    
    print('Epoch: {}, Training Loss: {}, Validation Loss: {}'.format(epoch, epoch_loss, val_loss))

Calculate performance of the trained model (2 points)

In [ ]:
for batch in test_iter:
    x = batch.text
    y = batch.label

Write down the calculated performance

Accuracy:

Precision:

Recall:

F1:

Experiments (5 points)

Experiment with the model and achieve better results. Implement and describe your experiments in details, mention what was helpful.

1. ?

2. ?

3. ?