我试图创建一个神经网络类,由连接在一起的神经元对象组成
我的神经元课
neuron1.dendrites[2]=0.12
伏李>list
。在这个方法中,我希望遍历外部树突的内部列表并重置它们的电压值这不起作用。我不明白为什么,所以在这里寻求帮助。我在下面提供了我的代码的精简版本:
import threading
class Neuron:
def __init__(self, dendrites, activation_Pot=0.24):
"""
Create a dendrites[] array.
Each element of this array represents the voltage of that dendrite.
We can then loop through the array to sum up the signal strength.
If the signal strength exceeds the Activation potential, then the all-or-nothing threshold has been breached and
we can transmit our signal along the axon.
"""
self.dendrites = [0]*dendrites
self.InputPotential = 0 # This variable will store the sum of all the dendrite voltages. It is being initialised here.
self.activation_Pot = activation_Pot
self.on = True
self.off = False
self.voltsOut = 0.12 # This constant dictates the potential of the axon when the neuron is firing.
self.outputPotential = 0 # This variable SETS the potential of the single axon when the threshold activation potential of the neuron has been reached and the neuron is firing.
self.firing = self.off
self.axonConnections = []
# Launch a thread to check on a timer the sum of all dendrite inputs and fire when output > Activation Potential.
t1 = threading.Thread(target = self.start, args=[])
t1.start()
def fire(self):
self.outputPotential = self.voltsOut
self.firing = self.on
print("Neuron is firing!")
for outputDendrites in self.axonConnections:
outputDendrites = self.outputPotential
def stopFiring(self):
self.outputPotential = 0
self.firing = self.off
print("Neuron has STOPPED firing!")
def setActivation_Pot(self, activation_Pot):
if (activation_Pot >= 0) and (activation_Pot <=1):
self.activation_Pot = activation_Pot
else:
print("activation_Pot value needs to be between 0 and 1")
print("activation_Pot has not been reset.")
print("Please consider re-inputting a valid value.")
def getActivation_Pot(self):
return self.activation_Pot
def setAxonConnections(self, axonConnections):
self.axonConnections = axonConnections
def getAxonConnections(self):
return self.axonConnections
def start(self):
while True:
while True:
self.InputPotential = 0
for dendrite in self.dendrites:
self.InputPotential+=dendrite
if self.InputPotential >= self.activation_Pot:
self.fire()
break
while True:
self.InputPotential = 0
for dendrite in self.dendrites:
self.InputPotential+=dendrite
if self.InputPotential < self.activation_Pot:
self.stopFiring()
break
以下是测试Neuron类的main.py脚本中的相关代码:
from neuron import Neuron
# Instantiate transmitting neurone...
n0 = Neuron(3, 0.36)
# Instantiate receiving neurones...
n1 = Neuron(3, 0.36)
n2 = Neuron(3, 0.36)
n3 = Neuron(3, 0.36)
# Make the connections: I do this by creating storing my external Dendrites into a list
# and passing that list to the transmitting neuron for it to update the voltages
# of each of these neurons. BUT THESE LIST VARIABLES ARE NOT GETTING UPDATED...
axonConns = [n1.dendrites[0], n2.dendrites[1], n3.dendrites[2]]
n0.setAxonConnections(axonConns) # THE LIST VARIABLES OF THE axonConns LIST ARE NOT GETTING UPDATED!!
n0.fire() # THE LIST VARIABLES OF THE axonConns LIST ARE NOT GETTING UPDATED by this fire() method!!
我希望这是有道理的。总之:我在n0.setAxonConnections(axonConns)
行传递main.py脚本中的变量列表。此列表中的变量没有通过我的Neuron类的neuron.fire()
方法得到更新。有人能解释一下原因吗?原谅我,我是一个Python新手
通过使用python中的以下代码,我认为我找到了一个解决方案:
上述报告的产出为:
起初,这似乎是可行的。因此,为了修正我的代码,我修改了两行:
因此,我的新
fire()
方法如下所示:上述实验的结果清楚地表明,python确实在默认情况下通过引用传递列表的变量,,当传递到参数中时
但是(由于@bdbd),我发现列表中的元素保持不变。那么比如说,
屈服
这也意味着在我的代码中,
axonConn
数组(存储外部树枝晶电压列表)正在更新,而树枝晶本身没有更新经过一些研究和实验,我终于解决了这个难题。首先是一些necessary background reading:
我最后得出结论:
python总是通过引用传递强>
但是,某些数据类型是不可变的(字符串、整数等)
当引用传递给函数时;对这些变量的任何操作都需要对变量进行复制,以存储操作的结果
变量的任何重复都会导致创建一个指向新值的新引用。这一点解释得很好
因此,当这种情况发生时,调用函数不再看到被操纵的结果。这是因为无法重新分配调用函数参数的引用以指向新引用。一旦必须复制变量的内容,调用函数将不再看到它,因为新内容具有新引用
这意味着像
list
这样的可变项可以被追加并保留相同的引用-因此看起来好像它们是通过引用传递的但是
strings
和integers
等不可变项在操作后将丢失其引用,因为必须使用该新值创建新对象(string
或integer
)。新对象将始终具有新引用因此,在这些场景中,函数调用将表现为通过值传递我在下面提供了一些代码,很好地说明了这一点:
以下是进一步的例子,证明了相同的发现:
您没有在类方法中正确地重新分配outputDendrites的值
可能不是最好的解决方案,但只是我的2c。如果您希望另一个神经元的树突也被更新,您可以这样声明连接:
您需要传递树突列表本身,并通过索引定义连接中要考虑的树突。然后更改
fire
方法以考虑索引:编辑:
来证明为什么OP的答案不足以更新
fire()
之外的神经元相关问题 更多 >
编程相关推荐