Natural Language Processing

PyTorch Basics: Understanding Autograd and Computation Graphs

- context
- intent

- pytorch - deep learning framework
- nlp fundamental methods to understand text

Bag of Words

bag of words representation

the cat sat on the mat  -> [2,1,0,1,1,1]
the dog sat on the cat

Sequential Representation

PyTorch

- it have tensors(data structure) with hardware accelerations (GPUs)
- dynamic computational graphs(data structures)

int -> 2 bytes (16 bits)

int a = 10;

float a = 10.65; -> 4 bytes(32 bits)

Neural Network
- forward pass
- backward pass

A-> B
|
C-> X

Tensors

import torch

x = torch.tensor([1,2])

print(x)

tensor([1, 2])

x = torch.tensor([1,2])
y = torch.tensor([3,4])

print(x * y)

tensor([3, 8])

x = torch.tensor([[1,2], [5,3], [0,4]])

print(x)

tensor([[1, 2],
        [5, 3],
        [0, 4]])

print(x[0][1])

tensor(2)

print(x[0][1].item())

2

print(x.shape)

torch.Size([3, 2])

Neural Network in PyTorch

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('train.csv')
df.head()

# 28x28 -> 784 pixels

from torch import nn, optim
import torch.nn.functional as F
import torch.utils.data as data
import torch
from torch.autograd import Variable

# plt.imshow(np.reshape(df.values[0][1:], (28,28)))
# plt.show()

train = pd.read_csv('train.csv')
train_labels = train['label'].values
train = train.drop('label', axis=1).values.reshape(len(train), 1,28,28)

x = torch.Tensor(train.astype(float))
y = torch.Tensor(train_labels).long()

# build the classifier

class MNISTClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 392)
        self.fc2 = nn.Linear(392, 196)
        self.fc3 = nn.Linear(196, 98)
        self.fc4 = nn.Linear(98, 10)

        self.dropout = nn.Dropout(p=0.2)

    def forward(self, x):
        x = x.view(x.shape[0], -1)                 # doubt
        x = self.dropout(F.relu(self.fc1(x)))
        x = self.dropout(F.relu(self.fc2(x)))
        x = self.dropout(F.relu(self.fc3(x)))
        x = F.log_softmax(self.fc4(x), dim=1)

        return x

model = MNISTClassifier()

loss_function = nn.NLLLoss()
opt = optim.Adam(model.parameters(), lr=0.001)

for epoch in range(50):
    images = Variable(x)
    labels = Variable(y)

    opt.zero_grad()
    outputs = model(images)

    loss = loss_function(outputs, labels)
    loss.backward()
    opt.step()
    print("Epoch [%d/%d] Loss: %.4f" %(epoch + 1, 50, loss.data.item()))

test = pd.read_csv('test.csv')
test_labels = test['label'].values
test = test.drop('label', axis=1).values.reshape(len(test), 1,28,28)

xtest = torch.Tensor(train.astype(float))
ytest = torch.Tensor(train_labels).long()

preds = model(xtest)

print(preds[0])

tensor([-19.8444,  -0.0893,  -8.2741, -11.2113, -11.4445,  -6.5766, -14.6546,
         -5.5649,  -2.5265, -12.1194], grad_fn=<SelectBackward0>)

_, predictionlabel = torch.max(preds.data, 1)
predictionlabel = predictionlabel.tolist()

predictionlabel = pd.Series(predictionlabel)
test_labels = pd.Series(test_labels)

pred_table = pd.concat([predictionlabel, test_labels], axis=1)
pred_table.columns = ['Predicted value', 'True Value']

pred_table.head()

   Predicted value  True Value
0                1         9.0
1                0         5.0
2                1         2.0
3                4         4.0
4                0         1.0

Word Embeddings

word into numerical representation(one-hot encoding)

- semantic relationship

cat == cat

cat <==> dog

Global Vector for Word Embeddings

GloVe: Global Vectors for Word Representation

def loadGlove(path):
    file = open(path, 'r', encoding='utf8')
    model = {}

    for l in file:
        line = l.split()
        word = line[0]
        value = np.array([float(val) for val in line[1:]])
        model[word] = value

    return model

glove = loadGlove('glove.6B.50d.txt')

glove['python']   # vector embedding for the word Python

array([ 0.5897  , -0.55043 , -1.0106  ,  0.41226 ,  0.57348 ,  0.23464 ,
       -0.35773 , -1.78    ,  0.10745 ,  0.74913 ,  0.45013 ,  1.0351  ,
        0.48348 ,  0.47954 ,  0.51908 , -0.15053 ,  0.32474 ,  1.0789  ,
       -0.90894 ,  0.42943 , -0.56388 ,  0.69961 ,  0.13501 ,  0.16557 ,
       -0.063592,  0.35435 ,  0.42819 ,  0.1536  , -0.47018 , -1.0935  ,
        1.361   , -0.80821 , -0.674   ,  1.2606  ,  0.29554 ,  1.0835  ,
        0.2444  , -1.1877  , -0.60203 , -0.068315,  0.66256 ,  0.45336 ,
       -1.0178  ,  0.68267 , -0.20788 , -0.73393 ,  1.2597  ,  0.15425 ,
       -0.93256 , -0.15025 ])