Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<categorify>
! pip install -q --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
! pip install -q /kaggle/input/iterative-stratification/iterative-stratification-master/
%%time
print("Transforming text data to sequences...")
 raw_text = np.hstack([full_df.item_description.str.lower() , full_df.name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to sequences...")
 full_df['seq_item_description'] = tok_raw.texts_to_sequences(full_df.item_description.str.lower())
 full_df['seq_name'] = tok_raw.texts_to_sequences(full_df.name.str.lower())
 del tok_raw<define_variables>
import pandas as pd
import numpy as np
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_TEXT = np.max([
np.max(full_df.seq_name.max()),
np.max(full_df.seq_item_description.max()),
])+ 4
MAX_CATEGORY = np.max(full_df.category_name.max())+ 1
MAX_BRAND = np.max(full_df.brand_name.max())+ 1
MAX_CONDITION = np.max(full_df.item_condition_id.max())+ 1<prepare_x_and_y>
import pandas as pd
import numpy as np
%%time
def get_keras_data(df):
X = {
'name': pad_sequences(df.seq_name, maxlen=MAX_NAME_SEQ),
'item_desc': pad_sequences(df.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ),
'brand_name': np.array(df.brand_name),
'category_name': np.array(df.category_name),
'item_condition': np.array(df.item_condition_id),
'num_vars': np.array(df[["shipping"]]),
}
return X
train = full_df[:n_trains]
dev = full_df[n_trains:n_trains+n_devs]
test = full_df[n_trains+n_devs:]
X_train = get_keras_data(train)
 X_dev = get_keras_data(dev)
 X_test = get_keras_data(test )<categorify>
! python.. /input/better-cv-pl-v1/v1_inference.py
! python.. /input/moa-sin-v2/v2_inference.py
! python.. /input/baseline-inference/moa_lstm_inference.py
! python.. /input/resnet-sin-v3/v3_inference.py
! python.. /input/inference-scripts/moa-tabnet-correct-inference-freezepca.py
! python.. /input/baseline-inference/moa_v6.py
! python.. /input/baseline-inference/moa_v7.py
! python.. /input/baseline-inference/moa_v8.py
! python.. /input/baseline-inference/moa_inference_non_scored.py
! python.. /input/resnet-inference-script/moa-multi-input-resnet-model-inference.py
submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 submission.iloc[:, 1:] = 0
subv1 = pd.read_csv('./submission_v1_sin.csv')
 subv2 = pd.read_csv('./submission_v2_sin.csv')
 subv3 = pd.read_csv('./submission_v3_sin.csv')
 subv4 = pd.read_csv('./submission_v4_sin.csv')
 subv5 = pd.read_csv('./submission_v5_sin.csv')
 subv6 = pd.read_csv('./submission_v6.csv')
 subv7 = pd.read_csv('./submission_v7.csv')
 subv8 = pd.read_csv('./submission_v8.csv')
 subv9 = pd.read_csv('./submission_tabnet_correct.csv')
 subv10 = pd.read_csv('./submission_resnet.csv' )
def new_rnn_model(lr=0.001, decay=0.0):
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 category_name = Input(shape=[1], name="category_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 emb_name = Embedding(MAX_TEXT, 20 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 60 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_category_name = Embedding(MAX_CATEGORY, 10 )(category_name)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name),
Flatten()(emb_category_name),
item_condition,
rnn_layer1,
rnn_layer2,
num_vars,
])
 main_l = Dense(256 )(main_l)
 main_l = Activation('elu' )(main_l)
 main_l = Dense(128 )(main_l)
 main_l = Activation('elu' )(main_l)
 main_l = Dense(64 )(main_l)
 main_l = Activation('elu' )(main_l)
 output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name , category_name, item_condition, num_vars], output)
 optimizer = Adam(lr=lr, decay=decay)
 model.compile(loss="mse", optimizer=optimizer)
 return model
model = new_rnn_model()
model.summary()
del model<train_model>
weight = [0.32507418/3, 0.32528637/3, 0., 0.34963945/3, 1/9, 1/9, 1/9, 1/9, 1/9, 1/9]
%%time
BATCH_SIZE = 1024
epochs = 2
exp_decay = lambda init, fin, steps:(init/fin)**(1/(steps-1)) - 1
steps = int(n_trains / BATCH_SIZE)* epochs
lr_init, lr_fin = 0.007, 0.0005
lr_decay = exp_decay(lr_init, lr_fin, steps)
 rnn_model = new_rnn_model(lr=lr_init, decay=lr_decay)
 print("Fitting RNN model to training examples...")
 rnn_model.fit(
 X_train, Y_train, epochs=epochs, batch_size=BATCH_SIZE,
validation_data=(X_dev, Y_dev), verbose=2,
)<predict_on_test>
print(sum(weight), len(weight))
%%time
print("Evaluating the model on validation data...")
 Y_dev_preds_rnn = rnn_model.predict(X_dev, batch_size=BATCH_SIZE)
 print(" RMSLE error:", rmsle(Y_dev, Y_dev_preds_rnn))<predict_on_test>
subs = [subv1, subv2, subv3, subv4, subv5, subv6, subv7, subv8, subv9, subv10]
rnn_preds = rnn_model.predict(X_test, batch_size=BATCH_SIZE, verbose=1)
 rnn_preds = np.expm1(rnn_preds )<concatenate>
for w, sub in zip(weight, subs):
submission.iloc[:, 1:] += sub.iloc[:,1:] * w
full_df = pd.concat([train_df, dev_df, test_df] )<data_type_conversions>
submission.to_csv('submission.csv', index=False )
%%time
full_df['shipping'] = full_df['shipping'].astype(str)
 full_df['item_condition_id'] = full_df['item_condition_id'].astype(str )<feature_engineering>
submission.to_csv('submission.csv', index=False )