2017-02-18 26 views
1

У меня есть модель, которая предсказывает цифры из набора данных SVHN. Я думаю, мне может понадобиться специальная метрика точности, потому что некоторые цифры правильные, даже если вся последовательность неверна. Вот код и образец вывода. Кто-нибудь знает, как я могу создать индивидуальный показатель?Точная метрика точности Keras для вывода списка

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] 
+0

Вы даже Google это? https://keras.io/metrics/#custom-metrics В противном случае я считаю, что точность фиксирует хорошие в последовательности с категориальной кроссентропией. Возможно, попробуйте только один пример из вашего тестового набора и предскажите его точность, чтобы увидеть, как он работает. –

ответ

1

Следующая рассчитает точность отдельных цифр:

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