Kaggle competition: Natural Language Processing with Disaster Tweets
def set_lr(model, lr):
K.set_value(model.optimizer.lr, float(lr))
def get_lr(model):
return K.get_value(model.optimizer.lr)
 def score_reshape(score, x, y=None):

 if y is None:
m = np.zeros(( x.shape[0], x.shape[0]), dtype=K.floatx())
 m[np.triu_indices(x.shape[0], 1)] = score.squeeze()
m += m.transpose()
else:
m = np.zeros(( y.shape[0], x.shape[0]), dtype=K.floatx())
 iy, ix = np.indices(( y.shape[0], x.shape[0]))
ix = ix.reshape(( ix.size,))
iy = iy.reshape(( iy.size,))
m[iy, ix] = score.squeeze()
return m
def compute_score(verbose=1):

 features = branch_model.predict_generator(FeatureGen(train, verbose=verbose), max_queue_size=12, workers=6,
verbose=0)
 score = head_model.predict_generator(ScoreGen(features, verbose=verbose), max_queue_size=12, workers=6, verbose=0)
 score = score_reshape(score, features)
 return features, score
def make_steps(step, ampl):

 global w2ts, t2i, steps, features, score, histories
random.shuffle(train)
 w2ts = {}
for w, hs in w2hs.items() :
for h in hs:
if h in train_set:
if w not in w2ts: w2ts[w] = []
if h not in w2ts[w]: w2ts[w].append(h)
 for w, ts in w2ts.items() : w2ts[w] = np.array(ts)
 t2i = {}
for i, t in enumerate(train): t2i[t] = i
features, score = compute_score()
history = model.fit_generator(
 TrainingData(score + ampl * np.random.random_sample(size=score.shape), steps=step, batch_size=32),
initial_epoch=steps, epochs=steps + step, max_queue_size=12, workers=6, verbose=1 ).history
steps += step<load_pretrained>
path_data_test='/kaggle/input/nlp-getting-started/test.csv'
test_data=pd.read_csv(path_data_test)
 path_data_train='/kaggle/input/nlp-getting-started/train.csv'
train_data=pd.read_csv(path_data_train )
histories = []
steps = 0
tmp = keras.models.load_model('.. /input/reset-v3-100/siamese_v3_100')
 model.set_weights(tmp.get_weights())
 set_lr(model, 4e-5)
 make_steps(5, 0.25)
 set_lr(model, 4e-5)
 make_steps(5, 0.25)
 model.save('siamese_v3_110' )<save_model>
def clean(text):
text = re.sub(r"
","",text)
 text = text.lower()
text = re.sub(r"\d","",text)
 text = re.sub(r'[^\x00-\x7f]',r' ',text)
 text = re.sub(r'[^\w\s]','',text)
 text = re.sub(r'http\S+|www.\S+', '', text)
 return text
set_lr(model, 4e-5)
 make_steps(5, 0.25)
 set_lr(model, 4e-5)
 make_steps(5, 0.25)
 model.save('siamese_v3_120' )<load_pretrained>
train_data['cleaned'] = train_data['text'].apply(lambda x : clean(x))
test_data['cleaned']= test_data['text'].apply(lambda x : clean(x))
<feature_engineering><EOS> 
tweets_pipeline = Pipeline([('CVec', CountVectorizer(stop_words='english')) , 
('Tfidf', TfidfTransformer())])
 X=train_data['cleaned'].to_numpy()
Y=train_data['target'].to_numpy()
X_train_tranformed = tweets_pipeline.fit_transform(X)
 X_test=test_data['cleaned']
X_test_tranformed = tweets_pipeline.transform(X_test)
 train_x,val_x,train_y,val_y=train_test_split(X,Y,random_state=0)
 model=KNeighborsClassifier(n_neighbors=7)
 model.fit(X_train_tranformed,Y)
 preds=model.predict(X_test_tranformed)
 output = pd.DataFrame({'id': test_data.id, 'target': preds})
 output.to_csv('sample_submission.csv', index=False)