略有不同,您可以使用Keras後端的API輕鬆實現邏輯..這也將確保你的度量上tensorflow和theano工作兩者。
與測試
這裏:
import numpy as np
import keras
from keras import backend as K
shift = 2
def custom_metric(y_true,y_pred):
diff = K.abs(K.argmax(y_true, axis=-1) - K.argmax(y_pred, axis=-1))
return K.mean(K.lesser_equal(diff, shift))
t1 = np.asarray([ [0,0,0,0,0,0,1,0,0,],
[0,0,0,0,0,0,1,0,0,],
[0,0,0,0,0,0,1,0,0,],
[0,0,0,0,0,0,1,0,0,],
[0,0,0,0,0,0,1,0,0,],
[0,0,0,0,0,0,1,0,0,],
])
p1 = np.asarray([ [0,0,0,0,0,1,0,0,0,],
[0,0,0,0,1,0,0,0,0,],
[0,0,0,0,0,0,0,1,0,],
[0,0,0,0,0,0,0,0,1,],
[1,0,0,0,0,0,0,0,0,],
[0,0,0,0,0,0,1,0,0,],
])
print K.eval(keras.metrics.categorical_accuracy(K.variable(t1),K.variable(p1)))
print K.eval(custom_metric(K.variable(t1),K.variable(p1)))
現在在你的compile
語句中使用它:metrics=custom_metric
你建立迴歸模型的一個真實值輸出,帶20點的尺寸爲每分鐘一個分類模型。 – indraforyou
@indraforyou具有20個維度的分類模型。 – Pratyush