Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<choose_model_class>
stop = set(stopwords.words('english'))
print('Imports Done' )
def ema_model(target_model, source_model, alpha):
target_params = target_model.state_dict()
source_params = source_model.state_dict()
for param_name, param in source_params.items() :
target_params[param_name] *= alpha
target_params[param_name] +=(1-alpha)* param
def ma_model(target_model, source_model, n):
target_params = target_model.state_dict()
source_params = source_model.state_dict()
for param_name, param in source_params.items() :
target_params[param_name] *=(n /(n+1))
target_params[param_name] +=(1/(n+1)) * param
def copy_model_params(target_model, source_model):
target_model.load_state_dict(source_model.state_dict())
 if use_averaging_model:
ma_count = 0
netG_ma = copy.deepcopy(netG)
<compute_train_metric>
train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 print('Train df shape: ' + str(train.shape))
test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 print('Validation/Test df shape: ' + str(test.shape))
test.head()
def get_lossD_sGAN() :
criterion = nn.BCELoss()
def lossD_real(outputR, real_labels):
return criterion(outputR, real_labels)
 def lossD_fake(outputF, fake_labels):
return criterion(outputF, fake_labels)
 return lossD_real, lossD_fake
def get_lossG_sGAN() :
criterion = nn.BCELoss()
def lossG(outputR, outputF, real_labels, fake_labels):
return criterion(outputF, real_labels)
 return lossG
def get_lossD_RaLSGAN() :
def lossD_rerative(outputR, outputF, real_labels, fake_labels):
errD =(torch.mean(( outputR - torch.mean(outputF)- real_labels)** 2)+
torch.mean(( outputF - torch.mean(outputR)- fake_labels)** 2)) /2
return errD
return lossD_rerative
def get_lossG_RaLSGAN() :
def lossG_rerative(outputR, outputF, real_labels, fake_labels):
errG =(torch.mean(( outputR - torch.mean(outputF)+ real_labels)** 2)+
torch.mean(( outputF - torch.mean(outputR)+ fake_labels)** 2)) /2
return errG
return lossG_rerative
loss_type = config.loss.name
if loss_type=='sGAN':
print('loss type: sGAN')
 criterionD_real, criterionD_fake = get_lossD_sGAN()
criterionG = get_lossG_sGAN()
relative_loss = False
else:
print('loss type: RaLSGAN')
 criterionD = get_lossD_RaLSGAN()
criterionG = get_lossG_RaLSGAN()
relative_loss = True<define_variables>
df = pd.concat([train, test])
 df.shape
def remove_url(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 def remove_punctuation(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 df['text']=df['text'].apply(lambda x : remove_punctuation(x))
df['text']=df['text'].apply(lambda x : remove_emoji(x))
df['text']=df['text'].apply(lambda x : remove_html(x))
df['text']=df['text'].apply(lambda x : remove_url(x))
df.head()
output_dir = '.. /tmp'
outimg_dir = '.. /tmp/output_images'
if not os.path.exists(output_dir):
os.mkdir(output_dir)
 if not os.path.exists(outimg_dir):
os.mkdir(outimg_dir)
 start_time = tic()
lossD = []
lossG = []
for epoch in range(epochs):
elasped_time = toc(start_time)
 print('elasped_time[h]: ', elasped_time/3600)
 if elasped_time/3600 > 8.5:
break
lossD_epoch = 0
lossG_epoch = 0
for ii,(real_images, bleed_labels)in tqdm(enumerate(train_loader), total=len(train_loader)) :
real_images = real_images.to(device)
 bleed_labels = bleed_labels.to(device)
 bleed_labels[:] = 0
batch_size = real_images.size(0)
 if isinstance(real_label, list):
real_labels = torch.empty(( batch_size, 1), device=device ).uniform_(real_label[0], real_label[1])
 fake_labels = torch.empty(( batch_size, 1), device=device ).uniform_(fake_label[0], fake_label[1])
 else:
real_labels = torch.full(( batch_size, 1), real_label, device=device)
 fake_labels = torch.full(( batch_size, 1), fake_label, device=device)
 netD.zero_grad()
outputR = netD(real_images, bleed_labels)
 if relative_loss:
noise = torch.randn(batch_size, nz, 1, 1, device=device)
 fake = netG(noise, bleed_labels)
 outputF = netD(fake.detach() , bleed_labels)
 errD = criterionD(outputR, outputF, real_labels, fake_labels)
 errD.backward(retain_graph=True)
 else:
errD_real = criterionD_real(outputR, real_labels)
 errD_real.backward()
noise = torch.randn(batch_size, nz, 1, 1, device=device)
 fake = netG(noise, bleed_labels)
 outputF = netD(fake.detach() , bleed_labels)
 errD_fake = criterionD_fake(outputF, fake_labels)
 errD_fake.backward()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
outputF = netD(fake, bleed_labels)
 errG = criterionG(outputR, outputF, real_labels, fake_labels)
 errG.backward()
optimizerG.step()
lossD_epoch += errD.item()
lossG_epoch += errG.item()
if(ii+1)%(len(train_loader)//2)== 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f'
%(epoch + 1, epochs, ii+1, len(train_loader),
errD.item() , errG.item()))
if use_averaging_model:
if epoch >= config.train.params.ma_start:
if ma_count==0:
copy_model_params(netG_ma, netG)
 else:
if config.train.params.ema:
ema_model(netG_ma, netG, config.train.params.ema_alpha)
 else:
ma_model(netG_ma, netG, ma_count)
 ma_count += 1
if use_scheduler:
lr_schedulerD.step()
lr_schedulerG.step()
lossD.append(lossD_epoch)
 lossG.append(lossG_epoch)
 show_generated_img(epoch )<find_best_params>
def create_glove_corpus() :
corpus=[]
for tweet in tqdm(df['text']):
words=[word.lower() for word in word_tokenize(tweet)]
corpus.append(words)
 return corpus
corpus = create_glove_corpus()
embedding_dict={}
with open('.. /input/glove-global-vectors-for-word-representation/glove.6B.100d.txt','r')as f:
for line in f:
values=line.split()
word = values[0]
vectors=np.asarray(values[1:],'float32')
 embedding_dict[word]=vectors
f.close()
MAX_LEN=50
tokenizer_obj=Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
 sequences=tokenizer_obj.texts_to_sequences(corpus)
 tweet_pad=pad_sequences(sequences,maxlen=MAX_LEN,truncating='post',padding='post')
 word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))
num_words=len(word_index)+1
embedding_matrix=np.zeros(( num_words,100))
for word,i in tqdm(word_index.items()):
if i < num_words:
emb_vec=embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i]=emb_vec
tweet_pad[0][0:]
th = 1.0
if use_averaging_model:
generate_samples_truncated(outimg_dir, netG_ma)
 else:
generate_samples_truncated(outimg_dir, netG)
 shutil.make_archive('images', 'zip', outimg_dir)
 print('last epoch: ', epoch)
 elapsed_time = toc(start_time)
 print('elapsed time[h] :', elapsed_time/3600 )<set_options>
model=Sequential()
embedding=Embedding(num_words,100,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=3e-4)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
 model.summary()
glove_train=tweet_pad[:train.shape[0]]
glove_test=tweet_pad[train.shape[0]:]
x_train,x_test,y_train,y_test=train_test_split(glove_train,train['target'].values,test_size=0.2)
 print('Shape of train',x_train.shape)
 print("Shape of Validation ",x_test.shape )
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<define_variables>
history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, verbose=2 )
<define_variables><EOS> 
glove_pred = model.predict(glove_test)
 glove_pred_int = glove_pred.round().astype('int')
 submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 submission['target'] = glove_pred_int
submission.head()
submission.to_csv("glove_submission.csv", index=False, header=True )