Gradient Descent Algorithm的Python实现不起作用

时间:2015-06-10 01:34:44

标签: python-2.7 gradient-descent

我正在尝试实现简单线性回归的梯度下降算法。出于某种原因,它似乎并没有起作用。

from __future__ import division
import random

def error(x_i,z_i, theta0,theta1):
    return z_i - theta0 - theta1 * x_i

def squared_error(x_i,z_i,theta0,theta1):
    return error(x_i,z_i,theta0,theta1)**2

def mse_fn(x, z, theta0,theta1):
    m = 2 * len(x)
    return sum(squared_error(x_i,z_i,theta0,theta1) for x_i,z_i in zip(x,z)) / m

def mse_gradient(x, z, theta0,theta1):
    m = 2 * len(x)
    grad_0 = sum(error(x_i,z_i,theta0,theta1) for x_i,z_i in zip(x,z)) / m
    grad_1 = sum(error(x_i,z_i,theta0,theta1) * x_i for x_i,z_i in zip(x,z)) / m
    return grad_0, grad_1


def minimize_batch(x, z, mse_fn, mse_gradient_fn, theta0,theta1,tolerance=0.000001):
    step_sizes = 0.01    
    theta0 = theta0
    theta1 = theta1
    value = mse_fn(x,z,theta0,theta1)
    while True:
        grad_0, grad_1 = mse_gradient(x,z,theta0,theta1)
        next_theta0 =  theta0 - step_sizes * grad_0
        next_theta1 =  theta1 - step_sizes * grad_1
        next_value = mse_fn(x,z,next_theta0,theta1)
        if abs(value - next_value) < tolerance:
            return theta0, theta1
        else:
            theta0, theta1, value = next_theta0, next_theta1, next_value



#The data 
x = [i + 1 for i in range(40)]
y = [random.randrange(1,30) for i in range(40)]
z = [2*x_i + y_i + (y_i/7) for x_i,y_i in zip(x,y)]
theta0, theta1 = [random.randint(-10,10) for i in range(2)]
q = minimize_batch(x,z,mse_fn, mse_gradient, theta0,theta1,tolerance=0.000001)

当我跑步时,我收到以下错误:      返回错误(x_i,z_i,theta0,theta1)** 2溢出错误:(34,&#39;结果太大&#39;)

0 个答案:

没有答案