Gradient becoming larger and larger while implementing gradient descent

时间:2018-01-19 19:31:32

标签: python numpy machine-learning linear-regression gradient-descent

I am trying to implement the gradient descent in python. The data is that of housing prices and i want to predict the house price. But the problem is that the gradient is becoming larger and larger until python cannot process it anymore.

import numpy as np
import sys
from numpy import genfromtxt

train_file = sys.argv[1]
dev_file = sys.argv[2]
learning = sys.argv[3]

X_train = np.genfromtxt(train_file, dtype='f', delimiter = ',', skip_header 
= 1,filling_values  = 0, usecols = 
(3,4,5,6,8,9,10,11,12,13,14,15,17,18,19,20))
y_train = np.genfromtxt(train_file, dtype='f', delimiter = ',', skip_header 
= 1,filling_values = 0, usecols = (2))

training_examples = X_train.shape[0]
total_featues = X_train.shape[1]

Wprime = np.asarray([0]*total_featues)
W = Wprime.reshape(-1,1)


k = 0
while k<total_featues :
    i=0
    temp_sum = 0
    #print(X_train[i][k])
    while i< training_examples:
        A = Wprime
        B = X_train[i]
        #print(y_train[i])
        f = abs(np.dot(A,B)-y_train[i])
        #print("this is f"+str(f))
        f = f*X_train[i][k]
        temp_sum = temp_sum + f
        i=i+1
    print("this is temp sum " + str(temp_sum))
    update = temp_sum*0.0001/training_examples
    print("this is update "+str(update))
    Wprime[k] = Wprime[k] - update
    print(Wprime)
    k = k+1

Can anyone help me out?

0 个答案:

没有答案