Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Lianer:
- def __init__(self, typeN, matrix=[[0, 0]], bias=[0], func=lambda x: x):
- self.typeN = typeN
- if (typeN == 'linear'):
- if (len(bias) != len(matrix)):
- print("Error")
- for i in range(len(matrix)):
- if (len(matrix[0]) != len(matrix[i])):
- print("Error")
- self.matrix = matrix
- self.bias = bias
- else:
- self.func = func
- def typeN(self):
- return self.typeN
- def matrix(self):
- return self.matrix
- def bias(self):
- return self.bias
- def func(self, h=7):
- return self.func(h)
- def printLia(self):
- print("Тип: %s" % self.typeN)
- if (self.typeN == 'linear'):
- print("Веса: %s" % self.matrix)
- print("Смещение: %s" % self.bias)
- else:
- print("Функция: %s" % self.func)
- def matrixmult(m1, m2, b):
- s = 0
- m3 = []
- if len(m2[0]) != len(m1):
- print("Матрицы не могут быть перемножены")
- else:
- c1 = len(m1)
- c2 = len(m2)
- for j in range(c2):
- for i in range(c1):
- s += m1[i] * m2[j][i]
- s -= b[j]
- m3.append(s)
- s = 0
- return m3
- class make_feedforward_nn:
- def __init__(self, input_count, output_count):
- self.input_count = input_count
- self.output_count = output_count
- self.lian = [Lianer]
- def add_layer(self, typeN, matrix=[[0, 0]], bias=[0], func=lambda x: x):
- linr = Lianer(typeN, matrix, bias, func)
- self.lian.append(linr)
- def check_dimensions(self):
- inp = self.input_count
- for i in range(len(self.lian)):
- if (self.lian[i].typeN== 'linear'):
- if(inp != len(self.lian[i].matrix[0])):
- print("Error")
- else:
- inp = len(self.lian[i].bias)
- def __call__(self, x1, x2):
- x = [x1, x2]
- for i in range(len(self.lian)):
- if (self.lian[i].typeN == 'linear'):
- x = matrixmult(x, self.lian[i].matrix, self.lian[i].bias)
- else:
- #fun = (self.lian[i].func)
- for j in range(len(x)):
- h = x[j]
- x[j] = self.lian[i].func(h)
- sum = 0
- for j in range(len(x)):
- sum += x[j]
- return sum
- nn = make_feedforward_nn(
- input_count=2, # число входных нейронов
- output_count=1 # число выходных нейронов
- )
- nn.add_layer(
- 'linear',
- matrix=[
- [0.3, -0.1],
- [0.4, 0],
- [0.5, 1],
- [-0.23, -0.3],
- ],
- bias=[0, 0, 0, 0.3]
- ) # Обрати внимание, что этот слой отображает исходный 2-хмерный вектор в 4-хмерный
- nn.add_layer(
- 'function',
- func=lambda x: 1.0 / (1 + math.exp(-x)) # передаем сигмоиду
- ) # Обрати внимание, что этот слой не меняет размерности вектора
- nn.add_layer(
- 'linear',
- matrix=[
- [0.3, -0.0, 0, 0.3],
- [0.4, 0, 0.2, 0.7],
- ],
- bias=[0, 0.1]
- ) # Обрати внимание, что этот слой отображает исходный 4-хмерный вектор в 2-хмерный
- nn.add_layer(
- 'function',
- func=lambda x: 1.0 / (1 + math.exp(-x)) # передаем сигмоиду
- ) # Обрати внимание, что этот слой не меняет размерности вектора
- nn.check_dimensions() # внутри проверка того, что нейросеть
- #соответствует объявлению (2 числа на входе, 1 на выходе)
- result = nn(0.4, 0.1)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement