Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
HIT-UG-Group authored May 1, 2022
0 parents commit 01796be
Show file tree
Hide file tree
Showing 47 changed files with 73,148 additions and 0 deletions.
716 changes: 716 additions & 0 deletions code/04-数据读取和操作/数据操作.ipynb

Large diffs are not rendered by default.

456 changes: 456 additions & 0 deletions code/04-数据读取和操作/数据读取.ipynb

Large diffs are not rendered by default.

509 changes: 509 additions & 0 deletions code/07-自动求导.ipynb

Large diffs are not rendered by default.

560 changes: 560 additions & 0 deletions code/08-线性回归/linear-regression-concise.ipynb

Large diffs are not rendered by default.

648 changes: 648 additions & 0 deletions code/08-线性回归/linear-regression-scratch.ipynb

Large diffs are not rendered by default.

969 changes: 969 additions & 0 deletions code/09-Softmax回归/09-softmax回归简洁实现.ipynb

Large diffs are not rendered by default.

2,170 changes: 2,170 additions & 0 deletions code/09-Softmax回归/09-从零实现softmax回归.ipynb

Large diffs are not rendered by default.

961 changes: 961 additions & 0 deletions code/10-多层感知机/mlp-concise.ipynb

Large diffs are not rendered by default.

2,582 changes: 2,582 additions & 0 deletions code/10-多层感知机/mlp-scratch.ipynb

Large diffs are not rendered by default.

6,232 changes: 6,232 additions & 0 deletions code/10-多层感知机/mlp.ipynb

Large diffs are not rendered by default.

4,253 changes: 4,253 additions & 0 deletions code/12-权重衰退.ipynb

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions code/13-丢弃法.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "d5195177",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from torch import nn\n",
"from d2l import torch as d2l\n",
"\n",
"def dropout_layer (X,dropout): #X为dropout层的输入,dropout为设置的丢弃概率\n",
" assert 0<=dropout<=1 #丢弃概率介于0,1之间\n",
" if dropout == 1:\n",
" return torch.zeros_like(X) #若丢弃概率为1,则X的全部项均被置0\n",
" if dropout == 0:\n",
" return X #若丢弃概率为0,不对X作丢弃操作,直接返回X\n",
" mask=(torch.Tensor(X.shape).uniform_(0,1)>dropout).float() #用uniform函数生成0-1间的随机实数,利用”>\",将大于dropout的记为1,小于dropout的记为0,实现丢弃操作\n",
" return mask*X/(1-dropout) #将mask与X相乘实现丢弃操作,并除以(1-dropout),这里不使用选中X中元素置0的原因是相乘操作相比选中操作更快"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8ff5ba10",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0., 1., 2., 3., 4., 5., 6., 7.],\n",
" [ 8., 9., 10., 11., 12., 13., 14., 15.]])\n",
"tensor([[ 0., 1., 2., 3., 4., 5., 6., 7.],\n",
" [ 8., 9., 10., 11., 12., 13., 14., 15.]])\n",
"tensor([[ 0., 0., 4., 0., 0., 10., 0., 14.],\n",
" [16., 18., 20., 0., 0., 26., 0., 30.]])\n",
"tensor([[0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0.]])\n"
]
}
],
"source": [
"#丢弃法测试\n",
"X=torch.arange(16,dtype=torch.float32).reshape((2,8))\n",
"print(X)\n",
"print(dropout_layer (X,0.)) #丢弃率设置为0\n",
"print(dropout_layer (X,0.5)) #丢弃率设置为0.5\n",
"print(dropout_layer (X,1)) #丢弃率设置为1"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:py36torch040]",
"language": "python",
"name": "conda-env-py36torch040-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 01796be

Please sign in to comment.