Kaggle competition: Leaf Classification
import os
import json
from collections import Counter
import numpy as np
import pandas as pd<define_variables>
%pylab inline
%%time
!python.. /input/bert-base-random-code/run.py \
--sub_file=model2_bert_base_cased_pred.csv \
--data_path=/kaggle/input/google-quest-challenge/ \
--max_sequence_length=500 \
--max_title_length=26 \
--max_question_length=260 \
--max_answer_length=210 \
--batch_size=8 \
--bert_model=/kaggle/input/bert-base-pretrained/stackx-base-cased/<define_variables>
data = pd.DataFrame.from_csv('.. /input/train.csv')
 y = data['species']
y = LabelEncoder().fit(y ).transform(y)
 y_cat = to_categorical(y)
 margin = data.columns[1:65]
margin = data[margin].as_matrix()
margin = StandardScaler().fit(margin ).transform(margin)
 shape = data.columns[65:129]
shape = data[shape].as_matrix()
shape = StandardScaler().fit(shape ).transform(shape)
 texture = data.columns[129:193]
texture = data[texture].as_matrix()
texture = StandardScaler().fit(texture ).transform(texture)
ROBERTA_EXPERIMENT_DIR = "2-4-roberta-base-saved-5-head_tail-roberta-stackx-base-v2-pl1kksample20k-1e-05-210-260-500-26-roberta-200"
!mkdir $ROBERTA_EXPERIMENT_DIR
!ln -s /kaggle/input/roberta-stackx-base-pl20k/checkpoints $ROBERTA_EXPERIMENT_DIR/checkpoints
ROBERTA_CONFIG = {
"_seed": 42,
"batch_accumulation": 2,
"batch_size": 4,
"bert_model": "roberta-base-saved",
"folds": 5,
"head_tail": True,
"label": "roberta-stackx-base-v2-pl1kksample20k",
"lr": 1e-05,
"max_answer_length": 210,
"max_question_length": 260,
"max_sequence_length": 500,
"max_title_length": 26,
"model_type": "roberta",
"warmup": 200
}
with open(os.path.join(ROBERTA_EXPERIMENT_DIR, "config.json"), "w")as fp:
json.dump(ROBERTA_CONFIG, fp)
 !echo kek > $ROBERTA_EXPERIMENT_DIR/command<define_variables>
modelMargin = Sequential()
modelMargin.add(Dense(128, input_dim=64, activation='relu'))
modelMargin.add(Dropout(0.7))
modelShape = Sequential()
modelShape.add(Dense(128, input_dim=64, activation='relu'))
modelShape.add(Dropout(0.7))
modelTexture = Sequential()
modelTexture.add(Dense(128, input_dim=64, activation='relu'))
modelTexture.add(Dropout(0.7))
merged = Merge([modelMargin, modelShape, modelTexture], mode='concat' )
%%time
!python.. /input/roberta-base-code/infer.py \
--experiment $ROBERTA_EXPERIMENT_DIR \
--checkpoint=best_model.pth \
--bert_model=/kaggle/input/roberta-base-model \
--dataframe=/kaggle/input/google-quest-challenge/test.csv \
--output_dir=roberta-base-output<define_variables>
model = Sequential()
model.add(merged)
 model.add(Dense(99, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
 history = model.fit(
 [margin, shape, texture],
y_cat,
nb_epoch=350,
batch_size=32,
validation_split=0.1,
verbose=0
)
<load_from_csv><EOS> 
test1 = pd.read_csv('.. /input/test.csv')
 test = pd.DataFrame.from_csv('.. /input/test.csv')
 index = test1.pop('id')
 testMargin = test[test.columns[0:64]].as_matrix()
testShape = test[test.columns[64:128]].as_matrix()
testTexture = test[test.columns[128:192]].as_matrix()
testMargin = StandardScaler().fit(testMargin ).transform(testMargin)
 testShape = StandardScaler().fit(testShape ).transform(testShape)
 testTexture = StandardScaler().fit(testTexture ).transform(testTexture)
 yPred = model.predict_proba(
 [testMargin, testShape, testTexture] 
)
 columns = data['species'].unique()
yPred = pd.DataFrame(yPred, index=index, columns=sort(columns))
fp = open('merged_nn2.csv','w')
 fp.write(yPred.to_csv() )