Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
tf.random.set_seed(42)
 %matplotlib inline
pd.set_option('display.max_rows', 10)
 pd.set_option('display.max_columns', None)
 pd.set_option('float_format', '{:f}'.format)
 mpl.rcParams['figure.dpi'] = 600
warnings.filterwarnings('ignore')
 tf.get_logger().setLevel('INFO')
 train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv', index_col = 'ImageId')
 x = train_df.drop(columns = 'label' ).values.reshape(-1, 28, 28)
 y = train_df['label'].values
x_test = test_df.values.reshape(-1, 28, 28)
 n_labels = len(np.unique(y))
del train_df, test_df
gc.collect()
train_data = train_data.drop(['AgeBand'], axis=1)
 combine = [train_data, test_data]
train_data.head()<sort_values>
def define_model(input_shape, n_classes, n_conv_branches, dropout):
inputs = layers.Input(shape = input_shape)
 b_in = layers.experimental.preprocessing.Rescaling(1./ 255 )(inputs)
 branches = [b_in] * n_conv_branches
for i in range(n_conv_branches):
for filter_size in [32, 64, 128, 128]:
branches[i] = layers.Conv2D(
 filters = filter_size,
kernel_size = 3,
padding = 'same',
)(branches[i])
 branches[i] = layers.MaxPool2D(pool_size =(2, 2))(branches[i])
 branches[i] = layers.ReLU()(branches[i])
 branches[i] = layers.Dropout(dropout )(branches[i])
 if n_conv_branches > 1:
b_out = layers.concatenate(branches)
 b_out = layers.Flatten()(b_out)
 else:
b_out = layers.Flatten()(branches[0])
 b_out = layers.Dense(units = 128 )(b_out)
 b_out = layers.BatchNormalization()(b_out)
 b_out = layers.ReLU()(b_out)
 b_out = layers.Dropout(dropout )(b_out)
 outputs = layers.Dense(units = n_classes )(b_out)
 return Model(inputs, outputs )
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_data[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
N_SPLITS = 10
CHECKPOINT_DIR = './checkpoint'
cv = StratifiedKFold(n_splits = N_SPLITS, random_state = 42, shuffle = True)
 oof_pred = np.zeros(( x_test.shape[0], n_labels))
cv_val_scores = np.zeros(N_SPLITS)
 histories = []
k = 0
for train_i, val_i in cv.split(x, y):
x_train = x[train_i, :]
x_valid = x[val_i, :]
y_train = y[train_i]
y_valid = y[val_i]
model = define_model(( x.shape[1], x.shape[2], 1), n_labels, 2, 0.2)
 gc.collect()
optimizer = Adam(
 learning_rate = 5e-4, 
)
 model.compile(
 optimizer = optimizer,
loss = SparseCategoricalCrossentropy(from_logits = True),
metrics = ['accuracy'] 
)
 checkpoint_call = ModelCheckpoint(
 filepath = CHECKPOINT_DIR,
save_weights_only = True,
monitor = 'val_accuracy',
mode = 'max',
save_best_only = True 
)
 stopping_call = EarlyStopping(
 monitor = 'val_accuracy',
patience = 50,
mode = 'max' 
)
 history = model.fit(
 x_train, y_train,
validation_data =(x_valid, y_valid),
epochs = 200,
callbacks = [checkpoint_call, stopping_call],
batch_size = 1024, 
)
 histories += [history]
model.load_weights(CHECKPOINT_DIR)
 predictor_model = tf.keras.Sequential([model, layers.Softmax() ])
 cv_val_scores[k] = model.evaluate(x_valid, y_valid)[1]
oof_pred += predictor_model.predict(x_test)/ N_SPLITS
k += 1
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_data[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()<drop_column>
print('Validation AUC: {:.6} ± {:.4}'.format(cv_val_scores.mean() , cv_val_scores.std()))
train_data = train_data.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_data = test_data.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [train_data, test_data]
train_data.head()<feature_engineering>
submission.loc[:, 'Label'] = np.argmax(oof_pred, axis = 1)
 submission.to_csv('submission.csv' )