tf.keras.losses.MeanSquaredError()

import numpy as np
import tensorflow as tf
y_true = [[0., 1.], [2., 3.],[4., 5.]]
y_pred = [[6., 7.], [8., 9.],[10., 11.]]

mse = tf.keras.losses.MeanSquaredError()
print(mse(y_true, y_pred,sample_weight=[0.1,0.2,3]).numpy())

用上例简述tf.kears.losses.MeanSquaredError()中的sample_weight的作用:

解释:

0-11:按照位置对应即可。

0.1,0.2,0.3:权重参数。 

3:sample_weight的个数

6:len(tf.reshape(y_pred,(-1,1))),也就是y_pred里面数字的个数。

如果sample_weight换成[0.2,0.1,0.3]、[0.3,0.2,0.1]结果和[0.1,0.2,0.3]结果相同。看下面的例子。

import numpy as np
import tensorflow as tf


y_true = [[1., 2.], [3., 4.],[5., 6.]]
y_pred = [[7., 8.], [9., 10.],[11., 12.]]
mse = tf.keras.losses.MeanSquaredError()

print(mse(y_true, y_pred,sample_weight=[2,4,5]).numpy())
print(mse(y_true, y_pred,sample_weight=[2,5,4]).numpy())
print(mse(y_true, y_pred,sample_weight=[4,2,5]).numpy())
print(mse(y_true, y_pred,sample_weight=[4,5,2]).numpy())
print(mse(y_true, y_pred,sample_weight=[5,2,4]).numpy())
print(mse(y_true, y_pred,sample_weight=[5,4,2]).numpy())

结果tf.keras.losses.MeanSquaredError()_第1张图片

 可见参数的顺序并不能改变最后的结果。

你可能感兴趣的:(numpy,python,深度学习)