Kaggle competition: Otto Group Product Classification Challenge
def eval(valid_data_loader):
model0.eval()
model1.eval()
model2.eval()
model3.eval()
model4.eval()
model5.eval()
model6.eval()
model7.eval()
model8.eval()
model9.eval()
all_jaccard = 0
all_num = 0
rets = []
with torch.no_grad() :
for bi, d in enumerate(valid_data_loader):
ids = d["ids"]
token_type_ids = d["token_type_ids"]
mask = d["mask"]
sentiment = d["sentiment"]
orig_selected = d["orig_selected"]
orig_tweet = d["orig_tweet"]
jaccard = d["jaccard"]
score = d["score"]
ids = ids.to(device, dtype=torch.long)
 token_type_ids = token_type_ids.to(device, dtype=torch.long)
 mask = mask.to(device, dtype=torch.long)
 logits0 = model0(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits1 = model1(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits2 = model2(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits3 = model3(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits4 = model4(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits5 = model5(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits6 = model6(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits7 = model7(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits8 = model8(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 logits9 = model9(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids, 
)
 maxind = 0
maxjac =(logits0[0][0] + logits1[0][0] + logits2[0][0] + logits3[0][0] + logits4[0][0] + logits5[0][0] + logits6[0][0] + logits7[0][0] + logits8[0][0] + logits9[0][0])/ 10 + score[0] * 0.5
for i in range(1, val_nums):
tmpjac =(logits0[i][0] + logits1[i][0] + logits2[i][0] + logits3[i][0] + logits4[i][0] + logits5[i][0] + logits6[i][0] + logits7[i][0] + logits8[i][0] + logits9[i][0])/ 10 + score[i] * 0.5
if maxjac < tmpjac:
maxind = i
maxjac = tmpjac
rets.append(orig_selected[maxind])
 return rets<prepare_output>
INPUT_PATH = Path(".. /input/otto-group-product-classification-challenge/")
 STACKING_LEVEL0_INPUT_PATH = Path(".. /input/otto-stacking-level0/")
 XGB_PATH = Path(".. /input/otto-simple-xgb/")
 LGB_PATH = Path(".. /input/otto-simple-lgb/")
 NN_PATH = Path(".. /input/otto-neuralnetwork/" )
cand = eval(valid_data_loader )<string_transform>
train = pd.read_csv(INPUT_PATH / "train.csv")
 test = pd.read_csv(INPUT_PATH / "test.csv")
 train.shape, test.shape
def pp(filtered_output, real_tweet):
filtered_output = ' '.join(filtered_output.split())
 if len(real_tweet.split())< 2:
filtered_output = real_tweet
else:
if len(filtered_output.split())== 1:
if filtered_output.endswith(".. "):
if real_tweet.startswith(" "):
st = real_tweet.find(filtered_output)
 fl = real_tweet.find(" ")
 if fl != -1 and fl < st:
filtered_output = re.sub(r'(\.) \1{2,}', '', filtered_output)
 else:
filtered_output = re.sub(r'(\.) \1{2,}', '.', filtered_output)
 else:
st = real_tweet.find(filtered_output)
 fl = real_tweet.find(" ")
 if fl != -1 and fl < st:
filtered_output = re.sub(r'(\.) \1{2,}', '.', filtered_output)
 else:
filtered_output = re.sub(r'(\.) \1{2,}', '.. ', filtered_output)
 return filtered_output
if filtered_output.endswith('!!'):
if real_tweet.startswith(" "):
st = real_tweet.find(filtered_output)
 fl = real_tweet.find(" ")
 if fl != -1 and fl < st:
filtered_output = re.sub(r'(\!)\1{2,}', '', filtered_output)
 else:
filtered_output = re.sub(r'(\!)\1{2,}', '!', filtered_output)
 else:
st = real_tweet.find(filtered_output)
 fl = real_tweet.find(" ")
 if fl != -1 and fl < st:
filtered_output = re.sub(r'(\!)\1{2,}', '!', filtered_output)
 else:
filtered_output = re.sub(r'(\!)\1{2,}', '!!', filtered_output)
 return filtered_output
if real_tweet.startswith(" "):
filtered_output = filtered_output.strip()
text_annotetor = ' '.join(real_tweet.split())
 start = text_annotetor.find(filtered_output)
 end = start + len(filtered_output)
 start -= 0
end += 2
flag = real_tweet.find(" ")
 if flag < start:
filtered_output = real_tweet[start:end]
if " " in real_tweet and not real_tweet.startswith(" "):
filtered_output = filtered_output.strip()
text_annotetor = re.sub(" {2,}", " ", real_tweet)
 start = text_annotetor.find(filtered_output)
 end = start + len(filtered_output)
 start -= 0
end += 2
flag = real_tweet.find(" ")
 if flag < start:
filtered_output = real_tweet[start:end]
return filtered_output<load_from_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]
train.drop(columns=drop_cols + [target_col], inplace=True)
 test.drop(columns=drop_cols, inplace=True )
final_output = []
test = pd.read_csv(".. /input/tweet-sentiment-extraction/test.csv")
 tweet = list(test.text.values)
 for c, t in zip(cand, tweet):
final_output.append(pp(c, t))<load_from_csv>
keras_oof = np.load(NN_PATH / "keras_oof.npy")
 lgb_oof = np.load(LGB_PATH / "lgb_oof.npy")
 xgb_oof = np.load(XGB_PATH / "xgb_oof.npy")
 keras_oof.shape, lgb_oof.shape, xgb_oof.shape
sample = pd.read_csv(".. /input/tweet-sentiment-extraction/sample_submission.csv")
 sample.loc[:, 'selected_text'] = final_output
sample<save_to_csv>
keras_test = pd.read_csv(NN_PATH / "submit.csv" ).drop(columns=["id"])
 lgb_test = pd.read_csv(LGB_PATH / "submit.csv" ).drop(columns=["id"])
 xgb_test = pd.read_csv(XGB_PATH / "submit_xgboost.csv" ).drop(columns=["id"])
 keras_test.shape, lgb_test.shape, xgb_test.shape
sample.to_csv('submission.csv', index=False )<import_modules>
train = np.concatenate([keras_oof, lgb_oof, xgb_oof], axis=1)
 test = np.concatenate([keras_test, lgb_test, xgb_test], axis=1)
 train.shape, test.shape
print('TF version',tf.__version__ )<load_from_csv>
NFOLDS = 5
RANDOM_STATE = 871972
folds = StratifiedKFold(n_splits=NFOLDS, shuffle=True,
random_state=RANDOM_STATE)
 params = {
'metric':'multi_logloss',
'objective': 'multiclass',
'num_class': 9,
'verbosity': 1,
}
def read_train() :
train=pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 train['text']=train['text'].astype(str)
 train['selected_text']=train['selected_text'].astype(str)
 return train
def read_test() :
test=pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 test['text']=test['text'].astype(str)
 return test
def read_submission() :
test=pd.read_csv('.. /input/tweet-sentiment-extraction/sample_submission.csv')
 return test
train_df = read_train()
test_df = read_test()
submission_df = read_submission()<string_transform>
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[train_index], train[valid_index]
y_train, y_valid = target.loc[train_index].astype(int), target.loc[valid_index].astype(int)
 train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 lgb_model = lgb.train(params,train_data,num_boost_round=30000,
valid_sets=[train_data, valid_data],
verbose_eval=300,early_stopping_rounds=300)
 y_pred_valid = lgb_model.predict(X_valid,
num_iteration=lgb_model.best_iteration)
 oof[valid_index] = y_pred_valid
score += log_loss(y_valid, y_pred_valid)
 y_pred_test += lgb_model.predict(test, num_iteration=lgb_model.best_iteration)/NFOLDS
print('valid logloss average:', score/NFOLDS, log_loss(target, oof))
<define_variables><EOS> 
sample_submit = pd.read_csv(INPUT_PATH / "sampleSubmission.csv")
 submit = pd.concat([sample_submit[['id']], pd.DataFrame(y_pred_test)], axis=1)
 submit.columns = sample_submit.columns
submit.to_csv('submit.csv', index=False )