def test_calculate(self): n = BioNeuronTf(init_p=p) nn = BioNeuronTf(init_p=[p for _ in range(8)]) i = np.array([2., 3., 0.]) ii = np.array([[2., 2.], [3., 3.], [0., 0.]]) x = n.calculate(i) self.assertEqual(n._init_state.shape[0], x.shape[1]) self.assertEqual(x.shape[0], len(i)) x = n.calculate(ii) self.assertEqual(ii.shape[1], x.shape[2]) self.assertEqual(x.shape[0], ii.shape[0]) # same time self.assertEqual(x.shape[1], n._init_state.shape[0]) self.assertEqual(x.shape[2], ii.shape[1]) # same nb of batch x = nn.calculate(i) #several neurons, one batch self.assertEqual(x.shape[-1], nn.num) self.assertEqual(x.shape[0], len(i)) self.assertEqual(x.shape[1], nn._init_state.shape[0]) xx2 = nn.calculate(ii) #several neurons, several batches xx = nn.calculate( np.stack([ii for _ in range(8)], axis=ii.ndim)) # several neurons, several batches self.assertEqual(xx.shape[-1], nn.num) self.assertEqual(xx.shape[0], ii.shape[0]) # same time self.assertEqual(xx.shape[1], nn._init_state.shape[0]) self.assertEqual(xx.shape[2], ii.shape[1]) # same nb of batch self.assertEqual(xx.all(), xx2.all())
def test_build_graph(self): n = BioNeuronTf(init_p=p) nn = BioNeuronTf(init_p=[p for _ in range(8)]) i, res = n.build_graph() self.assertEqual(i.get_shape().as_list(), [None]) i, res = n.build_graph(3) self.assertEqual(i.get_shape().as_list(), [None, None]) i, res = n.build_graph(batch=1) self.assertEqual(i.get_shape().as_list(), [None, None]) i, res = nn.build_graph() self.assertEqual(i.get_shape().as_list(), [None, 8]) i, res = nn.build_graph(1) self.assertEqual(i.get_shape().as_list(), [None, None, 8]) i, res = nn.build_graph(batch=4) self.assertEqual(i.get_shape().as_list(), [None, None, 8])
def test_init(self): hh = BioNeuronTf(init_p=[p for _ in range(10)]) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 10) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (10, )) self.assertEqual(hh.init_params.keys(), p.keys()) self.assertEqual(hh.parameter_names, list(hh.default_params.keys())) hh = BioNeuronTf( init_p={var: [val for _ in range(10)] for var, val in p.items()}) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 10) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (10, )) self.assertEqual(hh.init_params.keys(), p.keys()) self.assertEqual(hh.parameter_names, list(hh.default_params.keys())) hh = BioNeuronTf(init_p=[PyBioNeuron.get_random() for _ in range(13)]) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 13) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (13, )) self.assertEqual(hh.init_params.keys(), p.keys()) self.assertEqual(hh.parameter_names, list(hh.default_params.keys())) hh = BioNeuronTf(p) self.assertEqual(hh.num, 1) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), )) self.assertIsInstance(hh.init_params, dict) self.assertEqual(hh.init_params, p) self.assertEqual(hh.parameter_names, list(hh.default_params.keys())) hh = BioNeuronTf(n_rand=15) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 15) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (15, )) self.assertEqual(hh.parameter_names, list(hh.default_params.keys()))
def test_parallelize(self): n = BioNeuronTf(init_p=p) sh = list(n._init_state.shape) n.parallelize(10) sh.append(10) shp = (10, ) self.assertEqual(n._init_state.shape, tuple(sh)) self.assertEqual(list(n.init_params.values())[0].shape, tuple(shp)) n = BioNeuronTf(init_p=[p for _ in range(8)]) sh = list(n._init_state.shape) shp = list(list(n.init_params.values())[0].shape) n.parallelize(11) sh.append(11) shp.append(11) self.assertEqual(n._init_state.shape, tuple(sh)) self.assertEqual(list(n.init_params.values())[0].shape, tuple(shp))
def test_optimize(self): print('LSTM'.center(40, '#')) n = NeuronLSTM(dt=dt) opt = NeuronOpt(neuron=n) self.assertEqual(opt._parallel, 1) w = [1 for _ in range(len(train[-1]))] n = opt.optimize(dir, w=w, train=train, epochs=1, plot=plot) print('LSTM, calcium None'.center(40, '#')) train2 = train train2[-1][-1] = None n = opt.optimize(dir, w=w, train=train, epochs=1, plot=plot) optim.get_model(dir) optim.get_vars_all(dir) t, tt = optim.get_data(dir) self.assertEqual(t[0].all(), train[0].all()) self.assertEqual(t[1].all(), train[1].all()) for i, tt in enumerate(t[-1]): if tt is not None: self.assertEqual(tt.all(), train[-1][i].all()) print('One neuron'.center(40, '#')) opt = NeuronOpt(nr) self.assertEqual(opt._parallel, 1) n = opt.optimize(dir, w=w, train=train, epochs=1, plot=plot) print('One neuron reload'.center(40, '#')) n = opt.optimize(dir, w=w, reload=True, train=train, epochs=1, plot=plot) print('One neuron with test'.center(40, '#')) n = opt.optimize(dir, w=w, train=train, test=train, epochs=1, plot=plot) print('Parallel'.center(40, '#')) pars = [PyBioNeuron.get_random() for _ in range(2)] opt = NeuronOpt(BioNeuronTf(init_p=pars, dt=dt)) self.assertEqual(opt._parallel, 2) n = opt.optimize(dir, w=w, train=train, epochs=1, plot=plot) self.assertEqual(opt._loss.shape[0], opt._parallel)
def test_pickle(self): hh1 = BioNeuronTf(init_p=[p for _ in range(10)]) with open(self.dir + 'yeee', 'wb') as f: pickle.dump(hh1, f) with open(self.dir + 'yeee', 'rb') as f: hh = pickle.load(f) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 10) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (10, )) self.assertEqual(hh.init_params.keys(), p.keys()) self.assertEqual(hh1.parameter_names, hh.parameter_names) hh = BioNeuronTf( init_p={var: [val for _ in range(10)] for var, val in p.items()}) with open(self.dir + 'yeee', 'wb') as f: pickle.dump(hh, f) with open(self.dir + 'yeee', 'rb') as f: hh = pickle.load(f) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 10) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (10, )) self.assertEqual(hh.init_params.keys(), p.keys()) hh = BioNeuronTf(init_p=[PyBioNeuron.get_random() for _ in range(13)]) with open(self.dir + 'yeee', 'wb') as f: pickle.dump(hh, f) with open(self.dir + 'yeee', 'rb') as f: hh = pickle.load(f) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 13) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (13, )) self.assertEqual(hh.init_params.keys(), p.keys()) hh = BioNeuronTf(n_rand=15) with open(self.dir + 'yeee', 'wb') as f: pickle.dump(hh, f) with open(self.dir + 'yeee', 'rb') as f: hh = pickle.load(f) self.assertEqual(len(hh._init_state), len(hh.default_init_state)) self.assertEqual(hh.num, 15) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), hh.num)) self.assertIsInstance(hh.init_params, dict) self.assertEqual(list(hh.init_params.values())[0].shape, (15, )) hh = BioNeuronTf(p) with open(self.dir + 'yeee', 'wb') as f: pickle.dump(hh, f) with open(self.dir + 'yeee', 'rb') as f: hh = pickle.load(f) self.assertEqual(hh.num, 1) self.assertEqual(hh._init_state.shape, (len(hh.default_init_state), )) self.assertIsInstance(hh.init_params, dict) self.assertEqual(hh.init_params, p)
def test_init_groups(self): hh = BioNeuronTf(n_rand=15, groups=np.zeros(15, dtype=np.int32)) hh.reset() sess = tf.Session() sess.run(tf.global_variables_initializer()) pars = sess.run(hh._param['C_m']) for i in range(1, 15): self.assertEqual(pars[i - 1], pars[i]) hh = BioNeuronTf(n_rand=6, groups=[1, 0, 0, 0, 2, 2]) hh = BioNeuronTf(n_rand=3, groups=[1, 0, 0, 0, 2, 2]) self.assertEqual(hh.parameter_names, list(hh.default_params.keys())) hh.reset() loss = tf.square(hh._param['C_m'] - [1., 20., 80., -10., -7., 90.]) train = tf.train.AdamOptimizer(0.1).minimize(loss) sess = tf.Session() sess.run(tf.global_variables_initializer()) pars = sess.run(hh._param['C_m']) self.assertEqual(pars[1], pars[2]) self.assertEqual(pars[1], pars[3]) self.assertEqual(pars[4], pars[5]) sess.run(train) pars = sess.run(hh._param['C_m']) self.assertEqual(pars[1], pars[2]) self.assertEqual(pars[1], pars[3]) self.assertEqual(pars[4], pars[5]) with self.assertRaises(ValueError): hh = BioNeuronTf(n_rand=2, groups=[1, 0, 0, 0, 2, 2])
from odynn import utils, datas from odynn.neuron import NeuronLSTM, BioNeuronTf, PyBioNeuron from odynn.noptim import NeuronOpt from odynn.nsimul import simul from odynn import optim import tensorflow as tf import numpy as np dir = utils.set_dir('unittest') dt = 0.5 t, i = datas.give_train(dt=dt, max_t=5.) default = PyBioNeuron.default_params pars = PyBioNeuron.get_random() train = simul(p=default, dt=dt, i_inj=i, show=False, suffix='train') plot = False nr = BioNeuronTf(init_p=pars, dt=dt) class TestNeuronOpt(TestCase): def test_init(self): with self.assertRaises(TypeError): no = NeuronOpt(5) def test_loss(self): co = NeuronOpt(nr) res = tf.zeros((len(t), len(PyBioNeuron.default_init_state), 3)) ys_ = [ tf.placeholder(shape=(len(t), 3), dtype=tf.float32, name="test") ] + [ tf.placeholder(shape=(len(t), 3), dtype=tf.float32, name="test") for _ in PyBioNeuron.ions.items()