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.
def gradient_descent(X, Y, w, b, alpha):
dl_dw = 0.0
dl_db = 0.0
N = len(X)
for i in range(N):
dl_dw += -1*float(X[i]) * (float(Y[i]) - (w*float(X[i]) + b))
dl_db += -1*(float(Y[i]) - (float(w*X[i]) + b))
w = w - (1/float(N)) * dl_dw * alpha
b = b - (1/float(N)) * dl_db * alpha
return w, b
import pandas as pd
data = pd.read_csv("train.csv")
print(data.head())
X = data.iloc[:, 0].values.reshape(-1, 1)
Y = data.iloc[:, 1].values.reshape(-1, 1)
print(gradient_descent(X, Y, 0.0, 0.0, 100))
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.