提交 3e1ff79b 编写于 作者: W wizardforcel

2021-10-14 22:51:06

上级 76ddc019
......@@ -19,7 +19,7 @@
如果一个输入变量定义`requires_grad`,那么他的输出也可以使用`requires_grad`;相反,只有当所有的输入变量都不定义`requires_grad`梯度,才不会输出梯度。如果其中所有的变量都不需要计算梯度,在子图中从不执行向后计算。
```
```py
>>> x = Variable(torch.randn(5, 5))
>>> y = Variable(torch.randn(5, 5))
>>> z = Variable(torch.randn(5, 5), requires_grad=True)
......@@ -35,7 +35,7 @@ True
例如,如果您想调整预训练的`CNN`,只要切换冻结模型中的`requires_grad`标志即可,直到计算到最后一层才会保存中间缓冲区,仿射变换和网络输出都需要使用梯度的权值。
```
```py
model = torchvision.models.resnet18(pretrained=True)
for param in model.parameters():
param.requires_grad = False
......
此差异已折叠。
......@@ -19,7 +19,7 @@ Torch 定义了七种 CPU 张量类型和八种 GPU 张量类型:
张量可以从 Python 的`list`或序列构成:
```
```py
>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
......@@ -28,7 +28,7 @@ Torch 定义了七种 CPU 张量类型和八种 GPU 张量类型:
可以通过指定它的大小来构建一个空的张量:
```
```py
>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
......@@ -37,7 +37,7 @@ Torch 定义了七种 CPU 张量类型和八种 GPU 张量类型:
可以使用 Python 的索引和切片符号来访问和修改张量的内容:
```
```py
>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
6.0
......@@ -52,7 +52,7 @@ Torch 定义了七种 CPU 张量类型和八种 GPU 张量类型:
> 注意: 改变张量的方法可以用一个下划线后缀来标示。比如,`torch.FloatTensor.abs_()`会在原地计算绝对值并返回修改的张量,而`tensor.FloatTensor.abs()`将会在新张量中计算结果。
```
```py
class torch.Tensor
class torch.Tensor(*sizes)
class torch.Tensor(size)
......@@ -317,7 +317,7 @@ class torch.Tensor(storage)
返回单个元素的字节大小。 例:
```
```py
>>> torch.FloatTensor().element_size()
4
>>> torch.ByteTensor().element_size()
......@@ -354,7 +354,7 @@ class torch.Tensor(storage)
例:
```
```py
>>> x = torch.Tensor([[1], [2], [3]])
>>> x.size()
torch.Size([3, 1])
......@@ -370,7 +370,7 @@ torch.Size([3, 1])
将 tensor 扩展为参数 tensor 的大小。 该操作等效与:
```
```py
self.expand(tensor.size())
```
......@@ -478,7 +478,7 @@ self.expand(tensor.size())
例:
```
```py
>>> x = torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
......@@ -502,7 +502,7 @@ self.expand(tensor.size())
例:
```
```py
>>> x = torch.Tensor(3 3)
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
......@@ -526,7 +526,7 @@ self.expand(tensor.size())
例:
```
```py
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2])
>>> x.index_fill_(0, index, -1)
......@@ -617,7 +617,7 @@ self.expand(tensor.size())
`callable`作用于本 tensor 和参数 tensor 中的每一个元素,并将结果存放在本 tensor 中。`callable`应该有下列标志:
```
```py
def callable(a, b) -> number
```
......@@ -697,7 +697,7 @@ def callable(a, b) -> number
例:
```
```py
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> x.narrow(0, 0, 2)
1 2 3
......@@ -776,7 +776,7 @@ def callable(a, b) -> number
例:
```
```py
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
......@@ -858,7 +858,7 @@ torch.Size([5, 2, 3])
例:
```
```py
>>> x = torch.Tensor([1, 2, 3])
>>> x.repeat(4, 2)
1 2 3 1 2 3
......@@ -880,7 +880,7 @@ torch.Size([4, 2, 3])
例:
```
```py
>>> x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
>>> x.resize_(2, 2)
>>> x
......@@ -893,7 +893,7 @@ torch.Size([4, 2, 3])
将当前张量调整为与指定张量相同的大小。这相当于:
```
```py
self.resize_(tensor.size())
```
......@@ -928,7 +928,7 @@ self.resize_(tensor.size())
例子:
```
```py
>>> x = torch.rand(2, 5)
>>> x
......@@ -1019,7 +1019,7 @@ self.resize_(tensor.size())
例:
```
```py
>>> torch.Tensor(3, 4, 5).size()
torch.Size([3, 4, 5])
```
......@@ -1060,7 +1060,7 @@ torch.Size([3, 4, 5])
以储存元素的个数的形式返回 tensor 在地城内存中的偏移量。 例:
```
```py
>>> x = torch.Tensor([1, 2, 3, 4, 5])
>>> x.storage_offset()
0
......@@ -1177,7 +1177,7 @@ torch.Size([3, 4, 5])
将此张量转换为给定类型的张量。 如果张量已经是正确的类型,则不会执行操作。等效于:
```
```py
self.type(tensor.type())
```
......@@ -1199,7 +1199,7 @@ self.type(tensor.type())
例子:
```
```py
>>> x = torch.arange(1, 8)
>>> x
......@@ -1252,7 +1252,7 @@ self.type(tensor.type())
例子:
```
```py
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
......@@ -1268,7 +1268,7 @@ torch.Size([2, 8])
返回被视作与给定的 tensor 相同大小的原 tensor。 等效于:
```
```py
self.view(tensor.size())
```
......
......@@ -23,7 +23,7 @@
使用方法:
```
```py
>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> print x.type()
torch.FloatTensor
......@@ -41,7 +41,7 @@ torch.FloatTensor
通过一个字符串:
```
```py
>>> torch.device('cuda:0')
device(type='cuda', index=0)
......@@ -54,7 +54,7 @@ device(type='cuda')
通过字符串和设备序号:
```
```py
>>> torch.device('cuda', 0)
device(type='cuda', index=0)
......@@ -64,13 +64,13 @@ device(type='cpu', index=0)
> **注意** `torch.device`函数中的参数通常可以用一个字符串替代。这允许使用代码快速构建原型。
>
> ```
> ```py
> >> # Example of a function that takes in a torch.device
> >> cuda1 = torch.device('cuda:1')
> >> torch.randn((2,3), device=cuda1)
> ```
>
> ```
> ```py
> >> # You can substitute the torch.device with a string
> >> torch.randn((2,3), 'cuda:1')
> ```
......@@ -79,7 +79,7 @@ device(type='cpu', index=0)
> **注意** 出于传统原因,可以通过单个设备序号构建设备,将其视为`cuda`设备。这匹配`Tensor.get_device()`,它为`cuda`张量返回一个序数,并且不支持`cpu`张量。
>
> ```
> ```py
> >> torch.device(1)
> device(type='cuda', index=1)
> ```
......@@ -88,7 +88,7 @@ device(type='cpu', index=0)
> **注意** 指定设备的方法可以使用(properly formatted)字符串或(legacy)整数型设备序数,即以下示例均等效:
>
> ```
> ```py
> >> torch.randn((2,3), device=torch.device('cuda:1'))
> >> torch.randn((2,3), device='cuda:1')
> >> torch.randn((2,3), device=1) # legacy
......@@ -102,7 +102,7 @@ device(type='cpu', index=0)
例:
```
```py
>>> x = torch.Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
>>> x.stride()
(5, 1)
......
......@@ -6,7 +6,7 @@ torch 支持 COO(rdinate)格式的稀疏张量,可以有效地存储和处
稀疏张量被表示为一对致密张量:值的张量和 2D 张量的索引。可以通过提供这两个张量来构造稀疏张量,以及稀疏张量的大小(不能从这些张量推断出!)假设我们要在位置(0,2)处定义具有条目 3 的稀疏张量, ,位置(1,0)的条目 4,位置(1,2)的条目 5。我们会写:
```
```py
>>> i = torch.LongTensor([[0, 1, 1],
[2, 0, 2]])
>>> v = torch.FloatTensor([3, 4, 5])
......@@ -18,7 +18,7 @@ torch 支持 COO(rdinate)格式的稀疏张量,可以有效地存储和处
请注意,LongTensor 的输入不是索引元组的列表。如果要以这种方式编写索引,则在将它们传递给稀疏构造函数之前,应该进行转置:
```
```py
>>> i = torch.LongTensor([[0, 2], [1, 0], [1, 2]])
>>> v = torch.FloatTensor([3, 4, 5 ])
>>> torch.sparse.FloatTensor(i.t(), v, torch.Size([2,3])).to_dense()
......@@ -29,7 +29,7 @@ torch 支持 COO(rdinate)格式的稀疏张量,可以有效地存储和处
您还可以构建混合稀疏张量,其中只有第一个 n 维是稀疏的,其余的维度是密集的。
```
```py
>>> i = torch.LongTensor([[2, 4]])
>>> v = torch.FloatTensor([[1, 3], [5, 7]])
>>> torch.sparse.FloatTensor(i, v).to_dense()
......@@ -43,7 +43,7 @@ torch 支持 COO(rdinate)格式的稀疏张量,可以有效地存储和处
可以通过指定一个空的稀疏张量来构建一个空的稀疏张量:
```
```py
print torch.sparse.FloatTensor(2, 3)
# FloatTensor of size 2x3 with indices:
# [torch.LongTensor with no dimension]
......
......@@ -12,25 +12,25 @@
[CUDA 语义](http://pytorch.org/docs/master/notes/cuda.html#cuda-semantics)有关于使用 CUDA 的更多细节。
```
```py
torch.cuda.current_blas_handle()
```
返回指向当前 cuBLAS 句柄的 cublasHandle_t 指针
```
```py
torch.cuda.current_device()
```
返回当前所选设备的索引。
```
```py
torch.cuda.current_stream()
```
返回当前选定的`Stream`
```
```py
class torch.cuda.device(idx)
```
......@@ -40,13 +40,13 @@ class torch.cuda.device(idx)
* idx(int) – 设备索引选择。如果这个参数是负的,则是无效操作。
```
```py
torch.cuda.device_count()
```
返回可用的 GPU 数量。
```
```py
class torch.cuda.device_of(obj)
```
......@@ -58,13 +58,13 @@ class torch.cuda.device_of(obj)
* obj (Tensor 或者 Storage) – 在选定设备上分配的对象。
```
```py
torch.cuda.is_available()
```
返回 bool 值,指示当前 CUDA 是否可用。
```
```py
torch.cuda.set_device(device)
```
......@@ -76,7 +76,7 @@ torch.cuda.set_device(device)
* device(int) - 选择的设备。如果此参数为负,则此函数是无操作的。
```
```py
torch.cuda.stream(stream)
```
......@@ -88,7 +88,7 @@ torch.cuda.stream(stream)
* stream(Stream) – 选择的流。如果为`None`,则这个管理器是无效的。
```
```py
torch.cuda.synchronize()
```
......@@ -96,7 +96,7 @@ torch.cuda.synchronize()
### 交流集
```
```py
torch.cuda.comm.broadcast(tensor, devices)
```
......@@ -109,7 +109,7 @@ torch.cuda.comm.broadcast(tensor, devices)
返回: 包含张量副本的元组,放置在对应于索引的设备上。
```
```py
torch.cuda.comm.reduce_add(inputs, destination=None)
```
......@@ -124,7 +124,7 @@ torch.cuda.comm.reduce_add(inputs, destination=None)
返回: 包含放置在`destination`设备上的所有输入的元素总和的张量。
```
```py
torch.cuda.comm.scatter(tensor, devices, chunk_sizes=None, dim=0, streams=None)
```
......@@ -139,7 +139,7 @@ torch.cuda.comm.scatter(tensor, devices, chunk_sizes=None, dim=0, streams=None)
返回: 包含`tensor`块的元组,传播给`devices`
```
```py
torch.cuda.comm.gather(tensors, dim=0, destination=None)
```
......@@ -157,7 +157,7 @@ torch.cuda.comm.gather(tensors, dim=0, destination=None)
## 流和事件
```
```py
class torch.cuda.Stream
```
......@@ -198,7 +198,7 @@ CUDA 流的包装。
提交到此流的所有未来工作将等待直到所有核心在调用完成时提交给给定的流。
```
```py
class torch.cuda.Event(enable_timing=False, blocking=False, interprocess=False, _handle=None)
```
......@@ -238,7 +238,7 @@ CUDA 事件的包装。
## NVIDIA 工具扩展(NVTX)
```
```py
torch.cuda.nvtx.mark(msg)
```
......@@ -246,7 +246,7 @@ CUDA 事件的包装。
* msg(string) - 与事件关联的 ASCII 消息。
```
```py
torch.cuda.nvtx.range_push(msg)
```
......@@ -254,7 +254,7 @@ CUDA 事件的包装。
* msg(string) - 与范围关联的 ASCII 消息
```
```py
torch.cuda.nvtx.range_pop()
```
......
......@@ -4,7 +4,7 @@
`torch.Storage`是单个数据类型的连续的`一维数组`,每个`torch.Tensor`都具有相同数据类型的相应存储。
```
```py
class torch.FloatStorage
```
......
......@@ -25,7 +25,7 @@
`Modules`还可以包含其他模块,允许将它们嵌套在树结构中。您可以将子模块分配为常规属性:
```
```py
import torch.nn as nn
import torch.nn.functional as F
......@@ -46,7 +46,7 @@ class Model(nn.Module):
将一个子模块添加到当前模块。 该模块可以使用给定的名称作为属性访问。 例:
```
```py
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
......@@ -59,7 +59,7 @@ print(model.conv)
输出:
```
```py
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
```
......@@ -67,7 +67,7 @@ Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
适用`fn`递归到每个子模块(如返回`.children()`),以及自我。典型用途包括初始化模型的参数(另见`torch-nn-init`)。 例如:
```
```py
>>> def init_weights(m):
>>> print(m)
>>> if type(m) == nn.Linear:
......@@ -144,7 +144,7 @@ Sequential (
> NOTE: 重复的模块只返回一次。在以下示例中,`l`将仅返回一次。
```
```py
>>> l = nn.Linear(2, 2)
>>> net = nn.Sequential(l, l)
>>> for idx, m in enumerate(net.modules()):
......@@ -162,7 +162,7 @@ Sequential (
例子:
```
```py
>>> for name, module in model.named_children():
>>> if name in ['conv4', 'conv5']:
>>> print(module)
......@@ -174,7 +174,7 @@ Sequential (
> 注意: 重复的模块只返回一次。在以下示例中,`l`将仅返回一次。
>
> ```
> ```py
> >> l = nn.Linear(2, 2)
> >> net = nn.Sequential(l, l)
> >> for idx, m in enumerate(net.named_modules()):
......@@ -190,7 +190,7 @@ Sequential (
>
> 返回模块参数的迭代器,同时产生参数的名称以及参数本身 例如:
>
> ```
> ```py
> >> for name, param in self.named_parameters():
> >> if name in ['bias']:
> >> print(param.size())
......@@ -202,7 +202,7 @@ Sequential (
例子:
```
```py
for param in model.parameters():
print(type(param.data), param.size())
......@@ -216,7 +216,7 @@ for param in model.parameters():
每当计算相对于模块输入的梯度时,将调用该钩。挂钩应具有以下签名:
```
```py
hook(module, grad_input, grad_output) -> Variable or None
```
......@@ -234,7 +234,7 @@ hook(module, grad_input, grad_output) -> Variable or None
例子:
```
```py
self.register_buffer('running_mean', torch.zeros(num_features))
```
......@@ -242,7 +242,7 @@ self.register_buffer('running_mean', torch.zeros(num_features))
在模块上注册一个`forward hook`。 每次调用`forward()`计算输出的时候,这个`hook`就会被调用。它应该拥有以下签名:
```
```py
hook(module, input, output) -> None
```
......@@ -262,7 +262,7 @@ hook(module, input, output) -> None
例子:
```
```py
module.state_dict().keys()
# ['bias', 'weight']
```
......@@ -283,7 +283,7 @@ module.state_dict().keys()
为了更容易理解,给出的是一个小例子:
```
```py
# Example of using Sequential
model = nn.Sequential(
......@@ -313,7 +313,7 @@ model = nn.Sequential(OrderedDict([
例子:
```
```py
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
......@@ -354,7 +354,7 @@ ParameterList 可以像普通 Python 列表一样进行索引,但是它包含
例子:
```
```py
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
......@@ -415,7 +415,7 @@ $$L*{out}=floor((L*{in}+2*padding-dilation*(kernerl_size-1)-1)/stride+1)$$
**example:**
```
```py
>>> m = nn.Conv1d(16, 33, 3, stride=2)
>>> input = autograd.Variable(torch.randn(20, 16, 50))
>>> output = m(input)
......@@ -452,7 +452,7 @@ $$W*{out}=floor((W*{in}+2*padding[1]-dilation[1]*(kernerl_size[1]-1)-1)/stride[1
Examples:
```
```py
>>> # With square kernels and equal stride
>>> m = nn.Conv2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
......@@ -491,7 +491,7 @@ $$out(N*i, C*{out*j})=bias(C*{out*j})+\sum^{C*{in}-1}*{k=0}weight(C*{out_j},k)\b
Examples:
```
```py
>>> # With square kernels and equal stride
>>> m = nn.Conv3d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
......@@ -558,7 +558,7 @@ Examples:
**Example**
```
```py
>>> # With square kernels and equal stride
>>> m = nn.ConvTranspose2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
......@@ -612,7 +612,7 @@ torch.Size([1, 16, 12, 12])
**Example**
```
```py
>>> # With square kernels and equal stride
>>> m = nn.ConvTranspose3d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
......@@ -646,7 +646,7 @@ $$L*{out}=floor((L*{in} + 2*padding - dilation*(kernel_size - 1) - 1)/stride + 1
**example:**
```
```py
>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = autograd.Variable(torch.randn(20, 16, 50))
......@@ -682,7 +682,7 @@ $$W*{out}=floor((W*{in} + 2*padding[1] - dilation[1]*(kernel_size[1] - 1) - 1)/s
**example:**
```
```py
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
......@@ -724,7 +724,7 @@ $$W*{out}=floor((W*{in} + 2*padding[2] - dilation[2]*(kernel_size[2] - 1) - 1)/s
**example:**
```
```py
>>> # pool of square window of size=3, stride=2
>>>m = nn.MaxPool3d(3, stride=2)
>>> # pool of non-square window
......@@ -753,7 +753,7 @@ $$H*{out}=(H*{in}-1)_stride[0]-2_padding[0]+kernel_size[0]$$ 也可以使用`out
**Example:**
```
```py
>>> pool = nn.MaxPool1d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool1d(2, stride=2)
>>> input = Variable(torch.Tensor([[[1, 2, 3, 4, 5, 6, 7, 8]]]))
......@@ -803,7 +803,7 @@ $$W*{out}=(W*{in}-1)_stride[1]-2_padding[1]+kernel_size[1]$$
**Example:**
```
```py
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = Variable(torch.Tensor([[[[ 1, 2, 3, 4],
......@@ -854,7 +854,7 @@ $$ \begin{aligned} D*{out}=(D*{in}-1)*stride[0]-2_padding[0]+kernel_size[0]\ H*{
**Example:**
```
```py
>>> # pool of square window of size=3, stride=2
>>> pool = nn.MaxPool3d(3, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool3d(3, stride=2)
......@@ -885,7 +885,7 @@ $$L*{out}=floor((L*{in}+2*padding-kernel_size)/stride+1)$$
**Example:**
```
```py
>>> # pool with window of size=3, stride=2
>>> m = nn.AvgPool1d(3, stride=2)
>>> m(Variable(torch.Tensor([[[1,2,3,4,5,6,7]]])))
......@@ -918,7 +918,7 @@ $$\begin{aligned} H*{out}=floor((H*{in}+2*padding[0]-kernel*size[0])/stride[0]+1
**Example:**
```
```py
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool2d(3, stride=2)
>>> # pool of non-square window
......@@ -942,7 +942,7 @@ $$ \begin{aligned} out(N*i,C_j,d,h,w)=1/(kD_kH_kW)*\sum^{kD-1}*{k=0}\sum^{kH-1}*
**Example:**
```
```py
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool3d(3, stride=2)
>>> # pool of non-square window
......@@ -964,7 +964,7 @@ $$ \begin{aligned} out(N*i,C_j,d,h,w)=1/(kD_kH_kW)*\sum^{kD-1}*{k=0}\sum^{kH-1}*
**Example:**
```
```py
>>> # pool of square window of size=3, and target output size 13x12
>>> m = nn.FractionalMaxPool2d(3, output_size=(13, 12))
>>> # pool of square window and target output size being half of input image size
......@@ -1000,7 +1000,7 @@ $$f(x)=pow(sum(X,p),1/p)$$
**Example:**
```
```py
>>> # power-2 pool of square window of size=3, stride=2
>>> m = nn.LPPool2d(2, 3, stride=2)
>>> # pool of non-square window of power 1.2
......@@ -1020,7 +1020,7 @@ $$f(x)=pow(sum(X,p),1/p)$$
**Example:**
```
```py
>>> # target output size of 5
>>> m = nn.AdaptiveMaxPool1d(5)
>>> input = autograd.Variable(torch.randn(1, 64, 8))
......@@ -1038,7 +1038,7 @@ $$f(x)=pow(sum(X,p),1/p)$$
**Example:**
```
```py
>>> # target output size of 5x7
>>> m = nn.AdaptiveMaxPool2d((5,7))
>>> input = autograd.Variable(torch.randn(1, 64, 8, 9))
......@@ -1058,7 +1058,7 @@ $$f(x)=pow(sum(X,p),1/p)$$
**Example:**
```
```py
>>> # target output size of 5
>>> m = nn.AdaptiveAvgPool1d(5)
>>> input = autograd.Variable(torch.randn(1, 64, 8))
......@@ -1075,7 +1075,7 @@ $$f(x)=pow(sum(X,p),1/p)$$
**Example:**
```
```py
>>> # target output size of 5x7
>>> m = nn.AdaptiveAvgPool2d((5,7))
>>> input = autograd.Variable(torch.randn(1, 64, 8, 9))
......@@ -1100,7 +1100,7 @@ shape:
例子:
```
```py
>>> m = nn.ReLU()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1120,7 +1120,7 @@ shape:
例子:
```
```py
>>> m = nn.ReLU6()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1138,7 +1138,7 @@ shape:
例子:
```
```py
>>> m = nn.ELU()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1163,7 +1163,7 @@ shape:
例子:
```
```py
>>> m = nn.PReLU()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1186,7 +1186,7 @@ shape:
例子:
```
```py
>>> m = nn.LeakyReLU(0.1)
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1212,7 +1212,7 @@ shape:
例子:
```
```py
>>> m = nn.Threshold(0.1, 20)
>>> input = Variable(torch.randn(2))
>>> print(input)
......@@ -1240,7 +1240,7 @@ shape:
例子:
```
```py
>>> m = nn.Hardtanh()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1260,7 +1260,7 @@ shape:
例子:
```
```py
>>> m = nn.Sigmoid()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1280,7 +1280,7 @@ shape:
例子:
```
```py
>>> m = nn.Tanh()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1298,7 +1298,7 @@ shape:
例子:
```
```py
>>> m = nn.LogSigmoid()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1327,7 +1327,7 @@ shape:
例子:
```
```py
>>> m = nn.Softplus()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1351,7 +1351,7 @@ shape:
例子:
```
```py
>>> m = nn.Softshrink()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1369,7 +1369,7 @@ shape:
例子:
```
```py
>>> m = nn.Softsign()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1389,7 +1389,7 @@ shape:
例子:
```
```py
>>> m = nn.Tanhshrink()
>>> input = autograd.Variable(torch.randn(2))
>>> print(input)
......@@ -1409,7 +1409,7 @@ shape:
例子:
```
```py
>>> m = nn.Softmin()
>>> input = autograd.Variable(torch.randn(2, 3))
>>> print(input)
......@@ -1433,7 +1433,7 @@ shape:
例子:
```
```py
>>> m = nn.Softmax()
>>> input = autograd.Variable(torch.randn(2, 3))
>>> print(input)
......@@ -1453,7 +1453,7 @@ shape:
例子:
```
```py
>>> m = nn.LogSoftmax()
>>> input = autograd.Variable(torch.randn(2, 3))
>>> print(input)
......@@ -1486,7 +1486,7 @@ $$ y = \frac{x - mean[x]}{ \sqrt{Var[x]} + \epsilon} * gamma + beta $$
**例子**
```
```py
>>> # With Learnable Parameters
>>> m = nn.BatchNorm1d(100)
>>> # Without Learnable Parameters
......@@ -1523,7 +1523,7 @@ $$ y = \frac{x - mean[x]}{ \sqrt{Var[x]} + \epsilon} * gamma + beta $$
**例子**
```
```py
>>> # With Learnable Parameters
>>> m = nn.BatchNorm2d(100)
>>> # Without Learnable Parameters
......@@ -1560,7 +1560,7 @@ $$ y = \frac{x - mean[x]}{ \sqrt{Var[x]} + \epsilon} * gamma + beta $$
**例子**
```
```py
>>> # With Learnable Parameters
>>> m = nn.BatchNorm3d(100)
>>> # Without Learnable Parameters
......@@ -1618,7 +1618,7 @@ $$ y = \frac{x - mean[x]}{ \sqrt{Var[x]} + \epsilon} * gamma + beta $$
示例:
```
```py
rnn = nn.RNN(10, 20, 2)
input = Variable(torch.randn(5, 3, 10))
h0 = Variable(torch.randn(2, 3, 20))
......
此差异已折叠。
......@@ -114,7 +114,7 @@ grad*outputs 应该是 output 包含每个输出的预先计算的梯度的长
每次`gradients`被计算的时候,这个`hook`都被调用。`hook`应该拥有以下签名:
```
```py
hook(grad) -> Variable or None
```
......@@ -124,7 +124,7 @@ hook(grad) -> Variable or None
例:
```
```py
>>> v = Variable(torch.Tensor([0, 0, 0]), requires_grad=True)
>>> h = v.register_hook(lambda grad: grad * 2) # double the gradient
>>> v.backward(torch.Tensor([1, 1, 1]))
......
......@@ -23,7 +23,7 @@
例子:
```
```py
optimizer = optim.SGD(model.parameters(), lr = 0.01, momentum=0.9)
optimizer = optim.Adam([var1, var2], lr = 0.0001)
```
......@@ -38,7 +38,7 @@ optimizer = optim.Adam([var1, var2], lr = 0.0001)
例如,当我们想指定每一层的学习率时,这是非常有用的:
```
```py
optim.SGD([
{'params': model.base.parameters()},
{'params': model.classifier.parameters(), 'lr': 1e-3}
......@@ -57,7 +57,7 @@ optim.SGD([
例子
```
```py
for input, target in dataset:
optimizer.zero_grad()
output = model(input)
......@@ -72,7 +72,7 @@ for input, target in dataset:
例子:
```
```py
for input, target in dataset:
def closure():
optimizer.zero_grad()
......@@ -85,7 +85,7 @@ for input, target in dataset:
#### 算法
```
```py
class torch.optim.Optimizer(params, defaults)
```
......@@ -96,7 +96,7 @@ class torch.optim.Optimizer(params, defaults)
1. params (iterable) —— 可迭代的`Variable` 或者 `dict`。指定应优化哪些变量。
2. defaults-(dict):包含优化选项的默认值的 dict(一个参数组没有指定的参数选项将会使用默认值)。
```
```py
load_state_dict(state_dict)
```
......@@ -106,7 +106,7 @@ load_state_dict(state_dict)
1. state_dict (dict) —— `optimizer`的状态。应该是`state_dict()`调用返回的对象。
```
```py
state_dict()
```
......@@ -117,7 +117,7 @@ state_dict()
1. state - 持有当前`optimization`状态的`dict`。它包含了 优化器类之间的不同。
2. param_groups - 一个包含了所有参数组的`dict`
```
```py
step(closure)
```
......@@ -129,7 +129,7 @@ step(closure)
清除所有优化过的`Variable`的梯度。
```
```py
class torch.optim.Adadelta(params, lr=1.0, rho=0.9, eps=1e-06, weight_decay=0)
```
......@@ -145,7 +145,7 @@ class torch.optim.Adadelta(params, lr=1.0, rho=0.9, eps=1e-06, weight_decay=0)
4. lr (float, 可选) – 将 delta 应用于参数之前缩放的系数(默认值:1.0)
5. weight_decay (float, 可选) – 权重衰减 (L2 范数)(默认值: 0)
```
```py
step(closure)
```
......@@ -155,7 +155,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.Adagrad(params, lr=0.01, lr_decay=0, weight_decay=0)
```
......@@ -170,7 +170,7 @@ class torch.optim.Adagrad(params, lr=0.01, lr_decay=0, weight_decay=0)
3. lr_decay (float, 可选) – 学习率衰减(默认: 0)
4. weight_decay (float, 可选) – 权重衰减(L2 范数)(默认: 0)
```
```py
step(closure)
```
......@@ -180,7 +180,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.Adam(params, lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)[source]
```
......@@ -196,7 +196,7 @@ class torch.optim.Adam(params, lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_d
4. eps (float, 可选) – 增加分母的数值以提高数值稳定性(默认:1e-8)
5. weight_decay (float, 可选) – 权重衰减(L2 范数)(默认: 0)
```
```py
step(closure)
```
......@@ -206,7 +206,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.Adamax(params, lr=0.002, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
```
......@@ -222,7 +222,7 @@ class torch.optim.Adamax(params, lr=0.002, betas=(0.9, 0.999), eps=1e-08, weight
4. eps (float, 可选) – 增加分母的数值以提高数值稳定性(默认:1e-8)
5. weight_decay (float, 可选) – 权重衰减(L2 范数)(默认: 0)
```
```py
step(closure=None)
```
......@@ -232,7 +232,7 @@ step(closure=None)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.ASGD(params, lr=0.01, lambd=0.0001, alpha=0.75, t0=1000000.0, weight_decay=0)
```
......@@ -249,7 +249,7 @@ class torch.optim.ASGD(params, lr=0.01, lambd=0.0001, alpha=0.75, t0=1000000.0,
5. t0 (float, 可选) – 指明在哪一次开始平均化(默认:1e6)
6. weight_decay (float, 可选) – 权重衰减(L2 范数)(默认: 0)
```
```py
step(closure)
```
......@@ -259,7 +259,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.LBFGS(params, lr=1, max_iter=20, max_eval=None, tolerance_grad=1e-05, tolerance_change=1e-09, history_size=100, line_search_fn=None)
```
......@@ -278,7 +278,7 @@ class torch.optim.LBFGS(params, lr=1, max_iter=20, max_eval=None, tolerance_grad
5. tolerance_change (float) – 功能值/参数更改的终止公差(默认:1e-9)
6. history_size (int) – 更新历史记录大小(默认:100)
```
```py
step(closure)
```
......@@ -288,7 +288,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.RMSprop(params, lr=0.01, alpha=0.99, eps=1e-08, weight_decay=0, momentum=0, centered=False)[source]
```
......@@ -308,7 +308,7 @@ class torch.optim.RMSprop(params, lr=0.01, alpha=0.99, eps=1e-08, weight_decay=0
6. centered (bool, 可选) – 如果为 True,计算中心化的 RMSProp,通过其方差的估计来对梯度进行归一化
7. weight_decay (float, 可选) – 权重衰减(L2 范数)(默认: 0)
```
```py
step(closure)
```
......@@ -318,7 +318,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.Rprop(params, lr=0.01, etas=(0.5, 1.2), step_sizes=(1e-06, 50))
```
......@@ -331,7 +331,7 @@ class torch.optim.Rprop(params, lr=0.01, etas=(0.5, 1.2), step_sizes=(1e-06, 50)
3. etas (Tuple[float, float], 可选) – 一对(etaminus,etaplis), 它们是乘数增加和减少因子(默认:0.5,1.2)
4. step_sizes (Tuple[float, float], 可选) – 允许的一对最小和最大的步长(默认:1e-6,50)
```
```py
step(closure)
```
......@@ -341,7 +341,7 @@ step(closure)
1. closure (callable,可选) – 重新评估模型并返回损失的闭包。
```
```py
class torch.optim.SGD(params, lr=, momentum=0, dampening=0, weight_decay=0, nesterov=False)
```
......@@ -360,7 +360,7 @@ Nesterov 动量基于[On the importance of initialization and momentum in deep l
例子:
```
```py
>>> optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
>>> optimizer.zero_grad()
>>> loss_fn(model(input), target).backward()
......@@ -371,7 +371,7 @@ Nesterov 动量基于[On the importance of initialization and momentum in deep l
> 带有动量/Nesterov 的 SGD 的实现稍微不同于 Sutskever 等人以及其他框架中的实现。 考虑到 Momentum 的具体情况,更新可以写成 v=ρ∗v+g p=p−lr∗v 其中,p、g、v 和ρ分别是参数、梯度、速度和动量。 这是在对比 Sutskever et. al。和其他框架采用该形式的更新 v=ρ∗v+lr∗g p=p−v Nesterov 版本被类似地修改。
```
```py
step(closure)
```
......@@ -385,7 +385,7 @@ step(closure)
`torch.optim.lr_scheduler` 提供了几种方法来根据 epoches 的数量调整学习率。 `torch.optim.lr_scheduler.ReduceLROnPlateau`允许基于一些验证测量来降低动态学习速率。
```
```py
class torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda, last_epoch=-1)
```
......@@ -399,7 +399,7 @@ class torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda, last_epoch=-1)
例子:
```
```py
>>> # Assuming optimizer has two groups.
>>> lambda1 = lambda epoch: epoch // 30
>>> lambda2 = lambda epoch: 0.95 ** epoch
......@@ -410,7 +410,7 @@ class torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda, last_epoch=-1)
>>> validate(...)
```
```
```py
class torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=-1)
```
......@@ -423,7 +423,7 @@ class torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoc
例子:
```
```py
>>> # Assuming optimizer uses lr = 0.5 for all groups
>>> # lr = 0.05 if epoch < 30
>>> # lr = 0.005 if 30 <= epoch < 60
......@@ -436,7 +436,7 @@ class torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoc
>>> validate(...)
```
```
```py
class torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1, last_epoch=-1)
```
......@@ -451,7 +451,7 @@ class torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1, las
例子:
```
```py
>>> # Assuming optimizer uses lr = 0.5 for all groups
>>> # lr = 0.05 if epoch < 30
>>> # lr = 0.005 if 30 <= epoch < 80
......@@ -463,7 +463,7 @@ class torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1, las
>>> validate(...)
```
```
```py
class torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma, last_epoch=-1)
```
......@@ -473,7 +473,7 @@ class torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma, last_epoch=-1)
2. gamma (float) – 学习率衰减的乘积因子。
3. last_epoch (int) – 最后一个指数。默认: -1.
```
```py
class torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10, verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)
```
......@@ -490,7 +490,7 @@ class torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0
9. min_lr (float or list) – 标量或标量的列表。对所有的组群或每组的学习速率的一个较低的限制。 默认: 0.
10. eps (float) – 适用于 lr 的最小衰减。如果新旧 lr 之间的差异小于 eps,则更新将被忽略。默认: 1e-8.
```
```py
>>> optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
>>> scheduler = torch.optim.ReduceLROnPlateau(optimizer, 'min')
>>> for epoch in range(10):
......
......@@ -17,7 +17,7 @@
例如:
```
```py
>>> x=torch.FloatTensor(5,7,3)
>>> y=torch.FloatTensor(5,7,3)
# 相同形状的质量可以被广播(上述规则总是成立的)
......@@ -45,7 +45,7 @@
例如:
```
```py
# 可以排列尾部维度,使阅读更容易
>>> x=torch.FloatTensor(5,1,4,1)
>>> y=torch.FloatTensor( 3,1,1)
......@@ -70,7 +70,7 @@ RuntimeError: The size of tensor a (2) must match the size of tensor b (3) at no
例如:
```
```py
>>> x=torch.FloatTensor(5,3,4,1)
>>> y=torch.FloatTensor(3,1,1)
>>> (x.add_(y)).size()
......@@ -91,7 +91,7 @@ RuntimeError: The expanded size of the tensor (1) must match the existing size (
例如:
```
```py
>>> torch.add(torch.ones(4,1), torch.randn(4))
```
......@@ -99,7 +99,7 @@ RuntimeError: The expanded size of the tensor (1) must match the existing size (
例如:
```
```py
>>> torch.utils.backcompat.broadcast_warning.enabled=True
>>> torch.add(torch.ones(4,1), torch.ones(4))
__main__:1: UserWarning: self and other do not have the same shape, but are broadcastable, and have the same number of elements.
......
# torch.nn.init
```
```py
torch.nn.init.calculate_gain(nonlinearity,param=None)
```
......@@ -22,11 +22,11 @@ torch.nn.init.calculate_gain(nonlinearity,param=None)
例子:
```
```py
gain = nn.init.gain('leaky_relu')
```
```
```py
torch.nn.init.uniform(tensor, a=0, b=1)[source]
```
......@@ -40,7 +40,7 @@ torch.nn.init.uniform(tensor, a=0, b=1)[source]
例子:
```
```py
w = torch.Tensor(3, 5)
print nn.init.uniform(w)
# 输出:
......@@ -50,7 +50,7 @@ print nn.init.uniform(w)
# [torch.FloatTensor of size 3x5]
```
```
```py
torch.nn.init.normal(tensor, mean=0, std=1)
```
......@@ -64,12 +64,12 @@ torch.nn.init.normal(tensor, mean=0, std=1)
例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.normal(w)
```
```
```py
torch.nn.init.constant(tensor, val)
```
......@@ -82,12 +82,12 @@ torch.nn.init.constant(tensor, val)
例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.constant(w)
```
```
```py
torch.nn.init.eye(tensor)
```
......@@ -99,12 +99,12 @@ torch.nn.init.eye(tensor)
例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.eye(w)
```
```
```py
torch.nn.init.dirac(tensor)
```
......@@ -116,12 +116,12 @@ torch.nn.init.dirac(tensor)
例子:
```
```py
w = torch.Tensor(3, 16, 5, 5)
print torch.nn.init.dirac(w)
```
```
```py
torch.nn.init.xavier_uniform(tensor, gain=1)
```
......@@ -134,12 +134,12 @@ torch.nn.init.xavier_uniform(tensor, gain=1)
例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.xavier_uniform(w, gain=nn.init.calculate_gain('relu'))
```
```
```py
torch.nn.init.xavier_normal(tensor, gain=1)
```
......@@ -152,12 +152,12 @@ torch.nn.init.xavier_normal(tensor, gain=1)
例子:
```
```py
>>> w = torch.Tensor(3, 5)
>>> nn.init.xavier_normal(w)
```
```
```py
torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
```
......@@ -171,12 +171,12 @@ torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
例子:
```
```py
w = torch.Tensor(3, 5)
torch.nn.init.kaiming_uniform(w, mode='fan_in')
```
```
```py
torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
```
......@@ -188,12 +188,12 @@ torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
2. a -此层后使用的整流器的负斜率(默认为 ReLU 为 0)
3. mode - "fan_in"(默认)或"fan_out"。"fan_in"保留正向传播时权值方差的量级,"fan_out"保留反向传播时的量级。
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.kaiming_normal(w, mode='fan_out')
```
```
```py
torch.nn.init.orthogonal(tensor, gain=1)
```
......@@ -206,12 +206,12 @@ torch.nn.init.orthogonal(tensor, gain=1)
例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.orthogonal(w)
```
```
```py
torch.nn.init.sparse(tensor, sparsity, std=0.01)
```
......@@ -224,7 +224,7 @@ torch.nn.init.sparse(tensor, sparsity, std=0.01)
3. std - 用于生成的正态分布的标准差
4. non-zero values (the) – 例子:
```
```py
w = torch.Tensor(3, 5)
print torch.nn.init.sparse(w, sparsity=0.1)
```
......
......@@ -18,19 +18,19 @@
## 战略管理
```
```py
torch.multiprocessing.get_all_sharing_strategies()
```
返回一组当前系统支持的共享策略。
```
```py
torch.multiprocessing.get_sharing_strategy()
```
返回共享 CPU 张量的当前策略
```
```py
torch.multiprocessing.set_sharing_strategy(new_strategy)
```
......
......@@ -74,7 +74,7 @@ Rank 是分配给分布式组中每个进程的唯一标识符。它们总是连
或者,地址必须是有效的 IP 多播地址,在这种情况下可以自动分配等级。组播初始化还支持一个 group_name 参数,只要使用不同的组名,就可以为多个作业使用相同的地址。
```
```py
import torch.distributed as dist
# Use address of one of the machines
......@@ -93,7 +93,7 @@ dist.init_process_group(init_method='tcp://[ff15:1e18:5d4c:4cf0:d02d:b659:53ba:b
该方法假设文件系统支持使用 fcntl 大多数本地系统进行锁定,NFS 支持它。
```
```py
import torch.distributed as dist
# Rank will be assigned automatically if unspecified
......
......@@ -4,13 +4,13 @@
用命令行运行它
```
```py
python -m torch.utils.bottleneck /path/to/source/script.py [args]
```
`[args]``script.py`中的任意参数,也可以运行如下代码获取更多使用说明。
```
```py
python -m torch.utils.bottleneck -h
```
......
......@@ -43,7 +43,7 @@
例:
```
```py
>>> model = nn.Sequential(...)
>>> input_var = checkpoint_sequential(model, chunks, input_var)
```
......
......@@ -10,7 +10,7 @@
```
```py
>>> from setuptools import setup
>>> from torch.utils.cpp_extension import BuildExtension, CppExtension
>>> setup(
......@@ -32,7 +32,7 @@
```
```py
>>> from setuptools import setup
>>> from torch.utils.cpp_extension import BuildExtension, CppExtension
>>> setup(
......@@ -83,7 +83,7 @@
```
```py
>>> from torch.utils.cpp_extension import load
>>> module = load(
name='extension',
......@@ -101,7 +101,7 @@
例如:
```
```py
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CppExtension
......
## torch.utils.data
```
```py
class torch.utils.data.Dataset
```
......@@ -8,7 +8,7 @@ class torch.utils.data.Dataset
所有其他数据集都应该进行子类化。所有子类应该覆盖`__len__``__getitem__``__len__`提供了数据集的大小,`__getitem__`支持整数索引,范围从 0 到 len(self)。
```
```py
class torch.utils.data.TensorDataset(data_tensor, target_tensor)
```
......@@ -23,7 +23,7 @@ class torch.utils.data.TensorDataset(data_tensor, target_tensor)
例子:
```
```py
x = torch.linspace(1, 10, 10) # x data (torch tensor)
y = torch.linspace(10, 1, 10) # y data (torch tensor)
......@@ -31,7 +31,7 @@ y = torch.linspace(10, 1, 10) # y data (torch tensor)
torch_dataset = torch.utils.data.TensorDataset(data_tensor=x, target_tensor=y)
```
```
```py
class torch.utils.data.ConcatDataset(datasets)
```
......@@ -42,7 +42,7 @@ class torch.utils.data.ConcatDataset(datasets)
* datasets 的参数:要连接的数据集列表
* datasets 样式:iterable
```
```py
class torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, sampler=None, num_workers=0, collate_fn=<function default_collate>, pin_memory=False, drop_last=False)
```
......@@ -60,7 +60,7 @@ class torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, sampler=
8. pin_memory (bool, optional) – 如果为 True,数据加载器在返回前将张量复制到 CUDA 固定内存中。
9. drop_last (bool, optional) – 如果数据集大小不能被 batch_size 整除,设置为 True 可删除最后一个不完整的批处理。如果设为 False 并且数据集的大小不能被 batch_size 整除,则最后一个 batch 将更小。(默认: False)
```
```py
class torch.utils.data.sampler.Sampler(data_source)
```
......@@ -68,7 +68,7 @@ class torch.utils.data.sampler.Sampler(data_source)
每个采样器子类必须提供一个`__iter__`方法,提供一种迭代数据集元素的索引的方法,以及返回迭代器长度的`__len__`方法。
```
```py
class torch.utils.data.sampler.SequentialSampler(data_source)
```
......@@ -78,7 +78,7 @@ class torch.utils.data.sampler.SequentialSampler(data_source)
* `data_source (Dataset)` – 采样的数据集。
```
```py
class torch.utils.data.sampler.RandomSampler(data_source)
```
......@@ -86,7 +86,7 @@ class torch.utils.data.sampler.RandomSampler(data_source)
参数: - `data_source (Dataset)` – 采样的数据集。
```
```py
class torch.utils.data.sampler.SubsetRandomSampler(indices)
```
......@@ -94,7 +94,7 @@ class torch.utils.data.sampler.SubsetRandomSampler(indices)
参数: - `indices (list)` – 索引的列表
```
```py
class torch.utils.data.sampler.WeightedRandomSampler(weights, num_samples, replacement=True)
```
......@@ -105,7 +105,7 @@ class torch.utils.data.sampler.WeightedRandomSampler(weights, num_samples, repla
* `weights (list)` – 权重列表。不需要加起来为 1
* `num_samples (int)` – 要绘制的样本数
```
```py
class torch.utils.data.distributed.DistributedSampler(dataset, num_replicas=None, rank=None)
```
......
# torch.utils.ffi
```
```py
torch.utils.ffi.create_extension(name, headers, sources, verbose=True, with_cuda=False, package=False, relative_to='.', **kwargs)
```
......
# torch.utils.model_zoo
```
```py
torch.utils.model_zoo.load_url(url, model_dir=None)
```
......@@ -18,7 +18,7 @@ torch.utils.model_zoo.load_url(url, model_dir=None)
例如:
```
```py
>>> state_dict = torch.utils.model_zoo.load_url('https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth')
```
......
......@@ -18,7 +18,7 @@
下面可以用一个小例子来展示:
```
```py
cuda = torch.device("cuda") # 默认为 CUDA 设备
cuda0 = torch.device("cuda:0")
cuda2 = torch.device("cuda:2") # GPU 2
......@@ -61,7 +61,7 @@ CUDA 流是属于特定设备的线性执行序列。您通常不需要明确创
除非显式的使用同步函数(例如 `synchronize()``wait_stream()` ),否则每个流内的操作都按照它们创建的顺序进行序列化,但是来自不同流的操作可以以任意相对顺序并发执行。例如,下面的代码是不正确的:
```
```py
cuda = torch.device("cuda")
s = torch.cuda.stream() # 在当前流中创建一个新的流
A = torch.empty((100,100), device = cuda).normal_(0.0, 1.0)
......@@ -83,7 +83,7 @@ with torch.cuda.stream(s):
第一步是确定是否应该使用 GPU。一种常见的模式是使用 Python 的 `argparse` 模块来读入用户参数,并且有一个标志可用于禁用 CUDA,并结合 `is_available()` 使用。在下面的内容中,`args.device` 会生成一个 `torch.device` 对象,该对象可用于将张量移动到 CPU 或 CUDA。
```
```py
import argparse
import torch
......@@ -99,14 +99,14 @@ else:
现在我们有了 `args.device`,我们可以使用它在所需的设备上创建一个张量。
```
```py
x = torch.empty((8, 42), device = args.device)
net = Network().to(device = args.device)
```
这可以在许多情况下用于生成设备不可知代码。以下是使用 `dataloader` 的例子:
```
```py
cuda0 = torch.device('cuda:0') # CUDA GPU 0
for i, x in enumerate(train_loader):
x = x.to(cuda0)
......@@ -114,7 +114,7 @@ for i, x in enumerate(train_loader):
在系统上使用多个 GPU 时,您可以使用 `CUDA_VISIBLE_DEVICES` 环境标志来管理 PyTorch 可用的 GPU。如上所述,要手动控制在哪个 GPU 上创建张量,最佳做法是使用 `torch.cuda.device` 上下文管理器。
```
```py
print("外部的设备是 0") # 在设备 0 上
with torch.cuda.device(1):
print("内部的设备是 1") # 设备 1
......@@ -125,7 +125,7 @@ print("外部的设备仍是 0") # 设备 0
这是建立模块时推荐的做法,在前向传递期间需要在内部创建新的张量
```
```py
cuda = torch.device("cuda")
x_cpu = torch.empty(2)
y_gpu = torch.empty(2, device = cuda)
......@@ -149,7 +149,7 @@ print(y_cpu_long)
如果要创建与另一个张量相同类型和大小的张量,并将其填充为 1 或 0,则可以使用 `ones_like()``zeros_like()` 作为便捷的辅助函数(也可以保留 `torch.device``torch.dtype` 的张量)。
```
```py
x_cpu = torch.empty(2,3)
x_gpu = torch.empty(2,3)
......
......@@ -6,7 +6,7 @@
这是一个简单的脚本,将`torchvision`中定义的预训练的`AlexNet`导出到`ONNX`中。它运行一轮推理,然后将结果跟踪模型保存到`alexnet.proto`
```
```py
from torch.autograd import Variable
import torch.onnx
import torchvision
......@@ -18,7 +18,7 @@ torch.onnx.export(model, dummy_input, "alexnet.proto", verbose=True)
保存文件`alexnet.proto`是一个二进制`protobuf`文件,其中包含您导出的模型(在本例中为`AlexNet`)的网络结构和参数。关键字参数`verbose=True`导致导出器打印出一个人类可读的网络表示:
```
```py
# All parameters are encoded explicitly as inputs. By convention,
# learned parameters (ala nn.Module.state_dict) are first, and the
# actual inputs are last.
......@@ -50,13 +50,13 @@ graph(%1 : Float(64, 3, 11, 11)
您也可以使用[onnx](https://github.com/onnx/onnx/)库来验证`protobuf`。你可以`onnx``conda`安装:
```
```py
conda install -c conda-forge onnx
```
然后,你可以运行:
```
```py
import onnx
# Load the ONNX model
......@@ -75,13 +75,13 @@ onnx.helper.printable_graph(model.graph)
* 2、你需要`onnx-caffe2`,一个纯`Python`库,为`ONNX`提供一个`Caffe2`后端。`onnx-caffe2`你可以用`pip`来安装:
```
```py
pip install onnx-caffe2
```
安装完成后,您可以使用`Caffe2`的后端:
```
```py
# ...continuing from above
import onnx_caffe2.backend as backend
import numpy as np
......@@ -114,7 +114,7 @@ print(outputs[0])
### torch.onnx 功能
```
```py
torch.onnx.export(model, args, f, export_params=True, verbose=False, training=False)
```
......
......@@ -4,13 +4,13 @@
如下代码用于获取加载图像的包的名称。
```
```py
torchvision.get_image_backend()
```
指定用于加载图像的包。
```
```py
torchvision.set_image_backend(backend)
```
......
......@@ -16,7 +16,7 @@
所有数据集都是`torch.utils.data.Dataset`的子类, 即它们具有**getitem****len**实现方法。因此,它们都可以传递给`torch.utils.data.DataLoader` 可以使用`torch.multiprocessing`工作人员并行加载多个样本的数据。例如:
```
```py
imagenet_data = torchvision.datasets.ImageFolder('path/to/imagenet_root/')
data_loader = torch.utils.data.DataLoader(imagenet_data,
batch_size=4,
......@@ -28,7 +28,7 @@ data_loader = torch.utils.data.DataLoader(imagenet_data,
#### MNIST
```
```py
dset.MNIST(root, train=True, transform=None, target_transform=None, download=False)
```
......@@ -44,7 +44,7 @@ dset.MNIST(root, train=True, transform=None, target_transform=None, download=Fal
需要安装[COCO API](https://github.com/pdollar/coco/tree/master/PythonAPI)
```
```py
dset.CocoCaptions(root="dir where images are", annFile="json annotation file", [transform, target_transform])
```
......@@ -57,7 +57,7 @@ dset.CocoCaptions(root="dir where images are", annFile="json annotation file", [
例子:
```
```py
import torchvision.datasets as dset
import torchvision.transforms as transforms
cap = dset.CocoCaptions(root = 'dir where images are',
......@@ -73,7 +73,7 @@ print(target)
输出:
```
```py
Number of samples: 82783
Image Size: (3L, 427L, 640L)
[u'A plane emitting smoke stream flying over a mountain.',
......@@ -87,7 +87,7 @@ u'A mountain view with a plume of smoke in the background']
检测:
```
```py
dset.CocoDetection(root="dir where images are", annFile="json annotation file", [transform, target_transform])
```
......@@ -102,7 +102,7 @@ dset.CocoDetection(root="dir where images are", annFile="json annotation file",
#### LSUN
```
```py
dset.LSUN(db_path, classes='train', [transform, target_transform])
```
......@@ -117,7 +117,7 @@ dset.LSUN(db_path, classes='train', [transform, target_transform])
一个通用的数据加载器,数据集中的数据以以下方式组织
```
```py
root/dog/xxx.png
root/dog/xxy.png
root/dog/xxz.png
......@@ -141,7 +141,7 @@ dset.ImageFolder(root="root folder path", [transform, target_transform])
#### CIFAR
```
```py
dset.CIFAR10(root, train=True, transform=None, target_transform=None, download=False)
dset.CIFAR100(root, train=True, transform=None, target_transform=None, download=False)
......@@ -157,7 +157,7 @@ dset.CIFAR100(root, train=True, transform=None, target_transform=None, download=
#### STL10
```
```py
dset.STL10(root, split='train', transform=None, target_transform=None, download=False)
```
......@@ -171,7 +171,7 @@ dset.STL10(root, split='train', transform=None, target_transform=None, download=
#### SVHN
```
```py
class torchvision.datasets.SVHN(root, split='train', transform=None, target_transform=None, download=False)
```
......@@ -185,7 +185,7 @@ class torchvision.datasets.SVHN(root, split='train', transform=None, target_tran
#### PhotoTour
```
```py
class torchvision.datasets.PhotoTour(root, name, train=True, transform=None, download=False)
```
......
......@@ -10,7 +10,7 @@
可以通过调用构造函数来构造具有随机权重的模型:
```
```py
import torchvision.models as models
resnet18 = models.resnet18()
alexnet = models.alexnet()
......@@ -20,7 +20,7 @@ densenet = models.densenet_161()
我们提供的 Pathway 变体和 alexnet 预训练的模型,利用 pytorch 的`torch.utils.model_zoo`。这些可以通过构建`pretrained=True`
```
```py
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)
alexnet = models.alexnet(pretrained=True)
......@@ -28,7 +28,7 @@ alexnet = models.alexnet(pretrained=True)
所有预训练的模型的期望输入图像相同的归一化,即小批量形状通道的 RGB 图像(3 x H x W),其中 H 和 W 预计将至少 224。这些图像必须被加载到[ 0, 1 ]的范围内,然后使用平均= [ 0.485,0.456,0.406 ]和 STD=[ 0.229,0.224,0.225 ]进行归一化。您可以使用以下转换来正常化:
```
```py
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
```
......@@ -54,7 +54,7 @@ normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0
| Densenet-201 | 22.80 | 6.43 |
| Densenet-161 | 22.35 | 6.20 |
```
```py
torchvision.models.alexnet(pretrained=False, ** kwargs)
```
......@@ -62,19 +62,19 @@ AlexNet 模型结构 paper 地址
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.resnet18(pretrained=False, ** kwargs)
```
构建一个 resnet18 模型 pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.resnet34(pretrained=False, ** kwargs)
```
构建一个 ResNet-34 模型. Parameters: pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.resnet50(pretrained=False, ** kwargs)
```
......@@ -82,7 +82,7 @@ torchvision.models.resnet50(pretrained=False, ** kwargs)
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.resnet101(pretrained=False, ** kwargs)
```
......@@ -90,7 +90,7 @@ torchvision.models.resnet101(pretrained=False, ** kwargs)
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.resnet152(pretrained=False, ** kwargs)
```
......@@ -98,7 +98,7 @@ torchvision.models.resnet152(pretrained=False, ** kwargs)
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.vgg11(pretrained=False, ** kwargs)
```
......@@ -106,13 +106,13 @@ VGG 11-layer model (configuration “A”) -
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.vgg11_bn(** kwargs)
```
VGG 11-layer model (configuration “A”) with batch normalization
```
```py
torchvision.models.vgg13(pretrained=False, ** kwargs)
```
......@@ -120,13 +120,13 @@ VGG 13-layer model (configuration “B”)
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.vgg13_bn(** kwargs)
```
VGG 13-layer model (configuration “B”) with batch normalization
```
```py
torchvision.models.vgg16(pretrained=False, ** kwargs)
```
......@@ -134,13 +134,13 @@ VGG 16-layer model (configuration “D”)
Parameters: pretrained (bool) – If True, returns a model pre-trained on ImageNet
```
```py
torchvision.models.vgg16_bn(** kwargs)
```
VGG 16-layer model (configuration “D”) with batch normalization
```
```py
torchvision.models.vgg19(pretrained=False, ** kwargs)
```
......@@ -148,7 +148,7 @@ VGG 19-layer model (configuration “E”)
pretrained (bool) – True, 返回在 ImageNet 上训练好的模型。
```
```py
torchvision.models.vgg19_bn(** kwargs)
```
......
......@@ -9,7 +9,7 @@
变换是常用的图像变换。它们可以用`Compose`连接在一起。
```
```py
class torchvision.transforms.Compose(transforms)
```
......@@ -17,7 +17,7 @@ class torchvision.transforms.Compose(transforms)
transforms: 由 transform 构成的列表. 例子:
```
```py
transforms.Compose([
transforms.CenterCrop(10),
transforms.ToTensor(),
......@@ -28,7 +28,7 @@ transforms.Compose([
* * *
```
```py
class torchvision.transforms.Scale(size, interpolation=2)
```
......@@ -39,31 +39,31 @@ class torchvision.transforms.Scale(size, interpolation=2)
1. size (sequence or int) - 期望输出尺寸。如果 size 是一个像(w, h)的序列,输出大小将按照 w,h 匹配到。如果大小是 int,则图像将匹配到这个数字。例如,如果原图的`height&gt;width`,那么改变大小后的图片大小是`(size*height/width, size)`
2. interpolation (int, optional) -需要添加值。默认的是`PIL.Image.BILINEAR`
```
```py
class torchvision.transforms.CenterCrop(size)
```
将给定的 PIL.Image 进行中心切割,得到给定的 size,size 可以是 tuple,(target_height, target_width)。size 也可以是一个 Integer,在这种情况下,切出来的图片的形状是正方形。
```
```py
class torchvision.transforms.RandomCrop(size, padding=0)
```
切割中心点的位置随机选取。size 可以是 tuple 也可以是 Integer。
```
```py
class torchvision.transforms.RandomHorizontalFlip
```
随机水平翻转给定的 PIL.Image,概率为 0.5。即:一半的概率翻转,一半的概率不翻转。
```
```py
class torchvision.transforms.RandomSizedCrop(size, interpolation=2)
```
先将给定的 PIL.Image 随机切,然后再 resize 成给定的 size 大小。
```
```py
class torchvision.transforms.Pad(padding, fill=0)
```
......@@ -73,7 +73,7 @@ class torchvision.transforms.Pad(padding, fill=0)
* * *
```
```py
class torchvision.transforms.Normalize(mean, std)
```
......@@ -90,7 +90,7 @@ class torchvision.transforms.Normalize(mean, std)
* * *
```
```py
class torchvision.transforms.ToTensor
```
......@@ -102,7 +102,7 @@ class torchvision.transforms.ToTensor
2. 返回结果: 转换后的图像。
3. 返回样式: Tensor 张量
```
```py
class torchvision.transforms.ToPILImage
```
......@@ -118,7 +118,7 @@ class torchvision.transforms.ToPILImage
* * *
```
```py
class torchvision.transforms.Lambda(lambd)
```
......
# torchvision.utils
```
```py
torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)
```
......@@ -19,7 +19,7 @@ torchvision.utils.make_grid(tensor, nrow=8, padding=2, normalize=False, range=No
查看下面的例子:
```
```py
torchvision.utils.save_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)
```
......
......@@ -17,7 +17,7 @@
你可以从下面的代码看到`torch.nn`模块的`Linear`函数, 以及注解
```
```py
# Inherit from Function
class Linear(Function):
......@@ -55,13 +55,13 @@ class Linear(Function):
现在,为了更方便使用这些自定义操作,推荐使用`apply`方法:
```
```py
linear = LinearFunction.apply
```
我们下面给出一个由非变量参数进行参数化的函数的例子:
```
```py
class MulConstant(Function):
@staticmethod
def forward(ctx, tensor, constant):
......@@ -79,7 +79,7 @@ class MulConstant(Function):
你可能想检测你刚刚实现的`backward`方法是否正确的计算了梯度。你可以使用小的有限差分法(`Finite Difference`)进行数值估计。
```
```py
from torch.autograd import gradcheck
# gradcheck takes a tuple of tensors as input, check if your gradient
......@@ -105,7 +105,7 @@ print(test)
下面是实现`Linear`模块的方式:
```
```py
class Linear(nn.Module):
def __init__(self, input_features, output_features, bias=True):
super(Linear, self).__init__()
......
......@@ -8,7 +8,7 @@
有时,当可微分变量可能发生时,它可能并不明显。考虑以下训练循环(从[源代码](https://discuss.pytorch.org/t/high-memory-usage-while-training/162)节选):
```
```py
total_loss = 0
for i in range(10000):
optimizer.zero_grad()
......@@ -27,7 +27,7 @@ for i in range(10000):
作用域的范围可能比你想象的要大。例如:
```
```py
for i in range(5):
intermdeiate = f(input[i])
result += g(intermediate)
......@@ -59,7 +59,7 @@ PyTorch 使用缓存内存分配器来加速内存分配。因此,`nvidia-smi`
的序列。例如,你可以写:
```
```py
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_squence
class MyModule(nn.Module):
......
......@@ -49,7 +49,7 @@
具体的 Hogwild 实现可以在 [示例库](https://github.com/pytorch/examples/tree/master/mnist_hogwild) 中找到,但为了展示代码的整体结构,下面还有一个最简单的示例:
```
```py
import torch.multiprocessing as mp
from model import MyModel
......
......@@ -6,26 +6,26 @@
第一个(推荐)只保存和加载模型参数:
```
```py
torch.save(the_model.state_dict(), PATH)
```
然后:
```
```py
the_model = TheModelClass(*args, **kwargs)
the_mdel.load_state_dict(torch.load(PATH))
```
第二个方法是保存并加载整个模型:
```
```py
torch.save(the_model, PATH)
```
然后:
```
```py
the_model = torch.load(PATH)
```
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册