简体   繁体   English

无法在梯度下降算法中更新 python 中的数组

[英]Unable to update array in python in gradient descent algorithm

In this code I have produced a dataset using gaussian distribution and then I have tried to apply stochastic gradient descent In each iteration, I am updating the theta array.在这段代码中,我使用高斯分布生成了一个数据集,然后我尝试应用随机梯度下降在每次迭代中,我都在更新 theta 数组。 But, it is not getting updated.但是,它没有得到更新。 It remains zero after every iteration.每次迭代后它都保持为零。 Gradient is non zero.梯度不为零。 But still, theta is not updated但是,theta 仍然没有更新

Help me please请帮帮我

import numpy as np # linear algebra
import pandas as pd 
# data processing, CSV file I/O (e.g. pd.read_csv)
import math
import random
import matplotlib.pyplot as plt

#generating random samples

theta=np.array([3,1,2])

    

X=[]
E=[]
Y=[]
a1=3
v1=4
a2=-1
v2=4
v3=2


for i in range (0,1000000):
    x1=(1/math.sqrt(2*3.14*v1))* math.exp(-(random.random()-a1)**2/(2*v1))
    x2=(1/math.sqrt(2*3.14*v2))* math.exp(-(random.random()-a2)**2/(2*v2))
    X.append([x1,x2])
    e=(1/math.sqrt(2*3.14*v3))* math.exp(-(random.random())**2/(2*v3))
    y=theta[0]+theta[1]*x1+theta[2]*x2 + e
    Y.append(y)
    E.append(e)


#Now Applying Stochastic Gradient
##Batch_Size = 1
r=1
learning_rate=0.001
theta=np.array([0,0,0])


theta = theta.reshape(3,1)

X= pd.DataFrame(X,columns=['X1','X2'])
Y=pd.DataFrame(Y,columns=['Y'])
Y.head()

from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,Y,test_size=40,random_state=50)

X_train['X0']=np.ones(len(X_train))
y_train.head()

def gradient_descent(x,y,theta,lr):
    m=len(y)
    prediction=(x.dot(theta)).to_numpy()
    gradient = prediction-y.to_numpy()
    current_cost= (1/2*m)*np.sum(np.square(prediction-y.to_numpy()) )
       
    return gradient,current_cost

n_iterations=1000
import random
theta_history=[]
cost_history=[]
for i in range(0,n_iterations):
    
    xi=X_train.sample(r)
    yi=y_train.sample(r)
    
    
    m=len(xi)
    gradient,current_cost= gradient_descent(xi,yi,theta,learning_rate)
    theta[0]= theta[0]-learning_rate* 
    ((1/m)*np.sum(np.multiply(gradient,xi['X1'].to_numpy().reshape(m,1))))  
    theta[1]= theta[1]-learning_rate* 
    ((1/m)*np.sum(np.multiply(gradient,xi['X2'].to_numpy().reshape(m,1))))
    theta[2]= theta[2]-learning_rate* 
    ((1/m)*np.sum(np.multiply(gradient,xi['X0'].to_numpy().reshape(m,1))))
    print("theta=",theta)
    
    theta_history.append(theta)
    cost_history.append(current_cost)
    
    
    
    if prev_index>=len(X_train):
        break  


You reassigned theta on line 34 to empty values: theta=np.array([0,0,0])您将第 34 行的theta重新分配为空值: theta=np.array([0,0,0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM