def _sample(self, num_samples): sigma, mu = self.natural_to_regular(self.regular_to_natural(self.get_parameters('regular'))) L = T.cholesky(sigma) sample_shape = T.concat([[num_samples], T.shape(mu)], 0) noise = T.random_normal(sample_shape) L = T.tile(L[None], T.concat([[num_samples], T.ones([T.rank(sigma)], dtype=np.int32)])) return mu[None] + T.matmul(L, noise[..., None])[..., 0]
def __init__(self, sensor_models, calibration_model, lr=1e-4, batch_size=20, log_dir=None, **kwargs): self.graph = T.core.Graph() self.log_dir = log_dir with self.graph.as_default(): self.calibration_model = calibration_model self.board_ids = list(sensor_models.keys()) self.board_map = {b: i for i, b in enumerate(self.board_ids)} self.sensor_map = sensor_models self.sensor_models = [ sensor_models[board_id] for board_id in self.board_ids ] self.architecture = pickle.dumps( [sensor_models, calibration_model]) self.batch_size = batch_size self.lr = lr self.learning_rate = T.placeholder(T.floatx(), []) self.sensors = T.placeholder(T.floatx(), [None, 3]) self.env = T.placeholder(T.floatx(), [None, 3]) self.board = T.placeholder(T.core.int32, [None]) self.boards = T.transpose( T.pack([self.board, T.range(T.shape(self.board)[0])])) self.rep = T.gather_nd( T.pack([ sensor_model(self.sensors) for sensor_model in self.sensor_models ]), self.boards) self.rep_ = T.placeholder(T.floatx(), [None, self.rep.get_shape()[-1]]) rep_env = T.concat([self.rep, self.env], -1) rep_env_ = T.concat([self.rep_, self.env], -1) self.y_ = self.calibration_model(rep_env) self.y_rep = self.calibration_model(rep_env_) self.y = T.placeholder(T.floatx(), [None, 2]) self.loss = T.mean((self.y - self.y_)**2) self.mae = T.mean(T.abs(self.y - self.y_)) T.core.summary.scalar('MSE', self.loss) T.core.summary.scalar('MAE', self.mae) self.summary = T.core.summary.merge_all() self.train_op = T.core.train.AdamOptimizer( self.learning_rate).minimize(self.loss) self.session = T.interactive_session(graph=self.graph)
score_ = cf.score(X, Y) q_w = make_variable( Gaussian([T.to_float(np.eye(D))[None], T.to_float(np.zeros(D))[None]])) x, y = T.matrix(), T.vector() lr = 1e-4 batch_size = T.shape(x)[0] num_batches = T.to_float(N / batch_size) with T.initialization('xavier'): # stats_net = Relu(D + 1, 20) >> Relu(20) >> GaussianLayer(D) stats_net = GaussianLayer(D + 1, D) net_out = stats_net(T.concat([x, y[..., None]], -1)) stats = T.sum(net_out.get_parameters('natural'), 0)[None] natural_gradient = (p_w.get_parameters('natural') + num_batches * stats - q_w.get_parameters('natural')) / N next_w = Gaussian(q_w.get_parameters('natural') + lr * natural_gradient, parameter_type='natural') l_w = kl_divergence(q_w, p_w)[0] p_y = Bernoulli(T.sigmoid(T.einsum('jw,iw->ij', next_w.expected_value(), x))) l_y = T.sum(p_y.log_likelihood(y[..., None])) elbo = l_w + l_y nat_op = T.assign(q_w.get_parameters('natural'), next_w.get_parameters('natural'))
def _sample(self, num_samples): shape = self.shape() sample_shape = T.concat([[num_samples], shape], 0) random_sample = T.random_uniform(sample_shape) m, b = Stats.X(self.m), Stats.X(self.b) return m[None] - b[None] * T.log(-T.log(random_sample))
def shape(self): return T.concat([[self.num], self.tensor.shape()], 0)