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

时间:2018-07-18 01:15:44

标签: python machine-learning linear-regression

我已经实现了具有梯度下降的线性回归,但是当我需要获得递减函数或常数函数时,它不起作用。

它适用于

之类的数据
x_train = np.array([30,60,70,100])
y_train= np.array([60,120,145,195])

我需要在此处获得递增功能,例如f(x_train) ≈ 2*x_train=y_train

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

f(x_train)=x_train=y_train在哪里,但不适用于这样的数据

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

减小函数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()

我尝试了其他一些算法,但是没有用。 预先感谢

2 个答案:

答案 0 :(得分:0)

我修改了一些部分。 确定合适的learning_rate和迭代时期非常重要。 那有些困难。 因此,我们使用了类似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()

答案 1 :(得分:0)

您可以使用自适应学习率:

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)

此实现适用于多维X