Example #1
0
def test_binary_hinge_loss(colvect):
    from lasagne.objectives import binary_hinge_loss
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    if not colvect:
        c = binary_hinge_loss(p, t, log_odds=True)
    else:
        c = binary_hinge_loss(p.dimshuffle(0, 'x'), t, log_odds=True)[:, 0]
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10).astype(floatX)
    targets = np.random.random_integers(0, 1, (10,)).astype("int8")
    hinge = np.maximum(0, 1 - predictions * (2 * targets - 1))
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #2
0
def test_binary_hinge_loss(colvect):
    from lasagne.objectives import binary_hinge_loss
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    if not colvect:
        c = binary_hinge_loss(p, t, log_odds=True)
    else:
        c = binary_hinge_loss(p.dimshuffle(0, 'x'), t, log_odds=True)[:, 0]
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10).astype(floatX)
    targets = np.random.random_integers(0, 1, (10, )).astype("int8")
    hinge = np.maximum(0, 1 - predictions * (2 * targets - 1))
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #3
0
def test_binary_hinge_loss():
    from lasagne.objectives import binary_hinge_loss
    from lasagne.nonlinearities import rectify
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    c = binary_hinge_loss(p, t)
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10).astype(floatX)
    targets = np.random.random_integers(0, 1, (10,)).astype("int8")
    hinge = rectify(1 - predictions * (2 * targets - 1))
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #4
0
def test_binary_hinge_loss():
    from lasagne.objectives import binary_hinge_loss
    from lasagne.nonlinearities import rectify
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    c = binary_hinge_loss(p, t)
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10).astype(floatX)
    targets = np.random.random_integers(0, 1, (10,)).astype("int8")
    hinge = rectify(1 - predictions * (2 * targets - 1))
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #5
0
def test_binary_hinge_loss_sigmoid_predictions():
    from lasagne.objectives import binary_hinge_loss
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    c = binary_hinge_loss(p, t, log_odds=False)
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10, ).astype(floatX)
    targets = np.random.random_integers(0, 1, (10, )).astype("int8")
    targets2 = 2 * targets - 1
    hinge = np.maximum(0, 1 - np.log(predictions / (1-predictions)) * targets2)
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #6
0
def test_binary_hinge_loss_sigmoid_predictions():
    from lasagne.objectives import binary_hinge_loss
    p = theano.tensor.vector('p')
    t = theano.tensor.ivector('t')
    c = binary_hinge_loss(p, t, log_odds=False)
    # numeric version
    floatX = theano.config.floatX
    predictions = np.random.rand(10, ).astype(floatX)
    targets = np.random.random_integers(0, 1, (10, )).astype("int8")
    targets2 = 2 * targets - 1
    hinge = np.maximum(0, 1 - np.log(predictions / (1-predictions)) * targets2)
    # compare
    assert np.allclose(hinge, c.eval({p: predictions, t: targets}))
Example #7
0
def test_binary_hinge_loss_invalid():
    from lasagne.objectives import binary_hinge_loss
    with pytest.raises(TypeError) as exc:
        binary_hinge_loss(theano.tensor.matrix(),
                          theano.tensor.vector())
    assert 'rank mismatch' in exc.value.args[0]
Example #8
0
    input_var = dtensor5('inputs')
    target_var = T.ivector('targets')
    network = build_cnn(input_var)['output']

    # Create loss function
    prediction = lasagne.layers.get_output(network)
    loss = lasagne.objectives.binary_hinge_loss(prediction, target_var)
    loss = loss.mean()

    # Create parameter update expressions (later I will make rates adaptive)
    params = lasagne.layers.get_all_params(network, trainable=True)
    # updates = nesterov_momentum(loss, params, learning_rate=0.01,
    #                                         momentum=0.9)
    updates = adam(loss, params)
    test_prediction = lasagne.layers.get_output(network, deterministic=True)
    test_loss = binary_hinge_loss(test_prediction, target_var)
    test_loss = test_loss.mean()
    test_acc = T.mean(T.eq(T.sgn(test_prediction), target_var),
                  dtype=theano.config.floatX)

    # Compile training function that updates parameters and returns training loss
    train_fn = theano.function([input_var, target_var], loss, updates=updates)
    val_fn = theano.function([input_var, target_var], [test_loss, test_acc])

    num_epochs = 8000 # Will probably not do this many b/c of early stopping 
    best_network_weights_epoch = 0 
    epoch_accuracies = [] 
    # Train network 
    for epoch in range(num_epochs):
        # In each epoch, we do a full pass over the training data:
        train_err = 0
Example #9
0
def test_binary_hinge_loss_invalid():
    from lasagne.objectives import binary_hinge_loss
    with pytest.raises(TypeError) as exc:
        binary_hinge_loss(theano.tensor.matrix(), theano.tensor.vector())
    assert 'rank mismatch' in exc.value.args[0]