Skip to content

Commit 22599de

Browse files
lemarakisantmarakis
authored andcommitted
Update deep_learning4e.py (#1122)
1 parent 467a07d commit 22599de

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

deep_learning4e.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from keras.preprocessing import sequence
1010

1111
from utils4e import sigmoid, dotproduct, softmax1D, conv1D, GaussianKernel, element_wise_product, \
12-
vector_add, random_weights, scalar_vector_product, matrix_multiplication, map_vector, mse_loss
12+
vector_add, random_weights, scalar_vector_product, matrix_multiplication, map_vector, mse_loss
1313

1414

1515
# DEEP NEURAL NETWORKS. (Chapter 19)
@@ -20,7 +20,7 @@
2020

2121
class Node:
2222
"""
23-
A node in computational graph, It contains the pointer to all its parents.
23+
A node in a computational graph. Contains the pointer to all its parents.
2424
:param val: value of current node.
2525
:param parents: a container of all parents of current node.
2626
"""
@@ -35,7 +35,7 @@ def __repr__(self):
3535

3636
class NNUnit(Node):
3737
"""
38-
A single unit of a Layer in a Neural Network
38+
A single unit of a layer in a Neural Network
3939
:param weights: weights between parent nodes and current node
4040
:param value: value of current node
4141
"""
@@ -47,7 +47,7 @@ def __init__(self, weights=None, value=None):
4747

4848
class Layer:
4949
"""
50-
A layer in a neural network based on computational graph.
50+
A layer in a neural network based on a computational graph.
5151
:param size: number of units in the current layer
5252
"""
5353

@@ -207,16 +207,14 @@ def gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1,
207207
gradient descent algorithm to update the learnable parameters of a network.
208208
:return: the updated network.
209209
"""
210-
# init data
211-
examples = dataset.examples
210+
examples = dataset.examples # init data
212211

213212
for e in range(epochs):
214213
total_loss = 0
215214
random.shuffle(examples)
216215
weights = [[node.weights for node in layer.nodes] for layer in net]
217216

218217
for batch in get_batch(examples, batch_size):
219-
220218
inputs, targets = init_examples(batch, dataset.inputs, dataset.target, len(net[-1].nodes))
221219
# compute gradients of weights
222220
gs, batch_loss = BackPropagation(inputs, targets, weights, net, loss)
@@ -231,6 +229,7 @@ def gradient_descent(dataset, net, loss, epochs=1000, l_rate=0.01, batch_size=1,
231229

232230
if verbose and (e + 1) % verbose == 0:
233231
print("epoch:{}, total_loss:{}".format(e + 1, total_loss))
232+
234233
return net
235234

236235

@@ -261,21 +260,26 @@ def adam_optimizer(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1 /
261260
for batch in get_batch(examples, batch_size):
262261
t += 1
263262
inputs, targets = init_examples(batch, dataset.inputs, dataset.target, len(net[-1].nodes))
263+
264264
# compute gradients of weights
265265
gs, batch_loss = BackPropagation(inputs, targets, weights, net, loss)
266+
266267
# update s,r,s_hat and r_gat
267268
s = vector_add(scalar_vector_product(rho[0], s),
268269
scalar_vector_product((1 - rho[0]), gs))
269270
r = vector_add(scalar_vector_product(rho[1], r),
270271
scalar_vector_product((1 - rho[1]), element_wise_product(gs, gs)))
271272
s_hat = scalar_vector_product(1 / (1 - rho[0] ** t), s)
272273
r_hat = scalar_vector_product(1 / (1 - rho[1] ** t), r)
274+
273275
# rescale r_hat
274276
r_hat = map_vector(lambda x: 1 / (math.sqrt(x) + delta), r_hat)
277+
275278
# delta weights
276279
delta_theta = scalar_vector_product(-l_rate, element_wise_product(s_hat, r_hat))
277280
weights = vector_add(weights, delta_theta)
278281
total_loss += batch_loss
282+
279283
# update the weights of network each batch
280284
for i in range(len(net)):
281285
if weights[i]:
@@ -284,6 +288,7 @@ def adam_optimizer(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1 /
284288

285289
if verbose and (e + 1) % verbose == 0:
286290
print("epoch:{}, total_loss:{}".format(e + 1, total_loss))
291+
287292
return net
288293

289294

@@ -327,6 +332,7 @@ def BackPropagation(inputs, targets, theta, net, loss):
327332

328333
previous = [layer_out[i] - t_val[i] for i in range(o_units)]
329334
h_layers = n_layers - 1
335+
330336
# Backward pass
331337
for i in range(h_layers, 0, -1):
332338
layer = net[i]
@@ -426,6 +432,7 @@ def perceptron_learner(dataset, learning_rate=0.01, epochs=100, verbose=None):
426432

427433
# initialize the network, add dense layer
428434
raw_net = [InputLayer(input_size), DenseLayer(input_size, output_size)]
435+
429436
# update the network
430437
learned_net = gradient_descent(dataset, raw_net, mse_loss, epochs, l_rate=learning_rate, verbose=verbose)
431438

@@ -497,6 +504,7 @@ def auto_encoder_learner(inputs, encoding_size, epochs=200):
497504
model.add(Dense(encoding_size, input_dim=input_size, activation='relu', kernel_initializer='random_uniform',
498505
bias_initializer='ones'))
499506
model.add(Dense(input_size, activation='relu', kernel_initializer='random_uniform', bias_initializer='ones'))
507+
500508
# update model with sgd
501509
sgd = optimizers.SGD(lr=0.01)
502510
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])

0 commit comments

Comments
 (0)