1
我正在使用keras後端來構建自定義指標。第一步是將PROBA張量(SOFTMAX的輸出)轉換爲分類數據 如:keras backend proba to categorical
from keras import backend as K
y_pred = K.variable([[0.7, 0.2, 0.1],[0.2, 0.8, 0],[0.2,0.2,0.6],[0.9,0.05,0.05]])
K.eval(y_pred)
給人
array([[ 0.69999999, 0.2 , 0.1 ],
[ 0.2 , 0.80000001, 0. ],
[ 0.2 , 0.2 , 0.60000002],
[ 0.89999998, 0.05 , 0.05 ]], dtype=float32)
我想獲得:
array([[ 1, 0, 0],
[ 0, 1, 0],
[ 0, 0, 1],
[ 1, 0, 0]], dtype=float32)
我發現K.argmax函數,但不知道如何用它來替換張量中的值。
max_index = K.cast(K.argmax(y_pred, 1), "int32")
lol_index = K.arange(y_pred.shape[0], dtype="int32")
y_pred[index, max_index]
給出:
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_32' (op: 'StridedSlice') with input shapes: [4,3], [2,4], [2,4], [2].
這裏差不多螺紋:https://stackoverflow.com/questions/44553722/restricting-輸出值在keras/44554112#44554112 – petezurich
這已被回答,儘管對於不同的問題,在這裏:https://stackoverflow.com/a/20295159/4895899 –
我在說的是keras後端,它與numpy不一樣。 –