Keras自定义精度指标用于列表输出

时间:2017-02-18 03:22:29

标签: deep-learning keras evaluation mnist

我有一个预测SVHN数据集中数字的模型。我想我可能需要一个自定义精度指标,因为即使整个序列不正确,某些数字也是正确的。这是代码和示例输出。有谁知道如何创建自定义指标?

batch_size2 = 128
nb_classes2 = 11 #change number of classes
nb_epoch2 = 2

img_rows2 =32 #change input size
img_cols2=32
img_channels2 = 1

model_input2=Input(shape=(img_rows2, img_cols2, img_channels2))

x2 = Convolution2D(32, 3, 3, border_mode='same')(model_input2)
x2 = Activation('relu')(x2)
x2 = Convolution2D(32, 3, 3)(x2)
x2 = Activation('relu')(x2)
x2 = MaxPooling2D(pool_size=(2, 2))(x2)
x2 = Dropout(0.25)(x2)
conv_out2 = Flatten()(x2)

x12 = Dense(nb_classes2, activation='softmax')(conv_out2)
x22 = Dense(nb_classes2, activation='softmax')(conv_out2)
x32 = Dense(nb_classes2, activation='softmax')(conv_out2)
x42 = Dense(nb_classes2, activation='softmax')(conv_out2)
x52 = Dense(nb_classes2, activation='softmax')(conv_out2)
#x62 = Dense(nb_classes2, activation='softmax')(conv_out2)

lst2 = [x12, x22, x32, x42, x52]

#model = Model(input=model_input, output=lst)
model2 = Model(input=model_input2, output=lst2)

model2.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

model2.fit(train_dataset,[tr_02, tr_12, tr_22, tr_32, tr_42], batch_size=batch_size, nb_epoch=nb_epoch, verbose=1) 

ypred_svhn = model2.predict(test_dataset)



 for n in range (0,10):
        print('predicted digits:', ypred_svhn[0][n].argmax(), ypred_svhn[1][n].argmax(), ypred_svhn[2][n].argmax(), ypred_svhn[3][n].argmax(), ypred_svhn[4][n].argmax(), ypred_svhn[5][n].argmax())
        print('actual digits:', test_labels[n])


predicted digits: 1 5 10 10 10 10
actual digits: [ 1  5 10 10 10 10]

predicted digits: 3 2 0 0 10 10
actual digits: [ 3  2  1  0 10 10]

predicted digits: 2 6 7 10 10 10
actual digits: [ 1  6 10 10 10 10]

predicted digits: 1 1 10 10 10 10
actual digits: [ 1  1 10 10 10 10]

predicted digits: 1 1 10 10 10 10
actual digits: [ 1  9 10 10 10 10]

predicted digits: 1 1 10 10 10 10
actual digits: [ 1  1 10 10 10 10]

predicted digits: 3 1 8 3 10 10
actual digits: [ 3  1  8  3 10 10]

predicted digits: 2 6 8 10 10 10
actual digits: [ 2  6  5 10 10 10]

predicted digits: 3 1 4 4 10 10
actual digits: [ 3  1  4  4 10 10]

predicted digits: 2 1 6 10 10 10
actual digits: [ 2  1  6 10 10 10]

1 个答案:

答案 0 :(得分:1)

以下将计算个别数字的准确性:

def new_accuracy(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 2).T == labels) / predictions.shape[1] 
          / predictions.shape[0])