Machine learning 成本函数训练目标与精度期望目标

Machine learning 成本函数训练目标与精度期望目标,machine-learning,neural-network,classification,gradient-descent,loss-function,Machine Learning,Neural Network,Classification,Gradient Descent,Loss Function,当我们训练神经网络时,我们通常使用梯度下降法,它依赖于一个连续的、可微的实值代价函数。例如,最终的成本函数可能采用均方误差。或者换句话说,梯度下降隐式地假设最终目标是回归——最小化实值误差度量 有时,我们希望神经网络做的是进行分类——给定一个输入,将其分类为两个或多个离散类别。在这种情况下,用户关心的最终目标是分类准确性——正确分类的案例百分比 但是当我们使用神经网络进行分类时,虽然我们的目标是分类精度,但这不是神经网络试图优化的。神经网络仍在试图优化实值成本函数。有时它们指向同一个方向,但有时

当我们训练神经网络时,我们通常使用梯度下降法,它依赖于一个连续的、可微的实值代价函数。例如,最终的成本函数可能采用均方误差。或者换句话说,梯度下降隐式地假设最终目标是回归——最小化实值误差度量

有时,我们希望神经网络做的是进行分类——给定一个输入,将其分类为两个或多个离散类别。在这种情况下,用户关心的最终目标是分类准确性——正确分类的案例百分比

但是当我们使用神经网络进行分类时,虽然我们的目标是分类精度,但这不是神经网络试图优化的。神经网络仍在试图优化实值成本函数。有时它们指向同一个方向,但有时不指向。特别是,我遇到过这样的情况:经过训练以正确最小化代价函数的神经网络,其分类精度比简单的手工编码阈值比较差

我已经使用TensorFlow将其归结为一个最小的测试用例。它建立一个感知器(无隐藏层的神经网络),在一个绝对最小的数据集(一个输入变量,一个二进制输出变量)上对其进行训练,评估结果的分类精度,然后将其与简单的手动编码阈值比较的分类精度进行比较;结果分别为60%和80%。直观地说,这是因为一个输入值较大的孤立点会产生相应较大的输出值,因此最小化成本函数的方法是在对两个更普通的情况进行错误分类的过程中,尽最大努力适应这一情况。感知器正在正确地执行它被要求执行的操作;只是这与我们实际需要的分类器不匹配。但分类精度不是一个连续可微函数,因此不能将其作为梯度下降的目标

我们如何训练神经网络,使其最终达到最大分类精度

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
pred = 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))

我想你忘了把输出通过simgoid。修正如下:

将numpy导入为np
导入tensorflow作为tf
sess=tf.InteractiveSession()
tf.set_random_seed(1)
#参数
纪元=10000
学习率=0.01
#资料
列车×=[
[0],
[0],
[2],
[2],
[9],
]
列车Y=[
0,
0,
1.
1.
0,
]
行=np.形状(列X)[0]
cols=np.形状(列X)[1]
#投入和产出
X=tf.placeholder(tf.float32)
Y=tf.placeholder(tf.float32)
#砝码
W=tf.变量(tf.随机_正态([cols]))
b=tf.Variable(tf.random_normal([]))
#模型
#改变:记住,你需要一个激活功能!
pred=tf.nn.sigmoid(tf.tensordot(X,W,1)+b)
成本=tf.减少总和((前一年)**2/行)
优化器=tf.train.GradientDescentOptimizer(学习率)。最小化(成本)
tf.global_variables_initializer().run()
#训练
对于范围内的历元(历元):
#按连续时间加倍打印更新
如果epoch&(epoch-1)==0或epoch==epoch-1:
打印({}{}{}{})。格式(
纪元
成本评估({X:train_X,Y:train_Y}),
W.eval(),
b、 eval(),
))
运行({X:train\ux,Y:train\uy})
#感知器的分类精度
分类=[X列中X的预测值({X:X})>0.5]
correct=总和([p==y表示zip(分类,序列y)中的(p,y)])
打印(“{}/{}=感知器精度”。格式(正确,行))
#手工编码阈值比较的分类精度
分类=[x[0]>1.0,用于列车x中的x]
correct=总和([p==y表示zip(分类,序列y)中的(p,y)])
打印(“{}/{}=阈值精度”。格式(正确,行))
输出:

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
我们如何训练神经网络,使其最终达到最大分类精度

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
pred = 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))
我在寻求一种方法来得到一个更接近精确的连续代理函数

首先,今天用于(深度)神经网络分类任务的损失函数并不是由它们发明的,但它可以追溯到几十年前,它实际上来自逻辑回归的早期。以下是二元分类的简单情况下的方程式:

它背后的想法就是提出一个连续可微函数,这样我们就能够利用(巨大的,仍在扩展的)分类问题的凸优化库

可以肯定地说,鉴于上述所需的数学约束,上述损失函数是迄今为止我们所拥有的最佳函数

我们是否应该考虑这个问题(即更好地逼近精度)解决和完成?至少在原则上,没有。我已经足够大了,可以记住一个时代,当时唯一可用的激活功能是
tanh
sigmoid
;然后是雷鲁,给球场带来了真正的助攻。类似地,有人最终可能会提出一个更好的损失函数,但可以说,这将在一篇研究论文中发生,而不是作为一个问题的答案

也就是说,当前损失函数这一事实来自概率论和信息论的基本考虑(与当前深度学习领域形成鲜明对比的是,这些领域建立在坚实的理论基础之上)这至少让人怀疑,是否有更好的亏损方案即将出台


关于损失和准确性之间的关系还有另一个微妙的点,这使得后者在性质上与前者有所不同,并且经常在此类讨论中丢失。让我详细说明一下

与本讨论相关的所有分类器(即神经网络、逻辑回归等)都是概率分类器;也就是说,他们不会努力回报