def test_dist_info(self, obs_dim, embedding_dim): env = TfEnv(DummyBoxEnv(obs_dim=obs_dim, action_dim=embedding_dim)) with mock.patch(('garage.tf.embeddings.' 'gaussian_mlp_encoder.GaussianMLPModel'), new=SimpleGaussianMLPModel): embedding_spec = InOutSpec(input_space=env.spec.observation_space, output_space=env.spec.action_space) embedding = GaussianMLPEncoder(embedding_spec) env.reset() obs, _, _, _ = env.step(1) obs_dim = env.spec.observation_space.flat_dim obs_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, obs_dim)) dist1_sym = embedding.dist_info_sym(obs_ph, name='p1_sym') # flatten output expected_mean = [np.full(np.prod(embedding_dim), 0.5)] expected_log_std = [np.full(np.prod(embedding_dim), np.log(0.5))] prob0 = embedding.dist_info(obs.flatten()) prob1 = self.sess.run(dist1_sym, feed_dict={obs_ph: [obs.flatten()]}) assert np.array_equal(prob0['mean'].flatten(), expected_mean[0]) assert np.array_equal(prob0['log_std'].flatten(), expected_log_std[0]) assert np.array_equal(prob1['mean'], expected_mean) assert np.array_equal(prob1['log_std'], expected_log_std)
def test_get_embedding(self, mock_normal, obs_dim, embedding_dim): mock_normal.return_value = 0.5 env = TfEnv(DummyBoxEnv(obs_dim=obs_dim, action_dim=embedding_dim)) with mock.patch(('garage.tf.embeddings.' 'gaussian_mlp_encoder.GaussianMLPModel'), new=SimpleGaussianMLPModel): embedding_spec = InOutSpec(input_space=env.spec.observation_space, output_space=env.spec.action_space) embedding = GaussianMLPEncoder(embedding_spec) env.reset() obs, _, _, _ = env.step(1) latent, prob = embedding.forward(obs) expected_embedding = np.full(embedding_dim, 0.75) expected_mean = np.full(embedding_dim, 0.5) expected_log_std = np.full(embedding_dim, np.log(0.5)) assert env.action_space.contains(latent) assert np.array_equal(latent, expected_embedding) assert np.array_equal(prob['mean'], expected_mean) assert np.array_equal(prob['log_std'], expected_log_std)