def test_gru(self): """Test that GRU can be invoked.""" batch_size = 10 n_hidden = 7 in_channels = 4 n_repeat = 2 n_steps = 6 in_tensor = np.random.rand(batch_size, n_steps, in_channels) with self.session() as sess: in_tensor = tf.convert_to_tensor(in_tensor, dtype=tf.float32) out_tensor = GRU(n_hidden, batch_size)(in_tensor) sess.run(tf.global_variables_initializer()) out_tensor = out_tensor.eval() assert out_tensor.shape == (batch_size, n_steps, n_hidden)
def test_recurrent_layer(self): """Test a model that includes a recurrent layer.""" batch_size = 5 tg = dc.models.TensorGraph(batch_size=batch_size, use_queue=False) features = Feature(shape=(None, 10, 1)) gru = GRU(10, batch_size, in_layers=features) loss = ReduceMean(in_layers=gru) tg.add_output(gru) tg.set_loss(loss) input = np.random.rand(batch_size, 10, 1) # If we don't specify the initial state, it should default to zeros. predictions1 = tg.predict_on_batch(input) # Explicitly specifying the zero state should give the same result. initial_state = gru.rnn_initial_states[0] zero_state = gru.rnn_zero_states[0] generator = [{features: input, initial_state: zero_state}] predictions2 = tg.predict_on_generator(generator) # Specifying a different initial state should produce a different result. generator = [{ features: input, initial_state: np.ones(zero_state.shape) }] predictions3 = tg.predict_on_generator(generator) assert np.allclose(predictions1, predictions2) assert not np.allclose(predictions1, predictions3)
def test_GRU_pickle(): tg = TensorGraph() feature = Feature(shape=(tg.batch_size, 10, 10)) layer = GRU(n_hidden=10, batch_size=tg.batch_size, in_layers=feature) tg.add_output(layer) tg.set_loss(layer) tg.build() tg.save()
def create_layers(self, state, **kwargs): reshaped = Reshape(shape=(1, -1, 10), in_layers=state) gru = GRU(n_hidden=10, batch_size=1, in_layers=reshaped) output = SoftMax( in_layers=[Reshape(in_layers=[gru], shape=(-1, env.n_actions))]) value = Variable([0.0]) return {'action_prob': output, 'value': value}