2017-02-18 24 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

你甚至谷歌呢? 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])