Skip to content

Commit bb4da55

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents ba5cdd1 + 2d0c6e5 commit bb4da55

File tree

7 files changed

+976
-2
lines changed

7 files changed

+976
-2
lines changed

.idea/FromCodingToDeepLearning.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ml/deep_learning/neural_network_keras/decision_boundaries/decision_boundary_2dimensional.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@
77

88
import numpy as np
99
import matplotlib.pyplot as plt
10+
import seaborn as sns
1011
from matplotlib.colors import ListedColormap
1112

1213

14+
def plot(history):
15+
"""
16+
A utility function that plots the training loss and validation loss from a Keras history object.
17+
:param history: A Keras history object.
18+
:return: None
19+
"""
20+
sns.set()
21+
plt.plot(history.history['loss'], label='Training set', color='blue', linestyle='-')
22+
plt.plot(history.history["val_loss"], label='Validation set', color='green', linestyle='--')
23+
plt.xlabel("Epochs", fontsize=30)
24+
plt.ylabel("Loss", fontsize=30)
25+
plt.xlim(0, len(history.history['loss']))
26+
plt.xticks(fontsize=20)
27+
plt.yticks(fontsize=20)
28+
plt.legend(fontsize=30)
29+
plt.show()
30+
31+
1332
def plot_boundary(model, points):
1433
# Generate a grid of points over the data
1534
RANGE = 0.55
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Learner: Nguyen Truong Thinh
2+
# Contact me: [email protected] || +84393280504
3+
#
4+
# Topic: Deep Learning: The Echidna dataset.
5+
# Building the echidna - four-layered neural network with L1 regularization
6+
7+
import os
8+
import numpy as np
9+
10+
from keras.models import Sequential
11+
from keras.layers import Dense
12+
from keras.optimizers import RMSprop
13+
from keras.regularizers import l1
14+
from keras.utils import to_categorical
15+
16+
from ml.deep_learning.neural_network_keras.decision_boundaries import decision_boundary_2dimensional as boundary
17+
from ml.util import load_seeded_shuffled_text_dataset
18+
19+
# Load the Echidna dataset
20+
21+
current_directory = os.path.dirname(__file__)
22+
text_dataset = os.path.join(current_directory, "echidna.txt")
23+
24+
X, Y = load_seeded_shuffled_text_dataset(text_dataset)
25+
26+
X_train, X_validation, X_test = np.split(X, 3)
27+
Y_train, Y_validation, Y_test = np.split(Y, 3)
28+
29+
Y_train_encoded = to_categorical(Y_train)
30+
Y_validation_encoded = to_categorical(Y_validation)
31+
32+
# Create a sequential model
33+
model = Sequential()
34+
model.add(Dense(100, activation='sigmoid', activity_regularizer=l1(0.002)))
35+
model.add(Dense(30, activation='sigmoid', activity_regularizer=l1(0.0004)))
36+
model.add(Dense(2, activation='softmax'))
37+
38+
# Compile a model
39+
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['accuracy'])
40+
41+
# Train a network
42+
history = model.fit(X_train, Y_train_encoded, validation_data=(X_validation, Y_validation_encoded), epochs=30000,
43+
batch_size=25)
44+
# Epoch 30000/30000 12/12 [==============================] - 0s 2ms/step - loss: 0.0036 - accuracy: 1.0000 -
45+
# val_loss: 0.7884 - val_accuracy: 0.8877 37813/37813 [==============================] - 17s 441us/step
46+
47+
# Draw a decision boundary
48+
boundary.show(model, X_train, Y_train, title="Training set")
49+
boundary.show(model, X_validation, Y_validation_encoded, title="Validation set")
50+
boundary.plot(history)

0 commit comments

Comments
 (0)