Ejemplo n.º 1
0
class MLP:
    def __init__(self, input_size, hidden_size, output_size):

        self.input_layer = InputLayer(input_size, hidden_size, Sigmoid)
        self.output_layer = SigmoidOutputLayer(hidden_size, output_size)

    def predict(self, x):
        x = self.input_layer.forward(x)
        pred = self.output_layer.predict(x)
        pred[pred >= 0.5] = 1
        pred[pred < 0.5] = 0
        return pred

    def loss(self, x, y):
        x = self.input_layer.forward(x)
        loss = self.output_layer.forward(x, y)
        return loss

    def gradient(self):
        d_prev = 1
        d_prev = self.output_layer.backward(d_prev=d_prev)
        self.input_layer.backward(d_prev)

    def update(self, learning_rate):
        self.input_layer.W -= self.input_layer.dW * learning_rate
        self.input_layer.b -= self.input_layer.db * learning_rate
        self.output_layer.W -= self.output_layer.dW * learning_rate
        self.output_layer.b -= self.output_layer.db * learning_rate
Ejemplo n.º 2
0
Predict: 
 [[0.90437415]
 [0.00750045]]
Forward: 
 0.05402041769476528
Backward: 
 [[-0.05371304  0.10152596 -0.14933889]
 [ 0.004213   -0.00796322  0.01171345]]
dW: 
 [[-0.05306324]
 [ 0.0574135 ]
 [ 0.00375022]]
db: 
 [-0.0440627]
"""
sigmoid_output_layer = SigmoidOutputLayer(3, 1)
sigmoid_output_layer.W = np.array([[1.1234, -2.1234, 3.1234]]).T
sigmoid_output_layer.b = np.array([-1])

temp_y_hat = np.array([0.0, 0.5, 1.0])
temp_y = np.array([1, 1, 0])
print('Binary Cross-entropy Loss: \n',
      sigmoid_output_layer.binary_ce_loss(temp_y_hat, temp_y))

temp8 = np.array([[1, -1, 0], [-1.4, 2.56, 1]])
print('Predict: \n', sigmoid_output_layer.predict(temp8))

temp_y = np.array([[1], [0]])
print('Forward: \n', sigmoid_output_layer.forward(temp8, temp_y))
print('Backward: \n', sigmoid_output_layer.backward(d_prev=1))
print('dW: \n', sigmoid_output_layer.dW)
Ejemplo n.º 3
0
    def __init__(self, input_size, hidden_size, output_size):

        self.input_layer = InputLayer(input_size, hidden_size, Sigmoid)
        self.output_layer = SigmoidOutputLayer(hidden_size, output_size)