OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why are these simple linear regression weights gradient numpy calculations giving different results?

  • Thread starter Thread starter sherif hesham
  • Start date Start date
S

sherif hesham

Guest
for the weights gradient calculations, they are giving different results for the same parameters.

Code:
# defining the training set
X_train = np.array([[2104, 5, 1, 45], [1416, 3, 2, 40], [852, 2, 1, 35]])
y_train = np.array([460, 232, 178])
b_init = 785.1811367994083
w_init = np.array([ 0.39133535, 18.75376741, -53.36032453, -26.42131618])

Approach (1)

Code:
def dj_dw(w,x,b,y):
    # number of examples
    m = x.shape[0]
    dj_dw = (1/m)*(np.dot(np.transpose(np.dot(x,w)+b-y),x))
    return dj_dw

Approach (2)

Code:
def dj_dw_2(w, x, b, y):
    m, n = x.shape
    dj_dw = np.zeros(n)
    for j in range(n):
        for i in range(m):
            dj_dw[j] += (1/m) * ((w[j]*x[i][j] + b - y[i]) * (x[i][j]))
    return dj_dw

and the results respectively

Code:
[-2.72623574e-03 -6.27197255e-06 -2.21745574e-06 -6.92403377e-05]
[ 1.59529824e+06  1.73748484e+03  5.72854200e+02 -2.33772157e+04]
<p>for the weights gradient calculations, they are giving different results for the same parameters.</p>
<pre><code># defining the training set
X_train = np.array([[2104, 5, 1, 45], [1416, 3, 2, 40], [852, 2, 1, 35]])
y_train = np.array([460, 232, 178])
b_init = 785.1811367994083
w_init = np.array([ 0.39133535, 18.75376741, -53.36032453, -26.42131618])
</code></pre>
<p>Approach (1)</p>
<pre><code>def dj_dw(w,x,b,y):
# number of examples
m = x.shape[0]
dj_dw = (1/m)*(np.dot(np.transpose(np.dot(x,w)+b-y),x))
return dj_dw
</code></pre>
<p>Approach (2)</p>
<pre><code>def dj_dw_2(w, x, b, y):
m, n = x.shape
dj_dw = np.zeros(n)
for j in range(n):
for i in range(m):
dj_dw[j] += (1/m) * ((w[j]*x[j] + b - y) * (x[j]))
return dj_dw
</code></pre>
<p>and the results respectively</p>
<pre><code>[-2.72623574e-03 -6.27197255e-06 -2.21745574e-06 -6.92403377e-05]
[ 1.59529824e+06 1.73748484e+03 5.72854200e+02 -2.33772157e+04]
</code></pre>
 

Latest posts

Top