def __init__(self, c, source): """ Initialize the variables of SORN :param c: Bunch(), the bunch of parameters :param source: the input source """ self.c = c self.source = source # Initialize weight matrix # W_oe: connections from excitatory to output(excitatory) neurons. self.W_ie = create_matrix((c.N_i, c.N_e), c.W_ie) self.W_ei = create_matrix((c.N_e, c.N_i), c.W_ei) self.W_ee = create_matrix((c.N_e, c.N_e), c.W_ee) self.W_oe = create_matrix((c.N_o, c.N_e), c.W_oe) self.W_eu = self.source.generate_connection_e(c.N_e) self.W_iu = self.source.generate_connection_i(c.N_i) self.x = np.random.rand(c.N_e) < c.h_ip_e self.y = np.zeros(c.N_i) self.o = np.zeros(c.N_o) # Initialize the Modulation-Factors self.m_r = 1 # reward modulation factor of recurrent layer self.m_o = 1 # reward modualtion factor of readout layer self.r = 0 # reward self.reward = deque(maxlen=c.window_size) # helper list to record self.output = deque(maxlen=c.N_o) # helper list to record # Initialize output self.output.append(0) # Initialize the pre-threshold variables self.R_x = np.zeros(c.N_e) self.R_y = np.zeros(c.N_i) self.R_o = np.zeros(c.N_o) if c.ordered_thresholds: self.T_i = (np.arange(c.N_i) + 0.5) * ((c.T_i_max - c.T_i_min) / (1. * c.N_i)) + c.T_i_min self.T_e = (np.arange(c.N_e) + 0.5) * ((c.T_e_max - c.T_e_min) / (1. * c.N_e)) + c.T_e_min self.T_o = (np.arange(c.N_o) + 0.5) * ((c.T_o_max - c.T_o_min) / (1. * c.N_o)) + c.T_o_min np.random.shuffle(self.T_e) np.random.shuffle(self.T_o) else: self.T_i = c.T_i_min + np.random.rand( c.N_i) * (c.T_i_max - c.T_i_min) self.T_e = c.T_e_min + np.random.rand( c.N_e) * (c.T_e_max - c.T_e_min) self.T_o = c.T_o_min + np.random.rand( c.N_o) * (c.T_o_max - c.T_o_min) # Seperately activate the plasticity mechanism in Recurrent and Output. self.update = True self.display = False
def __init__(self, c, source): """ Initializes the variables of SORN :param c: Bunch(), the bunch of parameters :param source: the input source """ self.c = c self.source = source # Initialize weight matrix # W_to_from (W_ie = from excitatory to inhibitory) self.W_ie = create_matrix((c.N_i, c.N_e), c.W_ie) self.W_ei = create_matrix((c.N_e, c.N_i), c.W_ei) self.W_ee = create_matrix((c.N_e, c.N_e), c.W_ee) self.W_eu = self.source.generate_connection_e(c.N_e) self.W_iu = self.source.generate_connection_i(c.N_i) # Initialize the neurons in SORN self.x = np.random.rand(c.N_e) < c.h_ip self.y = np.zeros(c.N_i) self.u = source.next() # Initialize the pre-threshold variables self.R_x = np.zeros(c.N_e) self.R_y = np.zeros(c.N_i) if c.ordered_thresholds: self.T_i = (np.arange(c.N_i) + 0.5) * ((c.T_i_max - c.T_i_min) / (1. * c.N_i)) + c.T_i_min self.T_e = (np.arange(c.N_e) + 0.5) * ((c.T_e_max - c.T_e_min) / (1. * c.N_e)) + c.T_e_min np.random.shuffle(self.T_e) else: self.T_i = c.T_i_min + np.random.rand( c.N_i) * (c.T_i_max - c.T_i_min) self.T_e = c.T_e_min + np.random.rand( c.N_e) * (c.T_e_max - c.T_e_min) # Activate plasticity mechanisms self.update = True
def generate_connection_i(self, N_i): c = utils.Bunch(use_sparse=False, lamb=np.inf, avoid_self_connection=False) ans = synapses.create_matrix((N_i, self.N_a), c) W = np.zeros((N_i, self.N_a)) if N_i > 0: available = set(range(N_i)) for a in range(self.N_a): temp = np.random.choice(list(available), self.N_u_i) W[temp, a] = 1 if '_' in self.lookup: W[:, self.lookup['_']] = 0 ans.W = W return ans
def generate_connection_e(self, N_e): W = np.zeros((N_e, self.N_a)) available = set(range(N_e)) for a in range(self.N_a): temp = np.random.choice(list(available), self.N_u_e) W[temp, a] = 1 if self.avoid: available = available.difference(temp) if '_' in self.lookup: W[:, self.lookup['_']] = 0 c = utils.Bunch(use_sparse=False, lamb=np.inf, avoid_self_connections=False) ans = synapses.create_matrix((N_e, self.N_a), c) ans.W = W return ans
def generate_connection_i(self, N_i): c = utils.Bunch(lamb=np.inf, avoid_self_connections=False) return synapses.create_matrix((N_i, self.N_i), c)
def generate_connection_e(self, N_e): c = utils.Bunch(lamb=np.inf, avoid_self_connections=False) tmpsyn = synapses.create_matrix((N_e, self.N_i), c) tmpsyn.set_synapses(tmpsyn.get_synapses() * 0) return tmpsyn