举例,下面这是损失函数的源码:
def mean_squared_error(y_true, y_pred):
return K.mean(math_ops.square(y_pred - y_true), axis=-1)
keras中compile时:
model.compile(loss=mean_squared_error)
从以上可以看到,compile时并不需要给mean_squared_error传入参数,这就说明keras默认把某一路的实际输出和预测输出传到mean_squared_error中。
从源码可以看到,只是返回了一个具体数值,这就说明了每训练一轮,得到一个损失值,我们可以通过以下方法:
history=model.fit()
print(history.history['loss'])
获取训练的损失值,训练多少轮就会得到多少个损失值。最后会以列表的形式输出每个损失值。