李沐《动手学深度学习》课程笔记:11 模型选择 + 过拟合和欠拟合

news/2024/11/22 20:01:44/

目录

11 模型选择 + 过拟合和欠拟合

1.模型选择

2.过拟合和欠拟合

​​​​​3.代码 


11 模型选择 + 过拟合和欠拟合

1.模型选择

 

2.过拟合和欠拟合

3.代码 

# 模型选择、欠拟合和过拟合
# 通过多项式拟合来交互地探索这些概念
import math
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch import nn
from d2l import torch as d2l
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"max_degree = 20
n_train, n_test = 100, 100
true_w = np.zeros(max_degree)
true_w[0:4] = np.array([5, 1.2, -3.4, 5.6])features = np.random.normal(size=(n_train + n_test, 1))
np.random.shuffle(features)
poly_features = np.power(features, np.arange(max_degree).reshape(1, -1))
for i in range(max_degree):poly_features[:, i] /= math.gamma(i + 1)
labels = np.dot(poly_features, true_w)
labels += np.random.normal(scale=0.1, size=labels.shape)true_w, features, poly_features, labels = [torch.tensor(x, dtype=torch.float32) for x in [true_w, features, poly_features, labels]
]print(features[:2], poly_features[:2, :], labels[:2])# 实现一个函数来评估模型在给定数据集上的损失
def evaluate_loss(net, data_iter, loss):'''评估给定数据集上模型的损失'''metric = d2l.Accumulator(2)for X, y in data_iter:out = net(X)y = y.reshape(out.shape)l = loss(out, y)metric.add(l.sum(), l.numel())return metric[0] / metric[1]# 定义训练函数
def train(train_features, test_features, train_labels, test_labels, num_epochs=400):loss = nn.MSELoss()input_shape = train_features.shape[-1]net = nn.Sequential(nn.Linear(input_shape, 1, bias=False))batch_size = min(10, train_labels.shape[0])train_iter = d2l.load_array((train_features, train_labels.reshape(-1, 1)), batch_size)test_iter = d2l.load_array((test_features, test_labels.reshape(-1, 1)), batch_size, is_train=False)trainer = torch.optim.SGD(net.parameters(), lr=0.01)animator = d2l.Animator(xlabel='epoch', ylabel='loss', yscale='log', xlim=[1, num_epochs], ylim=[1e-3, 1e2], legend=['train', 'test'])for epoch in range(num_epochs):d2l.train_epoch_ch3(net, train_iter, loss, trainer)if epoch == 0 or (epoch + 1) % 20 == 0:animator.add(epoch + 1, (evaluate_loss(net, train_iter, loss), evaluate_loss(net, test_iter, loss)))print('weight:', net[0].weight.data.numpy())plt.show()# 三阶多项式函数拟合(正态)
train(poly_features[:n_train, :4], poly_features[n_train:, :4], labels[:n_train], labels[n_train:])
train(poly_features[:n_train, :2], poly_features[n_train:, :2], labels[:n_train], labels[n_train:])
train(poly_features[:n_train, :], poly_features[n_train:, :], labels[:n_train], labels[n_train:])
tensor([[ 1.2666],[-0.1539]]) tensor([[ 1.0000e+00,  1.2666e+00,  8.0218e-01,  3.3869e-01,  1.0725e-01,2.7169e-02,  5.7354e-03,  1.0378e-03,  1.6432e-04,  2.3125e-05,2.9291e-06,  3.3728e-07,  3.5601e-08,  3.4687e-09,  3.1383e-10,2.6500e-11,  2.0979e-12,  1.5631e-13,  1.0999e-14,  7.3325e-16],[ 1.0000e+00, -1.5391e-01,  1.1844e-02, -6.0762e-04,  2.3380e-05,-7.1966e-07,  1.8460e-08, -4.0589e-10,  7.8087e-12, -1.3354e-13,2.0552e-15, -2.8756e-17,  3.6882e-19, -4.3665e-21,  4.8002e-23,-4.9253e-25,  4.7378e-27, -4.2893e-29,  3.6676e-31, -2.9709e-33]]) tensor([5.6214, 4.7863])
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
weight: [[ 4.9989963  1.1997855 -3.3960633  5.6231313]]

 

<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
weight: [[3.7553606 2.556017 ]]

 

<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
weight: [[ 4.9636006   1.4399645  -3.1924648   4.7269516  -0.60285604  0.9733015-0.01626889  0.23548631 -0.05793007  0.12515599 -0.11259881  0.02957132-0.03573195  0.04298536  0.17225894 -0.04172051 -0.15639223  0.145639030.19347033  0.17333683]]

 

 


http://www.ppmy.cn/news/186600.html

相关文章

华为matebook鸿蒙,16寸专业大屏+满血Zen3!华为MateBook 16评测:鸿蒙加持 平板变身新外设...

一、前言&#xff1a;屏幕更大 性能更强 这次还有更多智慧化新体验的“惊喜” 今年5月&#xff0c;华为笔记本迎来了一位新成员——华为MateBook 16&#xff0c;在华为MateBook系列产品中&#xff0c;首次采用了16寸全面屏的设计&#xff0c;不仅有更大的屏幕尺寸&#xff0c;其…

大屏幕的展示 借助的华为的数据

zhangti的作业 (huaweicloud.com) Python脚本模拟数据&#xff0c;由FlumeKafka完成数据采集&#xff0c;DLI中的Flink作业进行数据分析&#xff0c;分析结果存入RDS中&#xff0c;最后DLV读取RDS中的数据通过大屏完成可视化。

android 沉浸式 华为,华为沉浸式智真

全球首款MAX PRESENCE 华为智真&#xff0c;适用于大型会议室&#xff0c;可实现超宽幅全景显示&#xff0c;畅享影院级会议体验&#xff0c;可与视讯、监控、数据信息系统多业务融合&#xff0c;纵观全局&#xff0c;智慧决策。 l 全球首款MAX PRESENCE 华为智真&#xff0c;超…

华为智慧屏怎么使用

在连接智慧屏后长按电源键开机&#xff0c;配对遥控器&#xff1b;使用手机扫描或者手动输入登录到华为账号&#xff1b;进入智慧屏主界面后&#xff0c;使用遥控器触摸板或者语音唤起YOYO选择栏目&#xff1b;手机与智慧屏连接到同一个WiFi之后可以实现投屏&#xff1b;在更新…

华为鸿蒙大屏电视,大屏手机还是智能电视?华为用鸿蒙系统增智慧

[PConline 资讯]7月26日&#xff0c;华为手机官方微博表示&#xff1a;【华为智慧屏 让智慧更大】华为将持续推动产业升级&#xff0c;实现生态共赢&#xff0c;与业界广泛合作&#xff0c;开放生态赋能产业&#xff0c;推动标准化实现产业贡献&#xff1b;2019年9月&#xff0…

华为鸿蒙系统大屏,华为将推出鸿蒙2.0系统 打通手机、大屏电视等全平台

华为将推出鸿蒙2.0系统 打通手机、大屏电视等全平台 来源&#xff1a;快科技 2020-07-08 15:38:05 按照惯例&#xff0c;华为9月份的秋季发布会上将会正式推出Mate 40系列手机&#xff0c;除了5nm麒麟处理器之外&#xff0c;Mate 40还会使用新的EMUI 11系统。与此同时&#xff…

华为计算机变色,华为新机子大换血,大屏而且还能变色!

华为几乎每一段时间都要来一次大换血。今年他出来的新机子真是特别多&#xff0c;就像是母鸡下蛋一样。但是这些手机没有什么特别大的变化&#xff0c;从外表上来看&#xff0c;也就是可能屏幕会有一点不一样&#xff0c;然后这些小物件的摆放位置也可能会不一样。因为华为他在…

华为鸿蒙电视智慧屏,华为鸿蒙助力荣耀智慧屏,让大屏功能秀起来

华为鸿蒙OS对于荣耀智慧屏来说就是锦上添花&#xff0c;荣耀智慧屏搭载了鸿鹄818智慧芯片&#xff0c;带来了七大画质提升&#xff0c;音质方面搭载了远超同级6*10W扬声器&#xff0c;让用户有了聆听剧场级音效&#xff1b;配置升降式AI摄像头&#xff0c;可以实现众多视觉识别…