1. model.compile(loss=losses.mean_squared_error, optimizer='sgd')

你可以传递一个现有的损失函数名,或者一个TensorFlow/Theano符号函数。
该符号函数为每个数据点返回一个标量,有以下两个参数:

  • y_true: 真实标签. TensorFlow/Theano张量。
  • y_pred: 预测值. TensorFlow/Theano张量,其shape与y_true相同。

实际的优化目标是所有数据点的输出数组的平均值。

可用损失函数

  1. mean_squared_error(y_true, y_pred)

mean_absolute_error

  1. mean_absolute_error(y_true, y_pred)

mean_absolute_percentage_error

  1. mean_absolute_percentage_error(y_true, y_pred)

mean_squared_logarithmic_error



    hinge

    1. hinge(y_true, y_pred)

    categorical_hinge


    logcosh

    1. logcosh(y_true, y_pred)

    预测误差的双曲余弦的对数。

    对于小的xlog(cosh(x))近似等于(x ** 2) / 2。对于大的x,近似于abs(x) - log(2)。这表示’logcosh’与均方误差大致相同,但是不会受到偶尔疯狂的错误预测的强烈影响。

    • y_true: 目标真实值的张量。
    • y_pred: 目标预测值的张量。

    Returns

    每个样本都有一个标量损失的张量。



    sparse_categorical_crossentropy

    1. sparse_categorical_crossentropy(y_true, y_pred)

    binary_crossentropy


    kullback_leibler_divergence

    1. kullback_leibler_divergence(y_true, y_pred)

    1. poisson(y_true, y_pred)

    cosine_proximity


    1. from keras.utils.np_utils import to_categorical