Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<choose_model_class>
import pandas as pd
import numpy as np
import spacy
import re
import string
lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy',
patience=300,
verbose=1,
factor=0.5,
min_lr=0.00001 )<normalization>
train = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv" )
def lr_decay(epoch, initial_learningrate = 0.001):
return initial_learningrate * 0.99 ** epoch<choose_model_class>
def clean_text(text):

 text = str(text ).lower()
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 return text
batchsize = 200
epoch = 45
train_datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.1,
zoom_range=0.25,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(
 horizontal_flip=False,
rotation_range=15,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=0.15,
zoom_range=0.25, 
)
 callback = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=5)
 history = model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size = batchsize),
steps_per_epoch = 100,
epochs = epoch,
callbacks=[callback,
LearningRateScheduler(lr_decay),
lr
],
validation_data=valid_datagen.flow(X_dev, Y_dev),
validation_steps=50,
)<save_to_csv>
train["text"] = train["text"].apply(lambda x:clean_text(x))
test["text"] = test["text"].apply(lambda x:clean_text(x))
yhat = model.predict(X_valid ).argmax(axis=1)
 submission['label']=pd.Series(yhat)
 submission.to_csv('submission.csv',index=False )<save_model>
len(train["keyword"].unique() )
model.save('my_model.h5' )<set_options>
len(train["location"].unique() )
%matplotlib inline<set_options>
nlp = spacy.load('en_core_web_lg' )
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<load_from_csv>
train_samples = train["text"]
train_samples.head()
test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 dig_mnist = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 sample_submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv" )<data_type_conversions>
def concat_keyword_text(row):
return(str(row["text"])+ " " + str(row["keyword"]))
X_train = train.loc[:, train.columns!='label'].values.astype('uint8')
 print("X_train.shape", X_train.shape)
 y_train = train['label'].values
X_train = X_train.reshape(( X_train.shape[0],28,28))
print("X_train.shape", X_train.shape)
 print("y_train.shape",X_train.shape )<split>
train_samples = train.apply(concat_keyword_text, axis = 1)
 train_samples.head()
X_train = X_train[:,:,:,None]
X_test = X_test[:,:,:,None]<define_variables>
vectorizer = TextVectorization()
text_ds = tf.data.Dataset.from_tensor_slices(train_samples ).batch(128)
 vectorizer.adapt(text_ds )
batch_size = 32
num_epochs = 50<count_unique_values>
voc = vectorizer.get_vocabulary()
num_samples = X_train.shape[0]
num_classes = np.unique(y_train ).shape[0]
img_rows, img_cols = X_train[0,:,:,0].shape
classes = np.unique(y_train )<define_variables>
num_tokens = len(voc)
 embedding_dim = len(nlp('The' ).vector)
 embedding_matrix = np.zeros(( num_tokens, embedding_dim))
print("num_samples",num_samples)
 print("num_classes",num_classes)
 print("img_rows",img_rows)
 print("img_cols",img_cols)
 print("classes",classes )<categorify>
for i, word in enumerate(voc):
embedding_matrix[i] = nlp(word ).vector
y_train = np_utils.to_categorical(y_train, num_classes)
 y_train.shape<data_type_conversions>
embedding_layer = Embedding(
 num_tokens,
embedding_dim,
embeddings_initializer=keras.initializers.Constant(embedding_matrix),
trainable=False,
)
X_train_norm = X_train.astype('float32')
 X_test_norm = X_test.astype('float32')
 X_train_norm /= 255
X_test_norm /= 255<choose_model_class>
lbl_enc = preprocessing.LabelEncoder()
train["target"] = lbl_enc.fit_transform(train["target"].values )
learning_rate_reduction=ReduceLROnPlateau(monitor='val_loss',
patience=5,
verbose=1,
factor=0.2
)<choose_model_class>
df_train = train.sample(frac=0.7, random_state=0)
 df_valid = train.drop(df_train.index )
early_stopping = EarlyStopping(monitor='val_loss',
mode='min',
verbose=1,
patience=10
)<choose_model_class>
X_train = df_train.drop(['target'], axis=1)
 X_valid = df_valid.drop(['target'], axis=1)
 y_train = df_train['target']
y_valid = df_valid['target']
def build_model() :
model = Sequential()
x_in = layers.Input(shape=(28, 28, 1))
x = layers.Conv2D(64, kernel_size=(3, 3), activation='relu' )(x_in)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(128, kernel_size=(3, 3), activation='relu' )(x)
 x = layers.BatchNormalization()(x)
 x = layers.MaxPooling2D(pool_size=(2, 2))(x)
 x = layers.Dropout(0.25 )(x)
 x = layers.Conv2D(256, kernel_size=(3, 3), activation='relu' )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Conv2D(512, kernel_size=(3, 3), activation='relu' )(x)
 x = layers.BatchNormalization()(x)
 x = layers.MaxPooling2D(pool_size=(2, 2))(x)
 x = layers.Dropout(0.25 )(x)
 x = layers.Flatten()(x)
 x = layers.Dense(512 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(256 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(128 )(x)
 x = layers.BatchNormalization()(x)
 classes = layers.Dense(num_classes, activation='softmax' )(x)
 model = Model(inputs=x_in, outputs=classes)
 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<choose_model_class>
x_train = vectorizer(np.array([[s] for s in X_train["text"]])).numpy()
x_valid = vectorizer(np.array([[s] for s in X_valid["text"]])).numpy()
model = build_model()<train_model>
y_train_ = np_utils.to_categorical(y_train.values)
 y_valid_ = np_utils.to_categorical(y_valid.values )
history = model.fit(X_train_norm,
y_train,
batch_size = batch_size,
epochs = num_epochs,
validation_split = 0.1,
shuffle = True,
callbacks = [learning_rate_reduction, early_stopping]
)<save_model>
int_sequences_input = keras.Input(shape=(None,), dtype="int64")
 embedded_sequences = embedding_layer(int_sequences_input)
 x = layers.Conv1D(64, 5, activation="relu",padding='same' )(embedded_sequences)
 x = layers.MaxPooling1D(3 )(x)
 x = layers.Conv1D(32, 5, activation="relu",padding='same' )(x)
 x = layers.MaxPooling1D(3 )(x)
 x = layers.Conv1D(16, 5, activation="relu",padding='same' )(x)
 x = layers.GlobalMaxPooling1D()(x)
 x = layers.Dense(16, activation="relu" )(x)
 x = layers.Dropout(0.5 )(x)
 preds = layers.Dense(2, activation="softmax" )(x)
 model = keras.Model(int_sequences_input, preds)
 model.summary()
model.save('model.h5' )<predict_on_test>
early_stopping = callbacks.EarlyStopping(
 min_delta=0.001,
patience=20,
restore_best_weights=True, 
)
 model.compile(
 optimizer='adam',
loss='categorical_crossentropy',
metrics ='accuracy' 
)
 history = model.fit(
 x_train, y_train_,
validation_data=(x_valid, y_valid_),
batch_size=128,
epochs=500,
callbacks=[early_stopping],
verbose=1,
)
pred = model.predict(X_test_norm )<prepare_output>
x_test = vectorizer(np.array([[s] for s in test["text"]])).numpy()
pred=np.argmax(pred, axis=1 )<prepare_output>
predictions = model.predict(x_test )
sample_submission['label'] = pred<save_to_csv>
sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sub.head()
<set_options><EOS> 
submission = pd.DataFrame({"id": test.iloc[:,0].values,"target": np.argmax(predictions,axis=1)})
 submission.to_csv("submission.csv", index=False)
 submission.head()