Kaggle competition: Hackathon Sentimento
def known_contractions(embed):
known = []
for contract in contraction_mapping:
if contract in embed:
known.append(contract)
 return known<string_transform>
trainDF = pd.read_csv('.. /input/train.csv')
 trainDF.head()
def clean_contractions(text, mapping):
specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text<feature_engineering>
train_x, val_x, train_y, val_y = train_test_split(trainDF['review'], trainDF['sentiment'], test_size=0.1, shuffle=True )
train['question_text'] = train['question_text'].apply(lambda x: clean_contractions(x, contraction_mapping))
test['question_text'] = test['question_text'].apply(lambda x: clean_contractions(x, contraction_mapping))<concatenate>
tfidf_vect = TfidfVectorizer(analyzer='word', token_pattern=r'\w{1,}', max_features=25000)
 tfidf_vect.fit(trainDF['review'])
 xtrain_tfidf = tfidf_vect.transform(train_x)
 xval_tfidf = tfidf_vect.transform(val_x )
df = pd.concat([train ,test], sort=False)
 vocab = build_vocab(df['question_text'])
 print("Glove: ")
 oov_glove = check_coverage(vocab, embed_glove )<define_variables>
xtrain_tfidf = xtrain_tfidf.toarray()
xval_tfidf = xval_tfidf.toarray()
train_y = np.array(train_y)
 val_y = np.array(val_y )
punct_mapping = "/-'?!.,
punct_mapping += '©^®` <→°€™› ♥←×§″′Â█½à…“★”–●â►−¢²¬░¶↑±¿▾═¦║―¥▓—‹─▒：¼⊕▼▪†■’▀¨▄♫☆é¯♦¤▲è¸¾Ã⋅‘∞∙）↓、│（»，♪╩╚³・╦╣╔╗▬❤ïØ¹≤‡√'
def unknown_punct(embed, punct):
unknown = ''
for p in punct:
if p not in embed:
unknown += p
unknown += ' '
return unknown<categorify>
train_dataset = TensorDataset(torch.from_numpy(xtrain_tfidf ).double() , torch.from_numpy(train_y ).double())
 train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=64)
 val_dataset = TensorDataset(torch.from_numpy(xval_tfidf ).double() , torch.from_numpy(val_y ).double())
 val_dataloader = DataLoader(val_dataset, shuffle=True, batch_size=64)
 dataloaders = {'train': train_dataloader, 'val': val_dataloader}
dataset_sizes = {'train': len(train_dataset), 'val': len(val_dataset)}
puncts = {"‘": "'", "´": "'", "°": "", "€": "e", "—": "-", "–": "-", "’": "'", "_": "-", "`": "'", '“': '"', '”': '"', '“': '"', "£": "e", '∞': 'infinity', 'θ': 'theta', '÷': '/', 'α': 'alpha', '•': '.', 'à': 'a', '−': '-', 'β': 'beta', '∅': '', '³': '3', 'π': 'pi', '…': ' '}
def clean_special_chars(text, punct, mapping):
for p in mapping:
text = text.replace(p, mapping[p])
 for p in punct:
text = text.replace(p, f' {p} ')
 return text<feature_engineering>
epochs = 30
input_dim = 25000
output_dim = 1
lr_rate = 0.001
train['question_text'] = train['question_text'].apply(lambda x: clean_special_chars(x, punct_mapping, puncts))
test['question_text'] = test['question_text'].apply(lambda x: clean_special_chars(x, punct_mapping, puncts))<concatenate>
model = nn.Sequential(nn.Linear(input_dim, 2048),
nn.Dropout(0.5),
nn.ReLU() ,
nn.Linear(2048, 256),
nn.Dropout(0.5),
nn.ReLU() ,
nn.Linear(256, output_dim),
nn.Sigmoid() ).double()
df = pd.concat([train ,test], sort=False)
 vocab = build_vocab(df['question_text'])
 print("Glove: ")
 oov_glove = check_coverage(vocab, embed_glove )<split>
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=lr_rate )
train, val = train_test_split(train, test_size=0.2, random_state=42 )<prepare_x_and_y>
def train_model(model):
model = model.cuda()
best_acc = 0.0
best_model_wts = copy.deepcopy(model.state_dict())
 for epoch in range(int(epochs)) :
train_loss = 0
val_loss = 0
val_acc = 0
model.train()
for inputs, labels in train_dataloader:
inputs, labels = inputs.cuda() , labels.cuda()
optimizer.zero_grad()
outputs = model(inputs)
 loss = criterion(outputs.squeeze() , labels)
 loss.backward()
optimizer.step()
train_loss += loss.item()
else:
model.eval()
num_correct = 0
for inputs, labels in val_dataloader:
inputs, labels = inputs.cuda() , labels.cuda()
outputs = model(inputs)
 predictions = torch.round(outputs.squeeze())
 loss = criterion(predictions, labels)
 val_loss += loss.item()
equals =(predictions == labels.data)
 num_correct += torch.sum(equals.data ).item()
val_acc = num_correct / len(val_dataset)
 if val_acc > best_acc:
best_acc = val_acc
best_model_wts = copy.deepcopy(model.state_dict())
 print('---------Epoch {} -----------'.format(epoch))
print('Train Loss: {:.6f} Val Loss: {:.6f} Val Accuracy: {:.6f}'.format(
 train_loss/len(train_dataset), val_loss/len(val_dataset), val_acc))
model.load_state_dict(best_model_wts)
 return model
xtrain = train['question_text'].fillna('_na_' ).values
xval = val['question_text'].fillna('_na_' ).values
xtest = test['question_text'].fillna('_na_' ).values<feature_engineering>
model = train_model(model )
tokenizer = Tokenizer(num_words=MAX_FEATURES)
 tokenizer.fit_on_texts(list(xtrain))
xtrain = tokenizer.texts_to_sequences(xtrain)
 xval = tokenizer.texts_to_sequences(xval)
 xtest = tokenizer.texts_to_sequences(xtest )<string_transform>
test_df = pd.read_csv('.. /input/test.csv')
 xtest_tfidf = tfidf_vect.transform(test_df['review'])
 xtest_tfidf = xtest_tfidf.toarray()
xtrain = pad_sequences(xtrain, maxlen=MAXLEN)
 xval = pad_sequences(xval, maxlen=MAXLEN)
 xtest = pad_sequences(xtest, maxlen=MAXLEN )<prepare_x_and_y>
test_y = np.zeros(xtest_tfidf.shape[0] )
ytrain = train['target'].values
yval = val['target'].values<prepare_x_and_y>
test_dataset = TensorDataset(torch.from_numpy(xtest_tfidf), torch.from_numpy(test_y))
test_dataloader = DataLoader(test_dataset, shuffle=False, batch_size=64 )
np.random.seed(42)
 trn_idx = np.random.permutation(len(xtrain))
val_idx = np.random.permutation(len(xval))
xtrain = xtrain[trn_idx]
ytrain = ytrain[trn_idx]
xval = xval[val_idx]
yval = yval[val_idx]<load_pretrained>
def predict(model, test_dataloader):
model.eval()
predictions = []
for inputs, _ in test_dataloader:
inputs = inputs.cuda()
output = model(inputs)
 preds = torch.round(output)
 predictions.extend([p.item() for p in preds])
 return predictions
predictions = predict(model, test_dataloader )
print("Extracting GloVe embedding matrix")
 embedding_matrix_glove = load_glove_matrix(tokenizer.word_index, embed_glove)
 print("Embedding matrix extraction done" )<import_modules>
sub_df = pd.DataFrame()
sub_df['Id'] = test_df['Id']
sub_df['sentiment'] = [int(p)for p in predictions]
from sklearn import metrics
from sklearn.linear_model import LinearRegression
from keras.layers import Dense, Input, CuDNNLSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D
from keras.layers import Bidirectional, GlobalMaxPool1D, GlobalMaxPooling1D, GlobalAveragePooling1D
from keras.layers import Input, Embedding, Dense, Conv2D, MaxPool2D, concatenate, Lambda
from keras.layers import Reshape, Flatten, Concatenate, Dropout, SpatialDropout1D
from keras.optimizers import Adam
from keras.models import Model
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints, optimizers, layers
from keras.layers import concatenate<set_options>
sub_df.to_csv('my_submission.csv', index=False)