Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<normalization>
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
import string, re
from bs4 import BeautifulSoup
from wordcloud import WordCloud
from keras.preprocessing import text, sequence
from nltk.tokenize.toktok import ToktokTokenizer
from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense,Embedding,LSTM,Dropout, Bidirectional
from keras.callbacks import ReduceLROnPlateau
import tensorflow as tf
def lr_decay(epoch):
return learning_rate * 0.99 ** epoch<choose_model_class>
np.random.seed(1)
 tf.random.set_seed(1 )
train_data_generator = ImageDataGenerator(rescale = 1./255.,
rotation_range = 20,
width_shift_range = 0.1,
height_shift_range = 0.1,
shear_range = 0.1,
zoom_range = [0.2, 1.2],
horizontal_flip = False)
 val_data_generator = ImageDataGenerator(rescale=1./255 )<choose_model_class>
train_df = pd.read_csv('.. /input/nlp-getting-started/train.csv', index_col='id')
 test_df = pd.read_csv('.. /input/nlp-getting-started/test.csv', index_col='id' )
model.compile(optimizer=optimizer, loss=['categorical_crossentropy'], metrics=['accuracy'] )<train_model>
stop = set(stopwords.words('english'))
punctuation = list(string.punctuation)
 stop.update(punctuation )
start = time.time()
time_spent = 0
accuracy = []
val_accuracy = []
for epoch in range(220):
time_spent = time.time() -start
if time_spent >= 7150:
break
else:
epoch += 1
print('epoch:', epoch)
 history = model.fit_generator(
 train_data_generator.flow(x_train,y_train, batch_size=batch_size),
steps_per_epoch=100,
epochs=1,
callbacks=[LearningRateScheduler(lr_decay)],
validation_data=val_data_generator.flow(x_val,y_val),
validation_steps=50,
verbose=1, shuffle = True)
 accuracy.append(history.history['accuracy'])
 val_accuracy.append(history.history['val_accuracy'])
<load_from_csv>
def strip_html(text):
soup = BeautifulSoup(text, "html.parser")
 return soup.get_text()
def remove_between_square_brackets(text):
return re.sub('\[[^]]*\]', '', text)
 def remove_url(text):
return re.sub(r'http\S+', '', text)
 def add_space(text):
return re.sub('%20', ' ', text)
 def remove_stopwords(text):
final_text = []
for i in text.split() :
if i.strip().lower() not in stop:
final_text.append(i.strip())
 return " ".join(final_text)
 def denoise_text(text):
text = strip_html(text)
 text = remove_between_square_brackets(text)
 text = add_space(text)
 text = remove_url(text)
 text = remove_stopwords(text)
 return text
x_test = x_test/255
predictions = model.predict_classes(x_test)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.head()<save_to_csv>
def preprocess_df(df):
df = df.fillna("")
 df['text'] = df['location'] + " " + df['keyword'] + " " + df['text']
del df['keyword']
del df['location']
df['text'] = df['text'].apply(denoise_text)
 return df
submission.to_csv("submission.csv",index=False )<train_model>
train_df = preprocess_df(train_df)
 test_df = preprocess_df(test_df )
print('end...' )<init_hyperparams>
X_train, X_dev, y_train, y_dev = train_test_split(train_df.text.values, train_df.target.values )
<load_from_csv>
max_features = 10000
max_len = 300
test5000 = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 train = pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 print(train.shape)
 print(test5000.shape )<prepare_x_and_y>
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(X_train )
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 Y_trainlabel = train["label"]
Y_train = to_categorical(Y_trainlabel, num_classes = 10)
 X_test5000 = test5000.drop(labels = ["id"],axis = 1)
 X_test5000 = X_test5000 / 255.0
X_test5000 = X_test5000.values.reshape(-1,28,28,1)
 x_train = X_train
y_train = Y_train
print(x_train.shape)
 print(y_train.shape)
 print(X_test5000.shape )<choose_model_class>
tokenized_train = tokenizer.texts_to_sequences(X_train)
 X_train = sequence.pad_sequences(tokenized_train, maxlen=max_len)
 tokenized_dev = tokenizer.texts_to_sequences(X_dev)
 X_dev = sequence.pad_sequences(tokenized_dev, maxlen=max_len )
init = he_normal(seed=82)
 nets = 3
model = [0] *(nets+1)
 for j in range(nets+1):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size=3, activation='relu' , kernel_initializer=init, input_shape=(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=3, activation='relu' , kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=5, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size=5, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=7, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=7, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size=4, activation='relu', kernel_initializer=init))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary()<load_pretrained>
EMBEDDING_FILE = '.. /input/glove-twitter/glove.twitter.27B.100d.txt'
for j in range(nets):
loadmodelname = ".. /input/kmnist-trio/weights_N" + str(j)
 model[j].load_weights(loadmodelname)
 savemodelname = "weights_N" + str(j)
 model[j].save_weights(savemodelname)
 print("Saving", loadmodelname, "back to", savemodelname )<train_model>
all_embs = np.stack(list(embeddings_index.values()))
emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose=0)
 nets2train = 0
history = [0] * nets2train
epoks = 35
for j in range(nets2train):
rs = 10 * j + 1
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(x_train, y_train, train_size = 0.95, random_state = rs)
 print(X_train2.shape,Y_train2.shape, X_val2.shape, Y_val2.shape)
 modelfilename = "weights_N" + str(j)
 checkpoint = ModelCheckpoint(modelfilename, monitor='val_acc', verbose=0, save_best_only=False, save_weights_only=False, period=epoks)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size = 64), epochs = epoks, steps_per_epoch = X_train2.shape[0]//64, validation_data =(X_val2, Y_val2), callbacks=[annealer, checkpoint], verbose=0)
 print("CNN", j,": Training done." )<save_to_csv>
batch_size = 1024
epochs = 15
embed_size = 100
results5000 = np.zeros(( X_test5000.shape[0], 10))
nets4predict = 3
allthree = False
for j in range(nets4predict):
if allthree or j== 2:
print("CNN",j)
 loadmodelname = "weights_N" + str(j)
 model[j].load_weights(loadmodelname)
 results5000 = results5000 + model[j].predict(X_test5000)
 results5000 = np.argmax(results5000,axis = 1)
 submission = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = results5000
submission.to_csv("submission.csv",index=False)
 print("DONE." )<import_modules>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', patience = 2, verbose=1,factor=0.5, min_lr=0.00001 )
import numpy as np
import pandas as pd
import tensorflow as tf<import_modules>
model = Sequential()
model.add(Embedding(max_features, output_dim=embed_size, weights=[embedding_matrix], input_length=max_len, trainable=False))
model.add(LSTM(units=128 , return_sequences = False , recurrent_dropout = 0.3 , dropout = 0.3))
model.add(Dense(units=64 , activation = 'relu', kernel_regularizer='l2'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=keras.optimizers.Adam(lr = 0.01), loss='binary_crossentropy', metrics=['accuracy'] )
Activation, LeakyReLU, Flatten, Dropout, BatchNormalization 
<load_from_csv>
history = model.fit(X_train, y_train, batch_size = batch_size ,
validation_data =(X_dev,y_dev),
epochs = epochs , callbacks = [learning_rate_reduction] )
train_datas=pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 val_datas = pd.read_csv(".. /input/Kannada-MNIST/Dig-MNIST.csv" )<split>
print("Accuracy of the model on Training Data is - " , model.evaluate(X_train,y_train)[1]*100)
 print("Accuracy of the model on Dev Data is - " , model.evaluate(X_dev,y_dev)[1]*100 )
datas = pd.concat([train_datas,val_datas],axis=0)
 print(datas.shape)
 datas_X = np.array(datas.drop("label",axis=1),dtype=np.float32)
 datas_Y = np.array(datas[["label"]],dtype=np.int32)
 train_X,val_X,train_Y,val_Y = train_test_split(datas_X,datas_Y,test_size=0.2,shuffle=True )<train_model>
X_test = test_df.text.values
train_X = train_X / 255.0
val_X = val_X / 255.0
train_X = np.reshape(train_X,(-1,28,28,1))
val_X = np.reshape(val_X,(-1,28,28,1))<import_modules>
tokenized_dev = tokenizer.texts_to_sequences(X_test)
 X_test = sequence.pad_sequences(tokenized_dev, maxlen=max_len )
from tensorflow.keras.layers import Activation,GlobalAveragePooling2D<choose_model_class>
classes = model.predict_classes(X_test)[:, 0]
model = Sequential()
model.add(Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(256,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(256,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(512,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(512,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(1024,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(1024,(3,3), padding='same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))

 model.add(GlobalAveragePooling2D())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss="sparse_categorical_crossentropy",metrics=["accuracy"])
 print(model.summary() )<choose_model_class>
submission = pd.DataFrame(
 {'id': list(test_df.index.values),
'target': list(classes),
} ).set_index('id' )
<set_options><EOS> 
submission.to_csv('submission.csv' )