简体   繁体   English

线性回归,梯度下降不适用于某些值

[英]Linear regression, gradient descent not working for some values

I have implemented linear regression with gradient descent but it doesn't work when I need to get decreasing function or constant function. 我已经实现了带有梯度下降的线性回归,但是当我需要获得递减函数或常数函数时它不起作用。

It works for data like 它适用于像

x_train = np.array([30,60,70,100])
y_train= np.array([60,120,145,195])

where I need to get increasing function like here f(x_train) ≈ 2*x_train=y_train or for data like 在这里我需要增加函数f(x_train) ≈ 2*x_train=y_train或用于类似数据

x_train = np.array([30,60,70,100])
y_train= np.array([30,60,70,100])

where is f(x_train)=x_train=y_train , but it doesn't work for data like this f(x_train)=x_train=y_train ,但不适用于此类数据

x_train = np.array([50,100,150,200])
y_train= np.array([150,100,50,0])

decreasing function f(x_train)=200-x_train=y_train or for constant function f(x_train)=100=y_train 递减函数f(x_train)=200-x_train=y_train或对于恒定函数f(x_train)=100=y_train

x_train = np.array([50,100,150,200])
y_train= np.array([100,100,100,100])

import numpy as np
import matplotlib.pyplot as plt

#doesnt work
#x_train = np.array([50,100,150,200])
#y_train= np.array([150,100,50,0])

#work
x_train = np.array([30,60,70,100])
y_train= np.array([60,120,145,195])

def model(x,w,b):
    return x*w+b;

def cost(y,y_hat):
    return np.sum((y-y_hat)**2)

learning_rate=0.000001
def trainning_round(x_train,y_train,w,b,learning_rate):

    y_hat=model(x_train,w,b)
    print(cost(y_train,y_hat))

    w_gradient=-2*x_train.dot(y_train-y_hat)
    b_gradient=-2*np.sum(y_train-y_hat)

    w-=learning_rate*w_gradient
    b-=learning_rate*b_gradient

    return w,b

num_epoch=200
def train(X,Y):

    w=0
    b=0

    #for plt
    ar = np.arange(0, 200, 0.5)
    def f(t):
        return t*w+b

    for i in range(num_epoch):
            w,b=trainning_round(X,Y,w,b,learning_rate)
            plt.plot(ar,f(ar))
            plt.axis([0, 200, 0, 200])

    plt.plot(X, Y, 'ro')

train(x_train,y_train)
plt.show()

I have tried some other algorithms but they didn't work. 我尝试了其他一些算法,但是它们没有用。 Thanks in advance 提前致谢

I modified some parts. 我修改了一些部分。 It is important that you decide appropriate learning_rate and iteration epoch. 确定合适的learning_rate和迭代时期非常重要。 That's some difficult. 那有些困难。 So we use a framework like tensorflow. 因此我们使用了一个类似tensorflow的框架。

import numpy as np
import matplotlib.pyplot as plt

#doesnt work
x_train = np.array([50,100,150,200])
y_train= np.array([150,100,50,0])

#work
# x_train = np.array([30,60,70,100])
# y_train= np.array([60,120,145,195])

def model(x,w,b):
    return x*w+b;

def cost(y,y_hat):
    return np.sum((y-y_hat)**2)/y.size

learning_rate=0.0001
def trainning_round(x_train,y_train,w,b,learning_rate):

    y_hat=model(x_train,w,b)
    j = cost(y_train,y_hat)

    # w_gradient=-2*x_train.dot(y_train-y_hat)
    # b_gradient=-2*np.sum(y_train-y_hat)
    w_gradient=x_train.dot(y_hat-y_train) / y_train.size
    b_gradient=np.sum(y_hat-y_train) / y_train.size

    print(w_gradient, b_gradient)

    w=w-learning_rate*w_gradient
    b=b-learning_rate*b_gradient
    print(j, w,b)
    return w,b

num_epoch=200000
def train(X,Y):

    w=2.1
    b=1.5

    #for plt
    ar = np.arange(0, 200, 0.5)

    for i in range(num_epoch):
        w,b=trainning_round(X,Y,w,b,learning_rate)

    plt.plot(ar,model(ar, w, b))
    plt.axis([0, 300, 0, 200])

    plt.plot(X, Y, 'ro')

train(x_train,y_train)
plt.show()

You can use an adaptive learning rate: 您可以使用自适应学习率:

def optimal_learning_rate(X,y,W):
    grad = -np.matmul(X.T,y-np.matmul(X,W))/len(y)
    hessian = np.matmul(X.T,X)
    return np.matmul(grad.T,grad)/np.matmul(np.matmul(grad.T,hessian,grad)

This implementation is for a multidimensional X 此实现适用于多维X

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

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