Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
warnings.filterwarnings(category=FutureWarning, action="ignore")
 %matplotlib inline
backend.set_image_data_format('channels_last')
 DATA_PATH = '.. /input/'
SERIES = 'A'
VERSION = 1
print('{:s}{:d}'.format(SERIES, VERSION))
%time fcst.model.fit(X_train, y_train, eval_set=[(X_train, y_train),(X_valid, y_valid)], verbose=20 )<predict_on_test>
train_data = pd.read_csv(DATA_PATH+'train.csv')
 train_data.head()
def my_predict_fn(model, new_x, features_order, alpha):
new_x = new_x.reset_index()
new_x = new_x.merge(cal)
 new_x = new_x.merge(prices)
 new_x = new_x.sort_values('unique_id')
 new_x = new_x[features_order]
predictions = model.predict(new_x)
 return alpha * predictions<define_variables>
test_data = pd.read_csv(DATA_PATH+'test.csv')
 test_data.index =([x+1 for x in range(test_data.shape[0])])
 print(test_data.shape)
 test_data.head()
fcst.ts.num_threads<predict_on_test>
def get_model_params(layers)-> str:
res = {}
for layer in layers:
lres = {}
config = layer.get_config()
for key in ['filters', 'kernel_size', 'activation', 'pool_size',
'padding', 'strides', 'rate', 'units', 'kernel_regularizer',
'batch_input_shape']:
if key in config.keys() :
lres[key] = config[key]
res[layer.get_config() ['name']] = lres
rep = ''
for elem in res:
rep += elem+': '
rep += str(res[elem])+'
'
return rep
%%time
alphas = [1.028, 1.023, 1.018]
preds = None
for alpha in alphas:
alpha_preds = fcst.predict(28, my_predict_fn, alpha=alpha)
 alpha_preds = alpha_preds.set_index('ds', append=True)
 if preds is None:
preds = 1 / 3 * alpha_preds
else:
preds += 1 / 3 * alpha_preds
preds<rename_columns>
x_train = train_data.iloc[:, 1:].values.reshape(
(train_data.shape[0], 28, 28, 1)).astype('float32')
 x_train = x_train / 255.0
x_test = test_data.values.reshape(
(test_data.shape[0], 28, 28, 1)).astype('float32')
 x_test = x_test / 255.0
lb = LabelBinarizer()
y_train_ = lb.fit_transform(train_data.iloc[:, 0])
wide = preds.reset_index().pivot_table(index='unique_id', columns='ds')
 wide.columns = [f'F{i+1}' for i in range(28)]
wide.columns.name = None
wide.index.name = 'id'
wide<save_to_csv>
train_gen = ImageDataGenerator(
 rotation_range=9,
zoom_range=0.09,
width_shift_range=0.09,
height_shift_range=0.11,
validation_split=0.05 
)
 train_gen.fit(x_train)
 train_iterator = train_gen.flow(
 x=x_train, y=y_train_, batch_size=256, subset='training')
 val_iterator = train_gen.flow(
 x=x_train, y=y_train_, batch_size=256, subset='validation')
 train_x, train_y = train_iterator.next()
sample_sub = pd.read_csv(
 '.. /input/m5-forecasting-accuracy/sample_submission.csv', index_col='id' 
)
 sample_sub.update(wide)
 np.testing.assert_allclose(sample_sub.sum().sum() , preds['y_pred'].sum())
 sample_sub.to_csv('submission.csv' )<set_options>
model = Sequential([
Conv2D(filters=128, kernel_size=(3, 3),
activation='relu', input_shape=(28, 28, 1)) ,
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(filters=256, kernel_size=(3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)) ,
Conv2D(filters=512, kernel_size=(4, 4), activation='relu'),
MaxPooling2D(pool_size=(2, 2)) ,
Flatten() ,
Dense(units=256, activation='relu'),
Dropout(rate=0.35),
Dense(units=128, activation='relu'),
Dense(units=64, activation='relu'),
Dense(10, activation='softmax')
 ])
 print(model.summary())
 plot_model(model, show_shapes=True, show_layer_names=True,)
warnings.filterwarnings("ignore")
 pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 100)
 DATA_PATH = '.. /input/jane-street-market-prediction/'
NFOLDS = 5
TRAIN = False
CACHE_PATH = '.. /input/mlp012003weights'
def save_pickle(dic, save_path):
with open(save_path, 'wb')as f:
pickle.dump(dic, f)
 def load_pickle(load_path):
with open(load_path, 'rb')as f:
message_dict = pickle.load(f)
 return message_dict
feat_cols = [f'feature_{i}' for i in range(130)]
target_cols = ['action', 'action_1', 'action_2', 'action_3', 'action_4']
f_mean = np.load(f'{CACHE_PATH}/f_mean_online.npy')
 all_feat_cols = [col for col in feat_cols]
all_feat_cols.extend(['cross_41_42_43', 'cross_1_2'])
 class Model(nn.Module):
def __init__(self):
super(Model, self ).__init__()
self.batch_norm0 = nn.BatchNorm1d(len(all_feat_cols))
self.dropout0 = nn.Dropout(0.2)
 dropout_rate = 0.2
hidden_size = 256
self.dense1 = nn.Linear(len(all_feat_cols), hidden_size)
 self.batch_norm1 = nn.BatchNorm1d(hidden_size)
 self.dropout1 = nn.Dropout(dropout_rate)
 self.dense2 = nn.Linear(hidden_size+len(all_feat_cols), hidden_size)
 self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(dropout_rate)
 self.dense3 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(dropout_rate)
 self.dense4 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm4 = nn.BatchNorm1d(hidden_size)
 self.dropout4 = nn.Dropout(dropout_rate)
 self.dense5 = nn.Linear(hidden_size+hidden_size, len(target_cols))
self.Relu = nn.ReLU(inplace=True)
 self.PReLU = nn.PReLU()
self.LeakyReLU = nn.LeakyReLU(negative_slope=0.01, inplace=True)
 self.RReLU = nn.RReLU()
def forward(self, x):
x = self.batch_norm0(x)
 x = self.dropout0(x)
 x1 = self.dense1(x)
 x1 = self.batch_norm1(x1)
 x1 = self.LeakyReLU(x1)
 x1 = self.dropout1(x1)
 x = torch.cat([x, x1], 1)
 x2 = self.dense2(x)
 x2 = self.batch_norm2(x2)
 x2 = self.LeakyReLU(x2)
 x2 = self.dropout2(x2)
 x = torch.cat([x1, x2], 1)
 x3 = self.dense3(x)
 x3 = self.batch_norm3(x3)
 x3 = self.LeakyReLU(x3)
 x3 = self.dropout3(x3)
 x = torch.cat([x2, x3], 1)
 x4 = self.dense4(x)
 x4 = self.batch_norm4(x4)
 x4 = self.LeakyReLU(x4)
 x4 = self.dropout4(x4)
 x = torch.cat([x3, x4], 1)
 x = self.dense5(x)
 return x
if True:
device = torch.device("cpu")
 model_list = []
tmp = np.zeros(len(feat_cols))
for _fold in range(NFOLDS):
torch.cuda.empty_cache()
model = Model()
model.to(device)
 model_weights = f"{CACHE_PATH}/online_model{_fold}.pth"
model.load_state_dict(torch.load(model_weights, map_location=torch.device('cpu')))
 model.eval()
model_list.append(model )<choose_model_class>
NEPOCHS = 300
early_stopping_cb = EarlyStopping(monitor='val_acc', min_delta=1e-5,
patience=15, restore_best_weights=True)
 rl_reduce = ReduceLROnPlateau(monitor='val_loss', patience=10,factor=0.25,verbose=1,min_delta=1e-5)
 opt_rms = RMSprop(learning_rate=1e-3, centered=False)
 model.compile(loss='categorical_crossentropy', optimizer=opt_rms,
metrics=['accuracy'])
 start = dt.datetime.now()
history = model.fit_generator(
 generator=train_iterator,
verbose=1,
epochs=NEPOCHS,
max_queue_size=10,
validation_data=val_iterator,
callbacks=[
early_stopping_cb,
rl_reduce
] 
)
 print('
Evaluation :')
 model.evaluate_generator(generator=val_iterator,
verbose=1, 
)
 print('Finished in: ', dt.datetime.now() -start)
<choose_model_class><EOS> 
test_gen = ImageDataGenerator(
 rotation_range=9,
zoom_range=0.09,
width_shift_range=0.09,
height_shift_range=0.11, 
)
 test_gen.fit(x_test)
 test_iterator = test_gen.flow(x=x_test, batch_size=len(x_test), shuffle=False)
 test_x = test_iterator.next()
test_x
res = model.predict(test_x)
 y_pred = pd.DataFrame([test_data.index, [x.argmax() for x in res]] ).T
y_pred.columns = ['ImageId', 'Label']
y_pred.to_csv('digit_submission_2.csv', index=False)