import numpy as np
# функция активации — сигмоида
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# класс одного нейрона
class Neuron:
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias
def feedforward(self, inputs):
total = np.dot(self.weights, inputs) + self.bias
return sigmoid(total)
# 1 нейросеть:
# 3 входа: x1, x2, x3
# 3 скрытых нейрона: h1, h2, h3
# 1 выход: o1
class NeuralNetwork1:
def __init__(self):
weights = np.array([0.5, 0.5, 0.5])
bias = 0
self.h1 = Neuron(weights, bias)
self.h2 = Neuron(weights, bias)
self.h3 = Neuron(weights, bias)
self.o1 = Neuron(weights, bias)
def feedforward(self, x):
out_h1 = self.h1.feedforward(x)
out_h2 = self.h2.feedforward(x)
out_h3 = self.h3.feedforward(x)
out_hidden = np.array([out_h1, out_h2, out_h3])
out_o1 = self.o1.feedforward(out_hidden)
return out_o1
# 2 нейросеть:
# 2 входа: x1, x2
# 2 скрытых нейрона: h1, h2
# 2 выхода: o1, o2
class NeuralNetwork2:
def __init__(self):
weights = np.array([1, 0])
bias = 1
self.h1 = Neuron(weights, bias)
self.h2 = Neuron(weights, bias)
self.o1 = Neuron(weights, bias)
self.o2 = Neuron(weights, bias)
def feedforward(self, x):
out_h1 = self.h1.feedforward(x)
out_h2 = self.h2.feedforward(x)
out_hidden = np.array([out_h1, out_h2])
out_o1 = self.o1.feedforward(out_hidden)
out_o2 = self.o2.feedforward(out_hidden)
return out_o1, out_o2
# проверка первой нейросети
network1 = NeuralNetwork1()
x1 = np.array([2, 3, 4])
print("Результат первой нейросети:")
print(network1.feedforward(x1))
# проверка второй нейросети
network2 = NeuralNetwork2()
x2 = np.array([2, 3])
print("Результат второй нейросети:")
print(network2.feedforward(x2))