def __init__(self, layer_sizes, n_samples, alpha, learning_rate, v_prior, batch_size, X_train, y_train, N_train):
layer_sizes = copy.copy(layer_sizes)
layer_sizes[ 0 ] = layer_sizes[ 0 ] + 1
print layer_sizes
self.batch_size = batch_size
self.N_train = N_train
self.X_train = X_train
self.y_train = y_train
self.rate = learning_rate
# We create the network
self.network = network.Network(layer_sizes, n_samples, v_prior, N_train)
# index to a batch
index = T.lscalar()
self.indexes = T.vector('index', dtype = 'int32')
indexes_train = theano.shared(value = np.array(range(0, N_train), dtype = np.int32), borrow = True)
self.x = T.tensor3('x',dtype=theano.config.floatX)
self.y = T.matrix('y', dtype =theano.config.floatX)
self.lr = T.fscalar()
# The logarithm of the values for the likelihood factors
sampl = T.bscalar()
self.fwpass = theano.function(outputs=self.network.output(self.x,False,samples=sampl,use_indices=False), inputs=[self.x,sampl],allow_input_downcast=True)
ll_train = self.network.log_likelihood_values(self.x, self.y, self.indexes, 0.0, 1.0)
self.estimate_marginal_ll = (-1.0 * N_train / (self.x.shape[ 1 ] * alpha) * \
T.sum(LogSumExp(alpha * (T.sum(ll_train, 2) - self.network.log_f_hat() - self.network.log_f_hat_z()), 0)+ \
T.log(1.0 / n_samples)) - self.network.log_normalizer_q() - 1.0 * N_train / self.x.shape[ 1 ] * self.network.log_normalizer_q_z() + \
self.network.log_Z_prior())
# We create a theano function for updating q
upd = adam(self.estimate_marginal_ll, self.network.params,indexes_train[index*batch_size:(index+1)*batch_size],self.rate,rescale_local=np.float32(N_train/batch_size))
self.process_minibatch = theano.function([ index], self.estimate_marginal_ll, \
updates = upd, \
givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size] , [ n_samples, 1, 1 ]),
self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ],
self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] })
# We create a theano function for making predictions
self.error_minibatch_train = theano.function([ index ],
T.sum((T.mean(self.network.output(self.x,self.indexes), 0, keepdims = True)[ 0, :, : ] - self.y)**2) / layer_sizes[ -1 ],
givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size ], [ n_samples, 1, 1 ]),
self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ],
self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] })
self.ll_minibatch_train = theano.function([ index ], T.sum(LogSumExp(T.sum(ll_train, 2), 0) + T.log(1.0 / n_samples)), \
givens = { self.x: T.tile(self.X_train[ index * batch_size: (index + 1) * batch_size ], [ n_samples, 1, 1 ]),
self.y: self.y_train[ index * batch_size: (index + 1) * batch_size ],
self.indexes: indexes_train[ index * batch_size : (index + 1) * batch_size ] })
评论列表
文章目录