深度学习-pytorch-卷积层
卷积层图像卷积123456789101112131415import torchfrom torch import nnfrom d2l import torch as d2l# 实现矩阵与核矩阵的运算,K是核矩阵def corr2d(X, K): # 计算二维互相关运算 h, w = K.shape Y = torch.zeros((X.shape[0]-h+1, X.shape[1]-w+1)) for i in range(Y.shape[0]): for j in range(Y.shape[1]): Y[i, j] = (X[i:i+h, j:j+w]*K).sum() return Y
试验一下
1234X = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])corr2d(X, K)
tensor([[19., 25.],
...
深度学习-pytorch-神经网络基础
神经网络基础层的定义123456789import torchfrom torch import nnfrom torch.nn import functional as Fnet = nn.Sequential(nn.Linear(20,256), nn.ReLU(), nn.Linear(256, 10))X = torch.rand(2, 20)net(X)
tensor([[-0.0226, 0.1110, 0.1338, 0.0594, 0.0579, 0.0372, 0.2026, -0.2140,
0.0259, 0.0358],
[-0.1102, 0.0507, 0.0410, 0.1030, 0.1872, 0.0963, 0.1452, -0.1649,
-0.0152, 0.1379]], grad_fn=<AddmmBackward0>)
nn.Sequential定义了一个特殊的Moudule,用法看下面
1234567891011121314151617 ...
暑假集训week4
暑假集训-week4题解-暑假结构进阶A - ST 表ST
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#include <bits/stdc++.h>using namespace std;// https://www.luogu.com.cn/problem/P3865const int maxn = 1e6 + 10;int n, m;int a[maxn], f[maxn][100]; inline int read(){ int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); ...
深度学习-pytorch-MLP提高泛化
MLP提高泛化的方法
权重衰减-pytorch框架实现1234%matplotlib inlineimport torchfrom torch import nnfrom d2l import torch as d2l
造数据
12345678n_train, n_test, num_inputs, batch_size = 20, 100, 200, 5# 训练数,测试数,输入数,步长true_w, true_b = torch.ones((num_inputs, 1)) * 0.01, 0.05train_data = d2l.synthetic_data(true_w, true_b, n_train) # 生成训练集train_iter = d2l.load_array(train_data, batch_size)test_data = d2l.synthetic_data(true_w, true_b, n_test) # 生成验证集test_iter = d2l.load_array(test_data, batch_size, is_train=False)
开 ...
深度学习-pytorch-多层感知机(MLP)
多层感知机MLP引子之前的回归模型已经能解决一些线性模型,但是对于非线性问题,用之前的去模拟是不合适的,我们需要在神经网络中加入隐藏层,形成非线性模型,实现对模型预测的普适性
从零开始实现MLP一些准备工作:引入包,加载数据集
123456789101112131415161718192021222324252627282930from d2l import torch as d2lfrom torch.utils import datafrom torchvision import transformsfrom torch import nnimport torchvisionimport torchd2l.use_svg_display()def load_data_fashion_mnist(batch_size, resize=None): """下载Fashion-MNIST数据集,然后将其加载到内存中""" trans = [transforms.ToTensor()] if resize: ...
深度学习-pytorch-图片集与Softmax回归
Softmax回归引子回归可以用于预测多少的问题。 比如预测房屋被售出价格,或者棒球队可能获得的胜场数,又或者患者住院的天数。
事实上,我们也对分类问题感兴趣:不是问“多少”,而是问“哪一个”!
图像分类数据集12345678from d2l import torch as d2lfrom torch.utils import datafrom torchvision import transformsimport torchvisionimport torch%matplotlib inlined2l.use_svg_display()
下载并读取数据集1234567# 下载数据集trans = transforms.ToTensor()mnist_train = torchvision.datasets.FashionMNIST( root="./data", train=True, transform=trans, download=True)mnist_test = torchvision.datasets.FashionMNIST( root ...
深度学习-pytorch-自动求导与线性回归
自动求导尝试torch.Tensor 是这个包的核心类。如果设置它的属性 .requires_grad 为 True,那么它将会追踪对于该张量的所有操作。当完成计算后可以通过调用 .backward(),来自动计算所有的梯度。这个张量的所有梯度将会自动累加到.grad属性.
要阻止一个张量被跟踪历史,可以调用 .detach() 方法将其与计算历史分离,并阻止它未来的计算记录被跟踪。
为了防止跟踪历史记录(和使用内存),可以将代码块包装在 with torch.no_grad(): 中。在评估模型时特别有用,因为模型可能具有 requires_grad = True 的可训练的参数,但是我们不需要在此过程中对他们进行梯度计算。
还有一个类对于autograd的实现非常重要:Function。
Tensor 和 Function 互相连接生成了一个无圈图(acyclic graph),它编码了完整的计算历史。每个张量都有一个 .grad_fn 属性,该属性引用了创建 Tensor 自身的Function(除非这个张量是用户手动创建的,即这个张量的 grad_fn 是 None ...
深度学习-pytorch-张量tensor语法
1234import torchimport d2lzh_pytorchimport osimport pandas as pd
123456x=torch.arange(12)print(x)x.shapex.numel #元素数量x.reshape(3,4)y=torch.zeros(2,3,4)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
12345678910111213x = torch.tensor([[1, 2, 3], [3, 4, 5], [4, 3, 2]])print(x)x = torch.tensor([2.0, 2, 2, 2])print(x)x = torch.arange(12, dtype=torch.float32).reshape((3, 4))print(x)y = torch.tensor([[2.0, 1, 5, 3], [3, 2, 2, 3], [3, 2, 3, 3]])print(y)torch.cat((x, y), dim=0), torch.cat( ...
2018-HBCPC题解
2018HBCPC部分题解Mex Query
12345678910111213141516171819202122232425262728293031323334#include <bits/stdc++.h>using namespace std;// 传送门:http://newoj.acmclub.cn/problems/2011int T;int n;int main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> T; while (T--) { set<int> s; cin >> n; for (int i = 1; i <= n; i++) { int remp; cin >> remp; s.insert(remp); } ...
暑假集训week3
暑假集训-week3-动态规划A - 最大子段和经典的最大子段和
12345678910111213141516171819202122#include <bits/stdc++.h>using namespace std;// 最大子段和(dp模板题)// https://www.luogu.com.cn/problem/P1115int a[200001];int dp[200001];int main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin >> n; int ans = -9999999; for (int i = 1; i <= n; i++) { cin >> a[i]; dp[i] = max(dp[i - 1] + a[i], a[i]); ans = max(dp[i], ans); } cout <& ...