Ejemplo n.º 1
0
def plot_tfilters(ndnmod, kts=None, ffnet=0, to_plot=True):
    """Can pass in weights to relevant layer in first argument, as well as NDN model.
    Will use default tkerns variable, but this can also be passed in as kts argument."""

    assert kts is not None, 'Must include tkerns.'
    ntk = kts.shape[1]
    if type(
            ndnmod
    ) is np.ndarray:  # then passing in filters and need more information
        ws = deepcopy(ndnmod)
    else:
        ws = deepcopy(ndnmod.networks[ffnet].layers[0].weights)

    if len(ws.shape) > 2:
        nx, ntk2, numcells = ws.shape
        ws2 = ws
    else:
        nx = ws.shape[0] // ntk
        numcells = ws.shape[1]
        ws2 = np.reshape(ws, [nx, ntk, numcells])

    ks = np.expand_dims(kts, axis=0) @ ws2
    if to_plot:
        DU.plot_filters(filters=ks)
    else:
        return ks
Ejemplo n.º 2
0
def compute_binocular_tfilters(binoc_mod, kts=None, to_plot=True):

    assert kts is not None, 'Must include tkerns.'
    BFs = compute_binocular_filters(binoc_mod, to_plot=False)
    Bks = np.transpose(np.tensordot(kts, BFs, axes=[1, 0]), (1, 0, 2))

    if to_plot:
        DU.plot_filters(filters=Bks)
    else:
        return Bks
Ejemplo n.º 3
0
def compute_binocular_filters(binoc_mod, to_plot=True):

    # Find binocular layer
    blayer, bnet = None, None
    for mm in range(len(binoc_mod.networks)):
        for nn in range(len(binoc_mod.networks[mm].layers)):
            if binoc_mod.network_list[mm]['layer_types'][nn] == 'biconv':
                if nn < len(binoc_mod.networks[mm].layers) - 1:
                    bnet, blayer = mm, nn + 1
                elif mm < len(binoc_mod.networks) - 1:
                    bnet, blayer = mm + 1, 0  # split in hierarchical network
    assert blayer is not None, 'biconv layer not found'

    NF = binoc_mod.networks[0].layers[blayer].output_dims[0]
    Nin = binoc_mod.networks[0].layers[blayer].input_dims[0]
    NX = binoc_mod.networks[0].layers[blayer].filter_dims[1]
    ks1 = DU.compute_spatiotemporal_filters(binoc_mod)
    ws = np.reshape(binoc_mod.networks[0].layers[blayer].weights,
                    [NX, Nin, NF])
    num_lags = binoc_mod.networks[0].layers[0].input_dims[0]
    if binoc_mod.networks[0].layers[0].filter_dims[
            1] > 1:  # then not temporal layer
        filter_dims = [
            num_lags, binoc_mod.networks[0].layers[0].filter_dims[1]
        ]
    else:
        filter_dims = [
            num_lags, binoc_mod.networks[0].layers[1].filter_dims[1]
        ]
    nfd = [filter_dims[0], filter_dims[1] + NX]
    # print(filter_dims, nfd)
    Bfilts = np.zeros(nfd + [NF, 2])
    for nn in range(NX):
        Bfilts[:, np.add(range(filter_dims[1]), nn), :,
               0] += np.reshape(np.matmul(ks1, ws[nn, range(Nin // 2), :]),
                                [filter_dims[1], filter_dims[0], NF])
        Bfilts[:, np.add(range(filter_dims[1]), nn), :, 1] += np.reshape(
            np.matmul(ks1, ws[nn, range(Nin // 2, Nin), :]),
            [filter_dims[1], filter_dims[0], NF])
    bifilts = np.concatenate((Bfilts[:, :, :, 0], Bfilts[:, :, :, 1]), axis=1)
    if to_plot:
        DU.plot_filters(filters=bifilts, flipxy=True)
    else:
        return bifilts
Ejemplo n.º 4
0
def compute_binocular_filters(binoc_mod,
                              ffnet_n=0,
                              to_plot=True,
                              num_space=36):
    """using standard binocular model, compute filters. defaults to first ffnet and
    num_space = 36. Set num_space=None to go to minimum given convolutional constraints"""

    # Find binocular layer
    blayer, bnet = None, None
    for mm in range(len(binoc_mod.networks)):
        for nn in range(len(binoc_mod.networks[mm].layers)):
            if binoc_mod.network_list[mm]['layer_types'][nn] == 'biconv':
                if nn < len(binoc_mod.networks[mm].layers) - 1:
                    bnet, blayer = mm, nn + 1
                elif mm < len(binoc_mod.networks) - 1:
                    bnet, blayer = mm + 1, 0  # split in hierarchical network
    assert blayer is not None, 'biconv layer not found'

    NF = binoc_mod.networks[ffnet_n].layers[blayer].output_dims[0]
    Nin = binoc_mod.networks[ffnet_n].layers[blayer].input_dims[0]
    NX = binoc_mod.networks[ffnet_n].layers[blayer].filter_dims[1]
    ks1 = DU.compute_spatiotemporal_filters(binoc_mod)
    ws = np.reshape(binoc_mod.networks[ffnet_n].layers[blayer].weights,
                    [NX, Nin, NF])
    num_lags = binoc_mod.networks[ffnet_n].layers[0].input_dims[0]
    if binoc_mod.networks[ffnet_n].layers[0].filter_dims[
            1] > 1:  # then not temporal layer
        filter_dims = [
            num_lags, binoc_mod.networks[0].layers[0].filter_dims[1]
        ]
    else:
        filter_dims = [
            num_lags, binoc_mod.networks[0].layers[1].filter_dims[1]
        ]
    num_cspace = filter_dims[1] + NX
    nfd = [filter_dims[0], num_cspace]
    # print(filter_dims, nfd)
    Bfilts = np.zeros(nfd + [NF, 2])
    for nn in range(NX):
        Bfilts[:, np.add(range(filter_dims[1]), nn), :,
               0] += np.reshape(np.matmul(ks1, ws[nn, range(Nin // 2), :]),
                                [filter_dims[1], filter_dims[0], NF])
        Bfilts[:, np.add(range(filter_dims[1]), nn), :, 1] += np.reshape(
            np.matmul(ks1, ws[nn, range(Nin // 2, Nin), :]),
            [filter_dims[1], filter_dims[0], NF])

    # Cast into desired num_space
    if num_space is None:
        num_space = num_cspace
    if num_space == num_cspace:
        BfiltsX = Bfilts
    elif num_space > num_cspace:
        BfiltsX = np.zeros([filter_dims[0], num_space, NF, 2])
        padding = (num_space - num_cspace) // 2
        BfiltsX[:, padding + np.arange(num_cspace), :, :] = Bfilts
    else:  # crop
        unpadding = (num_cspace - num_space) // 2
        BfiltsX = Bfilts[:, unpadding + np.arange(num_space), :, :]

    bifilts = np.concatenate((BfiltsX[:, :, :, 0], BfiltsX[:, :, :, 1]),
                             axis=1)
    if to_plot:
        DU.plot_filters(filters=bifilts, flipxy=True)
    else:
        return bifilts
Ejemplo n.º 5
0
side2b = side2.copy_model()

_ = side2b.train(input_data=input_data, output_data=Rvalid, train_indxs=Ui, test_indxs=Xi, silent=False, 
    learning_alg='adam', opt_params=adam_params, blocks=blocks)

LLs2n = side2b.eval_models(input_data=input_data, output_data=Rvalid, data_indxs=Xi, nulladjusted=True, blocks=blocks)
print(np.mean(LLs2n))
#%%
plt.hist(LLs2n)
plt.xlabel('Nats/Spike')
plt.show()
plt.savefig(figdir + '/' + exname + 'testLLraw.pdf', format='pdf')

#%%
DU.plot_filters(side2b)
# %% convert model to be convolutional (make spatial expand fast!)

base_mod = side2b.copy_model()

# old network list
netlist_old = deepcopy(base_mod.network_list)[0]

use_tlayer = True

core_net = 1
side_net = 2

pre_net = deepcopy(base_mod.network_list[0])
pre_net['input_dims'] = [1, NX, NY]