/ 猿问

# Keras binary_crossentropy vs categorical

2019-09-19 10:56:53

# convolution layers

filter_length=4,

border_mode='valid',

activation='relu'))

# dense layers

# output layer

## 3 回答

evaluate当使用带有2个以上标签的binary_crossentropy时，使用Keras方法计算的精度是完全错误的

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])  # WRONG way

model.fit(x_train, y_train,

batch_size=batch_size,

epochs=2,  # only 2 epochs, for demonstration purposes

verbose=1,

validation_data=(x_test, y_test))

# Keras reported accuracy:

score = model.evaluate(x_test, y_test, verbose=0)

score[1]

# 0.9975801164627075

# Actual accuracy calculated manually:

import numpy as np

y_pred = model.predict(x_test)

acc = sum([np.argmax(y_test[i])==np.argmax(y_pred[i]) for i in range(10000)])/10000

acc

# 0.98780000000000001

score[1]==acc

# False

from keras.metrics import categorical_accuracy

# Keras reported accuracy:

score = model.evaluate(x_test, y_test, verbose=0)

score[1]

# 0.98580000000000001

# Actual accuracy calculated manually:

y_pred = model.predict(x_test)

acc = sum([np.argmax(y_test[i])==np.argmax(y_pred[i]) for i in range(10000)])/10000

acc

# 0.98580000000000001

score[1]==acc

# True

Python version 3.5.3

Tensorflow version 1.2.1

Keras version 2.0.4

• 二元分类（两个目标类）

• 多级分类（超过两个独家目标）

• 多标签分类（超过两个非独占目标），其中多个目标类可以同时打开

• for `binary_crossentropy`：sigmoid激活，标量目标

• for `categorical_crossentropy`：softmax激活，单热编码目标

• 3 回答
• 0 关注
• 1668 浏览

0/150