Kaggle competition: University of Liverpool - Ion Switching
def get_string_predictions(test_data_dict, sentiment, predictions, test_dict):
preds_start, preds_end = predictions[sentiment]
input_ids_t, attention_mask_t, token_type_ids_t = test_data_dict[sentiment]
test = test_dict[sentiment]
all = []
for k in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[k,])
 b = np.argmax(preds_end[k,])
 if a>b:
st = test.loc[k,'text']
else:
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 all.append(st)
 return all<predict_on_test>
%matplotlib inline
!pip install uc==2.2.0
def get_string_predictions_neutral(test_dict, sentiment):
test = test_dict[sentiment]
return list(test["text"] )<predict_on_test>
data = pd.read_csv('.. /input/data-without-drift/train_clean.csv')
 data.head()
predictions = dict()
string_predictions = dict()
for sentiment in all_sentiments:
if not sentiment == "neutral":
predictions[sentiment] = train_model_for_sentiment(train_data_dict, test_data_dict, sentiment, train_dict)
 string_predictions[sentiment] = get_string_predictions(test_data_dict, sentiment, predictions, test_dict)
 else:
string_predictions[sentiment] = get_string_predictions_neutral(test_dict, "neutral" )<sort_values>
df = divide_and_add_features(data['signal'])
 df.head()
concat_string_predictions = []
concat_index = []
for sentiment in all_sentiments:
concat_string_predictions += string_predictions[sentiment]
concat_index += test_dict_index[sentiment]
sorted_concat_string_predictions = sorted(list(zip(concat_index, concat_string_predictions)) , key=lambda x:x[0])
 sorted_concat_string_predictions_list = list(zip(*sorted_concat_string_predictions)) [1]<save_to_csv>
x_train, x_test, y_train, y_test = train_test_split(df.values, data['open_channels'].values, test_size=0.2)
 del data, df
print('x_train.shape=', x_train.shape)
 print('x_test.shape=', x_test.shape)
 print('y_train.shape=', y_train.shape)
 print('y_test.shape=', y_test.shape )
test['selected_text'] = sorted_concat_string_predictions_list
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )<feature_engineering>
mlp = MLP(
 layer_size = [x_train.shape[1], 100, 100, 100, 11],
activation = 'a2m2l',
op='fc',
leaky = -0.2,
rate_init = 0.04,
bias_rate = [],
regularization = 1,
importance_mul = 0.0001,
output_shrink = 0.1,
output_range = None,
loss_type = "softmax",
verbose=1,
importance_out=False,
rate_decay = 0.9,
epoch_train = 30 / 10 / 3,
epoch_decay = 3 / 10 / 3,
epoch_log = 0.01, 
)
def get_predictions_on_oof_for_sentiment(train_data_dict, test_data_dict, sentiment, train_dict):
input_ids, attention_mask, token_type_ids, start_tokens, end_tokens = train_data_dict[sentiment]
input_ids_t, attention_mask_t, token_type_ids_t = test_data_dict[sentiment]
train = train_dict[sentiment]
PATH_SAVED_WEIGHTS = ''
train["prediction_oof"] = ""
train["jaccard_sim"] = np.nan
jac = []; VER='v1'; DISPLAY=1
oof_start = np.zeros(( input_ids.shape[0],MAX_LEN))
oof_end = np.zeros(( input_ids.shape[0],MAX_LEN))
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
skf = StratifiedKFold(n_splits=5,shuffle=True,random_state=777)
 print('
print('
print('
for fold,(idxT,idxV)in enumerate(skf.split(input_ids,train.sentiment.values)) :
print('
print('
print('
K.clear_session()
model = build_model()
print('Loading model...')
 model.load_weights(PATH_SAVED_WEIGHTS+'%s-roberta-%i-%s.h5'%(VER,fold,sentiment))
print('Predicting OOF...')
 oof_start[idxV,],oof_end[idxV,] = model.predict([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],verbose=DISPLAY)
 all = []
for k in idxV:
a = np.argmax(oof_start[k,])
 b = np.argmax(oof_end[k,])
 if a>b:
st = train.loc[k,'text']
else:
text1 = " "+" ".join(train.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 jaccard_sim = jaccard(st,train.loc[k,'selected_text'])
 train.at[k, "jaccard_sim"] = jaccard_sim
train.at[k, "prediction_oof"] = st
all.append(jaccard_sim)
 jac.append(np.mean(all))
print('>>>> FOLD %i Jaccard ='%(fold+1),np.mean(all))
print()
print('>>>> OVERALL 5Fold CV Jaccard =',np.mean(jac))
return train<predict_on_test>
mlp.fit(x_train, y_train )
oof_predictions = get_predictions_on_oof_for_sentiment(train_data_dict, test_data_dict, "positive", train_dict )<set_options>
print('Reading data...')
 data = pd.read_csv('.. /input/data-without-drift/test_clean.csv')
 print('Feature engineering...')
 df = divide_and_add_features(data['signal'])
 mlp_pred = mlp.predict(df.values)
 mlp_pred = np.clip(mlp_pred, 0, 10)
 mlp_pred = np.int0(np.rint(mlp_pred))
print('Writing submission...')
 submission = pd.DataFrame()
submission['time'] = data['time']
submission['open_channels'] = mlp_pred
submission.to_csv('submission.csv', index=False, float_format='%.4f')
 print('Submission finished!')
 submit_result(mlp_pred )