I have written function for gradient descent and used pandas to read csv file. But when I use data read by pandas, the function returns “nan”. I can’t understand why.
Thanks in advance.
JavaScript
x
26
26
1
def gradient_descent(X, Y, w, b, alpha):
2
3
dl_dw = 0.0
4
dl_db = 0.0
5
6
N = len(X)
7
8
for i in range(N):
9
dl_dw += -1*float(X[i]) * (float(Y[i]) - (w*float(X[i]) + b))
10
dl_db += -1*(float(Y[i]) - (float(w*X[i]) + b))
11
12
13
w = w - (1/float(N)) * dl_dw * alpha
14
b = b - (1/float(N)) * dl_db * alpha
15
16
return w, b
17
18
import pandas as pd
19
data = pd.read_csv("train.csv")
20
print(data.head())
21
22
X = data.iloc[:, 0].values.reshape(-1, 1)
23
Y = data.iloc[:, 1].values.reshape(-1, 1)
24
25
print(gradient_descent(X, Y, 0.0, 0.0, 100))
26
Advertisement
Answer
It might be a vanishing gradient problem. You gradients might be very close or even zero. Try to initialize your weights with non zero values.