Kaggle competition: Jane Street Market Prediction
num_outliers_per_cent = round(len(outliers)*100/len(test_data), 1 )<compute_test_metric>
tf.__version__
acc_round = round(acc,3)
 print('acc =', acc, '=', acc_round )<define_variables>
SEED = 1111
tf.random.set_seed(SEED)
 np.random.seed(SEED)
if n_splits == 1:
n_splits_res = ""
else: n_splits_res = f"n_splits = {n_splits}, "<train_model>
CACHE_PATH='.. /input/tensorflow-resnet-training'
features = [f'feature_{i}' for i in range(130)]
f_mean = np.load(f'{CACHE_PATH}/f_mean_online.npy' )
print(f"Model - {model_type}, {model_name}")
 print(f"* {dataset} - Commit __(LB = 0._____): lr = {model_args['learning_rate']}, {n_splits_res}num_epochs = {model_args['num_train_epochs']}, seed = {seed}, acc = {acc_round}, num_outliers = {len(outliers)}({num_outliers_per_cent}%), weight = {weight}" )<install_modules>
def stats_features(tmp_df):
tmp_df['feature_cross_41_42_43']=tmp_df['feature_41']+tmp_df['feature_42']+tmp_df['feature_43']
tmp_df['feature_cross_1_2']=tmp_df['feature_1']/(tmp_df['feature_2']+1e-5)
 tmp_df.head()
return tmp_df
features.extend(['feature_cross_41_42_43','feature_cross_1_2'] )
VERSION = "nightly"
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
!python pytorch-xla-env-setup.py --version $VERSION<install_modules>
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
dropout_rate=0.2
hidden_size=256
inp = tf.keras.layers.Input(shape=(num_columns,))
x=tf.keras.layers.BatchNormalization()(inp)
 x=tf.keras.layers.Dropout(dropout_rate )(x)
 x1=tf.keras.layers.Dense(hidden_size )(x)
 x1=tf.keras.layers.BatchNormalization()(x1)
 x1=tf.keras.layers.LeakyReLU()(x1)
 x1=tf.keras.layers.Dropout(dropout_rate )(x1)
 x = tf.keras.layers.concatenate([x, x1], axis=1)
 x2=tf.keras.layers.Dense(hidden_size )(x)
 x2=tf.keras.layers.BatchNormalization(axis=1 )(x2)
 x2=tf.keras.layers.LeakyReLU()(x2)
 x2=tf.keras.layers.Dropout(dropout_rate )(x2)
 x = tf.keras.layers.concatenate([x1, x2], axis=1)
 x3=tf.keras.layers.Dense(hidden_size )(x)
 x3=tf.keras.layers.BatchNormalization(axis=1 )(x3)
 x3=tf.keras.layers.LeakyReLU()(x3)
 x3=tf.keras.layers.Dropout(dropout_rate )(x3)
 x = tf.keras.layers.concatenate([x2, x3], axis=1)
 x4=tf.keras.layers.Dense(hidden_size )(x)
 x4=tf.keras.layers.BatchNormalization(axis=1 )(x4)
 x4=tf.keras.layers.LeakyReLU()(x4)
 x4=tf.keras.layers.Dropout(dropout_rate )(x4)
 x = tf.keras.layers.concatenate([x3, x4], axis=1)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tfa.optimizers.AdamW(learning_rate=learning_rate,weight_decay=1e-5),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model
<save_to_csv><EOS> 
TRAINING=False
NUM_FOLDS=5
if not TRAINING:
models=[]
for i in range(NUM_FOLDS):
batch_size = 5000
hidden_units = [150, 150, 150]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 0.005
learning_rate = 1e-3
clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.load_weights('.. /input/tensorflow-resnet-training/online_model_{}.weights'.format(i))
clf.call = tf.function(clf.call, experimental_relax_shapes=True)
 models.append(clf)
 th = 0.502
f = np.median
env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
test_df=stats_features(test_df)
 x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:130].sum()):
x_tt[:, 1:130] = np.nan_to_num(x_tt[:, 1:130])+ np.isnan(x_tt[:, 1:130])* f_mean
pred = np.mean([model(x_tt, training=False ).numpy() for model in models], axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )