-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerPerceptron.py
77 lines (69 loc) · 2.68 KB
/
LayerPerceptron.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
from Perceptron import Perceptron
import numpy as np
class LayerPerceptron(object):
"""
Simple layer perceptron architecture of a multilayer-perceptron.
"""
def __init__(self, weights):
"""
Initializes a layer of a multilayer-perceptron model.
"""
if not type(weights) == np.ndarray or weights.size == 0 or weights.ndim != 2:
raise Exception("weights must be a non-empty numpy ndarray of dimension 2.")
self.__neurons = weights.shape[0]
self.__dendrites = weights.shape[1]
self.__perceptrons = np.zeros(self.__neurons, dtype = object)
for pos in range(self.__neurons):
self.__perceptrons[pos] = Perceptron(weights[pos])
return
def __str__(self):
"""
Describes the structure of the layer.
"""
msg = "Layer Perceptron has {} neurons where:\n".format(self.__neurons)
for neuron in self.__perceptrons:
msg += "\tPerceptron has {} dendrites with weights: {}\n".format(neuron.get_dendrites(), neuron.get_weights())
return msg
def set_weights(self, weights):
"""
Sets the weights of the neurons of the layer.
"""
for neuron in range(self.__neurons):
self.__perceptrons[neuron].set_weights(weights[neuron])
return
def get_weights(self):
"""
Returns the weights of the neurons of the layer.
"""
weights = np.zeros((self.__neurons, self.__dendrites))
for neuron in range(self.__neurons):
weights[neuron] = self.__perceptrons[neuron].get_weights()
return weights
def get_neurons(self):
"""
Returns the number of neurons of the layer.
"""
return self.__neurons
def get_dendrites(self):
"""
Returns the number of dendrites of the perceptron of the layer.
"""
return self.__dendrites
def get_perceptrons(self):
"""
Returns the perceptrons of the layer.
"""
return self.__perceptrons
def feed_forward(self, inputs):
"""
Performs a feed forward operation with the stored weights.
"""
if not type(inputs) == np.ndarray or inputs.ndim != 1 or inputs.size != self.__dendrites:
raise Exception("inputs argument must be a numpy ndarray of dimension 1 with size {}.".format(self.__dendrites))
output = np.zeros(self.__neurons + 1)
output[0] = 1.0
for pos in range(self.__neurons):
transfer = self.__perceptrons[pos].transfer(inputs)
activation = self.__perceptrons[pos].activation(transfer)
output[pos + 1] = activation
return output