Kaggle competition: Sentence Relatedness
LogisticRegression=LogisticRegression(random_state=2019)
 model_LogisticRegression_OVR = OneVsRestClassifier(LogisticRegression)
 scores_LogisticRegression = cross_validate(model_LogisticRegression_OVR, X_train, y_train, cv=3)
 print("The test accuracy of LogisticRegression is {}".format(scores_LogisticRegression['test_score'].mean()))<save_to_csv>
def tokenize(text):
return text.split()
def proc_float(value):
return float(value)
 def proc_int(value):
return int(value)
 class DropoutField(Field):
def __init__(self,alpha=0.1, **kwargs):
self.train()
self.alpha = alpha
super(DropoutField, self ).__init__(kwargs)
 def train(self):
self.train_mode = True
def eval(self):
self.train_mode = False
def numericalize(self, arr, device=None):
def sample_unk(token,alpha=0.5):
if token == '<pad>':
return token
C = self.vocab.freqs[token]
if C == 0:
return '<unk>'
r = random()
if r <(self.alpha/C):
return '<unk>'
return token
if self.train_mode:
transformed = []
for sentence in arr:
transformed.append([sample_unk(elt,alpha=self.alpha)for elt in sentence])
 else:
transformed = arr
return super(DropoutField, self ).numericalize(transformed,device)
 TEXT = Field(sequential=True, tokenize=tokenize,pad_first=True)
 FLOAT = Field(sequential=False, use_vocab=False,dtype=torch.float,preprocessing=proc_float)
 INTEGER = Field(sequential=False, use_vocab=False,preprocessing=proc_int)
 df_train = TabularDataset(".. /input/SICK_train_logistic.txt","tsv",skip_header=True,\
fields=[('idx',INTEGER),('sentA',TEXT),('sentB',TEXT),('Relatedness',FLOAT)])
 df_dev = TabularDataset(".. /input/SICK_dev_logistic.txt","tsv",skip_header=True,\
fields=[('idx',INTEGER),('sentA',TEXT),('sentB',TEXT),('Relatedness',FLOAT)])
 TEXT.build_vocab(df_train)
 print(0,TEXT.vocab.itos[0])
 print(1,TEXT.vocab.itos[1])
 print(2,TEXT.vocab.itos[2])
 print(3,TEXT.vocab.itos[3])
 print(4,TEXT.vocab.itos[4])
 print("train size",len(df_train))
print("dev size",len(df_dev))
print('vocab size',len(TEXT.vocab))
for elt in df_dev[:10]:
print(elt.idx,' '.join(elt.sentA),'||',' '.join(elt.sentB),elt.Relatedness )
model_SVC_OVR.fit(X_train,y_train)
 y_pred=model_SVC_OVR.predict(X_test)
 y_pred=LabelEncoder.inverse_transform(y_pred)
 test['cuisine']=y_pred
test[['id','cuisine']].to_csv("submission.csv",index=False)
<import_modules>
class ParaphraseClassifier(nn.Module):
def __init__(self,hidden_dim,embedding_dim):
super(ParaphraseClassifier, self ).__init__()
self.hidden_dim = hidden_dim
self.embedding_dim = embedding_dim
self.embedding = nn.Embedding(len(TEXT.vocab), embedding_dim)
 self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=1,bidirectional=False)
 self.Wadd = nn.Linear(hidden_dim,hidden_dim)
 self.Wtimes = nn.Linear(hidden_dim,hidden_dim)
 self.Wout = nn.Linear(hidden_dim,1)
 def forward(self,xinputA,xinputB):

 xembeddedA = self.embedding(xinputA)
 lstm_outA,(hiddenA,cellA)= self.lstm(xembeddedA.view(len(xinputA), -1, self.embedding_dim), None)
 xembeddedB = self.embedding(xinputB)
 lstm_outB,(hiddenB,cellB)= self.lstm(xembeddedB.view(len(xinputB), -1, self.embedding_dim), None)
 hiddenT = hiddenA * hiddenB
hiddenD = torch.abs(hiddenA - hiddenB)
 hidden = torch.tanh(self.Wtimes(hiddenT)+ self.Wadd(hiddenD))
return torch.sigmoid(self.Wout(hidden))
def run_test(self,test_set,pred_filename,useGPU=True):
device_code = int(useGPU-1)
 test_iterator = Iterator(test_set,batch_size=64,device=device_code,train=False,sort=False)
 idxList = []
predList = []
with torch.no_grad() :
for batch in test_iterator:
idx,xvecA,xvecB = batch.idx,batch.sentA,batch.sentB
prob = 10 * self.forward(xvecA,xvecB ).squeeze() / 2.0
idxList.extend(idx.tolist())
 predList.extend(prob.tolist())
 df_submission = pa.DataFrame({'pairID':idxList,'Relatedness':predList})
 df_submission.to_csv(pred_filename , index=False)
 print(df_submission.head(n=10))
print("done.")
 def run_train(self,train_set,dev_set,epochs,learning_rate=0.001,useGPU=True):
loss_func = nn.BCELoss()
optimizer = optim.Adam(self.parameters() , lr=learning_rate,weight_decay=0.00001)
 self.embedding = nn.Embedding(len(TEXT.vocab), self.embedding_dim)
 device_code = int(useGPU-1)
 train_iterator = BucketIterator(train_set,batch_size=64, device=device_code, sort=True,sort_key=lambda x:x.sentA,train=True)
 dev_iterator = BucketIterator(dev_set, batch_size=64, device=device_code, sort=True,sort_key=lambda x:x.sentA,train=False)
 max_corr = 0.0
for e in range(epochs):
train_loss = 0
N = 0
train_predictions = []
train_reference = []
for batch in train_iterator:
xvecA,xvecB,yRelness = batch.sentA,batch.sentB,batch.Relatedness
self.zero_grad()
prob = self.forward(xvecA,xvecB ).squeeze()
loss = loss_func(prob,yRelness)
 loss.backward()
optimizer.step()
train_loss += loss.item()
train_predictions.extend(prob.tolist())
 train_reference.extend(yRelness.tolist())
 N += 1
R = np.corrcoef(np.array(train_predictions),np.array(train_reference)) [0][1]
print('Epoch',e,'loss =',train_loss/N,'train correlation(pearson r)=',R)
 with torch.no_grad() :
dev_predictions = []
dev_reference = []
for batch in dev_iterator:
xvecA,xvecB,yRelness = batch.sentA,batch.sentB,batch.Relatedness
prob = self.forward(xvecA,xvecB ).squeeze()
dev_predictions.extend(prob.tolist())
 dev_reference.extend(yRelness.tolist())
 R = np.corrcoef(np.array(dev_predictions),np.array(dev_reference)) [0][1]
print('Epoch',e,'dev correlation(pearson r)=',R)
 if R > max_corr:
torch.save(self.state_dict() , 'para_model.wt')
 max_corr = R
print('model saved.')
 self.load_state_dict(torch.load('para_model.wt'))
import pandas as pd
import numpy as np
import json
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tokenize import RegexpTokenizer
from nltk.stem import WordNetLemmatizer
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
import re
from sklearn.linear_model import LogisticRegression<load_from_disk>
pc = ParaphraseClassifier(150,30)
 pc.run_train(df_train,df_dev,50 )
trainData = pd.read_json('.. /input/train.json')
 testData = pd.read_json('.. /input/test.json')
 trainData.head()<string_transform>
df_test = TabularDataset(".. /input/SICK_test.txt","tsv",skip_header=True,\
fields=[('idx',INTEGER),('sentA',TEXT),('sentB',TEXT)])
 pc.run_test(df_test,'submission.csv',useGPU=True )