Kaggle competition: Natural Language Processing with Disaster Tweets
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same', input_shape =(28, 28, 1)) ,
tf.keras.layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(256, activation = 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.35),
tf.keras.layers.Dense(10, activation = 'softmax')
 ] )<choose_model_class>
train= pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test= pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 submission= pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv' )
model.compile(optimizer = Adam(lr = 1e-3),
loss = 'categorical_crossentropy',
metrics = ['accuracy'] )<train_model>
train['hashtags']=[re.findall(r"
datagen = ImageDataGenerator(
 rotation_range= 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 
)
 datagen.fit(X_train )<define_variables>
hts={}
ht=pd.DataFrame()
for i in tqdm(train.hashtags.values):
if len(i)>0:
for j in i:
if j not in hts:
hts[j]=1
else:
hts[j]+=1
ht=pd.DataFrame({"hashtags":[i for i in hts.keys() ], "count":[i for i in hts.values() ]})
 ht['targets']=[[j for i,j in zip(train.hashtags, train.target)if k in i] for k in ht.hashtags]
ht['targets']=[sum(i)for i in ht.targets]
ht['pc']=ht['targets']/ht['count']
train_generator = datagen.flow(X_train, y_train, batch_size = 64)
 validation_generator = datagen.flow(X_test, y_test, batch_size = 64 )<choose_model_class>
train['hashtag_score']=0
for i in tqdm(range(0,len(train))):
for j in train['hashtags'].iloc[i]:
train['hashtag_score'].iloc[i]+=ht['pc'][ht.hashtags==j].values[0]
train['hashtag_score_normalized']=[i/len(j)if len(j)>0 else 0 for i,j in zip(train['hashtag_score'],train['hashtags'])]
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_loss', patience = 3, verbose = 1, factor = 0.5, min_lr = 1e-6)
 model_checkpoint = ModelCheckpoint('./best_model.hdf5',monitor = 'val_loss', mode = "min", verbose = 1, save_best_model = True )<train_model>
test['hashtags']=[re.findall(r"
test['hashtag_score']=0
for i in tqdm(range(0,len(test))):
for j in test['hashtags'].iloc[i]:
try:
val=ht['pc'][ht.hashtags==j].values[0]
except:
val=0
test['hashtag_score'].iloc[i]+=val
test['hashtag_score_normalized']=[i/len(j)if len(j)>0 else 0 for i,j in zip(test['hashtag_score'],test['hashtags'])]
history = model.fit_generator(
 train_generator,
steps_per_epoch = X_train.shape[0] // 64,
epochs = 50,
validation_data = validation_generator,
validation_steps = X_test.shape[0] // 64,
callbacks = [learning_rate_reduction, model_checkpoint]
)<load_pretrained>
keyword={}
for i in train.groupby('keyword'):
keyword[i[0]]=(round(i[1].target.sum() /len(i[1]),2))
train['keyword_score']=[keyword[i] if i in keyword else 0 for i in train['keyword']]
keyword= train[['keyword','keyword_score']]
keyword=keyword.drop_duplicates()
model = load_model("./best_model.hdf5" )<compute_test_metric>
test['keyword_score']=[keyword[keyword.keyword==i]['keyword_score'].values[0] if len(keyword[keyword.keyword==i]['keyword_score']>0)else 0 for i in test['keyword']]
model.evaluate(X_test, y_test, verbose = 1 )<load_from_csv>
train['location'][train['location'].isnull() ]=""
train['location']=train['location'].str.replace(r'[^A-Za-z]+',' ')
 train['location']=train['location'].str.lower()
train['location']=[i.strip() for i in train['location']]
loc= set([i for i in train['location']])
 loc=[i.strip() for i in loc]
test['location'][test['location'].isnull() ]=""
test['location']=test['location'].str.replace(r'[^A-Za-z]+',' ')
 test['location']=test['location'].str.lower()
test['location']=[i.strip() if i in loc else '' for i in test['location']]
test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" ).values
test = test.reshape(-1, 28, 28, 1 ).astype('float32')/ 255.0
y_pred = model.predict(test ).argmax(axis=1)
 y_pred.shape<save_to_csv>
train['text_new']=train['text'].str.lower()
train['text_new']=train['text_new'].str.replace(r'[^a-z]+'," ")
 train['text_new']=[i.strip() for i in train['text_new']]
test['text_new']=test['text'].str.lower()
test['text_new']=test['text_new'].str.replace(r'[^a-z]+'," ")
 test['text_new']=[i.strip() for i in test['text_new']]
submission = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label': y_pred})
 submission.to_csv("submission.csv", index = False)
 print("Your submission was successfully saved!" )<import_modules>
!pip install tensorflow-text==2.0.0 --user
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPooling2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, Callback
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
import random
import matplotlib.pyplot as plt
import seaborn as sns<set_options>
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as textb
print(tf.config.list_physical_devices('GPU'),'//',tf.test.is_built_with_cuda() )<load_from_csv>
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3" )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head()<count_missing_values>
X_train = []
for r in tqdm(train.text_new.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_train.append(review_emb)
 X_train = np.array(X_train)
 y_train = train.target.values
X_test = []
for r in tqdm(test.text_new.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_test.append(review_emb)
 X_test = np.array(X_test )
print('Nº of missing values in train set: ', train.isnull().any().sum())
 print()
print('Nº of missing values in test set: ', test.isnull().any().sum() )<prepare_x_and_y>
train_arrays, test_arrays, train_labels, test_labels = train_test_split(X_train,
y_train,
random_state =42,
test_size=0.20 )
X = np.array(train.drop('label',axis=1)) / 255.
 X = X.reshape(( -1,28,28,1))
y = np.array(train['label'] )<split>
def svc_param_selection(X, y, nfolds):
Cs = [1.07]
gammas = [2.075]
param_grid = {'C': Cs, 'gamma' : gammas}
grid_search = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=nfolds, n_jobs=8)
 grid_search.fit(X, y)
 grid_search.best_params_
return grid_search
model = svc_param_selection(train_arrays,train_labels, 5 )
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2)
 print('X_train: ' + str(train_X.shape))
print('Y_train: ' + str(train_y.shape))
print('X_test: ' + str(test_X.shape))
print('Y_test: ' + str(test_y.shape))<define_variables>
pred = model.predict(test_arrays)
batch_size = 128
epochs = 110
epochs_to_wait_to_improve = 10
num_classes = max(pd.unique(train['label'])) +1
seed = 7
random.seed(seed )<choose_model_class>
accuracy = accuracy_score(test_labels,pred)
 accuracy
datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.11,
height_shift_range=0.11,
shear_range=0.15,
zoom_range = 0.09,
validation_split=0.3,
horizontal_flip=False,
vertical_flip=False
)<define_variables>
model_final = svc_param_selection(X_train,y_train, 5)
train_generator = datagen.flow(train_X,
train_y,
batch_size=batch_size,
shuffle=True,
subset='training')
 val_generator = datagen.flow(test_X,
test_y,
batch_size=batch_size,
subset='validation' )<choose_model_class>
pred=model_final.predict(X_test)
model = Sequential()
model.add(Conv2D(32, kernel_size =(3,3), input_shape=(28, 28, 1), padding = 'Same', activation='relu'))
model.add(Conv2D(64, kernel_size =(3,3), padding = 'Same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size =(3,3), padding = 'Same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.20))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.35))
model.add(Dense(num_classes, activation='softmax')) 
<choose_model_class>
Counter(pred )
class myCallback(Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.999):
print("
Reached 99.9% accuracy so cancelling training!")
 self.model.stop_training = True
mycallback = myCallback()
early_stopping_callback = EarlyStopping(monitor='val_loss',
patience=epochs_to_wait_to_improve,
verbose = 2,
restore_best_weights=True)
 optimizer = Adam(lr=0.001, beta_1=0.9)
 model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'] )<train_model>
test['pred']=pred.tolist()
test['pred']=[1 if i>=.75 else j for i,j in zip(test['hashtag_score_normalized'],test['pred'])]
test['pred']=[1 if i>=.90 else j for i,j in zip(test['keyword_score'],test['pred'])]
test.pred.value_counts()
<compute_test_metric><EOS> 
output = pd.DataFrame({'id': test.id, 'target': test.pred})
 output.to_csv('my_submission_hub.csv', index=False)
 print("Your submission was successfully saved!")