当前位置: 首页 > news >正文

正则化-机器学习

  1. 正则化 regularization

    在这里插入图片描述

    虚线是真实函数g(x) 圆点是加入的噪声:

    在这里插入图片描述

    不应用正则化的实现:

    import numpy as np
    import matplotlib.pyplot as plt#真正的函数
    def g(x):return 0.1 * (x**3 + x**2 + x)#加入噪声
    train_x = np.linspace(-2,2,8)
    train_y = g(train_x) + np.random.randn(train_x.size)*0.05#绘图确认
    x = np.linspace(-2,2,100)
    plt.plot(train_x,train_y,'o')
    plt.plot(x,g(x),linestyle='dashed')
    plt.ylim(-1,2)
    plt.show()#标准化
    mu = train_x.mean()
    sigma = train_x.std()
    def standardize(x):return (x-mu)/sigmatrain_z = standardize(train_x)#创建训练数据的矩阵
    def to_matrix(x):return np.vstack([np.ones(x.size),x,x**2,x**3,x**4,x**5,x**6,x**7,x**8,x**9,x**10,]).TX = to_matrix(train_z)#参数初始化
    theta = np.random.randn(X.shape[1])#预测函数
    def f(x):return np.dot(x,theta)#不应用正则化的实现#目标函数
    def E(x,y):return 0.5*np.sum((y-f(x))**2)#学习率
    ETA = 1e-4#误差
    diff = 1#重复学习
    error = E(X,train_y)
    while diff>1e-6:theta = theta - ETA*np.dot(f(X)-train_y,X)current_error = E(X,train_y)diff = error - current_errorerror = current_error#对结果绘图
    z = standardize(x)
    plt.plot(train_z,train_y,'o')
    plt.plot(z,f(to_matrix(z)))
    plt.show()
    

    过拟合的曲线:

    在这里插入图片描述

    应用正则化的实现:

    import numpy as np
    import matplotlib.pyplot as plt#真正的函数
    def g(x):return 0.1 * (x**3 + x**2 + x)#加入噪声
    train_x = np.linspace(-2,2,8)
    train_y = g(train_x) + np.random.randn(train_x.size)*0.05x = np.linspace(-2,2,100)#标准化
    mu = train_x.mean()
    sigma = train_x.std()
    def standardize(x):return (x-mu)/sigmatrain_z = standardize(train_x)#创建训练数据的矩阵
    def to_matrix(x):return np.vstack([np.ones(x.size),x,x**2,x**3,x**4,x**5,x**6,x**7,x**8,x**9,x**10,]).TX = to_matrix(train_z)#参数初始化
    theta = np.random.randn(X.shape[1])#预测函数
    def f(x):return np.dot(x,theta)#应用正则化的实现
    #正则化常量
    LAMBDA = 1#目标函数
    def E(x,y):return 0.5*np.sum((y-f(x))**2)#学习率
    ETA = 1e-4#误差
    diff = 1#重复学习
    error = E(X,train_y)
    while diff>1e-6:#正则化项 偏置项不适合用正则化 所以为0reg_term = LAMBDA*np.hstack([0,theta[1:]])#应用正则化项 更新参数theta = theta - ETA*(np.dot(f(X)-train_y,X) + reg_term)current_error = E(X,train_y)diff = error - current_errorerror = current_error#对结果绘图
    z = standardize(x)
    plt.plot(train_z,train_y,'o')
    plt.plot(z,f(to_matrix(z)))
    plt.show()
    

    在这里插入图片描述

http://www.dtcms.com/a/277468.html

相关文章:

  • Redis面试精讲 Day 2:Redis数据类型全解析
  • 内存管理概念
  • Docker安装Nginx
  • Web:JS的三种引用方式
  • 《每日AI-人工智能-编程日报》--2025年7月12日
  • Windows 常用命令
  • 网络编程 JAVA
  • 视觉语言导航与目标导航
  • 【银行测试】基金项目测试详细,测试点+面试(一)
  • ​​LangChain专家养成:工具扩展/Agent决策/记忆控制三维进阶
  • 250707脑电分析课题进展——EEGLAB的使用
  • 前端工程化-构建打包
  • 大模型-量化技术
  • 前端构建工具 Webpack 5 的优化策略与高级配置
  • [2025CVPR]DenoiseCP-Net:恶劣天气下基于LiDAR的高效集体感知模型
  • 神经网络的层与块
  • 掌握系统设计的精髓:12个核心设计模式的通俗解读
  • 【编程实践】利用open3d生成物体的最长边方向并可视化
  • 面向对象设计模式详解
  • CD49.【C++ Dev】容器适配器模式
  • 深入解析5G核心网容灾:UDM 故障场景下 SMF 容灾机制深度解析
  • C++ 单例模式实现
  • 【读书笔记】《C++ Software Design》第五章:The Strategy and Command Design Patterns
  • Java学习------设计模式(1)
  • ZKmall开源商城技术攻略:轻松掌握规则引擎与Spring Boot3接口的开发技巧
  • Linux V4L2应用编程常用结构体介绍
  • STEP 7-Micro/WIN SMART 编程软件:从入门到精通的使用指南
  • 面试150 从前序与中序遍历构造二叉树
  • STM32-第五节-TIM定时器-1(定时器中断)
  • Clojure和Golang中的Channel有什么异同(TBC)