Example #1
0
# sta = (sta - np.min(sta)) / (np.max(sta) - np.min(sta))
# glm0.networks[0].layers[0].weights[:,0]=deepcopy((sta - np.min(sta)) / (np.max(sta) - np.min(sta)))

v2f0 = glm0.fit_variables(fit_biases=True)

# train initial model
_ = glm0.train(input_data=[Xstim],
               output_data=Robs,
               train_indxs=Ui,
               test_indxs=Xi,
               learning_alg='lbfgs',
               opt_params=lbfgs_params,
               fit_variables=v2f0)

# plot filters
DU.plot_3dfilters(glm0)

# Find best regularization

glmbest = glm0.copy_model()

[LLpath, glms] = NDNutils.reg_path(glmbest,
                                   input_data=[Xstim],
                                   output_data=Robs,
                                   train_indxs=Ui,
                                   test_indxs=Xi,
                                   reg_type='glocal',
                                   reg_vals=[1e-6, 1e-4, 1e-3, 1e-2, 0.1, 1],
                                   layer_target=0,
                                   ffnet_target=0,
                                   learning_alg='lbfgs',
stas = (Xstim.T @ (Rvalid-np.mean(Rvalid, axis=0))) / np.sum(Rvalid, axis=0)
stas /= np.sum(stas,axis=0)
gqm0.networks[0].layers[0].weights[:] = deepcopy(stas[:])
gqm0.set_regularization('d2x', reg_val=bestreg, ffnet_target=0)
gqm0.set_regularization('d2x', reg_val=bestreg, ffnet_target=1)
gqm0.set_regularization('d2x', reg_val=bestreg, ffnet_target=2)

# train initial model
_ = gqm0.train(input_data=[Xstim], output_data=Rvalid,
    train_indxs=Ui, test_indxs=Xi,
    learning_alg='adam', opt_params=adam_params,
     fit_variables=v2f0)
# f= plt.plot(np.asarray(LLxs))
#%% plot model
DU.plot_3dfilters(gqm0, ffnet=0)

DU.plot_3dfilters(gqm0, ffnet=1)

DU.plot_3dfilters(gqm0, ffnet=2)

#%% plot fit
LLx = gqm0.eval_models(input_data=Xstim, output_data=Rvalid, data_indxs=Xi, nulladjusted=True)
plt.figure()
plt.plot(LLx, '-o')
plt.axhline(0, color='k')

#%% Run eye correction
# Run it all once
eyeAtFrameCentered = (eyeAtFrame-(640, 380))
centers5, locs, LLspace1 = ne.get_corr_grid(gqm0, Stim, Robs, [NX,NY], cids,
# glm0.networks[0].layers[0].biases = np.mean(Rvalid,axis=0).astype('float32')

v2f0 = glm0.fit_variables(fit_biases=False)
v2f0[-1][-1]['biases'] = True

# train initial model
_ = glm0.train(input_data=[Xstim],
               output_data=Rvalid,
               train_indxs=Ui,
               test_indxs=Xi,
               learning_alg='lbfgs',
               opt_params=lbfgs_params,
               fit_variables=v2f0)

#%% plot filters
DU.plot_3dfilters(glm0)

LLx0 = glm0.eval_models(input_data=[Xstim],
                        output_data=Rvalid,
                        data_indxs=Xi,
                        nulladjusted=True)

plt.plot(LLx0, '-o')
plt.axhline(0)

# %% get crop indices
# Cxinds = ne.crop_indx(NX, range(1,30), range(1,30))
Cxinds = ne.crop_indx(NX, range(9, 24), range(9, 24))
# Cxinds = ne.crop_indx(NX, range(5,20), range(5,20))
# Cxinds = ne.crop_indx(NX, range(20,44), range(20,44))
NX2 = np.sqrt(len(Cxinds)).astype(int)
v2f = nim0.fit_variables(fit_biases=True)

# train
_ = nim0.train(input_data=[Xstim],
               output_data=Robs,
               train_indxs=Ui,
               test_indxs=Xi,
               learning_alg=optimizer,
               opt_params=opt_params,
               use_dropout=False,
               fit_variables=v2f)

print("Done")

DU.plot_3dfilters(nim0)

# only include cells that are well fit
LLx = nim0.eval_models(input_data=[Xstim],
                       output_data=Robs,
                       data_indxs=Xi,
                       nulladjusted=True)
plt.plot(LLx, 'o')

cids = np.where(LLx > 0.05)[0]
NC = len(cids)
Robsv = deepcopy(Robs[:, cids])

num_subs = NC // 2

nim_par = NDNutils.ffnetwork_params(input_dims=[1, NX, NY, num_lags],
Example #5
0
v2f = retV1.fit_variables(fit_biases=False)
v2f[0][0]['biases'] = True
v2f[-1][-1]['biases'] = True

#%% train
_ = retV1.train(input_data=[Xstim],
                output_data=Robs,
                train_indxs=Ui,
                test_indxs=Xi,
                silent=False,
                learning_alg='adam',
                opt_params=adam_params,
                fit_variables=v2f)
# %% fit
DU.plot_3dfilters(retV1)

# ======================================================================
# ======================================================================
# ======================================================================
# ======================================================================

# STRAY CODE BELOW HERE

# ======================================================================
# ======================================================================
# ======================================================================

#%%

#%%
v2f = retV1.fit_variables(fit_biases=True)
v2f[0][0]['biases'] = True
v2f[-1][0]['biases'] = True

#%% train
_ = retV1b.train(input_data=[Xstim, Rvalid],
                 output_data=Rvalid,
                 train_indxs=Ui,
                 test_indxs=Xi,
                 silent=False,
                 learning_alg='adam',
                 opt_params=adam_params,
                 fit_variables=v2fb)
# %% fit
DU.plot_3dfilters(retV1b)
#%%
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(retV1b.networks[0].layers[0].weights)
plt.title("Temporal Kernels")
plt.subplot(1, 2, 2)
plt.plot(retV1b.networks[1].layers[1].weights)
plt.title("Latent temporal kernel")
#%% get test likelihood
LLx1 = retV1b.eval_models(input_data=[Xstim, Rvalid],
                          output_data=Rvalid,
                          data_indxs=Xi,
                          nulladjusted=True,
                          use_gpu=False)