keras.metrics.MeanSquaredError与keras.losses.mean_squared_error的区别

总的来说:keras.metrics下面的指标是累积的,在当前batch上的结果会和之前的batch做平均。而keras.losses下面的不会。

具体举例说明:

# metric使用

metric = keras.metrics.MeanSquaredError()
print(metric([5.], [2.]))
print(metric([0.], [1.]))
print(metric.result())

metric.reset_states()
metric([1.], [3.])
print(metric.result())

输出:

tf.Tensor(9.0, shape=(), dtype=float32)
tf.Tensor(5.0, shape=(), dtype=float32)
tf.Tensor(5.0, shape=(), dtype=float32)
tf.Tensor(4.0, shape=(), dtype=float32)

从上面的结果可以看到当运行“print(metric([5.], [2.]))”时,输出结果为“9”,(即:(5-2)**2 / 1 = 9),而运行第二句时,如果不累加的话,应该为1。((0-1)**2 / 1 = 1), 但是可以看到,输出并不是1,而是5,原因就是keras.metrics下面的指标是累积的,即((9+1)/ 2 = 5),想要清除累积,需要执行“metric.reset_states()”即可。而keras.losses.mean_squared_error是不累积的。

原创文章 46 获赞 49 访问量 2185

猜你喜欢

转载自blog.csdn.net/qq_41660119/article/details/105816018