Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"

Conclusion

--The label used is different. That's the only difference. For "categorical_crossentropy", use the label onehot (somewhere is 1 and all others are 0). Use an integer for the label "sparse_categorical_crossentropy".

Difference between one-hot and integer representation

Example) In the case of 10 classifications

one-hot expression Integer representation
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [9]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [2]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [1]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [5]

Convert integer labels to one-hot labels

I get the impression that many datasets have integer labels, but many loss functions do not work unless you give them one-hot labels instead of integer labels. In such a case, you need to convert. (Rather, I feel that there are a minority of loss functions that can be learned with integer labels, such as "sparse_categorical_crossentropy".)

The code is shown below.

import numpy as np

n_labels = len(np.unique(train_labels))
train_labels_onehot = np.eye(n_labels)[train_labels]

n_labels = len(np.unique(test_labels))
test_labels_onehot = np.eye(n_labels)[test_labels]

Recommended Posts

Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between process and job
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between == and is in python
Memorandum (difference between csv.reader and csv.dictreader)
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Difference between sort and sorted (memorial)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Difference between SQLAlchemy flush () and commit ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
[Xg boost] Difference between softmax and softprob
difference between statements (statements) and expressions (expressions) in Python
[Django ORM] Difference between values () and only ()
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Difference between linear regression, Ridge regression and Lasso regression
[Python] Difference between class method and static method
Difference between docker-compose env_file and .env file
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
speed difference between wsgi, Bottle and Flask
Difference between numpy.ndarray and list (dimension, size)
Difference between ls -l and cat command
Difference and compatibility verification between keras and tf.keras # 1
What is the difference between `pip` and` conda`?
Difference between using and import on shield language
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?
Center difference and forward difference
Between parametric and nonparametric
Consideration of the difference between ROC curve and PR curve
The rough difference between Unicode and UTF-8 (and their friends)
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
Difference between Ruby and Python in terms of variables
What is the difference between usleep, nanosleep and clock_nanosleep?
Difference between Numpy (n,) and (n, 1) notation [Difference between horizontal vector and vertical vector]
Difference between return, return None, and no return description in Python
How to use argparse and the difference between optparse
What is the difference between a symbolic link and a hard link?
Correspondence between pandas and SQL
Conversion between unixtime and datetime
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
List concatenation method in python, difference between list.extend () and “+” operator
Collaboration between PTVS and Anaconda
Connection between flask and sqlite3