예제 #1
0
def test_predict(input_units, hidden_units, nr_samples, rgen):
    nn = FcClassifier(input_units, hidden_units)
    nn.init_random()
    parameters = nn.get_weights()
    weights = list(rgen.randn(*w.shape) for w, _ in parameters)
    biases = list(np.zeros(b.shape) for _, b in parameters)
    for n, (w, b) in enumerate(zip(weights, biases)):
        nn.set_weights(n, w, b)

    x_in = rgen.randn(input_units, nr_samples)
    y_ref = fcnn_predict(x_in, weights, biases, it.repeat(sigmoid))[0, :]
    y_hat = nn.predict(x_in)
    assert_almost_equal(y_hat, y_ref)
예제 #2
0
def test_view_set_weights():
    shape = (5, 8, 1)
    nn = FcClassifier(shape[0], shape[1:-1])
    weights = nn.get_weights()

    for i in range(len(shape) - 1):
        # +1 Due to implicit weights
        w, b = weights[i]
        assert w.shape == (shape[i + 1], shape[i])
        assert b.shape == (shape[i + 1], )

    new_weight = np.random.randn(shape[1], shape[0])
    new_bias = np.random.randn(shape[1], )
    nn.set_weights(0, new_weight, new_bias)
    assert_array_equal(new_weight, nn.get_weights()[0][0])
    assert_array_equal(new_bias, nn.get_weights()[0][1])
예제 #3
0
def test_set_weights_exception(rgen):
    shape = (5, 1)
    nn = FcClassifier(shape[0], tuple())

    try:
        nn.set_weights(0, rgen.randn(shape[1] + 3, shape[0]),
                       rgen.randn(shape[1]))
    except ValueError:
        pass
    else:
        raise AssertionError(
            "Setting weight with wrong shape should raise error")

    try:
        nn.set_weights(0, rgen.randn(shape[1], shape[0]),
                       rgen.randn(shape[1] + 1))
    except ValueError:
        pass
    else:
        raise AssertionError(
            "Setting bias with wrong shape should raise error")
예제 #4
0
def test_view_set_weights_permissions():
    shape = (5, 1)
    nn = FcClassifier(shape[0], tuple())
    new_weight = np.random.randn(shape[1], shape[0])
    new_bias = np.random.randn(shape[1], )

    new_weight_copy = new_weight.copy()
    new_bias_copy = new_bias.copy()
    nn.set_weights(0, new_weight, new_bias)

    new_weight[:] = 0
    new_bias[:] = 0
    nn_weight, nn_bias = nn.get_weights()[0]

    assert_array_equal(new_weight_copy, nn_weight)
    assert_array_equal(new_bias_copy, nn_bias)
    assert not nn_weight.flags['OWNDATA']
    assert not nn_bias.flags['OWNDATA']
    assert not nn_weight.flags['WRITEABLE']
    assert not nn_bias.flags['WRITEABLE']
    del nn
    assert_array_equal(new_weight_copy, nn_weight)
    assert_array_equal(new_bias_copy, nn_bias)