def test_chain_graph_single_obs(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 1 num_timesteps = 5 batch_size = 2 state_size = 1 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose([[[1.426677], [-1.789461]]], out_observations)
def test_chain_graph_single_obs(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 1 num_timesteps = 5 batch_size = 2 state_size = 1 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose( [[[1.426677], [-1.789461]]], out_observations)
def test_chain_graph_state_dims(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 1 num_timesteps = 5 batch_size = 2 state_size = 3 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose([[[1.052287, -4.560759, 3.07988], [2.008926, 0.495567, 3.488678]]], out_observations)
def test_chain_graph_state_dims(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 1 num_timesteps = 5 batch_size = 2 state_size = 3 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose( [[[1.052287, -4.560759, 3.07988], [2.008926, 0.495567, 3.488678]]], out_observations)
def test_chain_graph_multiple_obs(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 3 num_timesteps = 6 batch_size = 2 state_size = 1 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, steps_per_observation=num_timesteps / num_observations, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose( [[[0.40051451], [1.07405114]], [[1.73932898], [3.16880035]], [[-1.98377144], [2.82669163]]], out_observations)
def test_chain_graph_fixed_obs(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 3 num_timesteps = 6 batch_size = 2 state_size = 1 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, steps_per_observation=num_timesteps/num_observations, state_size=state_size, fixed_observation=4.) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose( np.ones([num_observations, batch_size, state_size]) * 4., out_observations)
def create_graph(): """Creates the dataset, model, and bound.""" xs, lengths = datasets.create_chain_graph_dataset( config.batch_size, config.num_timesteps, steps_per_observation=1, state_size=1, transition_variance=config.variance, observation_variance=config.variance) model = ghmm.TrainableGaussianHMM(config.num_timesteps, config.proposal_type, transition_variances=config.variance, emission_variances=config.variance, random_seed=config.random_seed) true_likelihood = tf.reduce_mean( model.likelihood(tf.squeeze(xs)) / tf.to_float(lengths)) outs = [true_likelihood] outs.extend(list(create_bound(model, xs, lengths))) return outs
def test_chain_graph_multiple_obs(self): with self.test_session() as sess: np.random.seed(1234) num_observations = 3 num_timesteps = 6 batch_size = 2 state_size = 1 observations, lengths = datasets.create_chain_graph_dataset( batch_size=batch_size, num_timesteps=num_timesteps, steps_per_observation=num_timesteps/num_observations, state_size=state_size) out_observations, out_lengths = sess.run([observations, lengths]) self.assertAllEqual([num_observations, num_observations], out_lengths) self.assertAllClose( [[[0.40051451], [1.07405114]], [[1.73932898], [3.16880035]], [[-1.98377144], [2.82669163]]], out_observations)
def create_graph(): """Creates the training graph.""" global_step = tf.train.get_or_create_global_step() xs, lengths = datasets.create_chain_graph_dataset( config.batch_size, config.num_timesteps, steps_per_observation=1, state_size=1, transition_variance=config.variance, observation_variance=config.variance) model = ghmm.TrainableGaussianHMM(config.num_timesteps, config.proposal_type, transition_variances=config.variance, emission_variances=config.variance, random_seed=config.random_seed) loss, bound, true_ll, gap = create_losses(model, xs, lengths) opt = tf.train.AdamOptimizer(config.learning_rate) grads = opt.compute_gradients(loss, var_list=tf.trainable_variables()) train_op = opt.apply_gradients(grads, global_step=global_step) return bound, true_ll, gap, train_op, global_step
def create_graph(): """Creates the dataset, model, and bound.""" xs, lengths = datasets.create_chain_graph_dataset( config.batch_size, config.num_timesteps, steps_per_observation=1, state_size=1, transition_variance=config.variance, observation_variance=config.variance) model = ghmm.TrainableGaussianHMM( config.num_timesteps, config.proposal_type, transition_variances=config.variance, emission_variances=config.variance, random_seed=config.random_seed) true_likelihood = tf.reduce_mean( model.likelihood(tf.squeeze(xs)) / tf.to_float(lengths)) outs = [true_likelihood] outs.extend(list(create_bound(model, xs, lengths))) return outs
def create_graph(): """Creates the training graph.""" global_step = tf.train.get_or_create_global_step() xs, lengths = datasets.create_chain_graph_dataset( config.batch_size, config.num_timesteps, steps_per_observation=1, state_size=1, transition_variance=config.variance, observation_variance=config.variance) model = ghmm.TrainableGaussianHMM( config.num_timesteps, config.proposal_type, transition_variances=config.variance, emission_variances=config.variance, random_seed=config.random_seed) loss, bound, true_ll, gap = create_losses(model, xs, lengths) opt = tf.train.AdamOptimizer(config.learning_rate) grads = opt.compute_gradients(loss, var_list=tf.trainable_variables()) train_op = opt.apply_gradients(grads, global_step=global_step) return bound, true_ll, gap, train_op, global_step
def test_chain_graph_raises_error_on_wrong_steps_per_observation(self): with self.assertRaises(ValueError): datasets.create_chain_graph_dataset(batch_size=4, num_timesteps=10, steps_per_observation=9)
def test_chain_graph_raises_error_on_wrong_steps_per_observation(self): with self.assertRaises(ValueError): datasets.create_chain_graph_dataset( batch_size=4, num_timesteps=10, steps_per_observation=9)