Ejemplo n.º 1
0
def test_2in_3_1out():

    m = Machine((2, 3, 1))
    nose.tools.eq_(m.shape, (2, 3, 1))
    nose.tools.eq_(m.input_divide.shape, (2, ))
    nose.tools.eq_(m.input_subtract.shape, (2, ))
    nose.tools.eq_(len(m.weights), 2)
    nose.tools.eq_(m.weights[0].shape, (2, 3))
    assert numpy.allclose(m.weights[0], 0., rtol=1e-10, atol=1e-15)
    nose.tools.eq_(m.weights[1].shape, (3, 1))
    assert numpy.allclose(m.weights[1], 0., rtol=1e-10, atol=1e-15)
    nose.tools.eq_(len(m.biases), 2)
    nose.tools.eq_(m.biases[0].shape, (3, ))
    assert numpy.allclose(m.biases[0], 0., rtol=1e-10, atol=1e-15)
    nose.tools.eq_(m.biases[1].shape, (1, ))
    assert numpy.allclose(m.biases[1], 0., rtol=1e-10, atol=1e-15)
    nose.tools.eq_(m.hidden_activation, HyperbolicTangent())
    nose.tools.eq_(m.output_activation, HyperbolicTangent())

    # calculate and match
    weights = [numpy.random.rand(2, 3), numpy.random.rand(3, 1)]
    biases = [numpy.random.rand(3), numpy.random.rand(1)]

    m.weights = weights
    m.biases = biases

    pymac = PythonMachine(biases, weights, m.hidden_activation,
                          m.output_activation)

    X = numpy.random.rand(10, 2)
    assert numpy.allclose(m(X), pymac.forward(X), rtol=1e-10, atol=1e-15)
Ejemplo n.º 2
0
def test_20in_10_5_3out_with_momentum():

    machine = Machine((20, 10, 5, 3))
    machine.randomize()
    machine.hidden_activation = HyperbolicTangent()
    machine.output_activation = HyperbolicTangent()

    BATCH_SIZE = 10
    cost = SquareError(machine.output_activation)

    for k in range(10):
        check_training(machine, cost, True, BATCH_SIZE, 0.1, 0.1)
Ejemplo n.º 3
0
def test_2in_3_1out():

    machine = Machine((2, 3, 1))
    machine.randomize()
    machine.hidden_activation = HyperbolicTangent()
    machine.output_activation = HyperbolicTangent()

    BATCH_SIZE = 10
    cost = SquareError(machine.output_activation)

    for k in range(10):
        check_training(machine, cost, True, BATCH_SIZE, 0.1, 0.0)
Ejemplo n.º 4
0
def test_100in_10_10_5out_nobias():

    machine = Machine((100, 10, 10, 5))
    machine.randomize()
    machine.hidden_activation = HyperbolicTangent()
    machine.output_activation = HyperbolicTangent()
    machine.biases = 0

    BATCH_SIZE = 10
    cost = SquareError(machine.output_activation)

    for k in range(10):
        check_training(machine, cost, False, BATCH_SIZE, 0.1, 0.0)
Ejemplo n.º 5
0
def test_initialization():

    # Two inputs and 1 output
    m = Machine(2, 1)
    assert (m.weights == 0.0).all()
    nose.tools.eq_(m.weights.shape, (2, 1))
    assert (m.biases == 0.0).all()
    nose.tools.eq_(m.biases.shape, (1, ))

    # Start by providing the data
    w = numpy.array([[0.4, 0.1], [0.4, 0.2], [0.2, 0.7]], 'float64')
    m = Machine(w)
    b = numpy.array([0.3, -3.0], 'float64')
    isub = numpy.array([0., 0.5, 0.5], 'float64')
    idiv = numpy.array([0.5, 1.0, 1.0], 'float64')
    m.input_subtract = isub
    m.input_divide = idiv
    m.biases = b
    m.activation = HyperbolicTangent()

    assert (m.input_subtract == isub).all()
    assert (m.input_divide == idiv).all()
    assert (m.weights == w).all()
    assert (m.biases == b).all()
    nose.tools.eq_(m.activation, HyperbolicTangent())
    # Save to file
    # c = HDF5File("bla.hdf5", 'w')
    # m.save(c)

    # Start by reading data from a file
    c = HDF5File(MACHINE)
    m = Machine(c)
    assert (m.weights == w).all()
    assert (m.biases == b).all()

    # Makes sure we cannot stuff incompatible data
    w = numpy.array([[0.4, 0.4, 0.2], [0.1, 0.2, 0.7]], 'float64')
    m = Machine(w)
    b = numpy.array([0.3, -3.0, 2.7, -18, 52], 'float64')  #wrong
    nose.tools.assert_raises(RuntimeError, setattr, m, 'biases', b)
    nose.tools.assert_raises(RuntimeError, setattr, m, 'input_subtract', b)
    nose.tools.assert_raises(RuntimeError, setattr, m, 'input_divide', b)
Ejemplo n.º 6
0
def test_comparisons():

    # Start by creating the data
    w1 = numpy.array([[0.4, 0.1], [0.4, 0.2], [0.2, 0.7]], 'float64')
    w2 = numpy.array([[0.4, 1.1], [0.4, 0.2], [0.2, 0.7]], 'float64')
    b1 = numpy.array([0.3, -3.0], 'float64')
    b2 = numpy.array([0.3, 3.0], 'float64')
    isub1 = numpy.array([0., 0.5, 0.5], 'float64')
    isub2 = numpy.array([0.5, 0.5, 0.5], 'float64')
    idiv1 = numpy.array([0.5, 1.0, 1.0], 'float64')
    idiv2 = numpy.array([1.5, 1.0, 1.0], 'float64')

    # Creates Machine's
    m1 = Machine(w1)
    m1.input_subtract = isub1
    m1.input_divide = idiv1
    m1.biases = b1
    m1.activation = HyperbolicTangent()

    m1b = Machine(m1)
    m1c = Machine(w1)
    m1c.input_subtract = isub1
    m1c.input_divide = idiv1
    m1c.biases = b1
    m1c.activation = HyperbolicTangent()

    m2 = Machine(w2)
    m2.input_subtract = isub1
    m2.input_divide = idiv1
    m2.biases = b1
    m2.activation = HyperbolicTangent()

    m3 = Machine(w1)
    m3.input_subtract = isub2
    m3.input_divide = idiv1
    m3.biases = b1
    m3.activation = HyperbolicTangent()

    m4 = Machine(w1)
    m4.input_subtract = isub1
    m4.input_divide = idiv2
    m4.biases = b1
    m4.activation = HyperbolicTangent()

    m5 = Machine(w1)
    m5.input_subtract = isub1
    m5.input_divide = idiv1
    m5.biases = b2
    m5.activation = HyperbolicTangent()

    m6 = Machine(w1)
    m6.input_subtract = isub1
    m6.input_divide = idiv1
    m6.biases = b1
    m6.activation = Identity()

    # Compares them using the overloaded operators == and !=
    assert m1 == m1b
    assert not m1 != m1b
    assert m1.is_similar_to(m1b)
    assert m1 == m1c
    assert not m1 != m1c
    assert m1.is_similar_to(m1c)
    assert not m1 == m2
    assert m1 != m2
    assert not m1.is_similar_to(m2)
    assert not m1 == m3
    assert m1 != m3
    assert not m1.is_similar_to(m3)
    assert not m1 == m4
    assert m1 != m4
    assert not m1.is_similar_to(m4)
    assert not m1 == m5
    assert m1 != m5
    assert not m1.is_similar_to(m5)
    assert not m1 == m6
    assert m1 != m6
    assert not m1.is_similar_to(m6)