BinaryCrossentropy
類別keras.metrics.BinaryCrossentropy(
name="binary_crossentropy", dtype=None, from_logits=False, label_smoothing=0
)
計算標籤和預測之間的交叉熵指標。
當只有兩個標籤類別 (0 和 1) 時,應使用此交叉熵指標類別。
引數
[0, 1]
中的浮點數。當 > 0 時,標籤值會被平滑化,表示對標籤值的信心會放鬆。例如,label_smoothing=0.2
表示對於標籤 "0",我們會使用 0.1 的值,而對於標籤 "1",我們會使用 0.9 的值。範例
>>> m = keras.metrics.BinaryCrossentropy()
>>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
>>> m.result()
0.81492424
>>> m.reset_state()
>>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
... sample_weight=[1, 0])
>>> m.result()
0.9162905
搭配 compile()
API 使用
model.compile(
optimizer='sgd',
loss='mse',
metrics=[keras.metrics.BinaryCrossentropy()])
CategoricalCrossentropy
類別keras.metrics.CategoricalCrossentropy(
name="categorical_crossentropy",
dtype=None,
from_logits=False,
label_smoothing=0,
axis=-1,
)
計算標籤和預測之間的交叉熵指標。
當有多個標籤類別 (2 個或更多) 時,應使用此交叉熵指標類別。它假設標籤是以 one-hot 編碼的,例如,當標籤值為 [2, 0, 1]
時,則 y_true
為 [[0, 0, 1], [1, 0, 0], [0, 1, 0]]
。
引數
[0, 1]
中的浮點數。當 > 0 時,標籤值會被平滑化,表示對標籤值的信心會放鬆。例如,label_smoothing=0.2
表示對於標籤 "0",我們會使用 0.1 的值,而對於標籤 "1",我們會使用 0.9 的值。-1
。計算熵的維度。範例
>>> # EPSILON = 1e-7, y = y_true, y` = y_pred
>>> # y` = clip_ops.clip_by_value(output, EPSILON, 1. - EPSILON)
>>> # y` = [[0.05, 0.95, EPSILON], [0.1, 0.8, 0.1]]
>>> # xent = -sum(y * log(y'), axis = -1)
>>> # = -((log 0.95), (log 0.1))
>>> # = [0.051, 2.302]
>>> # Reduced xent = (0.051 + 2.302) / 2
>>> m = keras.metrics.CategoricalCrossentropy()
>>> m.update_state([[0, 1, 0], [0, 0, 1]],
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
>>> m.result()
1.1769392
>>> m.reset_state()
>>> m.update_state([[0, 1, 0], [0, 0, 1]],
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... sample_weight=np.array([0.3, 0.7]))
>>> m.result()
1.6271976
搭配 compile()
API 使用
model.compile(
optimizer='sgd',
loss='mse',
metrics=[keras.metrics.CategoricalCrossentropy()])
SparseCategoricalCrossentropy
類別keras.metrics.SparseCategoricalCrossentropy(
name="sparse_categorical_crossentropy", dtype=None, from_logits=False, axis=-1
)
計算標籤和預測之間的交叉熵指標。
當有兩個或更多標籤類別時,請使用此交叉熵指標。它期望標籤以整數形式提供。如果您想提供以 one-hot 編碼的標籤,請改用 CategoricalCrossentropy
指標。
y_pred
的每個特徵應有 num_classes
個浮點值,而 y_true
的每個特徵應有一個浮點值。
引數
-1
。計算熵的維度。範例
>>> # y_true = one_hot(y_true) = [[0, 1, 0], [0, 0, 1]]
>>> # logits = log(y_pred)
>>> # softmax = exp(logits) / sum(exp(logits), axis=-1)
>>> # softmax = [[0.05, 0.95, EPSILON], [0.1, 0.8, 0.1]]
>>> # xent = -sum(y * log(softmax), 1)
>>> # log(softmax) = [[-2.9957, -0.0513, -16.1181],
>>> # [-2.3026, -0.2231, -2.3026]]
>>> # y_true * log(softmax) = [[0, -0.0513, 0], [0, 0, -2.3026]]
>>> # xent = [0.0513, 2.3026]
>>> # Reduced xent = (0.0513 + 2.3026) / 2
>>> m = keras.metrics.SparseCategoricalCrossentropy()
>>> m.update_state([1, 2],
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
>>> m.result()
1.1769392
>>> m.reset_state()
>>> m.update_state([1, 2],
... [[0.05, 0.95, 0], [0.1, 0.8, 0.1]],
... sample_weight=np.array([0.3, 0.7]))
>>> m.result()
1.6271976
搭配 compile()
API 使用
model.compile(
optimizer='sgd',
loss='mse',
metrics=[keras.metrics.SparseCategoricalCrossentropy()])
KLDivergence
類別keras.metrics.KLDivergence(name="kl_divergence", dtype=None)
計算 y_true
和 y_pred
之間的 Kullback-Leibler 散度指標。
公式
metric = y_true * log(y_true / y_pred)
y_true
和 y_pred
預期為機率分佈,其值介於 0 和 1 之間。它們會被裁剪到 [0, 1]
範圍。
引數
範例
>>> m = keras.metrics.KLDivergence()
>>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]])
>>> m.result()
0.45814306
>>> m.reset_state()
>>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]],
... sample_weight=[1, 0])
>>> m.result()
0.9162892
搭配 compile()
API 使用
model.compile(optimizer='sgd',
loss='mse',
metrics=[keras.metrics.KLDivergence()])
Poisson
類別keras.metrics.Poisson(name="poisson", dtype=None)
計算 y_true
和 y_pred
之間的 Poisson 指標。
公式
metric = y_pred - y_true * log(y_pred)
引數
範例
>>> m = keras.metrics.Poisson()
>>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]])
>>> m.result()
0.49999997
>>> m.reset_state()
>>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]],
... sample_weight=[1, 0])
>>> m.result()
0.99999994
搭配 compile()
API 使用
model.compile(optimizer='sgd',
loss='mse',
metrics=[keras.metrics.Poisson()])