def test_dist(self): model = CategoricalGRUModel(output_dim=1, hidden_dim=1) step_hidden_var = tf.compat.v1.placeholder(shape=(self.batch_size, 1), name='step_hidden', dtype=tf.float32) dist = model.build(self.input_var, self.step_input_var, step_hidden_var).dist assert isinstance(dist, tfp.distributions.OneHotCategorical)
def test_is_pickleable(self): model = CategoricalGRUModel(output_dim=1, hidden_dim=1) step_hidden_var = tf.compat.v1.placeholder(shape=(self.batch_size, 1), name='step_hidden', dtype=tf.float32) network = model.build(self.input_var, self.step_input_var, step_hidden_var) dist = network.dist # assign bias to all one with tf.compat.v1.variable_scope('CategoricalGRUModel/gru', reuse=True): init_hidden = tf.compat.v1.get_variable('initial_hidden') init_hidden.load(tf.ones_like(init_hidden).eval()) hidden = np.zeros((self.batch_size, 1)) outputs1 = self.sess.run(dist.probs, feed_dict={self.input_var: self.obs_inputs}) output1 = self.sess.run( [network.step_output, network.step_hidden], # yapf: disable feed_dict={ self.step_input_var: self.obs_input, step_hidden_var: hidden }) # yapf: enable h = pickle.dumps(model) with tf.compat.v1.Session(graph=tf.Graph()) as sess: input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, 5)) model_pickled = pickle.loads(h) input_var = tf.compat.v1.placeholder(tf.float32, shape=(None, None, self.feature_shape), name='input') step_input_var = tf.compat.v1.placeholder( tf.float32, shape=(None, self.feature_shape), name='input') step_hidden_var = tf.compat.v1.placeholder(shape=(self.batch_size, 1), name='initial_hidden', dtype=tf.float32) network2 = model_pickled.build(input_var, step_input_var, step_hidden_var) dist2 = network2.dist outputs2 = sess.run(dist2.probs, feed_dict={input_var: self.obs_inputs}) output2 = sess.run( [network2.step_output, network2.step_hidden], # yapf: disable feed_dict={ step_input_var: self.obs_input, step_hidden_var: hidden }) # yapf: enable assert np.array_equal(outputs1, outputs2) assert np.array_equal(output1, output2)
def test_output_nonlinearity(self): model = CategoricalGRUModel(output_dim=1, hidden_dim=4, output_nonlinearity=lambda x: x / 2) obs_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, None, 1)) step_obs_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, 1)) step_hidden_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, 4)) obs = np.ones((1, 1, 1)) dist, _, _, _ = model.build(obs_ph, step_obs_ph, step_hidden_ph) probs = tf.compat.v1.get_default_session().run(dist.probs, feed_dict={obs_ph: obs}) assert probs == [0.5]
def test_output_normalized(self, output_dim): model = CategoricalGRUModel(output_dim=output_dim, hidden_dim=4) obs_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, None, output_dim)) step_obs_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, output_dim)) step_hidden_ph = tf.compat.v1.placeholder(tf.float32, shape=(None, 4)) obs = np.ones((1, 1, output_dim)) dist = model.build(obs_ph, step_obs_ph, step_hidden_ph).dist probs = tf.compat.v1.get_default_session().run(tf.reduce_sum( dist.probs), feed_dict={obs_ph: obs}) assert np.isclose(probs, 1.0)
def __init__(self, env_spec, name='CategoricalGRUPolicy', hidden_dim=32, hidden_nonlinearity=tf.nn.tanh, hidden_w_init=tf.initializers.glorot_uniform(), hidden_b_init=tf.zeros_initializer(), recurrent_nonlinearity=tf.nn.sigmoid, recurrent_w_init=tf.initializers.glorot_uniform(), output_nonlinearity=tf.nn.softmax, output_w_init=tf.initializers.glorot_uniform(), output_b_init=tf.zeros_initializer(), hidden_state_init=tf.zeros_initializer(), hidden_state_init_trainable=False, state_include_action=True, layer_normalization=False): if not isinstance(env_spec.action_space, akro.Discrete): raise ValueError('CategoricalGRUPolicy only works' 'with akro.Discrete action space.') super().__init__(name, env_spec) self._obs_dim = env_spec.observation_space.flat_dim self._action_dim = env_spec.action_space.n self._hidden_dim = hidden_dim self._hidden_nonlinearity = hidden_nonlinearity self._hidden_w_init = hidden_w_init self._hidden_b_init = hidden_b_init self._recurrent_nonlinearity = recurrent_nonlinearity self._recurrent_w_init = recurrent_w_init self._output_nonlinearity = output_nonlinearity self._output_w_init = output_w_init self._output_b_init = output_b_init self._hidden_state_init = hidden_state_init self._hidden_state_init_trainable = hidden_state_init_trainable self._layer_normalization = layer_normalization self._state_include_action = state_include_action if state_include_action: self._input_dim = self._obs_dim + self._action_dim else: self._input_dim = self._obs_dim self._f_step_prob = None self.model = CategoricalGRUModel( output_dim=self._action_dim, hidden_dim=self._hidden_dim, name='prob_network', hidden_nonlinearity=hidden_nonlinearity, hidden_w_init=hidden_w_init, hidden_b_init=hidden_b_init, recurrent_nonlinearity=recurrent_nonlinearity, recurrent_w_init=recurrent_w_init, hidden_state_init=hidden_state_init, hidden_state_init_trainable=hidden_state_init_trainable, output_nonlinearity=output_nonlinearity, output_w_init=output_w_init, output_b_init=output_b_init, layer_normalization=layer_normalization) self._prev_actions = None self._prev_hiddens = None