Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<feature_engineering>
%matplotlib inline
Dense,
Flatten,
Dropout,
Conv2D,
MaxPooling2D,
Activation,
BatchNormalization 
)
train['SalePrice'] = np.log1p(train['SalePrice'] )<sort_values>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
corr["SalePrice"].sort_values(ascending = False )<drop_column>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
data = pd.concat([train, test], axis = 0, sort = False)
 data.drop(['Id', 'SalePrice'], axis = 1)
 data<sort_values>
X_train = train.loc[:, train.columns!='label'].values.astype('uint8')
 y_train = train['label'].values
X_train = X_train.reshape(( X_train.shape[0],28,28))
missing = data.isnull().sum().sort_values(ascending = False)
 missing<concatenate>
X_test = test.loc[:, test.columns!='label'].values.astype('uint8')
 X_test = X_test.reshape(( X_test.shape[0],28,28))
missingg = missing*100/len(data)
 missing_data = pd.concat([missing, missingg], axis=1, keys=['missing', 'missing_%'])
 missing_data<drop_column>
X_train = X_train[:,:,:,None]
X_test = X_test[:,:,:,None]
data.drop(( missing_data[missing_data['missing'] > 5] ).index, axis = 1, inplace = True )<categorify>
batch_size = 32
num_samples = X_train.shape[0]
num_classes = np.unique(y_train ).shape[0]
num_epochs = 50
img_rows, img_cols = X_train[0,:,:,0].shape
img_channels = 1
classes = np.unique(y_train )
numeric = ['BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath', 'GarageArea', 'GarageCars']
for feature in numeric: data[feature] = data[feature].fillna(0)
 categorical = ['Exterior1st', 'Exterior2nd', 'SaleType', 'MSZoning', 'Electrical', 'KitchenQual']
for feature in categorical: data[feature] = data[feature].fillna(data[feature].mode() [0] )<data_type_conversions>
y_train = np_utils.to_categorical(y_train, num_classes)
data['Functional'] = data['Functional'].fillna('Typ' )<drop_column>
X_train_norm = X_train.astype('float32')
 X_test_norm = X_test.astype('float32')
 X_train_norm /= 255
X_test_norm /= 255
data.drop(['Utilities'], axis = 1, inplace = True )<sort_values>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
numeric_feats = data.dtypes[data.dtypes != 'object'].index
skewed_feats = data[numeric_feats].apply(lambda x: x.skew() ).sort_values(ascending = False)
 high_skew = skewed_feats[abs(skewed_feats)> 0.5]
high_skew<feature_engineering>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=5,
verbose=1,
factor=0.2)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10 )
for feature in high_skew.index: data[feature] = np.log1p(data[feature] )<categorify>
history = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
data = pd.get_dummies(data)
 data<prepare_x_and_y>
! mkdir newer
y_train = train["SalePrice"]
x_train = data[:len(y_train)]
x_test = data[len(y_train):]<compute_train_metric>
model.save('newer/simple.h5' )
scorer = make_scorer(mean_squared_error, greater_is_better = False)
 def rmse_CV_train(model):
kf = KFold(5, shuffle = True, random_state = 42 ).get_n_splits(x_train.values)
 rmse = np.sqrt(-cross_val_score(model, x_train, y_train, scoring = "neg_mean_squared_error", cv = kf))
return(rmse)
 def rmse_CV_test(model):
kf = KFold(5, shuffle = True, random_state = 42 ).get_n_splits(train.values)
 rmse = np.sqrt(-cross_val_score(model, x_test, y_test,scoring = "neg_mean_squared_error", cv = kf))
return(rmse )<train_model>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
model = XGB.XGBRegressor(colsample_bytree = 0.4603, gamma = 0.0468, learning_rate = 0.05, max_depth = 3, min_child_weight = 1.7817, n_estimators = 2200, reg_alpha = 0.4640, reg_lambda = 0.8571, subsample = 0.5213, random_state = 7, nthread = -1)
 model.fit(x_train, y_train )<predict_on_test>
history1 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
prediction = np.floor(np.expm1(model.predict(x_test)) )<create_dataframe>
model.save('newer/simple_batch.h5' )
submission = pd.DataFrame({'Id': test.Id, 'SalePrice': prediction})
 submission<save_to_csv>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
submission.to_csv('submission.csv', index = False )<load_from_csv>
history2 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
if tuning or training:
train_data = pd.read_csv('/kaggle/input/jane-street-market-prediction/train.csv')
 train_data.fillna(train_data.mean() ,inplace=True)
 start_date=86
feature_columns = [col for col in train_data.columns.values if 'feature' in col]
corr=abs(train_data[feature_columns].corr())
 ordered_feature_columns=[feature_columns.pop(0)]
for col in feature_columns:
corr_max = corr[col][ordered_feature_columns].idxmax()
corr_max_idx = ordered_feature_columns.index(corr_max)
 ordered_feature_columns.insert(corr_max_idx+1,col)
 f_mean = train_data[ordered_feature_columns].mean()
f_mean.to_csv('f_mean.csv',index=False)
 with open("ordered_columns.txt", 'wb')as f:
pickle.dump(( ordered_feature_columns), f)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
train_data = train_data[train_data['date']>=start_date]
X = train_data[ordered_feature_columns].values
y = np.stack([(train_data[c] > 0 ).astype('int')for c in resp_cols] ).T
max_date = train_data['date'].max()
split_date = int(0.7*(max_date-start_date))
train_test_split = train_data['date'][train_data['date']==split_date].index[0]
del train_data
X_train=X[:train_test_split,:]
X_test=X[train_test_split:,:]
y_train=y[:train_test_split,:]
y_test=y[train_test_split:,:]<choose_model_class>
model.save('newer/32x64_64x128.h5' )
tf.random.set_seed(SEED)
 def create_model(hp, num_columns, num_labels,encoder):
inp = tf.keras.layers.Input(shape =(num_columns, 1))
x1 = encoder(inp)
 x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Conv1D(filters=8,
kernel_size=hp.Int('kernel_size',5,10,step=5),
strides=1,
activation='relu' )(x)
 x = tf.keras.layers.MaxPooling1D(pool_size=2 )(x)
 x = tf.keras.layers.Flatten()(x)
 x = tf.keras.layers.Concatenate()([x,x1])
 for i in range(hp.Int('num_layers',4,6)) :
x = tf.keras.layers.Dense(hp.Int(f'num_units_{i}',256,512))(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Activation(tf.keras.activations.swish )(x)
 x = tf.keras.layers.Dropout(hp.Float(f'dropout_{i}',0,0.5))(x)
 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 = tf.keras.optimizers.Adam(learning_rate = hp.Float('lr',0.00001,0.1,default=0.001)) ,
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = 0.01),
metrics = tf.keras.metrics.AUC(name = 'AUC'), 
)
 return model
def create_autoencoder(input_dim,output_dim):
i = tf.keras.layers.Input(input_dim)
 encoded = tf.keras.layers.BatchNormalization()(i)
 encoded = tf.keras.layers.GaussianNoise(0.05 )(encoded)
 encoded = tf.keras.layers.Dense(64,activation='relu' )(encoded)
 decoded = tf.keras.layers.Dropout(0.2 )(encoded)
 decoded = tf.keras.layers.Dense(input_dim,name='decoded' )(decoded)
 x = tf.keras.layers.Dense(32,activation='relu' )(decoded)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.2 )(x)
 x = tf.keras.layers.Dense(output_dim,activation='sigmoid',name='label_output' )(x)
 encoder = Model(inputs=i,outputs=encoded)
 autoencoder = Model(inputs=i,outputs=[decoded,x])
 autoencoder.compile(optimizer=Adam(0.001),loss={'decoded':'mse','label_output':'binary_crossentropy'})
 return autoencoder, encoder<categorify>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5, 5), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
autoencoder, encoder = create_autoencoder(130,5)
 if training:
autoencoder.fit(X,(X,y),
epochs=1000,
batch_size=4096,
validation_split=0.1,
callbacks=[EarlyStopping('val_loss',patience=10,restore_best_weights=True)])
 encoder.save_weights('JS_CNN_encoder.hdf5')
 else:
encoder.load_weights('/kaggle/input/jscnn/JS_CNN_encoder_seed_111.hdf5')
 encoder.trainable = False<train_model>
history3 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
if tuning:
model_fn = lambda hp: create_model(hp,X_train.shape[-1],y_train.shape[-1],encoder)
 tuner = kt.tuners.bayesian.BayesianOptimization(
 hypermodel=model_fn,
objective= kt.Objective('val_AUC', direction='max'),
num_initial_points=4,
max_trials=20)
 tuner.search(X_train,y_train,batch_size=4096,epochs=20, validation_data =(X_test, y_test), callbacks=[EarlyStopping('val_AUC', mode='max',patience=5)])
 hp = tuner.get_best_hyperparameters(1)[0]
pd.to_pickle(hp,'best_hp_cnn_day_86_encoder_seed_111.pkl' )<train_model>
model.save('newer/32x64x64.h5' )
if training:
hp = pd.read_pickle('best_hp_cnn_day_86_encoder_seed_111.pkl')
 model_fn = lambda hp: create_model(hp,X_train.shape[-1],y_train.shape[-1],encoder)
 model = model_fn(hp)
 model.fit(X_train,y_train,validation_data=(X_test,y_test),epochs=100,batch_size=4096,
callbacks=[EarlyStopping('val_AUC',mode='max',patience=10,restore_best_weights=True)])
 model.save_weights('JS_CNN_day_86_encoder_seed_111.hdf5' )<load_from_csv>
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
if not training or tuning:
model_fn = lambda hp: create_model(hp,130,5,encoder)
 hp = pd.read_pickle('/kaggle/input/jscnn/best_hp_cnn_day_86_encoder_seed_111.pkl')
 model = model_fn(hp)
 model.load_weights('/kaggle/input/jscnn/JS_CNN_day_86_encoder_seed_111.hdf5')
 samples_mean = pd.read_csv('/kaggle/input/jscnn/f_mean.csv')
 env = janestreet.make_env()
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
weight = test_df.weight.iloc[0]
with open("/kaggle/input/jscnn/ordered_columns.txt", 'rb')as f:
ordered_cols = pickle.load(f)
 test_df = test_df[ordered_cols]
X_test = test_df.values[0]
if weight==0:
sample_prediction_df.action = 0
else:
for index, x in np.ndenumerate(X_test):
idx=index[0]
if np.isnan(x):
X_test[idx] = samples_mean.iloc[idx]
X=X_test.reshape(( 1,-1))
prediction = np.median(model(X, training=False ).numpy() [0])
 sample_prediction_df.action = int(round(prediction))
env.predict(sample_prediction_df)
<import_modules>
history4 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers.experimental.preprocessing import Normalization
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import GroupKFold
import tensorflow_addons as tfa
from tqdm import tqdm
from random import choices
import kerastuner as kt<import_modules>
model.save('newer/64x128.h5' )
class PurgedGroupTimeSeriesSplit(_BaseKFold):

 @_deprecate_positional_args
def __init__(self,
n_splits=5,
*,
max_train_group_size=np.inf,
max_test_group_size=np.inf,
group_gap=None,
verbose=False 
):
super().__init__(n_splits, shuffle=False, random_state=None)
 self.max_train_group_size = max_train_group_size
self.group_gap = group_gap
self.max_test_group_size = max_test_group_size
self.verbose = verbose
def split(self, X, y=None, groups=None):

 if groups is None:
raise ValueError(
 "The 'groups' parameter should not be None")
 X, y, groups = indexable(X, y, groups)
 n_samples = _num_samples(X)
 n_splits = self.n_splits
group_gap = self.group_gap
max_test_group_size = self.max_test_group_size
max_train_group_size = self.max_train_group_size
n_folds = n_splits + 1
group_dict = {}
u, ind = np.unique(groups, return_index=True)
 unique_groups = u[np.argsort(ind)]
n_samples = _num_samples(X)
 n_groups = _num_samples(unique_groups)
 for idx in np.arange(n_samples):
if(groups[idx] in group_dict):
group_dict[groups[idx]].append(idx)
 else:
group_dict[groups[idx]] = [idx]
if n_folds > n_groups:
raise ValueError(
("Cannot have number of folds={0} greater than"
" the number of groups={1}" ).format(n_folds,
n_groups))
group_test_size = min(n_groups // n_folds, max_test_group_size)
 group_test_starts = range(n_groups - n_splits * group_test_size,
n_groups, group_test_size)
 for group_test_start in group_test_starts:
train_array = []
test_array = []
group_st = max(0, group_test_start - group_gap - max_train_group_size)
 for train_group_idx in unique_groups[group_st:(group_test_start - group_gap)]:
train_array_tmp = group_dict[train_group_idx]
train_array = np.sort(np.unique(
 np.concatenate(( train_array,
train_array_tmp)) ,
axis=None), axis=None)
 train_end = train_array.size
for test_group_idx in unique_groups[group_test_start:
group_test_start +
group_test_size]:
test_array_tmp = group_dict[test_group_idx]
test_array = np.sort(np.unique(
 np.concatenate(( test_array,
test_array_tmp)) ,
axis=None), axis=None)
 test_array = test_array[group_gap:]
if self.verbose > 0:
pass
yield [int(i)for i in train_array], [int(i)for i in test_array]<split>
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
class CVTuner(kt.engine.tuner.Tuner):
def run_trial(self, trial, X, y, splits, batch_size=32, epochs=1,callbacks=None):
val_losses = []
for train_indices, test_indices in splits:
X_train, X_test = [x[train_indices] for x in X], [x[test_indices] for x in X]
y_train, y_test = [a[train_indices] for a in y], [a[test_indices] for a in y]
if len(X_train)< 2:
X_train = X_train[0]
X_test = X_test[0]
if len(y_train)< 2:
y_train = y_train[0]
y_test = y_test[0]
model = self.hypermodel.build(trial.hyperparameters)
 hist = model.fit(X_train,y_train,
validation_data=(X_test,y_test),
epochs=epochs,
batch_size=batch_size,
callbacks=callbacks)
 val_losses.append([hist.history[k][-1] for k in hist.history])
 val_losses = np.asarray(val_losses)
 self.oracle.update_trial(trial.trial_id, {k:np.mean(val_losses[:,i])for i,k in enumerate(hist.history.keys())})
 self.save_model(trial.trial_id, model )<data_type_conversions>
history5 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
TRAINING = True
USE_FINETUNE = True
FOLDS = 5
SEED = 1111
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv')
 train = train.query('date > 85' ).reset_index(drop = True)
 train = train.astype({c: np.float32 for c in train.select_dtypes(include='float64' ).columns})
 train.fillna(train.mean() ,inplace=True)
 train = train.query('weight > 0' ).reset_index(drop = True)
 train['action'] =(( train['resp_1'] > 0)&(train['resp_2'] > 0)&(train['resp_3'] > 0)&(train['resp_4'] > 0)&(train['resp'] > 0)).astype('int')
 features = [c for c in train.columns if 'feature' in c]
resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X = train[features].values
y = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
f_mean = np.mean(train[features[1:]].values,axis=0 )<categorify>
model.save('newer/64x128_256x512.h5' )
def create_autoencoder(input_dim,output_dim,noise=0.05):
i = Input(input_dim)
 encoded = BatchNormalization()(i)
 encoded = GaussianNoise(noise )(encoded)
 encoded = Dense(150,activation='relu' )(encoded)
 encoded = BatchNormalization()(encoded)
 encoded = Dropout(0.1 )(encoded)
 encoded = Dense(80,activation='relu' )(encoded)
 encoded = BatchNormalization()(encoded)
 encoded = Dropout(0.1 )(encoded)
 encoded = Dense(25,activation='relu',name='encoder' )(encoded)
 encoded = BatchNormalization()(encoded)
 decoded = Dropout(0.1 )(encoded)
 decoded = Dense(80,activation='relu' )(decoded)
 decoded = BatchNormalization()(decoded)
 decoded = Dropout(0.1 )(decoded)
 decoded = Dense(150,activation='relu' )(decoded)
 decoded = BatchNormalization()(decoded)
 decoded = Dropout(0.1 )(decoded)
 decoded = Dense(input_dim,name='decoded' )(decoded)
 encoder = Model(inputs=i,outputs=encoded)
 autoencoder = Model(inputs=i,outputs=decoded)
 autoencoder.compile(optimizer=tfa.optimizers.RectifiedAdam(0.001),loss={'decoded':'mse'})
 return autoencoder, encoder<categorify>
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
def create_model(hp,input_dim,output_dim,encoder):
inputs = Input(input_dim)
 x = encoder(inputs)
 x = Concatenate()([x,inputs])
 x = BatchNormalization()(x)
 x = Dropout(hp.Float('init_dropout',0.0,0.5))(x)
 for i in range(hp.Int('num_layers',1,3)) :
x = Dense(hp.Int('num_units_{i}',64,256))(x)
 x = BatchNormalization()(x)
 x = Lambda(tf.keras.activations.swish )(x)
 x = Dropout(hp.Float(f'dropout_{i}',0.0,0.5))(x)
 x = Dense(output_dim,activation='sigmoid' )(x)
 model = Model(inputs=inputs,outputs=x)
 model.compile(optimizer=tfa.optimizers.RectifiedAdam(hp.Float('lr',0.00001,0.1,default=0.001)) ,loss=BinaryCrossentropy(label_smoothing=hp.Float('label_smoothing',0.0,0.1)) ,metrics=[tf.keras.metrics.AUC(name = 'auc')])
 return model<categorify>
history6 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)
autoencoder, encoder = create_autoencoder(X.shape[-1],y.shape[-1],noise=0.1)
 if TRAINING:
autoencoder.fit(X,X,
epochs=1000,
batch_size=4096,
validation_split=0.1,
callbacks=[EarlyStopping('val_loss',patience=10,restore_best_weights=True)])
 encoder.save_weights('./encoder.hdf5')
 else:
encoder.load_weights('.. /input/ver-0-seed-1111/encoder.hdf5')
 encoder.trainable = False<feature_engineering>
model.save('newer/64x128_256x512_diff_fcnn.h5' )
if not TRAINING:
f = np.median
models = models[-2:]
env = janestreet.make_env()
th = 0.5
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* 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 )<import_modules>
model = load_model('newer/64x128_256x512_diff_fcnn.h5' )
warnings.filterwarnings("ignore")
 VERSION = ''<define_variables>
pred = model.predict_classes(X_test_norm )
SEED = 420
N_ESTIMATORS = 250
DEVICE = torch.device("cpu" )<compute_test_metric>
sample_submission['Label'] = pred
<load_pretrained><EOS> 
sample_submission.to_csv("submission.csv", index=False )