for我的代碼中有一個特定的回圈(迭代次數為 100)。不知何故,代碼卡在迭代次數 34/100 處。我很確定這個問題與我在回圈中使用的這兩個函式有關,
def sigmoid(self, a: np.float128):
"""
inputs:
v: float
returns:
the logistic sigmoid evaluated at a
"""
return 1 / (1 scipy.special.expit(-a))
# return 1 / (1 np.exp(-a))
def loss(self, w, X, y):
"""
inputs: w: an array of the current weights, of shape (d,)
X: an array of n datapoints, of shape (n, d)
outputs: the loss. This is exactly the negative log likelihood
"""
E = 10 ** (-8)
y_pred = self.sigmoid(np.dot(X, w))
cost = -np.sum(y * np.log(y_pred E) (1 - y) * np.log(1 - y_pred E))
return cost
代碼一開始就給出了警告:
<ipython-input-65-26bf85bda945>:72: RuntimeWarning:
overflow encountered in exp
所以我通過使用scipy.special.expit(x)而不是np.exp(x). 但這似乎并沒有解決回圈問題。有任何想法嗎?
uj5u.com熱心網友回復:
不要替換exp為expit; 替換sigmoid為expit. 是邏輯 sigmoid 函式。scipy.special.expit
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394351.html
