-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass_nn_fc.py
187 lines (155 loc) · 5.63 KB
/
class_nn_fc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.regularizers import l2
from keras.optimizers import Adam
from keras.layers import Input, Dense, LeakyReLU
from keras.initializers import he_uniform, he_normal
class nn_fc:
###########################################################################################
# Constructor #
###########################################################################################
def __init__(
self,
layer_dims, # [n_x, n_h1, .., n_hL, n_y], at least one hidden layer
learning_rate,
num_epochs,
weight_init='he_normal',
output_activation='softmax',
loss_function='categorical_crossentropy',
minibatch_size=64,
l2=0.0,
seed=0
):
# seed
self.seed = seed
# NN parameters
self.layer_dims = layer_dims
self.learning_rate = learning_rate
self.num_epochs = num_epochs
self.output_activation = output_activation
self.loss_function = loss_function
self.minibatch_size = minibatch_size
self.l2 = l2
if weight_init == 'he_uniform':
self.weight_init = he_uniform(seed=self.seed)
elif weight_init == 'he_normal':
self.weight_init = he_normal(seed=self.seed)
# model
self.model = self.create_fc_model()
# self.model.summary()
# configure model for training
self.model.compile(
optimizer=Adam(lr=self.learning_rate),
loss=self.loss_function,
metrics=['accuracy']
)
###########################################################################################
# Auxiliary #
###########################################################################################
############
# FC Model #
############
def create_fc_model(self):
# Input and output dims
n_x = self.layer_dims[0]
n_y = self.layer_dims[-1]
# Input layer
X_input = Input(shape=(n_x,), name='input')
# First hidden layer
X = Dense(
units=self.layer_dims[1],
activation=None,
use_bias=True,
kernel_initializer=self.weight_init,
bias_initializer='zeros',
kernel_regularizer=l2(self.l2),
bias_regularizer=None,
activity_regularizer=None
)(X_input)
X = LeakyReLU(alpha=0.01)(X)
# Other hidden layers (if any)
for l in self.layer_dims[2:-1]:
X = Dense(
units=l,
activation=None,
use_bias=True,
kernel_initializer=self.weight_init,
bias_initializer='zeros',
kernel_regularizer=l2(self.l2),
bias_regularizer=None,
activity_regularizer=None
)(X)
X = LeakyReLU(alpha=0.01)(X)
# Output layer
y_out = Dense(
units=n_y,
activation=self.output_activation,
use_bias=True,
kernel_initializer=self.weight_init,
bias_initializer='zeros',
kernel_regularizer=l2(self.l2),
bias_regularizer=None,
activity_regularizer=None,
name='output'
)(X)
# Model
return Model(inputs=X_input, outputs=y_out)
########
# Plot #
########
def plot_learning_curves(self, history, flag_val):
# Plot accuracy values
plt.plot(history.history['accuracy'])
if flag_val:
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
# plt.show()
# Plot loss values
plt.plot(history.history['loss'])
if flag_val:
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
# plt.show()
###########################################################################################
# API #
###########################################################################################
##############
# Prediction #
##############
def predict(self, x):
# probability prediction
y_hat = self.model.predict(x=x, verbose=0)
# class prediction
y_hat_max = np.max(y_hat)
y_hat_argmax = np.argmax(y_hat)
return y_hat, y_hat_max, y_hat_argmax
############
# Training #
############
def train(self, x, y, validation_data=None):
history = self.model.fit(
x=x,
y=y,
validation_data=validation_data,
epochs=self.num_epochs,
batch_size=self.minibatch_size,
shuffle=False,
verbose=0 # 0: off, 1: full, 2: brief
)
flag_val = False if validation_data is None else True
acc = history.history['accuracy'][-1]
loss = history.history['loss'][-1]
if flag_val:
val_acc = history.history['val_accuracy'][-1]
val_loss = history.history['val_loss'][-1]
return loss, acc, val_loss, val_acc
else:
return loss, acc