コード例 #1
0
    def test_lstm_symmetric_checks_dims(self):
        """
        Test that lstm_symmetric validates parameter requirements
        """

        # Raise an error if empty dimension and function layers defined.
        with self.assertRaises(ValueError):
            lstm_symmetric(4, dims=(), funcs=())

        # Ensure failure with default 3 dimension layers when 2 function layers passed.
        with self.assertRaises(ValueError):
            lstm_symmetric(4, funcs=("tanh", "tanh"))
コード例 #2
0
def test_lstm_symmetric_basic(n_features, n_features_out):
    """
    Tests that lstm_symmetric implements the correct parameters
    """
    model = lstm_symmetric(
        n_features=n_features,
        n_features_out=n_features_out,
        lookback_window=3,
        dims=(4, 3, 2, 1),
        funcs=("relu", "relu", "tanh", "tanh"),
        out_func="linear",
        optimizer="sgd",
        optimizer_kwargs={"lr": 0.01},
        loss="mse",
    )

    # Ensure that the input dimension to Keras model matches the number of features.
    assert model.layers[0].input_shape[2] == n_features

    # Ensure that the dimension of each encoding layer matches the expected dimension.
    assert [model.layers[i].input_shape[2] for i in range(1, 5)] == [4, 3, 2, 1]

    # Ensure that the dimension of each decoding layer (excluding last decoding layer)
    # matches the expected dimension.
    assert [model.layers[i].input_shape[2] for i in range(5, 8)] == [1, 2, 3]

    # Ensure that the dimension of last decoding layer matches the expected dimension.
    assert model.layers[8].input_shape[1] == 4

    # Ensure activation functions in the encoding part (layers 0-3)
    # match expected activation functions.
    assert [model.layers[i].activation.__name__ for i in range(0, 4)] == [
        "relu",
        "relu",
        "tanh",
        "tanh",
    ]

    # Ensure activation functions in the decoding part (layers 4-7)
    # match expected activation functions.
    assert [model.layers[i].activation.__name__ for i in range(4, 8)] == [
        "tanh",
        "tanh",
        "relu",
        "relu",
    ]

    # Ensure activation function for the output layer matches expected activation function.
    assert model.layers[8].activation.__name__ == "linear"

    # Assert that the expected Keras optimizer is used
    assert model.optimizer.__class__ == optimizers.SGD

    # Assert equality of difference up to 7 decimal places
    # Note that AlmostEquality is used as Keras can use a value approximately equal
    # to the given learning rate rather than the exact value.
    assert np.isclose((K.eval(model.optimizer.lr),), (0.01,))

    # Assert that the correct loss function is used.
    assert model.loss == "mse"
コード例 #3
0
def test_lstm_symmetric_basic(n_features, n_features_out):
    """
    Tests that lstm_symmetric implements the correct parameters
    """
    model = lstm_symmetric(
        n_features=n_features,
        n_features_out=n_features_out,
        lookback_window=3,
        dims=(4, 3, 2, 1),
        funcs=("relu", "relu", "tanh", "tanh"),
        out_func="linear",
        optimizer="SGD",
        optimizer_kwargs={"lr": 0.01},
        loss="mse",
    )

    # Ensure that the input dimension to Keras model matches the number of features.
    assert model.layers[0].input_shape[2] == n_features

    # Ensure that the dimension of each encoding layer matches the expected dimension.
    assert [model.layers[i].input_shape[2]
            for i in range(1, 5)] == [4, 3, 2, 1]

    # Ensure that the dimension of each decoding layer (excluding last decoding layer)
    # matches the expected dimension.
    assert [model.layers[i].input_shape[2] for i in range(5, 8)] == [1, 2, 3]

    # Ensure that the dimension of last decoding layer matches the expected dimension.
    assert model.layers[8].input_shape[1] == 4

    # Ensure activation functions in the encoding part (layers 0-3)
    # match expected activation functions.
    assert [model.layers[i].activation.__name__ for i in range(0, 4)] == [
        "relu",
        "relu",
        "tanh",
        "tanh",
    ]

    # Ensure activation functions in the decoding part (layers 4-7)
    # match expected activation functions.
    assert [model.layers[i].activation.__name__ for i in range(4, 8)] == [
        "tanh",
        "tanh",
        "relu",
        "relu",
    ]

    # Ensure activation function for the output layer matches expected activation function.
    assert model.layers[8].activation.__name__ == "linear"

    # Assert that the expected Keras optimizer is used
    assert model.optimizer.__class__ == optimizers.SGD

    # Assert that the correct loss function is used.
    assert model.loss == "mse"
コード例 #4
0
    def test_lstm_defaults(self):
        """
        Test that models with all defaults are created without failure and are equal
        """
        # Create models with default parameters
        base = lstm_model(4)
        symmetric = lstm_symmetric(4)
        hourglass = lstm_hourglass(4)

        # Ensure LSTM model layers are equal to base model layers
        for i in range(len(base.layers)):
            # Rename layers so as to not fail on names, only configuration
            config = base.layers[i].get_config().update({"name": "test"})
            symmetric_config = symmetric.layers[i].get_config().update(
                {"name": "test"})
            hourglass_config = hourglass.layers[i].get_config().update(
                {"name": "test"})
            assert config == symmetric_config
            assert config == hourglass_config
コード例 #5
0
 def test_lstm_symmetric_checks_dims(self):
     """
     Test that lstm_symmetric validates parameter requirements
     """
     with self.assertRaises(ValueError):
         lstm_symmetric(4, dims=[], funcs=[])