Advertisement
mattonit

Untitled

Mar 26th, 2023
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # Zdefiniujmy funkcję aktywacji sigmoid
  4. def sigmoid(x):
  5.     return 1 / (1 + np.exp(-x))
  6.  
  7. # Zdefiniujmy naszą sieć neuronową
  8. class NeuralNetwork:
  9.     def __init__(self):
  10.         # Ustawiamy losowe wagi początkowe
  11.         self.weights = np.random.rand(2, 1)
  12.        
  13.     def feedforward(self, inputs):
  14.         # Mnożymy wagi przez wejścia
  15.         # i zastosowujemy funkcję aktywacji sigmoid
  16.         return sigmoid(np.dot(inputs, self.weights))
  17.        
  18.     def train(self, inputs, labels, epochs):
  19.         for epoch in range(epochs):
  20.             # Feedforward dla każdego wejścia
  21.             prediction = self.feedforward(inputs)
  22.            
  23.             # Obliczenie błędu
  24.             error = labels - prediction
  25.            
  26.             # Korekta wag
  27.             adjustment = np.dot(inputs.T, error * prediction * (1 - prediction))
  28.             self.weights += adjustment
  29.  
  30. # Testowanie naszej sieci neuronowej
  31. nn = NeuralNetwork()
  32.  
  33. # Przykładowe dane
  34. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  35. labels = np.array([[0], [1], [1], [0]])
  36.  
  37. # Trenujemy naszą sieć
  38. nn.train(inputs, labels, 10000)
  39.  
  40. # Testujemy naszą sieć na nowych danych
  41. new_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  42. predictions = nn.feedforward(new_inputs)
  43.  
  44. # Wyświetlamy wyniki
  45. print(predictions)
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement