• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

カテゴリカルクロスエントロピー

date: 2022-02-05 excerpt: カテゴリカルクロスエントロピーついて

tag: statistics機械学習カテゴリカルクロスエントロピーcategorical cross entropy


カテゴリカルクロスエントロピーついて

概要

  • クロスエントロピーのカテゴリ軸への拡張
  • 単純に交差エントロピーをすべてのカテゴリについて和をとっているとも解釈できる

tensorflow, kerasによる実装

import tensorflow as tf
y_true = [[0, 1, 0], [0, 0, 1]]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
cce = tf.keras.losses.CategoricalCrossentropy()
print(cce(y_true, y_pred).numpy()) # 1.1769392

numpyによる実装

import numpy as np

def cross_entropy(predictions, targets, epsilon=1e-12):
    predictions = np.clip(predictions, epsilon, 1. - epsilon)
    N = predictions.shape[0]
    ce = -np.sum(targets*np.log(predictions+1e-9))/N
    return ce

predictions = np.array([[0.05,0.95,0.0],
                        [0.1,0.80,0.1]])
targets = np.array([[0,1,0],
                   [0,0,1]])
x = cross_entropy(predictions, targets)
print(x) # 1.1769391881644822


statistics機械学習カテゴリカルクロスエントロピーcategorical cross entropy Share Tweet