def learning_trials(f = None, trials = n_trials, debugging = True, save = False, protocol = 'Piron', familiar = True, type = 'learning', hist = False): trained = False while not trained: P, R, D, mBc, ABC, NoMove, RT = [], [], [], [], [], [], [] RP = np.zeros(n) AP = np.zeros(n) reset(protocol = 'Piron') for j in range(20): if debugging: print 'Trial: ', j + 1 if save: time, RT, P, D, RP, AP, mBc, ABC, NoMove = learning(f = f, trials = j, debugging = debugging, save = save, protocol = protocol, familiar = familiar, type = type, hist = hist, P = P, R = R, D = D, mBc = mBc, ABC = ABC, NoMove = NoMove, RT = RT, RP = RP, AP = AP) else: histor, time, RT, P, D, RP, AP, mBc, ABC, NoMove = learning(f = f, trials = j, debugging = debugging, save = save, protocol = protocol, familiar = familiar, type = type, hist = hist, P = P, R = R, D = D, mBc = mBc, ABC = ABC, NoMove = NoMove, RT = RT, RP = RP, AP = AP) if type == 'learning': if np.mean(P) > 0.70: trained = True else: trained = True else: for i in range(n_trials-20): if debugging: print 'Trial: ', i + j + 2 if save: time, RT, P, D, RP, AP, mBc, ABC, NoMove = learning(f = f, trials = i + j + 2, debugging = debugging, save = save, protocol = protocol, familiar = familiar, type = type, hist = hist, P = P, R = R, D = D, mBc = mBc, ABC = ABC, NoMove = NoMove, RT = RT, RP = RP, AP = AP) else: histor, time, RT, P, D, RP, AP, mBc, ABC, NoMove = learning(f = f, trials = i + j + 2, debugging = debugging, save = save, protocol = protocol, familiar = familiar, type = type, hist = hist, P = P, R = R, D = D, mBc = mBc, ABC = ABC, NoMove = NoMove, RT = RT, RP = RP, AP = AP) debug(f = f, RT = RT, P = P, D = D, RP = RP, AP = AP, mBc = mBc, ABC = ABC, NoMove = NoMove) debug_learning(connections["CTX.cog -> CTX.ass"].weights, connections["CTX.mot -> CTX.ass"].weights, connections["CTX.cog -> STR.cog"].weights, cues_value = CUE["value"], f = f) print if save: return P, RT, np.array(D).mean(), RP, AP, np.array(mBc).mean(), np.array(ABC).mean(), len(NoMove)/float(n_trials) return histor, P
return x net1 = Net() data_learn = [ torch.tensor([[0]], dtype=torch.float), torch.tensor([[1]], dtype=torch.float) ] targs_learn = torch.tensor([[1], [0]], dtype=torch.float) targs_learn2 = torch.tensor([[0], [1]], dtype=torch.float) learning(net1, lr=1, epoches_N=2000, data_learn=data_learn, targs_learn=targs_learn) print('{}\t{}'.format(net1(data_learn[0]), net1(data_learn[1]))) net2 = Net() learningLocal(net=net2, cnet=net1, lr=1, epoches_N=2000, data_learn=data_learn, targs_learn=targs_learn) print('{}\t{}'.format(net2(data_learn[0]), net2(data_learn[1])))
rp = np.zeros(n) ap = np.zeros(n) p_test,r_test,d_test,mbc_test,abc_test,nomove_test,rt_test = [], [], [], [], [], [], [] rp_test = np.zeros(n) ap_test = np.zeros(n) for j in range(n_trials): print 'Trial: ', j + 1 connections["GPI.cog -> THL.cog"].active = True connections["GPI.mot -> THL.mot"].active = True rt, p, d, rp, ap, mbc, abc, nomove, = learning(f=f, trial_n=j, debugging=False, protocol='Guthrie', learn=True, hist=False, P=p, D=d, mBc=mbc, ABC=abc, NoMove=nomove, RT=rt, RP=rp, AP=ap) connections["GPI.cog -> THL.cog"].active = False connections["GPI.mot -> THL.mot"].active = False rt_test, p_test, d_test, rp_test, ap_test, mbc_test, abc_test, nomove_test = learning( f=f, trial_n=j, debugging=False, protocol='Guthrie', learn=False,
list_overlaps=[] for i in range(nrep): #initial connectivity connectivity=np.random.binomial(1,pi_plus,(N,N)) first_stimulus=np.random.binomial(1,f,N) stimulus=first_stimulus first_stimulus_cod_lb=first_stimulus.sum() dp=1 the_prop_overlap=[] for age in range(0,pmax,dp): #stimulus f=np.random.uniform(0.02,0.04) #learning procedure mylearning=learning(stimulus,connectivity) mylearning.set_qp(qp) mylearning.set_qn(qn) learned_connectivity= mylearning.learning(1) thedynamics=AsynDynamics(thres,learned_connectivity,N) thedynamics.inhibition(myeta) thedynamics.load(first_stimulus) finalActivity=thedynamics.dynamics() overlap=float(len(np.intersect1d(sparse.find(finalActivity)[1],sparse.find(first_stimulus)[1]))) prop_overlap=overlap/first_stimulus_cod_lb print 'Age:',age,'proportion: ',prop_overlap the_prop_overlap.append(prop_overlap) connectivity=learned_connectivity stimulus=np.random.binomial(1,f,N) print '---------------------------------' print 'iteration number:',i
break elif b == "n": import sys sys.exit(0) generate_csv(method, "testA") generate_csv(method, "train") from learning import * def learning(method, n_splits=None): while True: b = raw_input("want to learning the data? (y/n) ") if b == "y": break elif b == "n": import sys sys.exit(0) if n_splits: RMSEs = test_trainset(method, LinearRegression(), n_splits=n_splits) print "RMSEs of method", method, "is\n", RMSEs print "average RMSE of method", method, "is", reduce(lambda x,y:x+y,RMSEs)/len(RMSEs) else: model, y_expand = test_trainset(method, LinearRegression()) pred_testset(method, model, y_expand) if __name__ == "__main__": method = "gaussiansum_d60" # pre(method) learning(method, n_splits=10)
ap_test = np.zeros(n) p_test_ctx,r_test_ctx,d_test_ctx,mbc_test_ctx,abc_test_ctx,nomove_test_ctx,rt_test_ctx = [], [], [], [], [], [], [] rp_test_ctx = np.zeros(n) ap_test_ctx = np.zeros(n) p_test_bg,r_test_bg,d_test_bg,mbc_test_bg,abc_test_bg,nomove_test_bg,rt_test_bg = [], [], [], [], [], [], [] rp_test_bg = np.zeros(n) ap_test_bg = np.zeros(n) for j in range(n_trials): #print 'Trial: ', j+1 connections["GPI.cog -> THL.cog"].active = True connections["GPI.mot -> THL.mot"].active = True temp_cog = connections["CTX.cog -> CTX.ass"].weights temp_mot = connections["CTX.mot -> CTX.ass"].weights temp_str = connections["CTX.cog -> STR.cog"].weights rt, p, d, rp, ap, mbc, abc, nomove = learning(trial_n = j, debugging = False, protocol = 'Guthrie', learn = True, hist = False, P = p, D = d, mBc = mbc, ABC = abc, NoMove = nomove, RT = rt, RP = rp, AP = ap) connections["CTX.cog -> CTX.ass"].weights = initWcog connections["CTX.mot -> CTX.ass"].weights = initWmot rt_test_bg, p_test_bg, d_test_bg, rp_test_bg, ap_test_bg, mbc_test_bg, abc_test_bg, nomove_test_bg = learning(trial_n = j, debugging = False, protocol = 'Guthrie', learn = False, hist = False, P = p_test_bg, D = d_test_bg, mBc = mbc_test_bg, ABC = abc_test_bg, NoMove = nomove_test_bg, RT = rt_test_bg, AP = ap_test_bg) connections["CTX.cog -> CTX.ass"].weights = temp_cog connections["CTX.mot -> CTX.ass"].weights = temp_mot connections["CTX.cog -> STR.cog"].weights = initWstr rt_test_ctx, p_test_ctx, d_test_ctx, rp_test_ctx, ap_test_ctx, mbc_test_ctx, abc_test_ctx, nomove_test_ctx = learning(trial_n = j, debugging = False, protocol = 'Guthrie', learn = False, hist = False, P = p_test_ctx, D = d_test_ctx, mBc = mbc_test_ctx, ABC = abc_test_ctx, NoMove = nomove_test_ctx, RT = rt_test_ctx, AP = ap_test_ctx) connections["CTX.cog -> STR.cog"].weights = temp_str connections["GPI.cog -> THL.cog"].active = False connections["GPI.mot -> THL.mot"].active = False rt_test, p_test, d_test, rp_test, ap_test, mbc_test, abc_test, nomove_test = learning(trial_n = j, debugging = False, protocol = 'Guthrie', learn = False, hist = False, P = p_test, D = d_test, mBc = mbc_test, ABC = abc_test, NoMove = nomove_test, RT = rt_test, AP = ap_test)
# the strenght of inhibition myeta = 1.0 * (pi_plus + C_delta_N * np.sqrt(gamma)) # ---------------------------------------------------- # ---------Training----------------------------------- # ---------------------------------------------------- # initial connectivity connectivity = np.random.binomial(1, pi_plus, (N, N)) # coding level f=0.04 f = 0.04 # stimulus stimulus004 = np.random.binomial(1, f, (p, N)) mylearning = learning(stimulus004, connectivity) mylearning.set_qp(qp) mylearning.set_qn(qn) # learning procedure conn_004_0 = mylearning.learning(1) # thedynamics=AsynDynamics(thres,conn_004_0,N) # thedynamics.inhibition(myeta) # thedynamics.load(stimulus004[0]) # sel_004_0,nonsel_004_0=thedynamics.fieldValues() # plt.plot(sel_004_0[0],sel_004_0[1],'+') # plt.plot(nonsel_004_0[0],nonsel_004_0[1],'o') # plt.show() mylearning = learning(stimulus004, connectivity) mylearning.set_qp(qp) mylearning.set_qn(qn) conn_004_200 = mylearning.learning(199)