def test_keras_style_two_separate_input_spaces(self): # Define two input Spaces first. Independently (no container). input_space_1 = IntBox(3, add_batch_rank=True) input_space_2 = FloatBox(shape=(4,), add_batch_rank=True) # One-hot flatten the int tensor. flatten_layer_out = ReShape(flatten=True, flatten_categories=True)(input_space_1) # Run the float tensor through two dense layers. dense_1_out = DenseLayer(units=3, scope="d1")(input_space_2) dense_2_out = DenseLayer(units=5, scope="d2")(dense_1_out) # Concat everything. cat_out = ConcatLayer()(flatten_layer_out, dense_2_out) # Use the `outputs` arg to allow your network to trace back the data flow until the input space. neural_net = NeuralNetwork(inputs=[input_space_1, input_space_2], outputs=cat_out) test = ComponentTest(component=neural_net, input_spaces=dict(inputs=[input_space_1, input_space_2])) var_dict = neural_net.variable_registry w1_value = test.read_variable_values(var_dict["neural-network/d1/dense/kernel"]) b1_value = test.read_variable_values(var_dict["neural-network/d1/dense/bias"]) w2_value = test.read_variable_values(var_dict["neural-network/d2/dense/kernel"]) b2_value = test.read_variable_values(var_dict["neural-network/d2/dense/bias"]) # Batch of size=n. input_ = [input_space_1.sample(4), input_space_2.sample(4)] expected = np.concatenate([ # concat everything one_hot(input_[0]), # int flattening dense_layer(dense_layer(input_[1], w1_value, b1_value), w2_value, b2_value) # float -> 2 x dense ], axis=-1) out = test.test(("call", input_), expected_outputs=expected) test.terminate()
def test_specifiable_server(self): action_space = IntBox(2) state_space = FloatBox() env_spec = dict(type="random_env", state_space=state_space, action_space=action_space, deterministic=True) # Create the server, but don't start it yet. This will be done fully automatically by the tf-Session. specifiable_server = SpecifiableServer( Environment, env_spec, dict(step_flow=[state_space, float, bool]), "terminate") # ret are ops now in the graph. ret1 = specifiable_server.step_flow(action_space.sample()) ret2 = specifiable_server.step_flow(action_space.sample()) # Check all 3 outputs of the Env step (next state, reward, terminal). self.assertEqual(ret1[0].shape, ()) self.assertEqual(ret1[0].dtype, convert_dtype("float32")) self.assertEqual(ret1[1].shape, ()) self.assertEqual(ret1[1].dtype, convert_dtype("float32")) self.assertEqual(ret1[2].shape, ()) self.assertEqual(ret1[2].dtype, convert_dtype("bool")) self.assertEqual(ret2[0].shape, ()) self.assertEqual(ret2[0].dtype, convert_dtype("float32")) self.assertEqual(ret2[1].shape, ()) self.assertEqual(ret2[1].dtype, convert_dtype("float32")) self.assertEqual(ret2[2].shape, ()) self.assertEqual(ret2[2].dtype, convert_dtype("bool")) # Start the session and run the op, then check its actual values. with tf.train.SingularMonitoredSession( hooks=[SpecifiableServerHook()]) as sess: out1 = sess.run(ret1) out2 = sess.run(ret2) # next state self.assertAlmostEqual(out1[0], 0.7713, places=4) self.assertAlmostEqual(out2[0], 0.7488, places=4) # reward self.assertAlmostEqual(out1[1], 0.0208, places=4) self.assertAlmostEqual(out2[1], 0.4985, places=4) # terminal self.assertTrue(out1[2] is np.bool_(False)) self.assertTrue(out2[2] is np.bool_(False))