Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<split>
def load_train_data(split=.0):
	with open('.. /input/digit-recognizer/train.csv')as file:
		data = np.asarray([x.replace('
', '' ).split(',')for x in file.readlines() [1:]])
 	train_data = data.astype('float32')[:, 1:].reshape(( len(data), 28, 28, 1))
	train_labels = to_categorical(data[:, 0])
 	
	indices = np.arange(len(data))
	np.random.shuffle(indices)
 	train_data = train_data[indices]
	train_labels = train_labels[indices]
	
	if split >.0:
		stop = int(len(data)* split)
 		val_data = train_data[:stop]
		val_labels = train_labels[:stop]
		train_data = train_data[stop:]
		train_labels = train_labels[stop:]
		return(train_data, train_labels),(val_data, val_labels)
 	else:
		return(train_data, train_labels)
 def load_test_data() :
	with open('.. /input/digit-recognizer/test.csv')as file:
		data = np.asarray([x.replace('
', '' ).split(',')for x in file.readlines() [1:]])
 	test_data = data.astype('float32' ).reshape(( data.shape[0], 28, 28, 1)) / 255
	return test_data
def get_model() :
	model = models.Sequential()
	model.add(layers.Conv2D(32,(3, 3), use_bias=False, padding='Same', input_shape=(28, 28, 1)))
 	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.Conv2D(32,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.MaxPooling2D(( 2, 2)))
 	model.add(layers.Dropout (.4))
	model.add(layers.Conv2D(64,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.Conv2D(64,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.MaxPooling2D(( 2, 2)))
 	model.add(layers.Dropout (.4))
	model.add(layers.Conv2D(128,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.Conv2D(128,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.Conv2D(128,(3, 3), use_bias=False, padding='Same'))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.MaxPooling2D(( 2, 2)))
 	model.add(layers.Dropout (.4))
	model.add(layers.Flatten())
 	model.add(layers.Dense(512, use_bias=False))
	model.add(layers.BatchNormalization())
 	model.add(layers.Activation('relu'))
	model.add(layers.Dropout (.4))
	model.add(layers.Dense(10, activation='softmax'))
	model.compile(optimizer='adam',
			 loss='categorical_crossentropy',
			 metrics=['accuracy'])
 	return model
def plot_losses(history):
	train_loss = history.history.get('loss')
 	val_loss = history.history.get('val_loss', None)
 	train_acc = history.history.get('accuracy')
 	val_acc = history.history.get('val_accuracy', None)
 	epochs = np.arange(len(train_loss)) + 1
	plt.figure(figsize=(10, 4))
	plt.subplot(121)
 	plt.plot(epochs, train_loss, color='blue', label='Training loss')
 	if val_loss:
		plt.plot(epochs, val_loss, color='red', label='Validation loss')
 	plt.xlabel('Epochs')
 	plt.ylabel('Loss')
 	plt.legend()
	plt.grid()
	plt.subplot(122)
 	plt.plot(epochs, train_acc, color='blue', label='Training acc')
 	if val_acc:
		plt.plot(epochs, val_acc, color='red', label='Validation acc')
 	plt.xlabel('Epochs')
 	plt.ylabel('Accuracy')
 	plt.legend()
	plt.grid()
	plt.show()
def confirm_submission(predict):
	data = [f'{n + 1},{x}
' for n, x in enumerate(predict)]
	with open('submission.csv', 'w')as file:
		file.write('ImageID,Label
')
 		for line in data:
			file.write(line)
train_x, val_x, train_y, val_y = train_test_split(train, target, train_size = 0.8, random_state = RANDOM_STATE )<normalization>
( train_data, train_labels),(val_data, val_labels)= load_train_data(split=.2)
 batch_size = 64
train_gen = ImageDataGenerator(
 	rescale=1./255,
	rotation_range=10,
	zoom_range=.1,
	width_shift_range=.1,
	height_shift_range=.1,
	fill_mode='nearest' ).flow(
 		train_data, train_labels,
		batch_size=batch_size)
 val_gen = ImageDataGenerator(
 	rescale=1./255 ).flow(
 		val_data, val_labels,
		batch_size=batch_size)
 model = get_model()
history = model.fit(train_gen,
	steps_per_epoch=len(train_data)// batch_size,
	validation_data=val_gen,
	validation_steps=len(val_data)// batch_size,
	epochs=100)
 plot_losses(history)
std_scaler = StandardScaler()
std_scaler.fit(train_x)
 train_x = std_scaler.transform(train_x)
 val_x = std_scaler.transform(val_x)
 test_set = std_scaler.transform(test_set)
<define_variables>
train_data, train_labels = load_train_data()
batch_size = 64
train_gen = ImageDataGenerator(
 	rescale=1./255,
	rotation_range=10,
	zoom_range=.1,
	width_shift_range=.1,
	height_shift_range=.1,
	fill_mode='nearest' ).flow(
 		train_data, train_labels,
		batch_size=batch_size)
 model = get_model()
history = model.fit(train_gen,
	steps_per_epoch=len(train_data)// batch_size,
	epochs=100)
 model.save('mnist.h5')
 plot_losses(history )
<split><EOS> 
test_data = load_test_data()
model = models.load_model('mnist.h5')
 predict = np.argmax(model.predict(test_data), axis=1)
 confirm_submission(predict )