Ejemplo n.º 1
0
def test_default_output(regtest):
    """default output should not change"""
    fv3fit.set_random_seed(0)
    config = fv3fit.ConvolutionalNetworkConfig()
    array = np.random.randn(2, 10, 10, 3)
    out = config.build(array, n_features_out=3)
    print_result(out, decimals=4, file=regtest)
Ejemplo n.º 2
0
def test_network_has_correct_number_of_hidden_layers(depth):
    fv3fit.set_random_seed(0)
    config = fv3fit.ConvolutionalNetworkConfig(depth=depth)
    n_features_in, n_features_out = 5, 5
    input = tf.keras.layers.Input(shape=(10, 10, n_features_in))
    convolutional_network = config.build(input, n_features_out=n_features_out)
    # one layer of depth is the output
    assert len(convolutional_network.hidden_outputs) == depth - 1
Ejemplo n.º 3
0
def test_output_is_correct_shape(input_shape, kernel_size, depth, features_out,
                                 base_output_shape):
    config = fv3fit.ConvolutionalNetworkConfig(kernel_size=kernel_size,
                                               depth=depth)
    array = np.random.randn(*input_shape)
    convolutional_network = config.build(array, n_features_out=features_out)
    output_shape = tuple(base_output_shape) + (features_out, )
    assert convolutional_network.output.shape == output_shape
Ejemplo n.º 4
0
def test_output_type():
    config = fv3fit.ConvolutionalNetworkConfig()
    array = np.random.randn(2, 10, 10, 3)
    convolutional_network = config.build(array, n_features_out=3)
    assert isinstance(convolutional_network, fv3fit.ConvolutionalNetwork)
    assert isinstance(convolutional_network.output, tf.Tensor)
    assert isinstance(convolutional_network.hidden_outputs, Sequence)
    assert all(
        isinstance(item, tf.Tensor)
        for item in convolutional_network.hidden_outputs)
Ejemplo n.º 5
0
def test_network_has_gaussian_noise_layer():
    fv3fit.set_random_seed(0)
    config = fv3fit.ConvolutionalNetworkConfig(gaussian_noise=0.1)
    n_features_in, n_features_out = 5, 5
    input = tf.keras.layers.Input(shape=(10, 10, n_features_in))
    convolutional_network = config.build(input, n_features_out=n_features_out)
    model = tf.keras.Model(inputs=input, outputs=convolutional_network.output)
    assert any(
        isinstance(layer, tf.keras.layers.GaussianNoise)
        for layer in model.layers)
Ejemplo n.º 6
0
def test_hidden_layers_have_samples_equal_to_filters(depth, filters):
    input_shape = (3, 10, 10, 5)
    kernel_size = 3
    features_out = 1
    config = fv3fit.ConvolutionalNetworkConfig(kernel_size=kernel_size,
                                               depth=depth,
                                               filters=filters)
    array = np.random.randn(*input_shape)
    convolutional_network = config.build(array, n_features_out=features_out)
    for output in convolutional_network.hidden_outputs:
        assert output.shape[-1] == filters
Ejemplo n.º 7
0
def test_standard_input_gives_standard_output():
    fv3fit.set_random_seed(0)
    config = fv3fit.ConvolutionalNetworkConfig()
    array = np.random.randn(2, 10, 10, 20)
    np.testing.assert_almost_equal(np.mean(array), 0.0, decimal=1)
    np.testing.assert_almost_equal(np.std(array), 1.0, decimal=1)
    convolutional_network = config.build(array, n_features_out=3)
    np.testing.assert_almost_equal(np.mean(convolutional_network.output),
                                   0.0,
                                   decimal=1)
    out_std = np.std(convolutional_network.output)
    # std isn't going to be 1 because of relu activation function, should be less
    assert out_std < 1.0
    assert out_std > 0.1
Ejemplo n.º 8
0
def test_modifying_one_input_modifies_correct_number_of_outputs(
        kernel_size, depth, n_expected_changes):
    """
    Test that when you modify an input in the center of the domain,
    the correct number of output values change for the initialized network.
    """
    features_out = 1
    input_shape = (1, 17, 17, 1)
    config = fv3fit.ConvolutionalNetworkConfig(kernel_size=kernel_size,
                                               depth=depth)
    array = np.random.randn(*input_shape)
    input_layer = tf.keras.layers.Input(shape=input_shape[1:])
    convolutional_network = config.build(input_layer,
                                         n_features_out=features_out)
    model = tf.keras.Model(inputs=input_layer,
                           outputs=convolutional_network.output)
    first_output = model.predict(array)
    array[0, 8, 8, 0] = np.random.randn()
    second_output = model.predict(array)
    assert np.sum(first_output != second_output) == n_expected_changes