Я новичок в питоне, но решил написать простую нейронную сеть. При попытке запуска выдает кучу ошибок:
Traceback (most recent call last):
File "D:/Python/Neural1/XOR.py", line 8, in <module>
print(network.forwardResult())
File "D:\Python\Neural1\classes.py", line 45, in forwardResult
x.calculateNeurons()
File "D:\Python\Neural1\classes.py", line 36, in calculateNeurons
x.selfValueDefine()
File "D:\Python\Neural1\classes.py", line 21, in selfValueDefine
self.selfValue = self.sigmoid()
File "D:\Python\Neural1\classes.py", line 17, in sigmoid
vectorsResult = self.counterVectors()
File "D:\Python\Neural1\classes.py", line 13, in counterVectors
counter+= self.previousLayer.getNeuronValueByNumber(x) * self.weigthsFromPreviousLayer[x]
TypeError: 'set' object is not subscriptable
Сама программа выглядит так:
import math
class Neuron:
def __init__(self, previousLayer, weigthsFromPreviousLayer, selfValue):
self.previousLayer = previousLayer
self.weigthsFromPreviousLayer = weigthsFromPreviousLayer
self.selfValue = selfValue
def counterVectors(self):
counter = 0
for x in range(self.previousLayer.getLengthOfLayer()):
counter+= self.previousLayer.getNeuronValueByNumber(x) * self.weigthsFromPreviousLayer[x]
return counter
def sigmoid(self):
vectorsResult = self.counterVectors()
return 1/(1 + math.exp(-vectorsResult))
def selfValueDefine(self):
self.selfValue = self.sigmoid()
class Layer:
def __init__(self, *neuronsInLayer):
self.neuronsInLayer = neuronsInLayer
def getNeuronValueByNumber(self, neuronNumber):
return self.neuronsInLayer[neuronNumber].selfValue
def getLengthOfLayer(self):
return len(self.neuronsInLayer)
def calculateNeurons(self):
for x in self.neuronsInLayer:
x.selfValueDefine()
class Network:
def __init__(self, layers):
self.layers = layers
def forwardResult(self):
for x in self.layers:
x.calculateNeurons()
return self.layers[2].neuronsInLayer[0].selfValue
Вот часть кода, которая запускает процесс:
from classes import *
inputLayer = Layer(Neuron(None, None, 1), Neuron(None, None, 0))
hiddenLayer = Layer(Neuron(inputLayer, {0.45, -0.12}, None), Neuron(inputLayer, {0.78, 0.13}, None))
outputLayer = Layer(Neuron(hiddenLayer, {1.5, -2.3}, None))
network = Network({inputLayer, hiddenLayer, outputLayer})
print(network.forwardResult())