Skip to content

Commit 9330a64

Browse files
authored
Add files via upload
1 parent ac266fb commit 9330a64

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

example_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
[8, 15, 20, 13]])
1414

1515
# Preparing the NumPy array of the outputs.
16-
data_outputs = numpy.array([0.1,
17-
1.5])
16+
data_outputs = numpy.array([[0.1, 0.2],
17+
[1.8, 1.5]])
1818

1919
# The number of inputs (i.e. feature vector length) per sample
2020
num_inputs = data_inputs.shape[1]

nn.py

+43-15
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def layers_weights(last_layer, initial=True):
3939
# Currently, the weights of the layers are in the reverse order. In other words, the weights of the first layer are at the last index of the 'network_weights' list while the weights of the last layer are at the first index.
4040
# Reversing the 'network_weights' list to order the layers' weights according to their location in the network architecture (i.e. the weights of the first layer appears at index 0 of the list).
4141
network_weights.reverse()
42-
return numpy.array(network_weights)
42+
return network_weights
4343

4444
def layers_weights_as_vector(last_layer, initial=True):
4545
"""
@@ -113,7 +113,7 @@ def layers_weights_as_matrix(last_layer, vector_weights):
113113
# Currently, the weights of the layers are in the reverse order. In other words, the weights of the first layer are at the last index of the 'network_weights' list while the weights of the last layer are at the first index.
114114
# Reversing the 'network_weights' list to order the layers' weights according to their location in the network architecture (i.e. the weights of the first layer appears at index 0 of the list).
115115
network_weights.reverse()
116-
return numpy.array(network_weights)
116+
return network_weights
117117

118118
def layers_activations(last_layer):
119119
"""
@@ -192,17 +192,23 @@ def softmax(layer_outputs):
192192
def train(num_epochs,
193193
last_layer,
194194
data_inputs,
195-
data_outputs,
196-
learning_rate):
195+
data_outputs,
196+
problem_type="classification",
197+
learning_rate=0.01):
197198
"""
198199
Trains the neural network.
199200
200201
num_epochs: Number of epochs.
201202
last_layer: Reference to the last (output) layer in the network architecture.
202203
data_inputs: Data features.
203204
data_outputs: Data outputs.
204-
learning_rate: Learning rate.
205+
problem_type: Can be either classification or regression to define the problem type.
206+
learning_rate: Learning rate which defaults to 0.01.
205207
"""
208+
209+
if not (problem_type in ["classification", "regression"]):
210+
raise ValueError("The value of the problem_type parameter can be either classification or regression but {problem_type_val} found.".format(problem_type_val=problem_type))
211+
206212
# To fetch the initial weights of the layer, the 'initial' argument is set to True.
207213
weights = layers_weights(last_layer, initial=True)
208214
activations = layers_activations(last_layer)
@@ -221,10 +227,18 @@ def train(num_epochs,
221227
r1 = sigmoid(r1)
222228
elif activations[idx] == "softmax":
223229
r1 = softmax(r1)
230+
elif activations[idx] == None:
231+
pass
232+
224233
curr_weights = weights[-1]
225234
r1 = numpy.matmul(r1, curr_weights)
226-
predicted_label = numpy.where(r1 == numpy.max(r1))[0][0]
227-
network_error = network_error + abs(predicted_label - data_outputs[sample_idx])
235+
236+
if problem_type == "classification":
237+
prediction = numpy.where(r1 == numpy.max(r1))[0][0]
238+
else:
239+
prediction = r1
240+
241+
network_error = network_error + numpy.mean(numpy.abs((prediction - data_outputs[sample_idx])))
228242

229243
# Updating the network weights once after completing an epoch (i.e. passing through all the samples).
230244
weights = update_weights(weights=weights,
@@ -246,9 +260,11 @@ def update_weights(weights, network_error, learning_rate):
246260
247261
It returns the new weights.
248262
"""
249-
weights = numpy.array(weights)
250-
new_weights = weights - network_error * learning_rate * weights
251-
return new_weights
263+
# weights = numpy.array(weights)
264+
for layer_idx in range(len(weights)):
265+
weights[layer_idx] = network_error * learning_rate * weights[layer_idx]
266+
267+
return weights
252268

253269
def update_layers_trained_weights(last_layer, final_weights):
254270
"""
@@ -267,23 +283,27 @@ def update_layers_trained_weights(last_layer, final_weights):
267283
# Go to the previous layer.
268284
layer = layer.previous_layer
269285

270-
def predict(last_layer, data_inputs):
286+
def predict(last_layer, data_inputs, problem_type="classification"):
271287
"""
272288
Uses the trained weights for predicting the samples' outputs.
273289
274290
last_layer: A reference to the last (output) layer in the network architecture.
275291
data_inputs: Data features.
292+
problem_type: Can be either classification or regression to define the problem type.
276293
277294
Returns the predictions of all samples.
278295
"""
296+
if not (problem_type in ["classification", "regression"]):
297+
raise ValueError("The value of the problem_type parameter can be either classification or regression but {problem_type_val} found.".format(problem_type_val=problem_type))
298+
279299
# To fetch the trained weights of the layer, the 'initial' argument is set to False.
280300
weights = layers_weights(last_layer, initial=False)
281301
activations = layers_activations(last_layer)
282302

283303
if len(weights) != len(activations):
284304
raise TypeError("The length of layers {num_layers} is not equal to the number of activations functions {num_activations} and they must be equal.".format(num_layers=len(weights), num_activations=len(activations)))
285305

286-
predictions = numpy.zeros(shape=(data_inputs.shape[0]))
306+
predictions = []
287307
for sample_idx in range(data_inputs.shape[0]):
288308
r1 = data_inputs[sample_idx, :]
289309
for curr_weights, activation in zip(weights, activations):
@@ -294,8 +314,16 @@ def predict(last_layer, data_inputs):
294314
r1 = sigmoid(r1)
295315
elif activation == "softmax":
296316
r1 = softmax(r1)
297-
predicted_label = numpy.where(r1 == numpy.max(r1))[0][0]
298-
predictions[sample_idx] = predicted_label
317+
elif activation == None:
318+
pass
319+
320+
if problem_type == "classification":
321+
prediction = numpy.where(r1 == numpy.max(r1))[0][0]
322+
else:
323+
prediction = r1
324+
325+
predictions.append(prediction)
326+
299327
return predictions
300328

301329
def to_vector(array):
@@ -351,7 +379,7 @@ def __init__(self, num_neurons, previous_layer, activation_function="sigmoid"):
351379
# Number of neurons in the dense layer.
352380
self.num_neurons = num_neurons
353381

354-
supported_activation_functions = ("sigmoid", "relu", "softmax")
382+
supported_activation_functions = ("sigmoid", "relu", "softmax", "None")
355383
if not (activation_function in supported_activation_functions):
356384
raise ValueError("The specified activation function '{activation_function}' is not among the supported activation functions {supported_activation_functions}. Please use one of the supported functions.".format(activation_function=activation_function, supported_activation_functions=supported_activation_functions))
357385
self.activation_function = activation_function

0 commit comments

Comments
 (0)