MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / DenseLayer

Class DenseLayer

neural_network/bpnn.py:29–93  ·  view source on GitHub ↗

Layers of BP neural network

Source from the content-addressed store, hash-verified

27 return 1 / (1 + np.exp(-1 * x))
28
29class DenseLayer():
30 '''
31 Layers of BP neural network
32 '''
33 def __init__(self,units,activation=None,learning_rate=None,is_input_layer=False):
34 '''
35 common connected layer of bp network
36 :param units: numbers of neural units
37 :param activation: activation function
38 :param learning_rate: learning rate for paras
39 :param is_input_layer: whether it is input layer or not
40 '''
41 self.units = units
42 self.weight = None
43 self.bias = None
44 self.activation = activation
45 if learning_rate is None:
46 learning_rate = 0.3
47 self.learn_rate = learning_rate
48 self.is_input_layer = is_input_layer
49
50 def initializer(self,back_units):
51 self.weight = np.asmatrix(np.random.normal(0,0.5,(self.units,back_units)))
52 self.bias = np.asmatrix(np.random.normal(0,0.5,self.units)).T
53 if self.activation is None:
54 self.activation = sigmoid
55
56 def cal_gradient(self):
57 if self.activation == sigmoid:
58 gradient_mat = np.dot(self.output ,(1- self.output).T)
59 gradient_activation = np.diag(np.diag(gradient_mat))
60 else:
61 gradient_activation = 1
62 return gradient_activation
63
64 def forward_propagation(self,xdata):
65 self.xdata = xdata
66 if self.is_input_layer:
67 # input layer
68 self.wx_plus_b = xdata
69 self.output = xdata
70 return xdata
71 else:
72 self.wx_plus_b = np.dot(self.weight,self.xdata) - self.bias
73 self.output = self.activation(self.wx_plus_b)
74 return self.output
75
76 def back_propagation(self,gradient):
77
78 gradient_activation = self.cal_gradient() # i * i 维
79 gradient = np.asmatrix(np.dot(gradient.T,gradient_activation))
80
81 self._gradient_weight = np.asmatrix(self.xdata)
82 self._gradient_bias = -1
83 self._gradient_x = self.weight
84
85 self.gradient_weight = np.dot(gradient.T,self._gradient_weight.T)
86 self.gradient_bias = gradient * self._gradient_bias

Callers 1

exampleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected