def test_activation_functions(self): # Test single activation functions (no other custom computations in layer). space = FloatBox(shape=(3, ), add_batch_rank=True) # ReLU. relu_layer = NNLayer(activation="relu") test = ComponentTest(component=relu_layer, input_spaces=dict(inputs=space)) input_ = space.sample(size=5) expected = relu(input_) test.test(("apply", input_), expected_outputs=expected) # Again manually in case util numpy-relu is broken. input_ = np.array([[1.0, 2.0, -5.0], [-10.0, -100.1, 4.5]]) expected = np.array([[1.0, 2.0, 0.0], [0.0, 0.0, 4.5]]) test.test(("apply", input_), expected_outputs=expected) # Sigmoid. sigmoid_layer = NNLayer(activation="sigmoid") test = ComponentTest(component=sigmoid_layer, input_spaces=dict(inputs=space)) input_ = space.sample(size=10) expected = sigmoid(input_) test.test(("apply", input_), expected_outputs=expected)
def test_bernoulli_action_adapter(self): # Last NN layer. previous_nn_layer_space = FloatBox(shape=(16, ), add_batch_rank=True) adapter_outputs_space = FloatBox(shape=(2, ), add_batch_rank=True) # Action Space. action_space = BoolBox(shape=(2, )) action_adapter = BernoulliDistributionAdapter( action_space=action_space, activation="relu") test = ComponentTest(component=action_adapter, input_spaces=dict( inputs=previous_nn_layer_space, adapter_outputs=adapter_outputs_space, ), action_space=action_space) action_adapter_params = test.read_variable_values( action_adapter.variable_registry) # Batch of n samples. inputs = previous_nn_layer_space.sample(32) expected_logits = relu( np.matmul( inputs, action_adapter_params[ "action-adapter/action-network/action-layer/dense/kernel"]) ) test.test(("call", inputs), expected_outputs=expected_logits, decimals=5) expected_probs = sigmoid(expected_logits) expected_log_probs = np.log(expected_probs) test.test(("get_parameters", inputs), expected_outputs=dict(adapter_outputs=expected_logits, parameters=expected_probs, probabilities=expected_probs, log_probs=expected_log_probs), decimals=5)