def reset(self): ''' 重置执行状态 :return: None ''' ns = self.getNeurons() collections.foreach(ns, lambda n: n.reset()) synapses = self.getSynapses() collections.foreach(synapses, lambda s: s.reset())
def scale(self, value): ''' 按比例修改sigma :param value: :return: ''' self._sigma *= value self.initGrids() collections.foreach(self.samples, lambda s: self.putgrid(s))
def getLayerCount(self): ''' 得到每层的神经元数量 :return: dict key是layerId,value是数量 ''' ls = {} ns = self.getNeurons() collections.foreach( ns, lambda s: exec('ls[s.layer] = ls.get(s.layer,0)+1')) return ls
def remove(self, ele): if ele is None: return if isinstance(ele, Synapse): self.synapses.remove(ele) return if isinstance(ele, Neuron): input_sypanses = self.getInputSynapse(ele.id) output_sypanses = self.getOutputSynapse(ele.id) synapses = output_sypanses + input_sypanses collections.foreach(synapses, lambda s: self.remove(s)) for i, ns in enumerate(self.neurons): for j, n in enumerate(ns): if n == ele: ns.remove(n) if len(ns) <= 0: self.neurons.remove(ns) return
def getNeurons(self, layer=-1, activation=None): ''' 取得特定层,且满足激活状态的神经元 :param layer: int 层,-1表示所有层 :param activation: bool 激活状态,None表示所有状态 :return: ''' if len(self.neurons) <= 0: return [] r = [] collections.foreach(self.neurons, lambda ns: r.extend(ns)) if layer < 0 and activation is None: return r return collections.findall( r, lambda n: (layer < 0 or n.layer == layer) and (r['activation'] == activation))
def getConnectionMarix(self, returntype='tuple', valuetype='01'): ''' @:param returntype str tuple 返回元组 tuple[fromId][toId] = synapse list 返回二维列表,值根据valuetype ::param valuetype str 01 有连接为1 synapse 值为突触对象 weight 值为权重 得到连接矩阵 :return: dict key是两个神经元的id ''' if returntype == 'tuple': marix = {} synapses = self.getSynapses() def func(s): marix[s.fromId][s.toId] = s collections.foreach(synapses, func) return marix else: marix = [] allneurons = self.getNeurons() for i in range(len(allneurons)): srcNeuron = allneurons[i] row = [] for j in range(len(allneurons)): destNeuron = allneurons[j] synapse = self.getSynapse(fromId=srcNeuron.id, toId=destNeuron.id) if valuetype == '01': row.append(0 if synapse is None else 1.) elif valuetype == 'synapse': row.append(synapse) else: row.append(synapse['weight']) marix.append(row) return marix
def __spilt(self, node=None): ''' 执行节点分裂,找到 :return: ''' # 找到待分裂的节点,是所有节点中发放率总和最高的节点 if node is None: firerates = np.array( list(map(lambda leaf: leaf.firerate, self.leafs))) index = np.argmax(firerates) node = self.leafs[index] # 找到该节点的分裂区,是样本出现最频繁的网格 spiltareas = [] while len(spiltareas) < Node.max_spilt_count: #分裂区数量不能大于设定 maxvalue = np.amax(node.grids) #取得节点中网格统计的最大值 if maxvalue <= 0: break position = np.where(node.grids == np.max(node.grids)) pos_center = [ node.center[i] - node.width / 2 + p[0] * node.width / Node.grid_num + node.width / (Node.grid_num * 2) for i, p in enumerate(position) ] position = np.delete(position, range(1, len(position[0])), axis=1) # 如果是第一个分裂区,直接加入 if len(spiltareas) <= 0: spiltareas.append(pos_center) node.grids.put(position, 0.0) continue # 否则需要计算距离 dis = [ np.linalg.norm(np.array(sa) - np.array(pos_center)) for sa in spiltareas ] if collections.any(dis, lambda d: d < node.width / 4): node.grids.put(position, 0.0) continue spiltareas.append(pos_center) node.grids.put(position, 0.0) if len(spiltareas) <= 0: return # 如果只有一个分裂区,则不创建子节点,而调整当前节点 if len(spiltareas) <= 1: node.center = np.array(spiltareas[0]) node.setWidth(node.width * Node.width_scale) samples = node.samples node.samples = [] if len(samples) > 0: collections.foreach(samples, lambda s: node.put_sample(s)) self.__evulate() return # 创建子节点 self.leafs.remove(node) for spiltarea in spiltareas: n = Node(np.array(spiltarea), max(node.width * Node.width_scale, Node.min_width)) n.parent = node node.childs.append(n) self.leafs.append(n) # 将属于父节点的样本分配到子节点 for s in node.samples: index = np.argmin( list(map(lambda c: np.linalg.norm(c - s), spiltareas))) if s not in node.childs[index].samples: node.childs[index].put_sample(s) node.samples = [] self.nodescount += len(spiltareas) self.__evulate()
def inference(self): depth = 1 while True: boxes = self.findBox(depth=depth) if boxes is None or len(boxes) <= 0: break collecitons.foreach(boxes, lambda b: b.do_inference)
def activate(self, net, inputs): ''' 激活网络 :param net: 测试网络 :param task: 测试任务 :return: outputs ''' # 取得输入 inputNeurons = net.getInputNeurons() # 重置神经元和突触状态 collections.foreach(net.getNeurons(), lambda n: n.reset()) collections.foreach(net.getSynapses(), lambda s: s.reset()) # 设置输入 for d, v in enumerate(inputs): if d >= len(inputNeurons): break model = models.nervousModels.find( inputNeurons[d].modelConfiguration.modelid) model.execute(inputNeurons[d], net, value=v) s = net.getOutputSynapse(inputNeurons[d].id) if collections.isEmpty(s): continue collections.foreach(s, lambda x: x.getModel().execute(x, net)) # 反复执行 ns = net.getNeurons() neuronCount = net.getNeuronCount() iterCount = 0 outputNeurons = net.getOutputNeurons() #while not collections.all(outputNeurons,lambda n:'value' in n.states.keys()) and iterCount<=neuronCount: while not collections.all( outputNeurons, lambda n: 'value' in n.states) and iterCount <= neuronCount: iterCount += 1 #uncomputeNeurons = collections.findall(ns,lambda n:'value' not in n.states.keys()) uncomputeNeurons = collections.findall( ns, lambda n: 'value' not in n.states) if collections.isEmpty(uncomputeNeurons): break for n in uncomputeNeurons: model = n.getModel() synapses = net.getInputSynapse(n.id) if collections.isEmpty(synapses): continue #if not collections.all(synapses,lambda s:'value' in s.states.keys()):continue if not collections.all(synapses, lambda s: 'value' in s.states): continue model.execute(n, net) synapses = net.getOutputSynapse(n.id) if collections.isEmpty(synapses): continue collections.foreach(synapses, lambda s: s.getModel().execute(s, net)) # 将没结果的输出神经元的值设置为0 #outputNeuronsWithNoResult = collections.findall(outputNeurons,lambda n:'value' not in n.states.keys()) outputNeuronsWithNoResult = collections.findall( outputNeurons, lambda n: 'value' not in n.states) if not collections.isEmpty(outputNeuronsWithNoResult): collections.foreach(outputNeuronsWithNoResult, lambda n: exec("n['value']=0")) # 取得结果 outputs = list(map(lambda n: n['value'], outputNeurons)) if len(outputs) == 1: outputs = outputs[0] return outputs