L2范数惩罚项,高维线性回归
%matplotlib inline
import mxnet
from mxnet import nd,autograd
from mxnet import gluon,init
from mxnet.gluon import data as gdata,loss as gloss,nn
import gluonbook as gb n_train, n_test, num_inputs = 20,100,200 true_w = nd.ones((num_inputs, 1)) * 0.01
true_b = 0.05 features = nd.random.normal(shape=(n_train+n_test, num_inputs))
labels = nd.dot(features,true_w) + true_b
labels += nd.random.normal(scale=0.01, shape=labels.shape) train_feature = features[:n_train,:]
test_feature = features[n_train:,:]
train_labels = labels[:n_train]
test_labels = labels[n_train:] #print(features,train_feature,test_feature) # 初始化模型参数
def init_params():
w = nd.random.normal(scale=1, shape=(num_inputs, 1))
b = nd.zeros(shape=(1,))
w.attach_grad()
b.attach_grad()
return [w,b] # 定义,训练,测试 batch_size = 1
num_epochs = 100
lr = 0.03 train_iter = gdata.DataLoader(gdata.ArrayDataset(train_feature,train_labels),batch_size=batch_size,shuffle=True) # 定义网络
def linreg(X, w, b):
return nd.dot(X,w) + b # 损失函数
def squared_loss(y_hat, y):
"""Squared loss."""
return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2 # L2 范数惩罚
def l2_penalty(w):
return (w**2).sum() / 2 def sgd(params, lr, batch_size):
for param in params:
param[:] = param - lr * param.grad / batch_size def fit_and_plot(lambd):
w, b = init_params()
train_ls, test_ls = [], []
for _ in range(num_epochs):
for X, y in train_iter:
with autograd.record():
# 添加了 L2 范数惩罚项。
l = squared_loss(linreg(X, w, b), y) + lambd * l2_penalty(w)
l.backward()
sgd([w, b], lr, batch_size)
train_ls.append(squared_loss(linreg(train_feature, w, b),
train_labels).mean().asscalar())
test_ls.append(squared_loss(linreg(test_feature, w, b),
test_labels).mean().asscalar())
gb.semilogy(range(1, num_epochs + 1), train_ls, 'epochs', 'loss',
range(1, num_epochs + 1), test_ls, ['train', 'test'])
print('L2 norm of w:', w.norm().asscalar())
fit_and_plot(0)
fit_and_plot(3)
训练集太少,容易出现过拟合,即训练集loss远小于测试集loss,解决方案,权重衰减——(L2范数正则化)
例如线性回归:
loss(w1,w2,b) = 1/n * sum(x1w1 + x2w2 + b - y)^2 /2 ,平方损失函数。
权重参数 w = [w1,w2],
新损失函数 loss(w1,w2,b) += lambd / 2n *||w||^2
迭代方程:

L2范数惩罚项,高维线性回归的更多相关文章
- 小白学习之pytorch框架(6)-模型选择(K折交叉验证)、欠拟合、过拟合(权重衰减法(=L2范数正则化)、丢弃法)、正向传播、反向传播
下面要说的基本都是<动手学深度学习>这本花书上的内容,图也采用的书上的 首先说的是训练误差(模型在训练数据集上表现出的误差)和泛化误差(模型在任意一个测试数据集样本上表现出的误差的期望) ...
- 机器学习中的范数规则化 L0、L1与L2范数 核范数与规则项参数选择
http://blog.csdn.net/zouxy09/article/details/24971995 机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http: ...
- 机器学习中的范数规则化之 L0、L1与L2范数、核范数与规则项参数选择
装载自:https://blog.csdn.net/u012467880/article/details/52852242 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理 ...
- 《机器学习实战》学习笔记第八章 —— 线性回归、L1、L2范数正则项
相关笔记: 吴恩达机器学习笔记(一) —— 线性回归 吴恩达机器学习笔记(三) —— Regularization正则化 ( 问题遗留: 小可只知道引入正则项能降低参数的取值,但为什么能保证 Σθ2 ...
- deep learning (五)线性回归中L2范数的应用
cost function 加一个正则项的原因是防止产生过拟合现象.正则项有L1,L2 等范数,我看过讲的最好的是这个博客上的:机器学习中的范数规则化之(一)L0.L1与L2范数.看完应该就答题明白了 ...
- paper 126:[转载] 机器学习中的范数规则化之(一)L0、L1与L2范数
机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http://blog.csdn.net/zouxy09 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...
- 机器学习中的范数规则化之(一)L0、L1与L2范数(转)
http://blog.csdn.net/zouxy09/article/details/24971995 机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http: ...
- L0、L1与L2范数、核范数(转)
L0.L1与L2范数.核范数 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理解下常用的L0.L1.L2和核范数规则化.最后聊下规则化项参数的选择问题.这里因为篇幅比较庞大 ...
- 机器学习中的范数规则化之(一)L0、L1与L2范数 非常好,必看
机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http://blog.csdn.net/zouxy09 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...
随机推荐
- C语言两种方式实现矩阵的转置
#include"stdio.h" typedef struct{ int i,j; int v; }Triple; typedef struct{ Triple date[]; ...
- linux运维之top命令
统计信息区前五行是系统整体的统计信息: 第一行是任务队列信息,同 uptime 命令的执行结果.其内容如下: 01:06:48 当前时间 up 1:22 系统运行时间,格式为时:分 1 user 当 ...
- 60分钟内从零起步驾驭Hive实战学习笔记(Ubuntu里安装mysql)
本博文的主要内容是: 1. Hive本质解析 2. Hive安装实战 3. 使用Hive操作搜索引擎数据实战 SparkSQL前身是Shark,Shark强烈依赖于Hive.Spark原来没有做SQL ...
- 线程同步(windows平台):临界区
一:介绍 临界区指的是一个访问共用资源(例:全局变量)的程序片段,该共用资源无法同时被多个线程访问的特性.有多个线程试图同时访问临界区,那么在有一个线程进入后其他所有试图访问此临界区的线程将被挂起,并 ...
- 《Python编程从入门到实践》_第八章_函数
一个简单的函数 先看一个简单的函数 def say_hello(): '''打印hello''' print("Hello!") say_hello() #运行结果 Hello! ...
- [转]ASP.NET MVC 5 List Editor with Bootstrap Modals
本文转自:https://www.codeproject.com/articles/786085/asp-net-mvc-list-editor-with-bootstrap-modals With ...
- axios请求报Uncaught (in promise) Error: Request failed with status code 404
使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...
- 3.storm-starter打包在storm集群上运行
1.使用maven或者其他打包工具将storm-starter打成jar包 2.请将jar包用解压工具打开在根目录下找到defaults.yaml文件并将其删除不然到时会报有multiply defa ...
- Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b
环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 目录结构: 可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了E ...
- 集合之Iterator迭代器
Iterator迭代器概述: java中提供了很多个集合,它们在存储元素时,采用的存储方式不同.我们要取出这些集合中的元素,可通过一种通用的获取方式来完成. Collection集合元素的通用获取 ...