zoukankan      html  css  js  c++  java
  • Tensorflow2(预课程)---1.5、线性回归-非tensorflow方式

    Tensorflow2(预课程)---1.5、线性回归-非tensorflow方式

    一、总结

    一句话总结:

    对这样的一个节点的神经网络而言,不同的数据的梯度和误差都是所有数据相加,不过误差求求平均即可
    # 计算误差
    # y = wx + b
    def compute_error_for_line_given_points(b, w, points):
        totalError = 0
        for i in range(0, len(points)):
            x = points.iloc[i,1]
            y = points.iloc[i,2]
            # 计算每个节点的误差(计算所有节点的误差和)
            # computer mean-squared-error
            totalError += (y - (w * x + b)) ** 2
        # average loss for each point
        # 计算平均梯度
        return totalError / float(len(points))
    
    
    def step_gradient(b_current, w_current, points, learningRate):
        # 梯度初始值
        b_gradient = 0
        w_gradient = 0
        N = float(len(points))
        for i in range(0, len(points)):
            # 对每个节点,
            x = points.iloc[i, 1]
            y = points.iloc[i, 2]
            # 梯度求和,公式里,这个梯度肯定是求和呀
            # grad_b = 2(wx+b-y)
            b_gradient += (2/N) * ((w_current * x + b_current) - y)
            # grad_w = 2(wx+b-y)*x
            w_gradient += (2/N) * x * ((w_current * x + b_current) - y)
        # update w'
        # 更新w和b
        new_b = b_current - (learningRate * b_gradient)
        new_w = w_current - (learningRate * w_gradient)
        # print("w为{},b为{}".format(new_b,new_w))
        # 输出更新的w和b
        return [new_b, new_w]

    二、线性回归-非tensorflow方式

    博客对应课程的视频位置:

     
    我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站: fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
    博主25岁,前端后端算法大数据人工智能都有兴趣。
    大家有啥都可以加博主联系方式(qq404006308,微信fan404006308)互相交流。工作、生活、心境,可以互相启迪。
    聊技术,交朋友,修心境,qq404006308,微信fan404006308
    26岁,真心找女朋友,非诚勿扰,微信fan404006308,qq404006308
    人工智能群:939687837

    作者相关推荐

  • 相关阅读:
    写日志文件
    内存文件映射应用举例『转』
    HOOK编程
    获取当前进程的名称
    C++实现FTP文件传输
    C/C++ 实现windows进程/线程/模块 遍历
    Unicode下的CString与char *转换
    MFC常用基本数据类型
    geos 3.6.1编译 win7 vs2015
    JQuery Mobile iscroll插件使用教程及注意事项
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13562897.html
Copyright © 2011-2022 走看看