Esempio n. 1
0
def test_connect_additional_input(num_neurons, expected_output):
    neurons = neuron.Neurons(num_neurons)
    other = neuron.Neurons(num_neurons)

    neurons.set('feedback', other)
    other.output.values = expected_output

    assert all(neurons.get('feedback').values == expected_output)
Esempio n. 2
0
def test_connect_input(num_neurons, expected_output):
    neurons = neuron.Neurons(num_neurons)
    other = neuron.Neurons(num_neurons)

    neurons.input = other
    other.output.values = expected_output

    assert all(neurons.input.values == expected_output)
Esempio n. 3
0
def test_standard_computation():
    compute = computation.StandardComputation(threshold=1.2)

    n1 = neuron.Neurons(3)
    n2 = neuron.Neurons(2, computation=compute)

    synapses = np.array([[0.8, 0.4], [0.6, 0.8], [0.5, 0.4]])

    n2.set(neuron.Neurons.MAIN_INPUT,
           n1,
           synapse_function=lambda inp_shape, out_shape: synapses)

    n1.output.values = np.array([1, 0, 1])
    output = n2.compute()

    expected = [True, False]

    assert all(output == expected)
Esempio n. 4
0
def test_sparse_computation_with_fixed_number():
    compute = computation.SparseComputation(n=0.25)

    n1 = neuron.Neurons(3)
    n2 = neuron.Neurons(4, computation=compute)

    synapses = np.array([[0.8, 0.4, 0.5, 0.4], [0.6, 0.8, 0.9, 0.2],
                         [0.5, 0.4, 0.5, 0.7]])

    n2.set(neuron.Neurons.MAIN_INPUT,
           n1,
           synapse_function=lambda inp_shape, out_shape: synapses)

    n1.output.values = np.array([1, 0, 1])
    output = n2.compute()

    expected = [True, False, False, False]

    assert all(output == expected)
Esempio n. 5
0
def test_hebbian_learning():
    hl = learning.HebbianLearning(increase=0.1, decrease=0.05)

    n1 = neuron.Neurons(3)
    n2 = neuron.Neurons(2, learning=hl)

    synapses = np.array([[0.8, 0.4], [0.6, 0.8], [0.5, 0.4]])

    n2.set(neuron.Neurons.MAIN_INPUT,
           n1,
           synapse_function=lambda inp_shape, out_shape: synapses)

    n1.output.values = np.array([1, 0, 1])
    n2.output.values = np.array([1, 0])

    n2.learn()

    expected_synapses = np.array([[0.90, 0.35], [0.55, 0.80], [0.60, 0.35]])

    testing.assert_allclose(n2.input.synapses, expected_synapses)
Esempio n. 6
0
 def create_brain(architecture,
                  sensor_dim,
                  computation=None,
                  learning=None,
                  random_seed=0):
     np.random.seed(random_seed)
     cortex = neuron.FeedForward([neuron.Neurons(i) for i in architecture])
     return brain.Brain(cortex,
                        create_sensor(sensor_dim),
                        computation=computation,
                        learning=learning)
Esempio n. 7
0
def decode_neurons(neurons_proto):
    """Obtain an instance of Neurons from a protobuf.

    Args:
        neurons_proto: The protobuf instance representing the neurons

    Returns:
        The neurons decoded from the protobuf
    """
    layers = [neuron.Neurons(n) for n in neurons_proto.layer]
    return neuron.FeedForward(layers)
Esempio n. 8
0
def neurons():
    return neuron.Neurons(4, None)
Esempio n. 9
0
def layers():
    return neuron.FeedForward([neuron.Neurons(i, None) for i in (4, 4, 4, 4)])
Esempio n. 10
0
def test_loop_back():
    layers = [neuron.Neurons(i) for i in [10, 10, 10]]
    neuron.LoopBack(layers, 'loopback')

    for layer in layers:
        layer.get('loopback')._connected_output == layer.output
Esempio n. 11
0
def test_feed_backward():
    layers = [neuron.Neurons(i) for i in [10, 10, 10]]
    neuron.FeedBackward(layers, 'feedback')

    for i, layer in enumerate(layers[:-1]):
        layer.get('feedback')._connected_output == layers[i + 1].output
Esempio n. 12
0
def test_feed_forward_custom_input():
    layers = [neuron.Neurons(i) for i in [10, 10, 10]]
    neuron.FeedForward(layers, 'extra')

    for i, layer in enumerate(layers[:-1]):
        layers[i + 1].get('extra')._connected_output == layer.output
Esempio n. 13
0
def test_feed_forward():
    layers = [neuron.Neurons(i) for i in [10, 10, 10]]
    neuron.FeedForward(layers)

    for i, layer in enumerate(layers[:-1]):
        layers[i + 1].input._connected_output == layer.output
Esempio n. 14
0
def test_no_connections():
    layers = [neuron.Neurons(i) for i in [10, 10, 10]]

    for layer in layers:
        with pytest.raises(IndexError):
            layer.input.values
Esempio n. 15
0
def test_null_inputs(num_neurons):
    neurons = neuron.Neurons(num_neurons)

    with pytest.raises(IndexError):
        neurons.input.values