Exemplo n.º 1
0
def with_LSTM():
    dir = utils.set_dir('withLSTM')
    conns = {(0, 1): circuit.SYNAPSE}
    conns_opt = {(0, 1): circuit.get_syn_rand(True)}

    dt = 0.5
    t, i = datas.give_train(dt=dt)
    i_1 = np.zeros(i.shape)
    i_injs = np.stack([i, i_1], axis=2)
    train = sim.simul(t, i_injs, [p, p], conns, n_out=[0, 1], show=False)

    neurons = nr.Neurons([nr.BioNeuronTf(PyBioNeuron.get_random(), fixed=[], dt=dt), nr.BioNeuronTf(p, fixed='all', dt=dt)])
    c = CircuitTf(neurons=neurons, synapses=conns_opt)
    co = CircuitOpt(circuit=c)
    co.optimize(dir, train=train, n_out=[0, 1], l_rate=(0.01, 9, 0.95))
Exemplo n.º 2
0
    def test_init(self):
        dt = 0.5
        p = nr.PyBioNeuron.get_random()
        neurons = nr.Neurons([
            nr.BioNeuronTf(init_p=[p for _ in range(2)], dt=dt),
            nr.NeuronLSTM(dt=dt)
        ])
        self.assertEqual(neurons.num, 3)
        self.assertEqual(
            neurons.init_state.all(),
            np.stack([nr.PyBioNeuron.default_init_state for _ in range(3)],
                     axis=1).all())

        with self.assertRaises(AttributeError):
            neurons = nr.Neurons([
                nr.BioNeuronTf(init_p=[p for _ in range(2)], dt=0.1),
                nr.NeuronLSTM(dt=0.2)
            ])
Exemplo n.º 3
0
def run():
    name = sys.argv[1]

    # plt.rcParams['figure.facecolor'] = 'Indigo'

    # c = circuit.CircuitFix([p for _ in range(9)], synapses=syns, gaps=gaps, labels=labels)
    # c.plot()

    t, i = datas.full4(dt=dt, nb_neuron_zero=5)
    i[:, :, :] = i[:, :, [0, 1, 4, 5, 6, 7, 8, 2, 3]]
    _, itest = datas.full4_test(dt=dt, nb_neuron_zero=5)
    itest[:, :, :] = itest[:, :, [0, 1, 4, 5, 6, 7, 8, 2, 3]]

    dir = utils.set_dir('Tapwith_' + name)
    n_out = [4, 5]
    train = sim.simul(pars=[p for _ in range(9)],
                      t=t,
                      i_injs=i,
                      synapses=syns,
                      gaps=gaps,
                      n_out=n_out,
                      suffix='train',
                      labels=labels)
    test = sim.simul(pars=[p for _ in range(9)],
                     t=t,
                     i_injs=itest,
                     synapses=syns,
                     gaps=gaps,
                     n_out=n_out,
                     labels=labels,
                     suffix='test')

    # n = nr.BioNeuronTf([rand() for _ in range(9)], dt=dt)
    n = nr.Neurons([
        nr.NeuronLSTM(dt=dt),
        nr.NeuronLSTM(dt=dt),
        nr.BioNeuronTf([p for _ in range(5)], dt=dt),
        nr.NeuronLSTM(dt=dt),
        nr.NeuronLSTM(dt=dt)
    ])
    ctf = CircuitTf(n,
                    synapses=syns_opt[0],
                    gaps=gaps_opt[0],
                    labels=labels,
                    commands={4, 5},
                    sensors={0, 1, 7, 8})
    ctf.plot(save=True)
    exit()
    # ctf = CircuitTf.create_random(neurons = n, n_neuron=9, dt=dt, syn_keys={k: v['E']>-60 for k,v in syns.items()}, gap_keys=gaps.keys(), labels=labels, commands={4, 5},
    #                 sensors={0, 1, 7, 8}, n_rand=n_parallel)
    copt = CircuitOpt(circuit=ctf)
    copt.optimize(subdir=dir,
                  train=train,
                  test=test,
                  n_out=[4, 5],
                  l_rate=(0.1, 9, 0.95))
Exemplo n.º 4
0
def test(nb_neuron, conns, conns_opt, dir, t, i_injs, n_out=[1]):
    pars = [p for _ in range(nb_neuron)]
    dir = utils.set_dir(dir)
    print("Feed with current of shape : ", i_injs.shape)

    # train = sim.simul(t, i_injs, pars, conns, n_out=n_out, show=False)
    n = nr.BioNeuronTf(init_p=pars, dt=t[1]-t[0])
    cr = CircuitTf(n, synapses=conns_opt)
    cr.plot(save=True);exit()
    c = CircuitOpt(cr)
    c.optimize(dir, n_out=n_out, train=train)
Exemplo n.º 5
0
 def test_settings(self):
     pars = [p for _ in range(n_neuron)]
     conns_opt = {
         (0, 1): odynn.circuit.get_syn_rand(False),
         (1, 0): odynn.circuit.get_syn_rand(False)
     }
     co = CircuitOpt(
         cr.CircuitTf(nr.BioNeuronTf(pars, dt=dt), synapses=conns_opt))
     train = [np.zeros(2), np.zeros(2), [None, None, np.zeros(4)]]
     co.l_rate = (0.1, 0.1, 0.1)
     co.n_batch = 3
     w = (1., 0)
     co.settings(w, train)
Exemplo n.º 6
0
    def test_init(self):

        class opt(optim.Optimizer):
            def _build_loss(self, w):
                pass

        nr = neuron.BioNeuronTf(n_rand=3)
        op = opt(nr)
        self.assertEqual(op._parallel, nr.num)
        self.assertEqual(nr.num, 3)

        c = circuit.CircuitTf.create_random(2, syn_keys={(0,1):True}, n_rand=7)
        op = opt(c)
        self.assertEqual(op._parallel, c.num)
        self.assertEqual(c.num, 7)
Exemplo n.º 7
0
 def test_loss(self):
     pars = [p for _ in range(n_neuron)]
     conns_opt = {
         (0, 1): odynn.circuit.get_syn_rand(False),
         (1, 0): odynn.circuit.get_syn_rand(False)
     }
     co = CircuitOpt(
         cr.CircuitTf(nr.BioNeuronTf(pars, dt=dt), synapses=conns_opt))
     res = tf.zeros(
         (len(t), len(nr.PyBioNeuron.default_init_state), 3, n_neuron))
     ys_ = [
         tf.placeholder(
             shape=(len(t), 3, n_neuron), dtype=tf.float32, name="test")
     ] + [
         tf.placeholder(
             shape=(len(t), 3, n_neuron), dtype=tf.float32, name="test")
         for _ in nr.PyBioNeuron.ions.items()
     ]
     w = [1] + [1 for _ in nr.PyBioNeuron.ions.items()]
     co.n_out = list(range(n_neuron))
     co._build_loss(res, ys_, w)
Exemplo n.º 8
0
import numpy as np
from odynn import utils, nsimul, neuron, noptim
#This file defines the model we will use
from odynn.models import cfg_model

dt = 1.
folder = 'Example'

# Function to call to set the target directories for plots and saved files
dir = utils.set_dir(folder)

#Definition of time and 2 input currents
t = np.arange(0., 1200., dt)
i_inj1 = 10. * ((t > 200) & (t < 600)) + 20. * ((t > 800) & (t < 1000))
i_inj2 = 5. * ((t > 200) & (t < 300)) + 30. * ((t > 500) & (t < 1000))
i_injs = np.stack([i_inj1, i_inj2], axis=-1)

#10 random initial parameters
params = [cfg_model.NEURON_MODEL.get_random() for _ in range(10)]
neuron = neuron.BioNeuronTf(params, dt=dt)

#This function will take the default parameters of the used model if none is given
train = nsimul.simul(t=t, i_inj=i_injs, show=True)

#Optimization
optimizer = noptim.NeuronOpt(neuron)
optimizer.optimize(dir=dir, train=train)
Exemplo n.º 9
0
    def test_opt_circuits(self):
        print('one target'.center(40, '#'))
        n_out = [1]
        train = csim.simul(t=t,
                           i_injs=i_injs,
                           pars=pars,
                           synapses=conns,
                           n_out=n_out,
                           show=False)
        co = CircuitOpt(
            cr.CircuitTf(nr.BioNeuronTf(pars, dt=dt), synapses=conns_opt))
        co.optimize(dir, n_out=n_out, train=train, epochs=1, plot=plot)
        optim.get_best_result(dir)

        self.assertEqual(co._loss.shape, ())

        print('one target parallel'.center(40, '#'))
        co = CircuitOpt(
            cr.CircuitTf(nr.BioNeuronTf(n_rand=n_neuron, dt=dt),
                         synapses=conns_opt_parallel))
        co.optimize(dir,
                    n_out=n_out,
                    train=train,
                    test=train,
                    epochs=1,
                    plot=plot,
                    evol_var=evol_var)
        self.assertEqual(co._loss.shape, (10, ))

        print('several targets'.center(40, '#'))
        n_out = [0, 1]
        train = csim.simul(t=t,
                           i_injs=i_injs,
                           pars=pars,
                           synapses=conns,
                           n_out=n_out,
                           show=False)
        co = CircuitOpt(
            cr.CircuitTf(nr.BioNeuronTf(pars, dt=dt), synapses=conns_opt))
        co.optimize(dir,
                    n_out=n_out,
                    train=train,
                    epochs=1,
                    w_n=(1, 2),
                    plot=plot,
                    evol_var=evol_var)
        self.assertEqual(co._loss.shape, ())

        print('several targets parallel'.center(40, '#'))
        co = CircuitOpt(
            cr.CircuitTf.create_random(
                n_neuron=2, syn_keys={k: False
                                      for k in conns.keys()}, dt=dt))
        co.optimize(dir,
                    n_out=n_out,
                    train=train,
                    epochs=1,
                    w_n=(1., 0.2),
                    plot=plot,
                    evol_var=evol_var)
        self.assertEqual(co._loss.shape, (10, ))

        print('1 LSTM'.center(40, '#'))
        neurons = nr.Neurons(
            [nr.NeuronLSTM(dt=dt),
             nr.BioNeuronTf(p, fixed='all', dt=dt)])
        c = cr.CircuitTf(neurons=neurons, synapses=conns_opt)
        co = CircuitOpt(circuit=c)
        co.optimize(dir,
                    train=train,
                    n_out=[0, 1],
                    l_rate=(0.01, 9, 0.95),
                    epochs=1,
                    plot=plot,
                    evol_var=evol_var)

        print('2 LSTM'.center(40, '#'))
        neurons = nr.Neurons([nr.NeuronLSTM(dt=dt), nr.NeuronLSTM(dt=dt)])
        c = cr.CircuitTf(neurons=neurons, synapses=conns_opt)
        co = CircuitOpt(circuit=c)
        co.optimize(dir,
                    train=train,
                    n_out=[0, 1],
                    l_rate=(0.01, 9, 0.95),
                    epochs=1,
                    plot=plot,
                    evol_var=evol_var)

        conns_opt[(0, 2)] = odynn.circuit.get_syn_rand()
        with self.assertRaises(AttributeError):
            c = cr.CircuitTf(neurons=neurons, synapses=conns_opt)
            co = CircuitOpt(circuit=c)
            co.optimize(dir,
                        train=train,
                        n_out=[0, 1],
                        l_rate=(0.01, 9, 0.95),
                        epochs=1,
                        plot=plot,
                        evol_var=evol_var)

        print('2 bio + 1 LSTM'.center(40, '#'))
        neurons = nr.Neurons([
            nr.BioNeuronTf(init_p=[p for _ in range(2)], dt=dt),
            nr.NeuronLSTM(dt=dt)
        ])
        c = cr.CircuitTf(neurons=neurons, synapses=conns_opt)
        co = CircuitOpt(circuit=c)
        train[1] = i_injs3
        co.optimize(dir,
                    train=train,
                    n_out=[0, 1],
                    l_rate=(0.01, 9, 0.95),
                    epochs=1,
                    plot=plot,
                    evol_var=evol_var)