本文整理汇总了Python中neuron.Neuron.addConnection方法的典型用法代码示例。如果您正苦于以下问题:Python Neuron.addConnection方法的具体用法?Python Neuron.addConnection怎么用?Python Neuron.addConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类neuron.Neuron
的用法示例。
在下文中一共展示了Neuron.addConnection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from neuron import Neuron [as 别名]
# 或者: from neuron.Neuron import addConnection [as 别名]
class Network:
def __init__(self,inputs, hiddentotal):
self.learning_constant = 0.5
self.InputNeurons = []
for i in xrange(inputs):
self.InputNeurons.append(Neuron())
self.InputNeurons.append(Neuron(1)) #adding bias neuron
self.HiddenNeurons = []
for i in xrange(hiddentotal):
self.HiddenNeurons.append(Neuron())
self.HiddenNeurons.append(Neuron(1)) #adding bias neuron
self.OutputNeuron = Neuron()
#connecting everything
for ind_in,val_in in enumerate(self.InputNeurons):
for ind_hid,val_hid in enumerate(self.HiddenNeurons):
c = Connection(val_in,val_hid)
self.InputNeurons[ind_in].addConnection(c)
self.HiddenNeurons[ind_hid].addConnection(c)
for ind,val in enumerate(self.HiddenNeurons):
c = Connection(val,self.OutputNeuron)
self.HiddenNeurons[ind].addConnection(c)
self.OutputNeuron.addConnection(c)
def feedForward(self,inputVals):
for ind,val in enumerate(inputVals):
self.InputNeurons[ind].set_input(val)
#add something that sets the output for the hidden neurons
#this will fix the bug
for ind,val in enumerate(self.InputNeurons):
self.HiddenNeurons[ind].set_input(self.InputNeurons[ind].calcOutput())
for ind,val in enumerate(self.HiddenNeurons):
self.HiddenNeurons[ind].calcOutput()
self.OutputNeuron.calcOutput()
return self.OutputNeuron.getOutput()
def train(self, inputs,answer):
result = self.feedForward(inputs)
deltaOutput = result*(1-result) * (answer-result)
#Backpropagation
connections = self.OutputNeurons.getConnections()
for ind,val in enumerate(connections):
neuron = val.getFrom()
output = neuron.getOutput()
deltaWeight = output*deltaOutput
connections[ind].adjustWeight(self.learning_constant*deltaWeight)
#Adjust hidden weights
for ind,val in enumerate(self.HiddenNeurons):
connections = val.getConnections()
summa = 0
for ind_con,val_con in enumerate(connections):
if val_con.getFrom() is self.HiddenNeurons[ind]:
summa += val_con.getWeight()*deltaOutput
for ind_con,val_con in enumerate(connections):
if val_con.getTo() is self.HiddenNeurons[ind]:
output = self.HiddenNeurons[ind].getOutput()
deltaHidden = output * (1 - output)
deltaHidden *= summa
neuron = val_con.getFrom()
deltaWeight = neuron.getOutput()*deltaHidden
connections[ind_con].adjustWeight(self.learning_constant*deltaWeight)
return result