Kaggle competition: Natural Language Processing with Disaster Tweets
batch_size = 88<prepare_x_and_y>
import os
import pandas as pd
import shutil
from eda import *
from csv2json import *
class MNISTDataset(Dataset):

 def __init__(self, feature, target=None, transform=None):
self.X = feature
self.y = target
self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.transform is not None:
return self.transform(self.X[idx]), self.y[idx]
elif self.y is None:
return [self.X[idx]]
return self.X[idx], self.y[idx]<create_dataframe>
sample_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )
data_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2)) ,
transforms.ToTensor() ])
 train_set = MNISTDataset(featuresTrain.float() , targetsTrain, transform=data_transform)
 validate_set = MNISTDataset(featuresValidation.float() , targetsValidation)
 test_set = MNISTDataset(Test.float() )<prepare_x_and_y>
train_data = prepare_data(train_data)
 test_data = prepare_data(test_data )
train_set = torch.utils.data.TensorDataset(featuresTrain.float() , targetsTrain)
 validate_set = torch.utils.data.TensorDataset(featuresValidation.float() , targetsValidation)
 test_set = torch.utils.data.TensorDataset(Test.float() )<load_pretrained>
create_sample_no_lambdas('/kaggle/working', train_data, 'train')
 create_sample_no_lambdas('/kaggle/working', test_data, 'test')
 create_sample_no_lambdas('/kaggle/working', test_data, 'dev' )
train_loader = torch.utils.data.DataLoader(train_set, batch_size = batch_size, shuffle = True)
 validate_loader = torch.utils.data.DataLoader(validate_set, batch_size = batch_size, shuffle = False)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size = batch_size, shuffle = False )<import_modules>
if os.path.exists('/kaggle/working/results'):
shutil.rmtree('/kaggle/working/results')
 !python run_classifier_TABSA.py \
--task_name rambler2011_json_NLI_M \
--data_dir /kaggle/working/ \
--vocab_file '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/vocab.txt' \
--bert_config_file '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/bert_config.json' \
--init_checkpoint '/kaggle/input/my-practices-for-nlp-real-or-not/NLPRealOrNot/conversational_cased_L-12_H-768_A-12_v1/pytorch_model.bin' \
--eval_test \
--do_lower_case \
--max_seq_length 512 \
--train_batch_size 12 \
--learning_rate 2e-5 \
--num_train_epochs 1.0 \
--output_dir /kaggle/working/results/ \
--seed 42
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self ).__init__()
self.cnn = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(0.25),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout(0.25))
self.classifier = nn.Sequential(nn.Linear(576, 256),
nn.Dropout(0.5),
nn.Linear(256, 10))
def forward(self, x):
x = self.cnn(x)
 x = x.view(x.size(0), -1)
 x = self.classifier(x)
 return x<choose_model_class>
data = pd.read_csv('/kaggle/working/test_NLI_M.tsv', sep='\t')
 results = []
with open('/kaggle/working/results/test_ep_1.txt')as f:
for line in f:
results.append(int(line.split() [0]))
model = CNNModel()
optimizer = optim.RMSprop(model.parameters() , lr=0.001, alpha=0.9)
 criterion = nn.CrossEntropyLoss()
lr_reduction = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=3, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0.00001)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()<define_variables>
data['answer'] = results
count = 0
loss_list = []
iteration_list = []
average_training_accuracy = []
average_validation_accuracy = []
average_training_loss = []
average_validation_loss = []<train_model>
sample_submission = sample_submission.set_index('id' ).join(data.set_index('tonal_word')) ['answer']
def train(epoch):
global count
model.train()
for batch_idx,(data, target)in enumerate(train_loader):
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
loss_list.append(loss.item())
 iteration_list.append(count)
 count += 1 
<compute_train_metric>
sample_submission = sample_submission.rename(columns={"id": "id", "answer": "target"} )
def evaluate(data_loader, validate=False):
model.eval()
loss = 0
correct = 0
for data, target in data_loader:
data, target = Variable(data), Variable(target)
 if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss += F.cross_entropy(output, target, size_average=False ).item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(data_loader.dataset)
 accuracy = 100.* correct / len(data_loader.dataset)
 if not validate:
lr_reduction.step(loss)
 average_training_accuracy.append(accuracy)
 average_training_loss.append(loss)
 else:
average_validation_accuracy.append(accuracy)
 average_validation_loss.append(loss)
 '.format(loss, correct, len(data_loader.dataset), accuracy))<predict_on_test>
encoder = {1:0, 0:1}
sample_submission['target'] = sample_submission['target'].map(encoder )
def prediciton(data_loader):
model.eval()
test_pred = torch.LongTensor()
for i, data in enumerate(data_loader):
data = Variable(data[0])
 if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred
test_pred = prediciton(test_loader )<prepare_output>
sample_submission.to_csv('/kaggle/working/submission.csv', index=False )