示例#1
0
    def test_normalize_dense_matrix_enum(self):
        normalization_parameters = {
            'f1':
            NormalizationParameters(identify_types.ENUM, None, None, None,
                                    None, [12.0, 4.2, 2.1]),
            'f2':
            NormalizationParameters(identify_types.CONTINUOUS, None, 0, 0, 1,
                                    None),
            'f3':
            NormalizationParameters(identify_types.ENUM, None, None, None,
                                    None, [15.1, -3.2])
        }
        features = list(normalization_parameters.keys())
        norm_net = core.Net("net")
        blobname_template = '{}_blob'
        blob_map = prepare_normalization(norm_net, normalization_parameters,
                                         features, blobname_template, False)

        inputs = np.array(
            [[12.0, 1.0, 15.1], [4.2, 2.0, -3.2], [2.1, 3.0, 15.1],
             [2.1, 3.0, normalization.MISSING_VALUE]],
            dtype=np.float32)
        normalized_outputs = normalize_dense_matrix(inputs, features,
                                                    normalization_parameters,
                                                    blob_map, norm_net,
                                                    blobname_template)

        np.testing.assert_array_equal(
            np.array([
                [1, 0, 0, 1.0, 1, 0],
                [0, 1, 0, 2.0, 0, 1],
                [0, 0, 1, 3.0, 1, 0],
                [0, 0, 1, 3.0, 0, 0]  # Missing values should go to all 0
            ]),
            normalized_outputs)
示例#2
0
 def _normalize_actions(self, actions: np.ndarray) -> np.ndarray:
     if self.skip_normalization:
         return actions
     return normalize_dense_matrix(actions, self._action_features,
                                   self._action_normalization_parameters,
                                   self.action_norm_blobs,
                                   self.action_norm_net,
                                   self.action_norm_blobname_template,
                                   self.num_action_features)
示例#3
0
    def _normalize_states(self, states: np.ndarray) -> np.ndarray:
        """
        Normalizes input states and replaces NaNs with 0. Returns a matrix of
        the same shape. Make sure to have set up the underlying normalization net
        with `_prepare_state_normalization`.

        :param states: Numpy array with shape (batch_size, state_dim) containing
            raw state inputs
        """
        if self.skip_normalization:
            return states
        return normalize_dense_matrix(
            states, self._state_features, self._state_normalization_parameters,
            self.state_norm_blobs, self.state_norm_net,
            self.state_norm_blobname_template, self.num_state_features
        )