def __init__(self, name = "Cleanup Memory", \ in_vec_list = None, out_vec_list = None, tau_in = 0.005, in_scale = 1.0, \ en_inhib = False, tau_inhib = 0.005, tau_smooth = 0.0001, inhib_scale = 2.0, \ en_mut_inhib = False, mut_inhib_scale = 2.0, \ num_neurons_per_vec = 10, threshold = 0.3, \ N_out_vec = None, en_X_out = False, input_name = "Input", \ sim_mode = SimulationMode.DEFAULT, quick = True, rand_seed = None, **params): NetworkImpl.__init__(self) self.setName(name) if( mut_inhib_scale <= 0 ): en_mut_inhib = False if( out_vec_list is None ): out_vec_list = in_vec_list self.dimension = len(out_vec_list[0]) if( isinstance(mut_inhib_scale, (int,float)) ): mut_inhib_scale = [mut_inhib_scale] * len(in_vec_list) if( isinstance(inhib_scale, (int,float)) ): inhib_scale = [inhib_scale] * len(in_vec_list) if( isinstance(threshold, (int,float)) ): threshold = [threshold] * len(in_vec_list) in_vec_list = [[in_vec_list[i][d] * in_scale for d in range(len(in_vec_list[i]))] \ for i in range(len(in_vec_list))] self.i_list = [] self.in_vec_list = [] if( str(sim_mode).lower() == 'ideal' ): node = CleanupMemoryNode(name, in_vec_list, out_vec_list, tau_in, en_inhib, tau_inhib, \ threshold = sum(threshold) / len(threshold), en_wta = en_mut_inhib, \ N_out_vec = N_out_vec) self.addNode(node) self.exposeTermination(node.getTermination("Input"), "Input") if( en_inhib ): self.exposeTermination(node.getTermination("Inhib"), "Inhib") self.exposeOrigin(node.getOrigin("Output"), "X") if( en_X_out ): self.exposeOrigin(node.getOrigin("X"), "x0") else: net = nef.Network(self, quick) enss = [] num_items = 0 for out_vec in out_vec_list: if( out_vec is None ): continue else: num_items += 1 in_terms = [] inhib_terms = [] origins = [] en_N_out = not (N_out_vec is None) out_relay = SimpleNEFEns("Output", self.dimension, pstc = tau_smooth) net.add(out_relay) if( en_X_out ): x_relay = SimpleNEFEns("X", num_items + en_N_out, pstc = tau_smooth) net.add(x_relay) for i,in_vec in enumerate(in_vec_list): if( out_vec_list[i] is None ): continue self.in_vec_list.append(in_vec) self.i_list.append(i) pdf = IndicatorPDF(threshold[i] + 0.1, 1) eval_points = [[pdf.sample()[0]] for _ in range(1000)] intercepts = [threshold[i] + n * (1-threshold[i])/(num_neurons_per_vec) for n in range(num_neurons_per_vec)] if( sim_mode == SimulationMode.DIRECT ): ens = SimpleNEFEns("Item" + str(i), 1, input_name = "") net.add(ens) else: ens = net.make("Item" + str(i), num_neurons_per_vec, 1, eval_points = eval_points, \ encoders = [[1]] * num_neurons_per_vec, intercept = intercepts, \ max_rate = (100,200), seed = rand_seed) if( input_name != "" and not input_name is None ): ens.addDecodedTermination(input_name, [in_vec], tau_in, False) in_terms.append(ens.getTermination(input_name)) ens.addDecodedOrigin("Output", [FilteredStepFunction(shift = threshold[i], \ step_val = out_vec_list[i][d]) for d in range(self.dimension)], \ "AXON") enss.append(ens) out_relay.addDecodedTermination("Item" + str(i), None, tau_smooth, False) out_relay.addNeuronCount(ens.getNeuronCount()) net.connect(ens.getOrigin("Output"), out_relay.getTermination("Item" + str(i))) if( en_X_out ): ens.removeDecodedOrigin("X") ens.addDecodedOrigin("X", [FilteredStepFunction(shift = threshold[i])], "AXON") x_relay.addDecodedTermination("Item" + str(i), transpose(delta(num_items + en_N_out, i)), tau_smooth, False) x_relay.addNeuronCount(ens.getNeuronCount()) net.connect(ens.getOrigin("X"), x_relay.getTermination("Item" + str(i))) if( en_inhib ): ens.addTermination("Inhib", [[-inhib_scale[i]]] * num_neurons_per_vec, tau_inhib, False) inhib_terms.append(ens.getTermination("Inhib")) if( not N_out_vec is None ): N_threshold = min(threshold) pdf = IndicatorPDF(-0.1, N_threshold - 0.1) eval_points = [[pdf.sample()[0]] for _ in range(1000)] intercepts = [-(n * (N_threshold)/(num_neurons_per_vec)) for n in range(num_neurons_per_vec)] if( sim_mode == SimulationMode.DIRECT ): ens = SimpleNEFEns("ItemN", 1, input_name = "") net.add(ens) else: ens = net.make("ItemN", num_neurons_per_vec, 1, eval_points = eval_points, \ encoders = [[-1]] * num_neurons_per_vec, intercept = intercepts, \ max_rate = (300,400), seed = rand_seed) for i in range(len(in_vec_list)): ens.addDecodedTermination("Item" + str(i), [[1]], 0.005, False) net.connect(enss[i].getOrigin("X"), ens.getTermination("Item" + str(i))) ens.addDecodedOrigin("Output", [FilteredStepFunction(shift = N_threshold, \ step_val = N_out_vec[d], mirror = True) for d in range(self.dimension)], \ "AXON") out_relay.addDecodedTermination("ItemN", None, tau_smooth, False) out_relay.addNeuronCount(ens.getNeuronCount()) net.connect(ens.getOrigin("Output"), out_relay.getTermination("ItemN")) if( en_X_out ): ens.removeDecodedOrigin("X") ens.addDecodedOrigin("X", [FilteredStepFunction(shift = N_threshold, mirror = True)], "AXON") x_relay.addDecodedTermination("ItemN", transpose(delta(num_items + en_N_out, num_items)), tau_smooth, False) x_relay.addNeuronCount(ens.getNeuronCount()) net.connect(ens.getOrigin("X"), x_relay.getTermination("ItemN")) if( en_inhib ): ens.addTermination("Inhib", [[-inhib_scale[i]]] * num_neurons_per_vec, tau_inhib, False) inhib_terms.append(ens.getTermination("Inhib")) if( en_mut_inhib ): for n in range(num_items): for i in range(num_items): if( n != i): enss[i].addTermination("Inhib" + str(n), [[-mut_inhib_scale[i]]] * num_neurons_per_vec, 0.005, False) net.connect(enss[n].getOrigin("X"), enss[i].getTermination("Inhib" + str(n))) if( len(in_terms) > 0 ): in_term = EnsembleTermination(net.network, "Input", in_terms) net.network.exposeTermination(in_term, "Input") net.network.exposeOrigin(out_relay.getOrigin("X"), "X") if( en_X_out ): self.exposeOrigin(x_relay.getOrigin("X"), "x0") if( en_inhib ): inhib_term = EnsembleTermination(net.network, "Inhib", inhib_terms) net.network.exposeTermination(inhib_term, "Inhib") # Reset random seed if( not seed is None ): seed() self.releaseMemory() if( str(sim_mode).lower() == 'ideal' ): sim_mode = SimulationMode.DIRECT NetworkImpl.setMode(self, sim_mode) if( sim_mode == SimulationMode.DIRECT ): self.fixMode()
def __init__(self, name = "Detector", detect_vec = None, inhib_vec = None, tau_in = 0.005, \ en_inhib = False, en_inhibN = None, tau_inhib = 0.005, in_scale = 1.0, inhib_scale = 2.0,\ en_out = True, en_N_out = False, en_X_out = False, num_neurons = 20, detect_threshold = 0.4, \ sim_mode = SimulationMode.DEFAULT, quick = True, rand_seed = 0, net = None, input_name = "Input"): self.dimension = 1 NetworkImpl.__init__(self) ens_name = name if( not isinstance(net, nef.Network) ): if( not net is None ): net = nef.Network(net, quick) else: ens_name = "detect" net = nef.Network(self, quick) self.setName(name) if( detect_vec is None ): detect_vec = [1] vec_dim = len(detect_vec) detect_vec_scale = [detect_vec[n] * in_scale for n in range(vec_dim)] if( en_inhib ): if( inhib_vec is None ): inhib_vec = [1] inhib_dim = len(inhib_vec) if( en_inhibN is None ): en_inhibN = en_inhib max_rate = (100,200) max_rateN = (300,400) detect_threshold = max(min(detect_threshold, 0.8), 0.2) intercepts = [detect_threshold + n * (1-detect_threshold)/(num_neurons) for n in range(num_neurons)] interceptsN = [-(n * (detect_threshold)/(num_neurons)) for n in range(num_neurons)] params = dict(intercept = intercepts , max_rate = max_rate , quick = quick) paramsN = dict(intercept = interceptsN, max_rate = max_rateN, quick = quick) out_func = FilteredStepFunction(shift = detect_threshold, mirror = False) out_funcN = FilteredStepFunction(shift = detect_threshold, mirror = True) if( rand_seed >= 0 ): PDFTools.setSeed(rand_seed) seed(rand_seed) params["encoders"] = [[1]] * num_neurons paramsN["encoders"] = [[-1]] * num_neurons pdf = IndicatorPDF(detect_threshold + 0.1, 1.1) pdfN = IndicatorPDF(-0.1, detect_threshold - 0.1) params["eval_points"] = [[pdf.sample()[0]] for _ in range(1000)] paramsN["eval_points"] = [[pdfN.sample()[0]] for _ in range(1000)] if( en_out ): if( sim_mode == SimulationMode.DIRECT or str(sim_mode).lower() == 'ideal' ): detect = SimpleNEFEns(ens_name, 1, input_name = "") net.add(detect) else: detect = net.make(ens_name, num_neurons, 1, **params) if( not input_name is None ): detect.addDecodedTermination(input_name, [detect_vec_scale], tau_in, False) if( en_inhib ): inhib_vec_scale = [inhib_vec[n] * -inhib_scale for n in range(inhib_dim)] detect.addTermination("Inhib", [inhib_vec_scale] * num_neurons, tau_inhib, False) detect.removeDecodedOrigin("X") detect.addDecodedOrigin("X", [out_func], "AXON") if( en_X_out ): detect.addDecodedOrigin("x0", [PostfixFunction("x0", 1)], "AXON") self.exposeOrigin(detect.getOrigin("x0"), "x0") if( en_N_out ): if( sim_mode == SimulationMode.DIRECT or str(sim_mode).lower() == 'ideal' ): detectN = SimpleNEFEns(ens_name + "N", 1, input_name = "") net.add(detectN) else: detectN = net.make(ens_name + "N", num_neurons, 1, **paramsN) if( not input_name is None ): detectN.addDecodedTermination(input_name, [detect_vec_scale], tau_in, False) if( en_inhibN ): detectN.addTermination("Inhib", [inhib_vec_scale] * num_neurons, tau_inhib, False) detectN.removeDecodedOrigin("X") detectN.addDecodedOrigin("X", [out_funcN], "AXON") if( en_X_out ): detectN.addDecodedOrigin("x0", [PostfixFunction("x0", 1)], "AXON") self.exposeOrigin(detectN.getOrigin("x0"), "x0N") input_terms = [] inhib_terms = [] if( en_out ): if( not input_name is None ): input_terms.append(detect.getTermination(input_name)) self.exposeOrigin(detect.getOrigin("X"), name) if( en_inhib ): inhib_terms.append(detect.getTermination("Inhib")) if( en_N_out ): if( not input_name is None ): input_terms.append(detectN.getTermination(input_name)) self.exposeOrigin(detectN.getOrigin("X"), str(name + "N")) if( en_inhibN ): inhib_terms.append(detectN.getTermination("Inhib")) if( len(input_terms) > 0 ): input_term = EnsembleTermination(self, input_name, input_terms) self.exposeTermination(input_term, input_name) if( len(inhib_terms) > 0 ): inhib_term = EnsembleTermination(self, "Inhib", inhib_terms) self.exposeTermination(inhib_term, "Inhib") if( str(sim_mode).lower() == 'ideal' ): sim_mode = SimulationMode.DIRECT NetworkImpl.setMode(self, sim_mode) if( sim_mode == SimulationMode.DIRECT ): self.fixMode() self.releaseMemory()