首页 > 其他分享 >【线性模式】

【线性模式】

时间:2022-08-26 21:45:07浏览次数:51  
标签:gradient 模式 current print rate points learning 线性

import numpy as np


def compute_error_for_line_given_points(b, w, points):
    total_error = 0
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        total_error += (y - (w * x + b)) ** 2
    return total_error / float(len(points))


def step_gradient(b_current, w_current, points, learning_rate):
    b_gradient = 0
    w_gradient = 0
    N = float(len(points))

    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += -(2 / N) * (y - ((w_current * x) + b_current))
        w_gradient += -(2 / N) * (y - ((w_current * x) + b_current)) * x
    print(w_gradient, b_gradient)
    new_b = b_current - (learning_rate * b_gradient)
    new_w = w_current - (learning_rate * w_gradient)
    return [new_b, new_w]


def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
    b = starting_b
    w = starting_w

    for i in range(num_iterations):
        b, w = step_gradient(b, w, points, learning_rate)
        if i % 100 == 0:
            print(compute_error_for_line_given_points(b, w, points))
    return [b, w]


def main():
    points = np.array([
        [1, 2.1],
        [2, 4.2],
        [3, 5.98],
    ])
    print(points)
    learning_rate = 0.01
    b_start = 0
    w_start = 0
    num_iter = 100000
    b, w = gradient_descent_runner(points, b_start, w_start, learning_rate, num_iter)
    print(w, b)
    print(1 * w + b)
    print(2 * w + b)
    print(3 * w + b)


if __name__ == '__main__':
    main()

 

标签:gradient,模式,current,print,rate,points,learning,线性
From: https://www.cnblogs.com/startnow/p/16629360.html

相关文章

  • 路由器的两种工作模式
    路由器的两种模式:hash,historyrouter=>index.js:一.hash和history的区别:对于一个url来说,什么是hash值?——#及其后面的内容就是hash值。hash值不会包含在HTTP请求......
  • 有一个线性表,采用带头结点的单链表L来存储,设计一个算法将其逆置,且不能建立新节点,只能
    有一个线性表,采用带头结点的单链表L来存储,设计一个算法将其逆置,且不能建立新节点,只能通过表中已有的节点的重新组合来完成。分析:线性表中关于逆序的问题,就是用建立链表......
  • 常见的设计模式-单例模式
    简述实现了类在当前进程中只有一个实例,比如python-GC重写__new__实现单例classA:def__new__(cls,*args,**kwargs):ifnothasattr(cls,'_instance......
  • 1.单例模式
    1.单例模式单例,顾名思义是一个实例,即在一个项目之中,单例的类只实例化一次。它常常应用于数据库操作、日志函数。在一个大型项目中使用到日志和数据库操作的地方很多,不能......
  • 设计模式之工厂模式
    packagecn.com.pep.model.simpleFactory;/***@Title:CheesePizza*@Description:*@authorwwh*@date2022-8-2214:22:53*/publicclassCheesePiz......
  • 生产者、消费者模式
    是什么生产者消费者模式(生产者消费者模式)是经典的线程同步案例,也称为有限缓冲问题。生产者产生数据,但是数据不能超出缓冲区的限制,当缓冲区满时,停止生产。消费者消费生产......
  • 第一章 模式识别的相关概念学习笔记
    1  相关概念1.1 什么是模式?可以被区分是否相似,存在于时间和空间中可观察的物体之中的信息。(模式不是事务本体,是从事物中获取的信息)1.2 模式的直观特性可观察性......
  • 【Account Kit】使用Authorization Code模式接入华为帐号,返回accessToken为空
    问题描述:使用AuthorizationCode模式接入华为帐号,返回AuthAccount的accessToken为空,并且没有返回uid解决方案:一般在静默登录的时候,需要在初始化HuaweiIdAuthParams对象......
  • 初识设计模式 - 单例模式
    简介一个类只允许创建一个对象(或实例),那么这个类就是一个单例类,这种设计模式称作单例设计模式(SingletonDesignPattern),简称单例模式。单例模式保证系统内存中只存在一个......
  • 01 Redis 三种搭建模式:主从模式-哨兵模式-高可用集群模式
    一、主从模式用域名指定主节点,当主节点宕机,改域名指向从节点缺点不知道什么时候挂掉,丢失数据,需要人工介入,运维24h待命 二、哨兵模式比主从模式,主要多了个哨兵,能自动......