Neural Network

from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.layers import BatchNormalization as BatchNorm
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
import random
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(path="mnist.npz")
print(x_train.shape)
# train, validation split (70%, 30%)
x_train, x_val = x_train[0:x_train.shape[0]*70//100], x_train[x_train.shape[0]*70//100:]
y_train, y_val = y_train[0:y_train.shape[0]*70//100], y_train[y_train.shape[0]*70//100:]
# Some other simple things you can do - Shuffle, Normalize.
# Other advanced things you can do to preprocess the data - PCA, Z-score.
# What other optimizations can you think of?
print(x_train.shape, x_val.shape)
# https://keras.io/api/layers/
# Keras offers an API for a lot of layers with multiple optional parameters to tune the network.
def create_network(network_input):
model = Sequential()
model.add(Flatten()) # Convert [28,28] -> [784,]
model.add(Dense(25)) # [784,] -> [25,]
model.add(Activation('relu'))
model.add(Dense(10)) # [25,] -> [10,] FCC
model.add(Activation('softmax'))

#optimizer and loss.
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))
return model
model = create_network(x_train)
#https://keras.io/api/models/model_training_apis/
#without validation
model.fit(x=x_train, y=y_train, epochs=5, batch_size = 8)
model = create_network(x_train)
#with validation
model.fit(x=x_train, y=y_train, epochs=5, batch_size = 8, validation_data=(x_val, y_val))