Kaggle competition: Otto Group Product Classification Challenge
print('TF version',tf.__version__ )<define_variables>
train = pd.read_csv(".. /input/train.csv")
 print(train.head())
 test = pd.read_csv(".. /input/test.csv" )
MAX_LEN = 96
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 EPOCHS = 3
BATCH_SIZE = 32
PAD_ID = 1
SEED = 88888
LABEL_SMOOTHING = 0.1
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 train.head()<define_variables>
X_train = train.drop(["id", "target"], axis=1)
 Y_train = train["target"].copy()
X_test = test.drop("id", axis = 1 ).copy()
ct = train.shape[0]
input_ids = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids = np.zeros(( ct,MAX_LEN),dtype='int32')
 start_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 end_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(train.shape[0]):
text1 = " "+" ".join(train.loc[k,'text'].split())
 text2 = " ".join(train.loc[k,'selected_text'].split())
 idx = text1.find(text2)
 chars = np.zeros(( len(text1)))
 chars[idx:idx+len(text2)]=1
if text1[idx-1]==' ': chars[idx-1] = 1
enc = tokenizer.encode(text1)
 offsets = []; idx=0
for t in enc.ids:
w = tokenizer.decode([t])
 offsets.append(( idx,idx+len(w)))
 idx += len(w)
 toks = []
for i,(a,b)in enumerate(offsets):
sm = np.sum(chars[a:b])
 if sm>0: toks.append(i)
 s_tok = sentiment_id[train.loc[k,'sentiment']]
input_ids[k,:len(enc.ids)+3] = [0, s_tok] + enc.ids + [2]
attention_mask[k,:len(enc.ids)+3] = 1
if len(toks)>0:
start_tokens[k,toks[0]+2] = 1
end_tokens[k,toks[-1]+2] = 1<feature_engineering>
params = {"objective": "multi:softprob", "eval_metric":"mlogloss", "num_class": 9}
iter = [110, 120, 130, 140, 150, 160, 170]
for i in iter:
skf = StratifiedKFold(n_splits=10)
 score = 0
for train, test in skf.split(X_train, Y_train):
X_train_k = X_train.iloc[train]
Y_train_k = Y_train.iloc[train]
X_test_k = X_train.iloc[test]
Y_test_k = Y_train.iloc[test]
T_train_xgb = xgb.DMatrix(X_train_k, Y_train_k)
 X_test_xgb = xgb.DMatrix(X_test_k)
 gbm = xgb.train(params, T_train_xgb, i)
 Y_pred = gbm.predict(X_test_xgb)
 val_score =0
Y_k = Y_test_k.as_matrix()
for i in range(len(Y_pred)) :
if(Y_k[i] == Y_pred[i].argmax()):
val_score +=1
val_score = val_score/len(Y_pred)
 score += val_score
score = score/10
print(i)
 print(score)
test = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 ct = test.shape[0]
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')
 for k in range(test.shape[0]):
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+3] = [0, s_tok] + enc.ids + [2]
attention_mask_t[k,:len(enc.ids)+3] = 1<train_model>
score = score/10
print(score)
<string_transform><EOS> 
submission = pd.DataFrame({ "id": test["id"]})
 i = 0
for num in class_range:
col_name = str("Class_{}".format(num))
submission[col_name] = Y_pred[:,i]
i = i + 1
submission.to_csv('otto1.csv', index=False )