Cost function training target versus accuracy desired goal

前端 未结 2 917
刺人心
刺人心 2020-12-07 06:14

When we train neural networks, we typically use gradient descent, which relies on a continuous, differentiable real-valued cost function. The final cost function might, for

2条回答
  •  抹茶落季
    2020-12-07 06:32

    I think you are forgetting to pass your output through a simgoid. Fixed below:

    import numpy as np
    import tensorflow as tf
    sess = tf.InteractiveSession()
    tf.set_random_seed(1)
    
    # Parameters
    epochs = 10000
    learning_rate = 0.01
    
    # Data
    train_X = [
        [0],
        [0],
        [2],
        [2],
        [9],
    ]
    train_Y = [
        0,
        0,
        1,
        1,
        0,
    ]
    
    rows = np.shape(train_X)[0]
    cols = np.shape(train_X)[1]
    
    # Inputs and outputs
    X = tf.placeholder(tf.float32)
    Y = tf.placeholder(tf.float32)
    
    # Weights
    W = tf.Variable(tf.random_normal([cols]))
    b = tf.Variable(tf.random_normal([]))
    
    # Model
    # CHANGE HERE: Remember, you need an activation function!
    pred = tf.nn.sigmoid(tf.tensordot(X, W, 1) + b)
    cost = tf.reduce_sum((pred-Y)**2/rows)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    tf.global_variables_initializer().run()
    
    # Train
    for epoch in range(epochs):
        # Print update at successive doublings of time
        if epoch&(epoch-1) == 0 or epoch == epochs-1:
            print('{} {} {} {}'.format(
                epoch,
                cost.eval({X: train_X, Y: train_Y}),
                W.eval(),
                b.eval(),
                ))
        optimizer.run({X: train_X, Y: train_Y})
    
    # Classification accuracy of perceptron
    classifications = [pred.eval({X: x}) > 0.5 for x in train_X]
    correct = sum([p == y for (p, y) in zip(classifications, train_Y)])
    print('{}/{} = perceptron accuracy'.format(correct, rows))
    
    # Classification accuracy of hand-coded threshold comparison
    classifications = [x[0] > 1.0 for x in train_X]
    correct = sum([p == y for (p, y) in zip(classifications, train_Y)])
    print('{}/{} = threshold accuracy'.format(correct, rows))
    

    The output:

    0 0.28319069743156433 [ 0.75648874] -0.9745011329650879
    1 0.28302448987960815 [ 0.75775659] -0.9742625951766968
    2 0.28285878896713257 [ 0.75902224] -0.9740257859230042
    4 0.28252947330474854 [ 0.76154679] -0.97355717420578
    8 0.28187844157218933 [ 0.76656926] -0.9726400971412659
    16 0.28060704469680786 [ 0.77650583] -0.970885694026947
    32 0.27818527817726135 [ 0.79593837] -0.9676888585090637
    64 0.2738055884838104 [ 0.83302218] -0.9624817967414856
    128 0.26666420698165894 [ 0.90031379] -0.9562843441963196
    256 0.25691407918930054 [ 1.01172411] -0.9567816257476807
    512 0.2461051195859909 [ 1.17413962] -0.9872989654541016
    1024 0.23519910871982574 [ 1.38549554] -1.088881492614746
    2048 0.2241383194923401 [ 1.64616168] -1.298340916633606
    4096 0.21433120965957642 [ 1.95981205] -1.6126530170440674
    8192 0.2075471431016922 [ 2.31746769] -1.989408016204834
    9999 0.20618653297424316 [ 2.42539024] -2.1028473377227783
    4/5 = perceptron accuracy
    4/5 = threshold accuracy
    

提交回复
热议问题