【发布时间】:2020-09-10 08:19:54
【问题描述】:
我在binary_crossentropy和categorical_crossentropy的用法之间做了一个实验。我试图了解这两个损失函数在同一个问题上的行为。
我解决了binary classification 这个data 的问题。
在第一个实验中,我在最后一层使用了1神经元,具有sigmoid激活函数和binary_crossentropy。我训练了这个模型 10 次,取平均准确率。平均精度为 74.12760416666666。
我用于第一个实验的代码如下。
total_acc = 0
for each_iter in range(0, 10):
print each_iter
X = dataset[:,0:8]
y = dataset[:,8]
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=32)
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
temp_acc = accuracy*100
total_acc += temp_acc
del model
在第二个实验中,我在最后一层使用2神经元,激活函数softmax和categorical_crossentropy。我将我的目标 `y, 转换为分类,然后我再次训练了这个模型 10 次并取平均准确率。平均精度为 66.92708333333334。
我用于第二个设置的代码如下:
total_acc_v2 = 0
for each_iter in range(0, 10):
print each_iter
X = dataset[:,0:8]
y = dataset[:,8]
y = np_utils.to_categorical(y)
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(2, activation='softmax'))
# compile the keras model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=32)
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
temp_acc = accuracy*100
total_acc_v2 += temp_acc
del model
我认为这两个实验是相同的,应该给出非常相似的结果。准确率差距如此之大的原因是什么?
【问题讨论】:
标签: machine-learning keras deep-learning neural-network loss-function