Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<data_type_conversions>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
submission['id'] = submission['id'].astype(int )<sort_values>
BASE_PATH = "/kaggle/input/nlp-getting-started/"
submission = submission.sort_values('id' )<save_to_csv>
train =pd.read_csv(BASE_PATH + "train.csv")
 train.head()
submission.to_csv('submission.csv', index=False )<save_to_csv>
test =pd.read_csv(BASE_PATH + "test.csv")
 test.head()
submission.to_csv('submission.csv', index=False )<define_variables>
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
labels=['dog','cat']<define_variables>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
create_directory(labels =labels)
<load_pretrained>
text = "This is a Goat, and I am riding a Boat.... "
tokenize_ = tokenizer.tokenize(text)
 print("Text after tokenization: ")
 print(tokenize_)
 max_len = 25
text = tokenize_[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 print("After adding [CLS] and [SEP]: ")
 print(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 print("After converting Tokens to Id: ")
 print(tokens)
 tokens += [0] * pad_len
print("tokens: ")
 print(tokens)
 pad_masks = [1] * len(input_sequence)+ [0] * pad_len
print("Pad Masking: ")
 print(pad_masks)
 segment_ids = [0] * max_len
print("Segment Ids: ")
 print(segment_ids )
def move_files(train_path,test_path):
print('Moving Training Files.. ')
 time.sleep(1)
 for i in tqdm(os.listdir(train_path)) :
if 'dog' in i:
shutil.copyfile(train_path+i,'.. /data/train/dog/'+i)
 elif 'cat' in i:
shutil.copyfile(train_path+i,'.. /data/train/cat/'+i)
 else:
print('unkown File', i)
 print('Moving Testing Files.. ')
 time.sleep(1)
 for i in tqdm(os.listdir(test_path)) :
shutil.copyfile(test_path+i,'.. /data/test/test_data/'+i)
<define_variables>
input_word_id = Input(shape=(max_len,),dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_id = Input(shape=(max_len,), dtype=tf.int32, name = "segment_id")
 _, sequence_output = bert_layer([input_word_id, input_mask, segment_id])
 clf_output = sequence_output[:, 0, :]
model = Model(inputs=[input_word_id, input_mask, segment_id],outputs=clf_output)
 model.compile(Adam(lr=2e-5), loss='binary_crossentropy', metrics=['accuracy'])
 model.summary()
print("shape of _ layer of BERT: "+str(_.shape))
print("shape of last layer of BERT: "+str(sequence_output.shape))
move_files('.. /input/train/','.. /input/test/' )<count_values>
def build_model(bert_layer, max_len=512):
input_word_id = Input(shape=(max_len,),dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_id = Input(shape=(max_len,), dtype=tf.int32, name = "segment_id")
 _, sequence_output = bert_layer([input_word_id, input_mask, segment_id])
 clf_output = sequence_output[:, 0, :]
dense_layer1 = Dense(units=256,activation='relu' )(clf_output)
 dense_layer1 = Dropout(0.4 )(dense_layer1)
 dense_layer2 = Dense(units=128, activation='relu' )(dense_layer1)
 dense_layer2 = Dropout(0.4 )(dense_layer2)
 out = Dense(1, activation='sigmoid' )(dense_layer2)
 model = Model(inputs=[input_word_id, input_mask, segment_id],outputs=out)
 model.compile(Adam(lr=2e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model
def get_num_files(path):
if not os.path.exists(path):
return 0
return sum([len(files)for r, d, files in os.walk(path)] )<define_variables>
train_input = pre_Process_data(train.text.values, tokenizer, max_len=260)
 test_input = pre_Process_data(test.text.values, tokenizer, max_len=260)
 train_labels = train.target.values
Image_width,Image_height = 299,299
batch_size=50
Number_FC_Neurons=1024
train_dir = '.. /data/train/'
total_samples = get_num_files(train_dir)
 val_split=0.3
n_train=total_samples*(1-val_split)
 n_val=total_samples*val_split
num_classes = len(labels)
 print(n_train,n_val)
 gc.collect()<define_variables>
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=10,
callbacks=[checkpoint],
batch_size=32
)
train_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),batch_size=batch_size,seed=42,subset='training',shuffle=True,class_mode='categorical')
 val_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),batch_size=batch_size,seed=42,subset='validation',shuffle=True,class_mode='categorical' )<choose_model_class>
submission = pd.read_csv(BASE_PATH + "sample_submission.csv")
 submission.head()
InceptionV3_base_model = InceptionV3(weights='imagenet', include_top=False)
 print('Inception v3 base model without last FC loaded' )<choose_model_class>
model.load_weights('model.h5')
 test_pred = model.predict(test_input)
 submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False)