ログ回帰用のミニバッチ勾配降下を作成しようとしています。
X_batch
( shape の(n_samples, n_features)
) およびy_batch
( shape の) numpy 行列が与えられ(n_samples,)
ます。
素朴な方法は、ループを書くことです:
def calc_loss_grad(self, X_batch, y_batch):
n_samples, n_features = X_batch.shape
loss_grad = np.zeros((n_features,))
for i in range(n_samples):
sigm = sigmoid(X_batch[i] @ self.weights)
loss_grad += - (y_batch[i] - sigm) * X_batch[i]
return loss_grad
しかし、ループを使用することは速度に関して悪い考えのようです。より良い方法はありますか?ループのない純粋なnumpy? 何とかグラデーションの式を書き換えますか?