# ----------------------------------------------------------------------------
## How to use RandomNormal initializer in a Deep Learning Model in Keras
# ----------------------------------------------------------------------------
def Learn_By_Example_305():
print()
print(format('How to use RandomNormal initializer to a Deep Learning Model in Keras','*^82'))
import warnings
warnings.filterwarnings("ignore")
# load libraries
import keras as K
from keras.initializers import RandomNormal
from keras.regularizers import l1_l2
from keras.models import Sequential
from keras.layers import Dense, Dropout
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
# simulated data
dataset = datasets.make_classification(n_samples=10000, n_features=20, n_informative=5,
n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=2,
weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0,
scale=1.0, shuffle=True, random_state=None)
X = dataset[0]; y = dataset[1]
print(X.shape); print(y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# Define a Deep Learning Model
model = Sequential()
model.add(Dense(45, input_dim=20,
kernel_regularizer=l1_l2(0.001), # weight regularizer
kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=None), # initializer
activation='relu'))
model.add(Dropout(0.5)) # Dropout Layer
model.add(Dense(22,
kernel_regularizer=l1_l2(0.01), # weight regularizer
kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=None), # initializer
activation='relu'))
model.add(Dropout(0.5)) # Dropout Layer
model.add(Dense(1, activation='sigmoid'))
# Compile the Model
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['acc','mae'])
# Train the Model
model.fit(X_train, y_train, epochs=100, batch_size=25, verbose = 1)
# Evaluate the model
scores = model.evaluate(X_test, y_test)
print(); print(model.metrics_names); print(scores)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# Confusion Matrix
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5)
cm = confusion_matrix(y_test, y_pred); print("\nConfusion Matrix:\n", cm)
# More on the Model
print("\n\nBackend: ", K.backend.backend())
print(model.summary())
Learn_By_Example_305()