如果我对您的理解正确,那么您可能会误以为是高估了。如果是这样,则需要一个适当的非对称成本函数。一种简单的选择是调整平方损失:
L:(x,α)→x2(sgnx+α)2
其中是一个参数,您可以用来权衡低估与高估之间的权衡。正值会惩罚高估,因此您需要将设置为负。在python中这看起来像α α−1<α<1ααdef loss(x, a): return x**2 * (numpy.sign(x) + a)**2

接下来让我们生成一些数据:
import numpy
x = numpy.arange(-10, 10, 0.1)
y = -0.1*x**2 + x + numpy.sin(x) + 0.1*numpy.random.randn(len(x))

最后,我们将在tensorflow
Google的机器学习库中进行回归,该库支持自动区分(使此类问题的基于梯度的优化更加简单)。我将以这个示例为起点。
import tensorflow as tf
X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")
w = tf.Variable(0.0, name="coeff")
b = tf.Variable(0.0, name="offset")
y_model = tf.mul(X, w) + b
cost = tf.pow(y_model-Y, 2) # use sqr error for cost function
def acost(a): return tf.pow(y_model-Y, 2) * tf.pow(tf.sign(y_model-Y) + a, 2)
train_op = tf.train.AdamOptimizer().minimize(cost)
train_op2 = tf.train.AdamOptimizer().minimize(acost(-0.5))
sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
for i in range(100):
for (xi, yi) in zip(x, y):
# sess.run(train_op, feed_dict={X: xi, Y: yi})
sess.run(train_op2, feed_dict={X: xi, Y: yi})
print(sess.run(w), sess.run(b))
cost
是规则平方误差,acost
而是上述不对称损失函数。
如果使用,cost
您会得到
1.00764 -3.32445

如果使用,acost
您会得到
1.02604 -1.07742

acost
显然不要小看。我没有检查收敛性,但是您明白了。