def eval_log_density(self, hsamp, buffers=(0, 1), for_eval=False): batch_size = self.batch_size n_samples = hsamp.shape[0] // batch_size if for_eval: batch_size = hsamp.shape[0] n_samples = 1 sample_output = T.concatenate( (T.zeros(hsamp[:, :1].shape), hsamp[:, :-1]), axis=1) sample_output = T.reshape( sample_output, [sample_output.shape[0], sample_output.shape[1], 1]) conv_output = T.repeat(self.conv_output, n_samples, axis=0) trace_output = T.repeat(self.trace_output, n_samples, axis=0) gru_inp = T.concatenate([conv_output, trace_output], axis=2) if self.forw_backw: back_output = T.repeat(self.back_output, n_samples, axis=0) gru_inp = T.concatenate([gru_inp, back_output], axis=2) gru_inp = T.concatenate([gru_inp, sample_output], axis=2) rnn_t = ll.get_output(self.gru_layer, inputs={ self.gru_layer_inp: gru_inp, self.init_rnn_forw_layer: self.init_rnn_forw.repeat( hsamp.shape[0] // self.init_rnn_forw.shape[0], 0) }) rnn_flat = T.concatenate([gru_inp[:, :, :-2], rnn_t], axis=2) rnn_flat = rnn_flat.reshape([-1, rnn_flat.shape[-1]]) prob = self.p_layer.get_output_for(rnn_flat) prob = T.clip(prob, 0.001, 0.999) prob = prob.reshape([batch_size, n_samples, -1]) hsamp = hsamp.reshape((batch_size, n_samples, -1)) log_density = -clipped_binary_crossentropy( prob, hsamp)[:, :, buffers[0]:-buffers[1]].sum(axis=-1) if self.n_genparams and not for_eval: gen_mu = self.gen_mu_layer.get_output_for(rnn_t.dimshuffle( 1, 0, 2)).mean(0) gen_sig = self.gen_sig_layer.get_output_for( rnn_t.dimshuffle(1, 0, 2)).mean(0) p_logl = -0.5 * (T.log(2 * np.pi) + 2 * T.log(gen_sig) + ((self.P - gen_mu) / gen_sig)**2) p_logl = p_logl.reshape((batch_size, n_samples)) log_density = log_density + p_logl return log_density
def __init__(self, RecognitionParams, Input, rng, n_samples=1): ''' h = Q_phi(z|x), where phi are parameters, z is our latent class, and x are data ''' super().__init__(Input, rng, n_samples) self.NN = RecognitionParams['network'] self.p = ll.get_output(self.NN, inputs=self.Input) self.p_det = ll.get_output(self.NN, inputs=self.Input, deterministic=True) sample = srng.binomial(self.p.shape, n=1, p=self.p, dtype=theano.config.floatX) self.recfunc = theano.function([self.Input], self.p) self.detfunc = theano.function([self.Input], self.p_det) self.dualfunc = theano.function([self.Input], outputs=[self.p, sample])
def __init__(self, recognition_params, X, S, P, rng, batch_size=1): """ Factorized posterior using just the convolutional network """ super().__init__(recognition_params, X, S, P, rng, batch_size) self.NN = recognition_params['network'] self.p = ll.get_output(self.NN, inputs=self.X) sample = srng.binomial(self.p.shape, n=1, p=self.p, dtype=theano.config.floatX) self.recfunc = theano.function([self.X], outputs={ 'Probs': self.p, 'Spikes': sample })
def __init__(self, recognition_params, X, S, P, rng, batch_size=1): super().__init__(recognition_params, X, S, P, rng, batch_size) ret_dict = {} self.n_units = recognition_params['rnn_units'] self.n_convfeatures = recognition_params['n_features'] self.init_rnn_forw = theano.shared( np.zeros([1, self.n_units]).astype(config.floatX)) self.init_rnn_back = theano.shared( np.zeros([1, self.n_units]).astype(config.floatX)) n_inputs = self.n_convfeatures + 1 + 1 + self.n_units * int( self.forw_backw) self.conv_layer = recognition_params['network'] self.conv_output = ll.get_output(self.conv_layer, inputs=self.X) x_inp = recognition_params['input'] x_inp.input_var = self.X trace_layer = ll.DimshuffleLayer(x_inp, (0, 1, 'x')) self.trace_output = ll.get_output(trace_layer) gru_cell_inp = ll.InputLayer((None, n_inputs)) self.init_rnn_forw_layer = ll.InputLayer( (None, self.n_units), input_var=self.init_rnn_forw.repeat( self.X.shape[0] // self.init_rnn_forw.shape[0], 0)) self.gru_layer_inp = ll.InputLayer((None, None, n_inputs)) self.gru_cell = ll.GRUCell(gru_cell_inp, self.n_units, grad_clipping=10., name='forw_rnn', hid_init=self.init_rnn_forw_layer) self.gru_layer = ll.RecurrentContainerLayer( {gru_cell_inp: self.gru_layer_inp}, self.gru_cell['output']) self.p_layer = ll.DenseLayer( (None, self.n_units + n_inputs - 2), 1, nonlinearity=lasagne.nonlinearities.sigmoid, b=lasagne.init.Constant(-5.), name='dense_output') hid_0 = self.init_rnn_forw.repeat( self.X.shape[0] // self.init_rnn_forw.shape[0], 0) samp_0 = T.zeros([self.X.shape[0], 1]) scan_inp = T.concatenate([self.conv_output, self.trace_output], axis=2) if self.forw_backw: inp_back = ll.ConcatLayer([self.conv_layer, trace_layer], axis=2) init_rnn_back_layer = ll.InputLayer( (None, self.n_units), input_var=self.init_rnn_back.repeat( self.X.shape[0] // self.init_rnn_back.shape[0], 0)) self.back_layer = ll.GRULayer(inp_back, self.n_units, backwards=True, name='back_rnn', hid_init=init_rnn_back_layer) self.back_output = ll.get_output(self.back_layer) scan_inp = T.concatenate([scan_inp, self.back_output], axis=2) def sample_step(scan_inp_, hid_tm1, samp_tm1): cell_in = T.concatenate([scan_inp_, samp_tm1], axis=1) rnn_t_ = self.gru_cell.get_output_for({ 'input': cell_in, 'output': hid_tm1 }) prob_in = T.concatenate([cell_in[:, :-2], rnn_t_['output']], axis=1) prob_t = self.p_layer.get_output_for(prob_in) samp_t = srng.binomial(prob_t.shape, n=1, p=prob_t, dtype=theano.config.floatX) return rnn_t_['output'], samp_t, prob_t ((rnn_t, s_t, p_t), updates) = \ theano.scan(fn=sample_step, sequences=[scan_inp.dimshuffle(1, 0, 2)], # Scan iterates over first dimension, so we have to put time in front. outputs_info = [hid_0, samp_0, None, ]) ret_dict['Probs'] = p_t[:, :, 0].T ret_dict['Spikes'] = s_t[:, :, 0].T if self.n_genparams: self.gen_mu_layer = ll.DenseLayer( (None, None, self.n_units), self.n_genparams, nonlinearity=lasagne.nonlinearities.linear, W=lasagne.init.Normal(std=0.01, mean=0.0), num_leading_axes=2, name='dense_gen_mu') self.gen_sig_layer = ll.DenseLayer( (None, None, self.n_units), self.n_genparams, nonlinearity=lasagne.nonlinearities.softplus, W=lasagne.init.Normal(std=0.01, mean=-0.0), b=lasagne.init.Constant(-2.), num_leading_axes=2, name='dense_gen_sig') ret_dict['Gen_mu'] = self.gen_mu_layer.get_output_for(rnn_t).mean( 0) ret_dict['Gen_sig'] = self.gen_sig_layer.get_output_for( rnn_t).mean(0) self.recfunc = theano.function([self.X], outputs=ret_dict, updates=updates, on_unused_input='ignore')
def __init__(self, RecognitionParams, Input, rng, n_samples=1): ''' h = Q_phi(z|x), where phi are parameters, z is our latent class, and x are data ''' super().__init__(Input, rng, n_samples) self.n_units = RecognitionParams['rnn_units'] self.n_convfeatures = RecognitionParams['n_features'] self.conv_back = RecognitionParams['network'] conv_cell = RecognitionParams['network'] conv_cell = ll.DimshuffleLayer(conv_cell, (1, 0, 2)) self.conv_cell = ll.get_output(conv_cell, inputs=self.Input) inp_cell = RecognitionParams['input'] inp_cell = ll.DimshuffleLayer(inp_cell, (1, 0, 'x')) self.inp_cell = ll.get_output(inp_cell, inputs=self.Input) inp_back = RecognitionParams['input'] inp_back = ll.DimshuffleLayer(inp_back, (0, 1, 'x')) inp_back = ll.ConcatLayer([self.conv_back, inp_back], axis=2) cell_inp = ll.InputLayer( (None, self.n_convfeatures + self.n_units + 1 + 1 + 1)) self.cell = rec.GRUCell(cell_inp, self.n_units, grad_clipping=100.) self.p_out = ll.DenseLayer((None, self.n_units + self.n_convfeatures), 1, nonlinearity=lasagne.nonlinearities.sigmoid, b=lasagne.init.Constant(-3.)) hid_0 = T.zeros([self.Input.shape[0], self.n_units]) samp_0 = T.zeros([self.Input.shape[0], 1]) self.back_nn = rec.GRULayer(inp_back, self.n_units, backwards=True) self.back_nn = ll.DimshuffleLayer(self.back_nn, (1, 0, 2)) self.backward = ll.get_output(self.back_nn, inputs=self.Input) def sampleStep(conv_cell, inp_cell, back, hid_tm1, samp_tm1, prob_tm1): cell_in = T.concatenate( [conv_cell, inp_cell, back, samp_tm1, prob_tm1], axis=1) rnn_t = self.cell.get_output_for({ 'input': cell_in, 'output': hid_tm1 }) prob_in = T.concatenate([conv_cell, rnn_t['output']], axis=1) prob_t = self.p_out.get_output_for(prob_in) samp_t = srng.binomial(prob_t.shape, n=1, p=prob_t, dtype=theano.config.floatX) return rnn_t['output'], samp_t, prob_t ((rnn_temp,s_t, p_t), updates) =\ theano.scan(fn=sampleStep, sequences=[self.conv_cell,self.inp_cell, self.backward], # outputs_info=[T.unbroadcast(hid_0,1), T.unbroadcast(samp_0,1), T.unbroadcast(samp_0,1)]) outputs_info=[hid_0, samp_0, samp_0]) for k, v in updates.items(): k.default_update = v self.recfunc = theano.function([self.Input], outputs=p_t[:, :, 0].T, updates=updates) self.samplefunc = theano.function([self.Input], outputs=s_t[:, :, 0].T, updates=updates) self.dualfunc = theano.function( [self.Input], outputs=[p_t[:, :, 0].T, s_t[:, :, 0].T], updates=updates) self.detfunc = self.recfunc