SHARE
TWEET

rede perceptron multicamadas

Azganoth Jun 29th, 2020 (edited) 822 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. class NeuralNetwork:
  4.     def __init__(self, n_neurons_input_layer: int, n_neurons_hidden_layer: int, n_neurons_output_layer: int):
  5.         self.input_hidden_weights = np.random.rand(n_neurons_input_layer, n_neurons_hidden_layer)
  6.         self.hidden_output_weights = np.random.rand(n_neurons_hidden_layer, n_neurons_output_layer)
  7.  
  8.     @staticmethod
  9.     def sigmoid(x):
  10.         return 1 / (1 + np.exp(-x))
  11.  
  12.     @staticmethod
  13.     def sigmoid_derivative(x):
  14.         return x * (1 - x)
  15.  
  16.     def think(self, inputs):
  17.         output_from_hidden_layer = self.sigmoid(np.dot(inputs, self.input_hidden_weights))
  18.         output_from_output_layer = self.sigmoid(np.dot(output_from_hidden_layer, self.hidden_output_weights))
  19.         return output_from_hidden_layer, output_from_output_layer
  20.  
  21.     def train(self, training_inputs, training_outputs, epochs):
  22.         for epoch in range(epochs):
  23.             output_from_hidden_layer, output_from_output_layer = self.think(training_inputs)
  24.  
  25.             output_layer_error = training_outputs - output_from_output_layer
  26.             output_layer_delta = output_layer_error * self.sigmoid_derivative(output_from_output_layer)
  27.  
  28.             hidden_layer_error = output_layer_delta.dot(self.hidden_output_weights.T)
  29.             hidden_layer_delta = hidden_layer_error * self.sigmoid_derivative(output_from_hidden_layer)
  30.  
  31.             self.input_hidden_weights += training_inputs.T.dot(hidden_layer_delta)
  32.             self.hidden_output_weights += output_from_hidden_layer.T.dot(output_layer_delta)
  33.  
  34. training_inputs = np.array([
  35.     [0.135, 0.0341, 0.050, 0.885],
  36.     [0.145, 0.0330, 0.020, 0.592],
  37.     [0.115, 0.0363, 0.500, 0.532],
  38.     [0.100, 0.0310, 0.507, 0.645],
  39.     [0.100, 0.0310, 0.422, 0.712],
  40.     [0.085, 0.0321, 0.420, 0.890],
  41.     [0.085, 0.0331, 0.420, 0.585],
  42.     [0.140, 0.0368, 0.033, 0.550],
  43.     [0.140, 0.0332, 0.036, 0.610],
  44.     [0.140, 0.0351, 0.032, 0.598]
  45. ])
  46.  
  47. training_outputs = np.array([
  48.     [0],
  49.     [0],
  50.     [1],
  51.     [1],
  52.     [1],
  53.     [1],
  54.     [1],
  55.     [0],
  56.     [0],
  57.     [0]
  58. ])
  59.  
  60. neural_network = NeuralNetwork(4, 4, 1)
  61.  
  62. print(f'Pesos entre a camada input e hidden antes do treino:\n{neural_network.input_hidden_weights}\n')
  63. print(f'Pesos entre a camada hidden e output antes do treino:\n{neural_network.hidden_output_weights}\n')
  64.  
  65. neural_network.train(training_inputs, training_outputs, 60000)
  66.  
  67. print(f'Pesos entre a camada input e hidden depois do treino:\n{neural_network.input_hidden_weights}\n')
  68. print(f'Pesos entre a camada hidden e output depois do treino:\n{neural_network.hidden_output_weights}\n')
  69.  
  70. test_input_index = 0
  71. print(f'Resultado: {neural_network.think(training_inputs[test_input_index])}.')
  72. print(f'Esperado: {training_outputs[test_input_index]}.')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top