Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在线性回归上使用梯度下降会产生不正确的偏差_Python_Machine Learning - Fatal编程技术网

Python 在线性回归上使用梯度下降会产生不正确的偏差

Python 在线性回归上使用梯度下降会产生不正确的偏差,python,machine-learning,Python,Machine Learning,我有一个玩具的例子,一个线性回归模型,有一个输入变量和一个输出变量。我遇到的问题是,偏差的输出与生成的数据相差很远。如果我手动设置偏差,那么它将产生一个足够接近原始值的权重和偏差 我已经编写了两段代码genu data,用于生成数据和GradientDescent用于执行梯度下降算法以找到权重和偏差 def gen_data(num_points=50, slope=1, bias=10, x_max=50): f = lambda z: slope * z + bias x =

我有一个玩具的例子,一个线性回归模型,有一个输入变量和一个输出变量。我遇到的问题是,偏差的输出与生成的数据相差很远。如果我手动设置偏差,那么它将产生一个足够接近原始值的权重和偏差

我已经编写了两段代码
genu data
,用于生成数据和
GradientDescent
用于执行梯度下降算法以找到权重和偏差

def gen_data(num_points=50, slope=1, bias=10, x_max=50):
    f = lambda z: slope * z + bias
    x = np.zeros(shape=(num_points, 1))
    y = np.zeros(shape=(num_points, 1))

    for i in range(num_points):
        x_temp = np.random.uniform()*x_max
        x[i] = x_temp
        y[i] = f(x_temp) + np.random.normal(scale=3.0)

    return (x, y)

# \mathbb{R}^1 with no regularization
def gradientDescent2(x, y, learning_rate=0.0001, epochs=100):
    theta = np.random.rand()
    bias = np.random.rand()

    for i in range(0, epochs):
        loss = (theta * x + bias) - y
        cost = np.mean(loss**2) / 2
        # print('Iteration {} | Cost: {}'.format(i, cost))

        grad_b = np.mean(loss)
        grad_t = np.mean(loss*x)

        # updates
        bias -= learning_rate * grad_b
        theta -= learning_rate * grad_t

    return (theta, bias)
  • 如果要使用批处理更新,请不要将批处理大小设置为简单大小。(我还认为batch_update不太适合这种情况。)
  • 2.您的梯度计算和参数更新不正确,梯度应为:

    grad_b =  1
    grad_t =  x
    
    对于参数更新,您应该始终尝试最小化
    损失
    ,因此应该

    if loss>0:
      bias -= learning_rate * grad_b
      theta -= learning_rate * grad_t
    elif loss< 0:
      bias += learning_rate * grad_b
      theta += learning_rate * grad_t
    
    如果损失>0:
    偏差-=学习率*梯度
    θ-=学习率*梯度
    elif损失<0:
    偏差+=学习率*梯度
    θ+=学习率*梯度
    
    毕竟,下面是修改后的代码运行良好。 将numpy作为np导入 导入系统

    def gen_data(num_points=500, slope=1, bias=10, x_max=50):
        f = lambda z: slope * z + bias
        x = np.zeros(shape=(num_points))
        y = np.zeros(shape=(num_points))
    
        for i in range(num_points):
            x_temp = np.random.uniform()*x_max
            x[i] = x_temp
            y[i] = f(x_temp) #+ np.random.normal(scale=3.0)
            #print('x:',x[i],'        y:',y[i])
    
        return (x, y)
    
    def gradientDescent2(x, y, learning_rate=0.001, epochs=100):
        theta = np.random.rand()
        bias = np.random.rand()
    
        for i in range(0, epochs):
          for j in range(len(x)):
            loss = (theta * x[j] + bias) - y[j]
            cost = np.mean(loss**2) / 2
            # print('Iteration {} | Cost: {}'.format(i, cost))
    
            grad_b = 1
            grad_t = x[j]
    
            if loss>0:
                bias -= learning_rate * grad_b
                theta -= learning_rate * grad_t
            elif loss< 0:
                bias += learning_rate * grad_b
                theta += learning_rate * grad_t
    
        return (theta, bias)
    
    def main():
        x,y =gen_data()
        ta,bias = gradientDescent2(x,y)
        print('theta:',ta)
        print('bias:',bias)
    
    if __name__ == '__main__':
        sys.exit(int(main() or 0))
    
    def gen_数据(num_points=500,slope=1,bias=10,x_max=50):
    f=λz:斜率*z+偏差
    x=np.0(形状=(点数))
    y=np.0(形状=(点数))
    对于范围内的i(num_点):
    x_temp=np.random.uniform()*x_max
    x[i]=x_温度
    y[i]=f(x_温度)#+np.随机.正常(标度=3.0)
    #打印('x:',x[i],'y:',y[i])
    返回(x,y)
    def gradientDescent2(x,y,学习率=0.001,时代=100):
    θ=np.random.rand()
    偏差=np.random.rand()
    对于范围内的i(0,历元):
    对于范围内的j(len(x)):
    损耗=(θ*x[j]+偏压)-y[j]
    成本=净现值平均值(损失**2)/2
    #打印('Iteration{}| Cost:{}'。格式(i,Cost))
    梯度b=1
    梯度t=x[j]
    如果损失>0:
    偏差-=学习率*梯度
    θ-=学习率*梯度
    elif损失<0:
    偏差+=学习率*梯度
    θ+=学习率*梯度
    返回(θ,偏差)
    def main():
    x、 y=发电机数据()
    ta,偏差=梯度下降2(x,y)
    打印('θ:',ta)
    打印('偏差:',偏差)
    如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
    sys.exit(int(main()或0))
    
    grad\u t=\frac{\partial Cost}{\partial\theta}=\frac{1}{n}loss\times\frac{\partial loss}{\partial\theta}=\frac{1}{n}\times loss\times x\neq\theta。你是如何计算梯度θ的?@Lukasz显然,梯度是错误的,但简单地将我的答案标记为无用是不酷的。关于损失,我回答的第二部分很有意义。另外,我会更新我的答案。顺便说一句,如果你想和别人交流,请先让你的评论可读。好的,有人能告诉我为什么我的答案没有用吗?首先谢谢你。