Kaggle competition: Otto Group Product Classification Challenge
<SOS> metric: MulticlassLoss Kaggle data source: otto-group-product-classification-challenge<define_variables>
INPUT_PATH = Path(".. /input/" )
jac = []; VER='v0'; 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=SEED)
 for fold,(idxT,idxV)in enumerate(skf.split(input_ids,train.sentiment.values)) :
print('
print('
print('
K.clear_session()
model, padded_model = build_model()
inpT = [input_ids[idxT,], attention_mask[idxT,], token_type_ids[idxT,]]
targetT = [start_tokens[idxT,], end_tokens[idxT,]]
inpV = [input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]]
targetV = [start_tokens[idxV,], end_tokens[idxV,]]
shuffleV = np.int32(sorted(range(len(inpV[0])) , key=lambda k:(inpV[0][k] == PAD_ID ).sum() , reverse=True))
inpV = [arr[shuffleV] for arr in inpV]
targetV = [arr[shuffleV] for arr in targetV]
weight_fn = '%s-roberta-%i.h5'%(VER,fold)
 for epoch in range(1, EPOCHS + 1):
shuffleT = np.int32(sorted(range(len(inpT[0])) , key=lambda k:(inpT[0][k] == PAD_ID ).sum() + np.random.randint(-3, 3), reverse=True))
num_batches = math.ceil(len(shuffleT)/ BATCH_SIZE)
 batch_inds = np.random.permutation(num_batches)
 shuffleT_ = []
for batch_ind in batch_inds:
shuffleT_.append(shuffleT[batch_ind * BATCH_SIZE:(batch_ind + 1)* BATCH_SIZE])
 shuffleT = np.concatenate(shuffleT_)
 inpT = [arr[shuffleT] for arr in inpT]
targetT = [arr[shuffleT] for arr in targetT]
model.fit(inpT, targetT,
epochs=epoch, initial_epoch=epoch - 1, batch_size=BATCH_SIZE, verbose=DISPLAY, callbacks=[],
validation_data=(inpV, targetV), shuffle=False)
 save_weights(model, weight_fn)
 print('Loading model...')
 load_weights(model, weight_fn)
 print('Predicting OOF...')
 oof_start[idxV,],oof_end[idxV,] = padded_model.predict([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],verbose=DISPLAY)
 print('Predicting Test...')
 preds = padded_model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=DISPLAY)
 preds_start += preds[0]/skf.n_splits
preds_end += preds[1]/skf.n_splits
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-2:b-1])
 all.append(jaccard(st,train.loc[k,'selected_text']))
jac.append(np.mean(all))
print('>>>> FOLD %i Jaccard ='%(fold+1),np.mean(all))
print()<string_transform>
train = pd.read_csv(INPUT_PATH / "train.csv")
 test = pd.read_csv(INPUT_PATH / "test.csv")
 train.shape, test.shape
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-2:b-1])
 all.append(st )<save_to_csv>
drop_cols = ["id"]
target_col = "target"
target = train[target_col]
feat_cols = [col for col in train.columns if col not in drop_cols + [target_col]]
train[target_col] = train[target_col].str.replace('Class_', '')
 train[target_col] = train[target_col].astype(int)- 1
target = train[target_col]
test['selected_text'] = all
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )<categorify>
NFOLDS = 2
RANDOM_STATE = 871972
excluded_column = ['target', 'id']
cols = [col for col in train.columns if col not in excluded_column]
folds = StratifiedKFold(n_splits=NFOLDS, shuffle=True,
random_state=RANDOM_STATE)
 params = {"objective": "multi:softprob",
"eval_metric":"mlogloss",
"num_class": 9
}
ct = 1
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 text_ = "you would be very sad for not getting an intern"
text1 = " "+" ".join(text_.split())
 enc = tokenizer.encode(text1)
 s_tok = 2430
input_ids_t[0,:len(enc.ids)+3] = [0, s_tok] + enc.ids + [2]
attention_mask_t[0,:len(enc.ids)+3] = 1
preds = padded_model.predict([input_ids_t,attention_mask_t,token_type_ids_t])
 preds_start = preds[0]
preds_end = preds[1]
a = np.argmax(preds_start[0,])
 b = np.argmax(preds_end[0,])
 text1 = " "+" ".join(text_.split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-2:b-1])
 print(st )<set_options>
y_pred_test = np.zeros(( len(test), 9))
oof = np.zeros(( len(train), 9))
score = 0
for fold_n,(train_index, valid_index)in enumerate(folds.split(train, y=target)) :
print('Fold', fold_n)
 X_train, X_valid = train.iloc[train_index], train.iloc[valid_index]
y_train, y_valid = target.loc[train_index].astype(int), target.loc[valid_index].astype(int)
 train_data = xgb.DMatrix(X_train[cols], label=y_train)
 valid_data = xgb.DMatrix(X_valid[cols], label=y_valid)
 model = xgb.train(params,
train_data,num_boost_round=30000,
evals=[(valid_data, "valid")],
verbose_eval=4,
maximize=False,
early_stopping_rounds=300)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid[cols]))
oof[valid_index] = y_pred_valid
score += log_loss(y_valid, y_pred_valid)
 y_pred_test += model.predict(xgb.DMatrix(test[cols])) /NFOLDS
print('valid logloss average:', score/NFOLDS, log_loss(target, oof))
warnings.filterwarnings('ignore' )<set_options>
sample_submit = pd.read_csv(".. /input/sampleSubmission.csv")
 submit = pd.concat([sample_submit[['id']], pd.DataFrame(y_pred_test)], axis = 1)
 submit.columns = sample_submit.columns
submit.to_csv('submit_xgboost.csv', index=False )
<define_variables><EOS> 
np.save("xgb_oof.npy", oof )