我试图在Keras中实现真正的积极指标:
def TP(y_true, y_pred):
estimated = K.argmax(y_pred, axis=1)
truth = K.argmax(y_true, axis=1)
TP = K.sum(truth * estimated)
return TP
基于我的最后一层输出形状:(batch,2)。 该功能已经在numpy argmax等效项下进行了测试,并且运行良好。
我使用了一个cross_entropy损失函数,每个时期给我度量值。但是这个值怎么可能是十进制数??我在做什么错呢?谢谢!
已编辑:以下是Keras模型的示例代码:
def TP(y_true, y_pred):
estimated = K.argmax(y_pred, axis=1)
truth = K.argmax(y_true, axis=1)
TP = K.sum(truth * estimated)
return TP
epochs = 10
batch_size = 2
model = Sequential([
Dense(32, input_shape=(4,)),
Activation('relu'),
Dense(2),
Activation('softmax'),
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy', TP])
model.summary()
train = np.array([[17,0,1,0],[17,0,1,0],[17,0,1,0],[17,0,1,0],[17,0,1,0], [2,1,0,1],[0,1,0,1],[0,1,0,1],[0,1,0,1],[0,1,0,1]])
labels = np.array([ [1,0],[1,0],[1,0],[1,0],[1,0], [0,1],[0,1],[0,1],[0,1],[0,1] ])
model.fit(train, labels, epochs=epochs, batch_size=batch_size, verbose=2)
在这里,显示 TP 功能的测试似乎有效
def npTP(y_true, y_pred):
estimated = np.argmax(y_pred, axis=1)
truth = np.argmax(y_true, axis=1)
TP = np.sum(truth * estimated)
return TP
y_true = np.array([ [1,0],[1,0],[1,0],[1,0],[1,0], [0,1],[0,1],[0,1],[0,1],[0,1] ])
y_pred = np.array([ [0,1],[0,1],[0,1],[0,1],[0,1], [0,1],[0,1],[0,1],[0,1],[0,1]])
print("np check : ")
print(npTP(y_true, y_pred))
运行此代码将给出以下输出:
Using TensorFlow backend.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 32) 160
_________________________________________________________________
activation_1 (Activation) (None, 32) 0
_________________________________________________________________
dense_2 (Dense) (None, 2) 66
_________________________________________________________________
activation_2 (Activation) (None, 2) 0
=================================================================
Total params: 226
Trainable params: 226
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
- 0s - loss: 0.3934 - acc: 0.6000 - TP: 0.2000
Epoch 2/10 ^^^^^^^^^^ here are the decimal values
- 0s - loss: 0.3736 - acc: 0.6000 - TP: 0.2000
Epoch 3/10 ^^^^^^^^^^
- 0s - loss: 0.3562 - acc: 0.6000 - TP: 0.2000
Epoch 4/10 ^^^^^^^^^^
- 0s - loss: 0.3416 - acc: 0.7000 - TP: 0.4000
Epoch 5/10 ^^^^^^^^^^
- 0s - loss: 0.3240 - acc: 1.0000 - TP: 1.0000
Epoch 6/10
- 0s - loss: 0.3118 - acc: 1.0000 - TP: 1.0000
Epoch 7/10
- 0s - loss: 0.2960 - acc: 1.0000 - TP: 1.0000
Epoch 8/10
- 0s - loss: 0.2806 - acc: 1.0000 - TP: 1.0000
Epoch 9/10
- 0s - loss: 0.2656 - acc: 1.0000 - TP: 1.0000
Epoch 10/10
- 0s - loss: 0.2535 - acc: 1.0000 - TP: 1.0000
np check :
5
谢谢!
答案 0 :(得分:0)
正如desertnaut所指出的,in this thread解释了答案。
这里有batch_size=2
和10个样本,每个时期进行5次训练(10/2=5
)。
要了解时期1的输出指标,在5次训练后TP
的总数必须为1,因此该指标为1/5 = 0.2
。纪元4在5次训练中有2个TP,给出了指标2/5 = 0.4
。