我正在做多标签分类,我想讨论和理解张量流2的Accuracy()
功能。
这是可重现结果的代码段:
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.metrics import Accuracy
from sklearn.metrics import accuracy_score
import numpy as np
np.random.seed(10)
model = Sequential([
Dense(64, activation='relu', input_shape=(784,)),
Dense(64, activation='relu'),
Dense(4, activation='sigmoid'),
])
# just for debugging
for layer in model.layers[:]:
layer.trainable = False
model.compile(optimizer=Adam(0.001), loss='binary_crossentropy',
metrics=['accuracy'])
x = np.random.normal(size=(10, 784))
y = np.random.choice(2, size=(10, 4))
# suppose that the output(after sigmoid) is:
output = np.array([[0.8, 0.1, 0.9, 0.9]])
# suppose that ground truth is:
target = np.array([[1, 0, 1, 1]])
print("accuracy: ", Accuracy()(y[:1], output).numpy())
# print("accuracy_score: ", accuracy_score(y[:1], output))
精度=0。我希望精度将应用一种阈值来对S型曲线的输出进行二值化。在这种情况下,我发现精度并不能真正反映出正确的精度!