【发布时间】:2021-06-29 00:48:28
【问题描述】:
我正在尝试创建一个由连接在一起的神经元对象组成的神经网络类。
我的神经元课有
-
树突
树突的数量在类初始化时在参数中指定。树突存储在一个列表中,其索引存储每个树突的电压。 例如:neuron1.dendrites[2]=0.12伏特。 - 激活(阈值)潜力 所有树突电位的总和提供了神经元电位。如果这个电位超过阈值电位,我的神经元就会发射。还有其他神经元连接到我神经元的轴突。我的神经元的轴突连接到其他神经元对象的树突。来自其他神经元的几个树突可能会连接到我的神经元的轴突。当我的神经元触发时,它们都会收到一个固定的电压(输出电压)。它以全有或全无的方式触发。
-
发射状态
当达到激活电位时,触发状态 = on (True) - 我的 Neuron 类也有一个 setConnections() 方法。此方法接收树突的 python
list。在这种方法中,我希望遍历外部 Dendrite 的内部列表并重置它们的电压值。 这不起作用。我不知道为什么,所以在这里寻求帮助。
我在下面提供了我的代码的精简版本:
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
以下是来自 main.py 脚本的相关代码,用于测试 Neuron 类:
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新手!
【问题讨论】:
-
axonConns不是“变量列表”。它是从一组变量派生的值列表。此外,您认为应该更新这些变量 (outputDendrites = self.outputPotential) 的行(我认为)只是分配给一个局部变量,该变量之前恰好具有您列表中的值。 -
@ScottHunter,我明白你在说什么。但我才刚刚开始学习python(字面意思是几天前)。有解决办法吗?在某些语言(例如 VB6.0)中,可以通过引用传递参数。这在 python 中可行吗?
-
完成这项工作的一种快速方法是:
axonConns = [(n1.dendrites, 0), (n2.dendrites, 1), (n3.dendrites, 3)]并使用fire()处理它时牢记索引。您需要传递列表本身,以按照您打算的方式反映更改。这是假设您希望更新 n1、n2 和 n3。但如果不是,那么刚刚从@aebange 传来的答案就足够了。 -
不,python 不支持引用调用。
标签: python python-3.x list variables