创建大量对象(神经元)并使用词典随机连接

时间:2016-04-18 19:52:44

标签: python multithreading neural-network julia perceptron

我正在尝试用这些标准创建一种新的神经网络:

  • 每个神经元必须是一个单独的对象。
  • 每个神经元都应该有自己的线程。
  • 网络必须部分随机连接(启动时)。
  • 神经元必须异步运行以计算其输出,更新其权重等。

这些是我在Julia和Python中的实现尝试:

的Python

import random
import itertools
import time
import signal
from threading import Thread
from multiprocessing import Pool
import multiprocessing

POTENTIAL_RANGE = 110000 # Resting potential: -70 mV Membrane potential range: +40 mV to -70 mV --- Difference: 110 mV = 110000 microVolt --- https://en.wikipedia.org/wiki/Membrane_potential
ACTION_POTENTIAL = 15000 # Resting potential: -70 mV Action potential: -55 mV --- Difference: 15mV = 15000 microVolt --- https://faculty.washington.edu/chudler/ap.html
AVERAGE_SYNAPSES_PER_NEURON = 8200 # The average number of synapses per neuron: 8,200 --- http://www.ncbi.nlm.nih.gov/pubmed/2778101

# https://en.wikipedia.org/wiki/Neuron

class Neuron():

    neurons = []

    def __init__(self):
        self.connections = {}
        self.potential = 0.0
        self.error = 0.0
        #self.create_connections()
        #self.create_axon_terminals()
        Neuron.neurons.append(self)
        self.thread = Thread(target = self.activate)
        #self.thread.start()
        #self.process = multiprocessing.Process(target=self.activate)

    def fully_connect(self):
        for neuron in Neuron.neurons[len(self.connections):]:
            if id(neuron) != id(self):
                self.connections[id(neuron)] = round(random.uniform(0.1, 1.0), 2)

    def partially_connect(self):
        if len(self.connections) == 0:
            neuron_count = len(Neuron.neurons)
            for neuron in Neuron.neurons[len(self.connections):]:
                if id(neuron) != id(self):
                    if random.randint(1,neuron_count/100) == 1:
                        self.connections[id(neuron)] = round(random.uniform(0.1, 1.0), 2)
            print "Neuron ID: " + str(id(self))
            print "    Potential: " + str(self.potential)
            print "    Error: " + str(self.error)
            print "    Connections: " + str(len(self.connections))

    def activate(self):
        while True:
            '''
            for dendritic_spine in self.connections:
                if dendritic_spine.axon_terminal is not None:
                    dendritic_spine.potential = dendritic_spine.axon_terminal.potential
                    print dendritic_spine.potential
                self.neuron_potential += dendritic_spine.potential * dendritic_spine.excitement
            terminal_potential = self.neuron_potential / len(self.axon_terminals)
            for axon_terminal in self.axon_terminals:
                axon_terminal.potential = terminal_potential
            '''
            #if len(self.connections) == 0:
            #   self.partially_connect()
            #else:
            self.partially_connect()
            pass

            '''
            if abs(len(Neuron.neurons) - len(self.connections) + 1) > 0:
                self.create_connections()

            if abs(len(Neuron.neurons) - len(self.axon_terminals) + 1) > 0:
                self.create_axon_terminals()
            '''

class Supercluster():

    def __init__(self,size):
        for i in range(size):
            Neuron()
        print str(size) + " neurons created."
        self.n = 0
        self.build_connections()
        #pool = Pool(4, self.init_worker)
        #pool.apply_async(self.build_connections(), arguments)
        #map(lambda x: x.partially_connect(),Neuron.neurons)
        #map(lambda x: x.create_connections(),Neuron.neurons)
        #map(lambda x: x.create_axon_terminals(),Neuron.neurons)

    def build_connections(self):
        for neuron in Neuron.neurons:
            self.n += 1
            #neuron.thread.start()
            neuron.partially_connect()
            print "Counter: " + str(self.n)

Supercluster(10000)

朱莉娅

global neurons = []

type Neuron
    connections::Dict{UInt64,Float16}
    potential::Float16
    error::Float16

    function Neuron(arg1,arg2,arg3)
        self = new(arg1,arg2,arg3)
        push!(neurons, self)
    end

end

function fully_connect(self)
    for neuron in neurons
        if object_id(neuron) != object_id(self)
            self.connections[object_id(neuron)] = rand(1:100)/100
            #push!(self.connections, rand(1:100)/100)
        end
    end
end

function partially_connect(self)
    if isempty(self.connections)
        neuron_count = length(neurons)
        for neuron in neurons
            if object_id(neuron) != object_id(self)
                if rand(1:neuron_count/100) == 1
                    self.connections[object_id(neuron)] = rand(1:100)/100
                    #push!(self.connections, rand(1:100)/100)
                end
            end
        end
        println("Neuron ID: ",object_id(self))
        println("    Potential: ",self.potential)
        println("    Error: ",self.error)
        println("    Connections: ",length(self.connections))
    end
end

function Build()
    for i = 1:10000
        Neuron(Dict(),0.0,0.0)
    end
    println(length(neurons), " neurons created.")
    n = 0
    @parallel for neuron in neurons
        n += 1
        partially_connect(neuron)
        println("Counter: ",n)
    end
end

Build()

首先这些部分正在部分和随机地在每个神经元之间建立连接,花费太多时间如何加快此流程/部分?

的Python

def build_connections(self):
    for neuron in Neuron.neurons:
        self.n += 1
        #neuron.thread.start()
        neuron.partially_connect()
        print "Counter: " + str(self.n)

朱莉娅

n = 0
@parallel for neuron in neurons
    n += 1
    partially_connect(neuron)
    println("Counter: ",n)

其次,当我的目标是创造至少一百万个神经元时,给每个神经元,它自己的线程 是个好主意吗?这意味着它会像一个百万线程。

我在这里要做的是严格意义上的模仿生物神经网络而不是使用矩阵计算

此外:

新版partially_connect功能根据回答:

def partially_connect(self):
    if len(self.connections) == 0:
        neuron_count = len(Neuron.neurons)
        #for neuron in Neuron.neurons:
        elected = random.sample(Neuron.neurons,100)
        for neuron in elected:
            if id(neuron) != id(self):
                #if random.randint(1,neuron_count/100) == 1:
                self.connections[id(neuron)] = round(random.uniform(0.1, 1.0), 2)
        print "Neuron ID: " + str(id(self))
        print "    Potential: " + str(self.potential)
        print "    Error: " + str(self.error)
        print "    Connections: " + str(len(self.connections))

性能急剧提升。

2 个答案:

答案 0 :(得分:2)

在Julia中,如果性能很重要:不要使用全局变量(请参阅neurons数组)并且不要使用无类型数组(再次参见neurons数组)。请参阅performance tips。您还应该剖析以确定瓶颈所在。我强烈建议您在没有@parallel的情况下进行尝试,直到您能够快速完成。

我自己看了看,除此之外我发现了一些令人惊讶的瓶颈:

  • rand(1:neuron_count/100)创建一个浮点范围,而不是整数范围。这是一个巨大的瓶颈,可以立即识别出分析。使用rand(1:neuron_count÷100)
  • 最好不要拨打object_id,只需使用!(neuron === self)。或者甚至更好,将neurons作为数组和要修改的条目的整数索引传递。

修复这些项目,我设法获得程序的执行时间(在删除@parallel之后,这不太可能有用,并在文本显示中注释掉)从大约140秒到4秒几乎所有的运行时都只是用于生成随机数;您可以通过一次生成一个大型池来加速它,而不是逐个生成它们。

这使用ProgressMeter包(您必须安装)来显示进度。

using ProgressMeter

type Neuron
    connections::Dict{UInt64,Float16}
    potential::Float16
    error::Float16
end

function fully_connect(self, neurons)
    for neuron in neurons
        if object_id(neuron) != object_id(self)
            self.connections[object_id(neuron)] = rand(1:100)/100
            #push!(self.connections, rand(1:100)/100)
        end
    end
end

function partially_connect(self, neurons)
    if isempty(self.connections)
        neuron_count = length(neurons)
        for neuron in neurons
            if !(neuron === self)
                if rand(1:neuron_count÷100) == 1
                    self.connections[object_id(neuron)] = rand(1:100)/100
                    #push!(self.connections, rand(1:100)/100)
                end
            end
        end
#         println("Neuron ID: ",object_id(self))
#         println("    Potential: ",self.potential)
#         println("    Error: ",self.error)
#         println("    Connections: ",length(self.connections))
    end
end

function Build()
    neurons = [Neuron(Dict(),0.0,0.0) for i = 1:10000]
    println(length(neurons), " neurons created.")
    @showprogress 1 "Connecting neurons..." for neuron in neurons
        partially_connect(neuron, neurons)
    end
    neurons
end

neurons = Build()

答案 1 :(得分:1)

只看这段代码:

def partially_connect(self):
    if len(self.connections) == 0:
        neuron_count = len(Neuron.neurons)
        for neuron in Neuron.neurons[len(self.connections):]:
            if id(neuron) != id(self):
                if random.randint(1,neuron_count/100) == 1:
                    self.connections[id(neuron)] = round(random.uniform(0.1, 1.0), 2)

根据你对我对OP的评论的回复,这里有几件事:

  1. 使用L[0:]等语法时,您正在复制列表。切片语法为每个函数调用生成Neuron.neurons数组的浅表副本。这是 O(n)操作,因为您为partially_connect函数中的每个神经元调用build_connections一次,这使得 O(n²)。 (糟糕!)

  2. 你正在用Python工作,可以而且应该在库中完成(在C中,我们希望!)。看看例如random.paretovariate()random.sample()函数。您可以轻松计算num_connections = random.paretovariate(1.0) * 100,然后说connected_nodes = random.sample(neurons, num_connections)。从self过滤掉connected_nodes,您就完成了。

  3. 我认为通过消除n²行为和使用内置库例程,可以大大提升性能。

    <强> ADDITION

    回应您的补充,请考虑以下事项:

    def partially_connect(self):
        if len(self.connections) == 0:
            elected = random.sample(Neuron.neurons,100)
            try:
                elected.remove(self)
            except ValueError:
                pass
    
            for neuron in elected:
                self.connections[id(neuron)] = round(random.uniform(0.1, 1.0), 2)
    

    (我现在忽略了打印件。)

    我不知道如何在不迭代所有寻找id()值匹配的神经元的情况下从神经元传递到其连接的神经元。我建议您将连接对象的引用存储为键,并使用权重作为值:

    self.connections = [n:round(random.uniform(0.1, 1.0), 2) for n in elected]
    

    这假设您需要遍历从源到目标的链接。

    至于线程解决方案,我没有一个好的建议。一个小小的谷歌搜索引导我到一些旧的电子邮件线程(嘿!)提到像405和254这样的数字作为线程创建限制。我还没有看到任何文档说“Python线程现在无法使用!”或者其他什么,所以我怀疑你将不得不改变你实施解决方案的方式。