E
E
Egor Mikheev2016-04-08 16:25:51
Mathematics
Egor Mikheev, 2016-04-08 16:25:51

How to form a correct stochastic gradient descent algorithm?

Hello, it is not possible to implement the execution of the step algorithm for stochastic gradient descent.
Here is the model I used:
33c354eb086d44dd8a7548fe4ece0e1e.png
Here is the step algorithm:

def stochastic_gradient_step(X, y, w, train_ind, eta=0.01):
    r = []
    X_train = X[train_ind]
   
    r.append(w[0] - 0.5 * eta * X_train[0] * (w[0] * X_train[0] + w[1] * X_train[1] + w[2] * X_train[2] + w[3]* X_train[3] - y[train_ind]))
    r.append(w[1] - 0.5 * eta * X_train[1] * (w[0] * X_train[0] + w[1] * X_train[1] + w[2] * X_train[2] + w[3]* X_train[3] - y[train_ind]))
    r.append(w[2] - 0.5 * eta * X_train[2] * (w[0] * X_train[0] + w[1] * X_train[1] + w[2] * X_train[2] + w[3]* X_train[3] - y[train_ind]))
    r.append(w[3] - 0.5 * eta * X_train[3] * (w[0] * X_train[0] + w[1] * X_train[1] + w[2] * X_train[2] + w[3]* X_train[3] - y[train_ind]))
    return r

I suspect the formula is wrong.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
t_ray, 2016-04-09
@ogregor

You can, for example, like this:
def stochastic_gradient_step(X, y, w, train_ind, eta=0.01):
l=len(y)
return w+(2*eta*X[train_ind]/l)*(y[train_ind]-sum ((xi*wi) for xi,wi in zip(X[train_ind],w)))
If I understand correctly what problem you are solving :)
But I have a similar problem - gradient descent works, the resulting weight vector is "like the truth", the mean square error function is implemented correctly. But the answer is no!

W
Wedoslaw, 2016-05-14
@Wedoslaw

There is a typo in the task, if nothing is corrected in the task, then you need to apply a different formula:
b695cb4b3e2d46449f7e66728c368ce1.png

B
blackchekist, 2016-12-13
@blackchekist

the topicstarter does not have a division by l (in your case by 4). Without this, the gradient is incorrectly calculated, so there will be no descent.

P
PavelBortnikov, 2017-02-05
@PavelBortnikov

matrunich sergey Also faced this problem. Can you share what was the problem? Already checked everything...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question