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

山东省建设厅举报网站软文推广服务

山东省建设厅举报网站,软文推广服务,网站网络推广方案,17岁日本免费完整版观看正则化方法(Regularization Techniques) 1. 目标 理解什么是过拟合及其影响掌握常见正则化技术:L2 正则化、Dropout、Batch Normalization、Early Stopping能够使用 PyTorch 编程实现这些正则化方法并进行比较分析 2. 数据构造与任务设定 …

正则化方法(Regularization Techniques)

1. 目标

  • 理解什么是过拟合及其影响
  • 掌握常见正则化技术:L2 正则化、Dropout、Batch Normalization、Early Stopping
  • 能够使用 PyTorch 编程实现这些正则化方法并进行比较分析

2. 数据构造与任务设定

本实验是一个带噪声的回归任务,目标函数为 y = x + N ( 0 , σ 2 ) y = x + \mathcal{N}(0, \sigma^2) y=x+N(0,σ2)。使用均匀分布采样输入 x ∈ [ − 1 , 1 ] x \in [-1, 1] x[1,1]

import numpy as np
import torch
import torch.utils.data as DataN_SAMPLES = 20
NOISE_RATE = 0.4train_x = np.linspace(-1, 1, N_SAMPLES)[:, np.newaxis]
train_y = train_x + np.random.normal(0, NOISE_RATE, train_x.shape)validate_x = np.linspace(-1, 1, N_SAMPLES // 2)[:, np.newaxis]
validate_y = validate_x + np.random.normal(0, NOISE_RATE, validate_x.shape)test_x = np.linspace(-1, 1, N_SAMPLES)[:, np.newaxis]
test_y = test_x + np.random.normal(0, NOISE_RATE, test_x.shape)# 转换为 Tensor
train_x = torch.tensor(train_x, dtype=torch.float32)
train_y = torch.tensor(train_y, dtype=torch.float32)
validate_x = torch.tensor(validate_x, dtype=torch.float32)
validate_y = torch.tensor(validate_y, dtype=torch.float32)
test_x = torch.tensor(test_x, dtype=torch.float32)
test_y = torch.tensor(test_y, dtype=torch.float32)train_dataset = Data.TensorDataset(train_x, train_y)
train_loader = Data.DataLoader(dataset=train_dataset, batch_size=10, shuffle=True)

3. 模型定义

3.1 原始 MLP(无正则化)

import torch.nn as nn
import torch.nn.init as initclass FC_Classifier(nn.Module):def __init__(self, input_dim=1, hidden_dim=100, output_dim=1):super().__init__()self.fc1 = nn.Linear(input_dim, hidden_dim)self.fc2 = nn.Linear(hidden_dim, output_dim)self.activation = nn.ReLU()self._init_weights()def _init_weights(self):init.normal_(self.fc1.weight, mean=0.0, std=0.1)init.constant_(self.fc1.bias, 0)init.normal_(self.fc2.weight, mean=0.0, std=0.1)init.constant_(self.fc2.bias, 0)def forward(self, x):x = self.activation(self.fc1(x))return self.fc2(x)

3.2 Dropout MLP

class DropoutMLP(nn.Module):def __init__(self, dropout_rate=0.5):super().__init__()self.fc1 = nn.Linear(1, 100)self.dropout = nn.Dropout(dropout_rate)self.fc2 = nn.Linear(100, 1)self.activation = nn.ReLU()self._init_weights()def _init_weights(self):init.normal_(self.fc1.weight, mean=0.0, std=0.1)init.constant_(self.fc1.bias, 0)init.normal_(self.fc2.weight, mean=0.0, std=0.1)init.constant_(self.fc2.bias, 0)def forward(self, x):x = self.dropout(self.fc1(x))x = self.activation(x)return self.fc2(x)

3.3 Batch Normalization MLP

class BNMLP(nn.Module):def __init__(self):super().__init__()self.bn_input = nn.BatchNorm1d(1)self.fc1 = nn.Linear(1, 100)self.bn_hidden = nn.BatchNorm1d(100)self.fc2 = nn.Linear(100, 1)self.activation = nn.ReLU()def forward(self, x):x = self.bn_input(x)x = self.fc1(x)x = self.bn_hidden(x)x = self.activation(x)return self.fc2(x)

4. Early Stopping 策略

当验证集误差连续若干轮无提升时,提前停止训练,避免过拟合。

max_patience = 5
patience = 0
best_val_loss = float("inf")
is_early_stop = False

5. RMSNorm 实现与讲解

5.1 原理说明

RMSNorm 是一种替代 LayerNorm 的轻量化归一化方法:

  • 不减均值
  • 仅用激活值的均方根进行归一化
  • 不依赖 batch 维度

数学公式:

RMS ( x ) = 1 n ∑ i = 1 n x i 2 \text{RMS}(x) = \sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2} RMS(x)=n1i=1nxi2

RMSNorm ( x ) = x RMS ( x ) + ϵ ⋅ γ \text{RMSNorm}(x) = \frac{x}{\text{RMS}(x) + \epsilon} \cdot \gamma RMSNorm(x)=RMS(x)+ϵxγ

其中 γ \gamma γ 为可学习参数, ϵ \epsilon ϵ 是一个很小的数避免除以 0。

5.2 代码实现

class RMSNorm(nn.Module):def __init__(self, hidden_size, eps=1e-6):super().__init__()self.weight = nn.Parameter(torch.ones(hidden_size))self.eps = epsdef forward(self, x):rms = torch.sqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + self.eps)return self.weight * x / rms

5.3 与其他归一化对比

方法是否减均值是否除方差是否依赖 batch
BatchNorm
LayerNorm
RMSNorm是 (仅 RMS)

6. 实验建议

  • 尝试不同的 Dropout 比例(如 0.1 / 0.3 / 0.5)并观察效果;
  • 对比是否每层都加 BatchNorm 是否更优;
  • 比较 L2 正则项中 weight decay 的不同取值;
  • 使用 RMSNorm 替代 LayerNorm 做对比实验。
http://www.cadmedia.cn/news/1715.html

相关文章:

  • 学生做网站的软件他达拉非的副作用和危害
  • 小型网站设计及建设毕业论文外贸网络营销
  • 南昌房地产网站建设搜索引擎优化是做什么的
  • 网站建设 福步 2018seo搜索引擎优化价格
  • 嘉里建设网站天津做网站的公司
  • 保定建站模板运营推广计划
  • 产业协会建设网站方案sku电商是什么意思
  • 阿旗建设局举报网站韩国网站
  • 网站搭建好有什么内容可以修改百度seo点击
  • wordpress入门视频教程seo排名谁教的好
  • 郑州模板建站系统百度网讯科技有限公司官网
  • 动漫制作专业调研目的seo关键词优化指南
  • 网站建设流程及费用怎么让网站排名上去
  • 网站建设保障方案域名批量查询系统
  • 甘肃路桥建设集团公司网站培训机构管理系统
  • 网站开发使用哪种工具好中囯联通腾迅
  • 网站建设案例精英网络营销推广策划的步骤是什么
  • 望城区政府门户网站城市建设网站服务费一年多少钱
  • 物流网站建设评析品牌营销
  • 深圳网站设计兴田德润优惠吗成都专业的整站优化
  • 任经理++徐州网站建设个人如何做网络推广
  • 做网站开发用什么APP好长沙网站优化指导
  • 嘉鱼网站建设哪家好软文拟发布的平台与板块
  • 鸟人 网站建设手机网站搜索优化
  • 局域网搭建的步骤seo是什么
  • be 设计网站免费推广seo
  • 山东省建设部网站廊坊seo整站优化软件
  • 做h5的网站有哪些seo关键词排名优化哪家好
  • 盐城网盐城网站建设站建设网站建设公司哪家好?
  • 贵阳网站建设优化如何自己开个网站平台