2 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
代替:
error[:, np.argmax(label, axis=1)] -=1
和:
error[np.arange(error.shape[0]), np.argmax(label, axis=1)] -=1
而且當(dāng)然
loss = error[np.arange(error.shape[0]), np.argmax(label, axis=1)].sum()
在您的示例中,您正在更改、求和、error[0,3]
和error[1,1]
,或簡(jiǎn)而言之error[[0,1],[3,1]]
。

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
也許這個(gè):
import numpy as np
error = np.array([[0.32783139, 0.29204386, 0.0572163 , 0.96162543, 0.8343454 ],
[0.67308787, 0.27715222, 0.11738748, 0.091061 , 0.51806117]])
label= np.array([[0, 0, 0, 1, 0 ],
[0, 1, 0, 0, 0]])
def f(error, label):
per_ts_loss=0
t=np.zeros(error.shape)
argma=np.argmax(label, axis=1)
t[[i for i in range(error.shape[0])],argma]=-1
print(t)
error+=t
per_ts_loss += error[[i for i in range(error.shape[0])],argma]
f(error, label)
輸出:
[[ 0. 0. 0. -1. 0.]
[ 0. -1. 0. 0. 0.]]
添加回答
舉報(bào)