class torch.nn.Parameter?
一種被視為模塊參數(shù)的 Tensor。
參數(shù)是 Tensor
子類,當(dāng)與 Module
一起使用時(shí),具有非常特殊的屬性-將它們分配為模塊屬性時(shí),它們會(huì)自動(dòng)添加到其列表中 參數(shù),并會(huì)出現(xiàn),例如 在 parameters()
迭代器中。 分配張量不會(huì)產(chǎn)生這種效果。 這是因?yàn)榭赡芤谀P椭芯彺嬉恍┡R時(shí)狀態(tài),例如 RNN 的最后一個(gè)隱藏狀態(tài)。 如果不存在 Parameter
這樣的類,這些臨時(shí)人員也將被注冊(cè)。
參數(shù)
class torch.nn.Module?
所有神經(jīng)網(wǎng)絡(luò)模塊的基類。
您的模型也應(yīng)該繼承此類。
模塊也可以包含其他模塊,從而可以將它們嵌套在樹形結(jié)構(gòu)中。 您可以將子模塊分配為常規(guī)屬性:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
以這種方式分配的子模塊將被注冊(cè),并且當(dāng)您調(diào)用 to()
等時(shí),也會(huì)轉(zhuǎn)換其參數(shù)。
add_module(name, module)?
將子模塊添加到當(dāng)前模塊。
可以使用給定名稱將模塊作為屬性訪問。
Parameters
apply(fn)?
將fn
遞歸應(yīng)用于每個(gè)子模塊(由.children()
返回)以及自身。 典型的用法包括初始化模型的參數(shù)(另請(qǐng)參見 torch.nn.init)。
Parameters
fn (Module
->無)–應(yīng)用于每個(gè)子模塊的功能
退貨
自
返回類型
模塊
例:
>>> def init_weights(m):
>>> print(m)
>>> if type(m) == nn.Linear:
>>> m.weight.data.fill_(1.0)
>>> print(m.weight)
>>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
>>> net.apply(init_weights)
Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[ 1., 1.],
[ 1., 1.]])
Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[ 1., 1.],
[ 1., 1.]])
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
buffers(recurse=True)?
返回模塊緩沖區(qū)上的迭代器。
Parameters
遞歸 (bool )–如果為 True,則產(chǎn)生此模塊和所有子模塊的緩沖區(qū)。 否則,僅產(chǎn)生作為該模塊直接成員的緩沖區(qū)。
Yields
torch張緊器 –模塊緩沖區(qū)
Example:
>>> for buf in model.buffers():
>>> print(type(buf.data), buf.size())
<class 'torch.FloatTensor'> (20L,)
<class 'torch.FloatTensor'> (20L, 1L, 5L, 5L)
children()?
返回直接子代模塊上的迭代器。
Yields
模塊 –子模塊
cpu()?
將所有模型參數(shù)和緩沖區(qū)移至 CPU。
Returns
self
Return type
Module
cuda(device=None)?
將所有模型參數(shù)和緩沖區(qū)移至 GPU。
這也使相關(guān)的參數(shù)并緩沖不同的對(duì)象。 因此,在構(gòu)建優(yōu)化程序之前,如果模塊在優(yōu)化過程中可以在 GPU 上運(yùn)行,則應(yīng)調(diào)用它。
Parameters
設(shè)備 (python:int , 可選)–如果指定,則所有參數(shù)都將復(fù)制到該設(shè)備
Returns
self
Return type
Module
double()?
將所有浮點(diǎn)參數(shù)和緩沖區(qū)強(qiáng)制轉(zhuǎn)換為double
數(shù)據(jù)類型。
Returns
self
Return type
Module
dump_patches = False?
這為 load_state_dict()
提供了更好的 BC 支持。 在 state_dict()
中,版本號(hào)將保存為返回狀態(tài) dict 的屬性 <cite>_metadata</cite> 中,因此會(huì)被腌制。 <cite>_metadata</cite> 是字典,其鍵遵循狀態(tài) dict 的命名約定。 有關(guān)如何在加載中使用此信息的信息,請(qǐng)參見_load_from_state_dict
。
如果從模塊添加/刪除了新的參數(shù)/緩沖區(qū),則該數(shù)字將增加,并且模塊的 <cite>_load_from_state_dict</cite> 方法可以比較版本號(hào),并且如果狀態(tài) dict 來自更改之前,則可以進(jìn)行適當(dāng)?shù)母摹?/p>
eval()?
將模塊設(shè)置為評(píng)估模式。
這僅對(duì)某些模塊有影響。 請(qǐng)參閱特定模塊的文檔,以了解其在訓(xùn)練/評(píng)估模式下的行為的詳細(xì)信息(如果受到影響),例如 Dropout
,BatchNorm
等
這等效于 self.train(False)
。
Returns
self
Return type
Module
extra_repr()?
設(shè)置模塊的額外表示形式
要打印自定義的額外信息,您應(yīng)該在自己的模塊中重新實(shí)現(xiàn)此方法。 單行和多行字符串都是可以接受的。
float()?
將所有浮點(diǎn)參數(shù)和緩沖區(qū)強(qiáng)制轉(zhuǎn)換為 float 數(shù)據(jù)類型。
Returns
self
Return type
Module
forward(*input)?
定義每次調(diào)用時(shí)執(zhí)行的計(jì)算。
應(yīng)該被所有子類覆蓋。
注意
盡管需要在此函數(shù)中定義向前傳遞的配方,但此后應(yīng)調(diào)用 Module
實(shí)例,而不是此實(shí)例,因?yàn)榍罢哓?fù)責(zé)運(yùn)行已注冊(cè)的鉤子,而后者則靜默地忽略它們。
half()?
將所有浮點(diǎn)參數(shù)和緩沖區(qū)強(qiáng)制轉(zhuǎn)換為half
數(shù)據(jù)類型。
Returns
self
Return type
Module
load_state_dict(state_dict, strict=True)?
將參數(shù)和緩沖區(qū)從 state_dict
復(fù)制到此模塊及其子代中。 如果strict
為True
,則 state_dict
的鍵必須與該模塊的 state_dict()
功能返回的鍵完全匹配。
Parameters
state_dict
中的鍵與 此模塊的 state_dict()
功能返回的鍵。 默認(rèn)值:True
Returns
Return type
具有missing_keys
和unexpected_keys
字段的NamedTuple
modules()?
返回網(wǎng)絡(luò)中所有模塊的迭代器。
Yields
模塊 –網(wǎng)絡(luò)中的模塊
Note
重復(fù)的模塊僅返回一次。 在以下示例中,l
將僅返回一次。
Example:
>>> l = nn.Linear(2, 2)
>>> net = nn.Sequential(l, l)
>>> for idx, m in enumerate(net.modules()):
print(idx, '->', m)
0 -> Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
1 -> Linear(in_features=2, out_features=2, bias=True)
named_buffers(prefix='', recurse=True)?
返回模塊緩沖區(qū)上的迭代器,同時(shí)產(chǎn)生緩沖區(qū)的名稱和緩沖區(qū)本身。
Parameters
Yields
(字符串,torch張量) –包含名稱和緩沖區(qū)的元組
Example:
>>> for name, buf in self.named_buffers():
>>> if name in ['running_var']:
>>> print(buf.size())
named_children()?
返回直接子模塊的迭代器,同時(shí)產(chǎn)生模塊名稱和模塊本身。
Yields
(字符串,模塊) –包含名稱和子模塊的元組
Example:
>>> for name, module in model.named_children():
>>> if name in ['conv4', 'conv5']:
>>> print(module)
named_modules(memo=None, prefix='')?
在網(wǎng)絡(luò)中的所有模塊上返回一個(gè)迭代器,同時(shí)產(chǎn)生模塊的名稱和模塊本身。
Yields
(字符串,模塊) –名稱和模塊的元組
Note
Duplicate modules are returned only once. In the following example, l
will be returned only once.
Example:
>>> l = nn.Linear(2, 2)
>>> net = nn.Sequential(l, l)
>>> for idx, m in enumerate(net.named_modules()):
print(idx, '->', m)
0 -> ('', Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
))
1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
named_parameters(prefix='', recurse=True)?
返回模塊參數(shù)上的迭代器,同時(shí)產(chǎn)生參數(shù)名稱和參數(shù)本身。
Parameters
Yields
(字符串,參數(shù)) –包含名稱和參數(shù)的元組
Example:
>>> for name, param in self.named_parameters():
>>> if name in ['bias']:
>>> print(param.size())
parameters(recurse=True)?
返回模塊參數(shù)上的迭代器。
通常將其傳遞給優(yōu)化器。
Parameters
recurse (bool) – if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
Yields
參數(shù) –模塊參數(shù)
Example:
>>> for param in model.parameters():
>>> print(type(param.data), param.size())
<class 'torch.FloatTensor'> (20L,)
<class 'torch.FloatTensor'> (20L, 1L, 5L, 5L)
register_backward_hook(hook)?
在模塊上注冊(cè)向后掛鉤。
每當(dāng)計(jì)算相對(duì)于模塊輸入的梯度時(shí),都會(huì)調(diào)用該掛鉤。 掛鉤應(yīng)具有以下簽名:
hook(module, grad_input, grad_output) -> Tensor or None
如果模塊具有多個(gè)輸入或輸出,則grad_input
和grad_output
可能是元組。 掛鉤不應(yīng)該修改其參數(shù),但可以選擇相對(duì)于輸入返回新的梯度,該梯度將在后續(xù)計(jì)算中代替grad_input
使用。
Returns
可以通過調(diào)用handle.remove()
來刪除添加的鉤子的句柄
Return type
torch.utils.hooks.RemovableHandle
警告
對(duì)于執(zhí)行許多操作的復(fù)雜 Module
,當(dāng)前實(shí)現(xiàn)不具有所呈現(xiàn)的行為。 在某些故障情況下,grad_input
和grad_output
將僅包含輸入和輸出的子集的梯度。 對(duì)于此類 Module
,應(yīng)在特定的輸入或輸出上直接使用 torch.Tensor.register_hook()
以獲取所需的梯度。
register_buffer(name, tensor)?
將持久性緩沖區(qū)添加到模塊。
這通常用于注冊(cè)不應(yīng)被視為模型參數(shù)的緩沖區(qū)。 例如,BatchNorm 的running_mean
不是參數(shù),而是持久狀態(tài)的一部分。
可以使用給定名稱將緩沖區(qū)作為屬性進(jìn)行訪問。
Parameters
Example:
>>> self.register_buffer('running_mean', torch.zeros(num_features))
register_forward_hook(hook)?
在模塊上注冊(cè)一個(gè)前向掛鉤。
每當(dāng) forward()
計(jì)算輸出后,該掛鉤都會(huì)被調(diào)用。 它應(yīng)具有以下簽名:
hook(module, input, output) -> None or modified output
掛鉤可以修改輸出。 它可以就地修改輸入,但是不會(huì)對(duì)正向產(chǎn)生影響,因?yàn)樵谡{(diào)用 forward()
之后會(huì)調(diào)用它。
Returns
a handle that can be used to remove the added hook by calling handle.remove()
Return type
torch.utils.hooks.RemovableHandle
register_forward_pre_hook(hook)?
在模塊上注冊(cè)前向預(yù)鉤。
每次調(diào)用 forward()
之前,都會(huì)調(diào)用該掛鉤。 它應(yīng)具有以下簽名:
hook(module, input) -> None or modified input
掛鉤可以修改輸入。 用戶可以在掛鉤中返回一個(gè)元組或一個(gè)修改后的值。 如果返回單個(gè)值,則將值包裝到一個(gè)元組中(除非該值已經(jīng)是一個(gè)元組)。
Returns
a handle that can be used to remove the added hook by calling handle.remove()
Return type
torch.utils.hooks.RemovableHandle
register_parameter(name, param)?
向模塊添加參數(shù)。
可以使用給定名稱將參數(shù)作為屬性訪問。
Parameters
requires_grad_(requires_grad=True)?
更改 autograd 是否應(yīng)記錄此模塊中參數(shù)的操作。
此方法就地設(shè)置參數(shù)的requires_grad
屬性。
此方法有助于凍結(jié)模塊的一部分以分別微調(diào)或訓(xùn)練模型的各個(gè)部分(例如 GAN 訓(xùn)練)。
Parameters
require_grad (bool )– autograd 是否應(yīng)記錄此模塊中參數(shù)的操作。 默認(rèn)值:True
。
Returns
self
Return type
Module
state_dict(destination=None, prefix='', keep_vars=False)?
返回包含模塊整個(gè)狀態(tài)的字典。
包括參數(shù)和持久緩沖區(qū)(例如運(yùn)行平均值)。 鍵是相應(yīng)的參數(shù)和緩沖區(qū)名稱。
Returns
包含模塊整體狀態(tài)的字典
Return type
字典
Example:
>>> module.state_dict().keys()
['bias', 'weight']
to(*args, **kwargs)?
移動(dòng)和/或強(qiáng)制轉(zhuǎn)換參數(shù)和緩沖區(qū)。
這可以稱為
to(device=None, dtype=None, non_blocking=False)
to(dtype, non_blocking=False)
to(tensor, non_blocking=False)
它的簽名類似于 torch.Tensor.to()
,但僅接受所需的浮點(diǎn)dtype
。 此外,此方法只會(huì)將浮點(diǎn)參數(shù)和緩沖區(qū)強(qiáng)制轉(zhuǎn)換為dtype
(如果給定)。 如果已給定,則積分參數(shù)和緩沖區(qū)將被移動(dòng)device
,但 dtypes 不變。 設(shè)置non_blocking
時(shí),如果可能,它將嘗試相對(duì)于主機(jī)進(jìn)行異步轉(zhuǎn)換/移動(dòng),例如,將具有固定內(nèi)存的 CPU 張量移動(dòng)到 CUDA 設(shè)備。
請(qǐng)參見下面的示例。
Note
此方法就地修改模塊。
Parameters
torch.device
)–該模塊中參數(shù)和緩沖區(qū)的所需設(shè)備torch.dtype
)–此模塊中浮點(diǎn)參數(shù)和緩沖區(qū)的所需浮點(diǎn)類型Returns
self
Return type
Module
Example:
>>> linear = nn.Linear(2, 2)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
[-0.5113, -0.2325]])
>>> linear.to(torch.double)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
[-0.5113, -0.2325]], dtype=torch.float64)
>>> gpu1 = torch.device("cuda:1")
>>> linear.to(gpu1, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
[-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')
>>> cpu = torch.device("cpu")
>>> linear.to(cpu)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
[-0.5112, -0.2324]], dtype=torch.float16)
train(mode=True)?
將模塊設(shè)置為訓(xùn)練模式。
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. Dropout
, BatchNorm
, etc.
Parameters
模式 (bool )–是設(shè)置訓(xùn)練模式(True
)還是評(píng)估模式(False
)。 默認(rèn)值:True
。
Returns
self
Return type
Module
type(dst_type)?
將所有參數(shù)和緩沖區(qū)強(qiáng)制轉(zhuǎn)換為dst_type
。
Parameters
dst_type (python:type 或 字符串)–所需類型
Returns
self
Return type
Module
zero_grad()?
將所有模型參數(shù)的梯度設(shè)置為零。
class torch.nn.Sequential(*args)?
順序容器。 模塊將按照在構(gòu)造函數(shù)中傳遞的順序添加到模塊中。 或者,也可以傳遞模塊的有序字典。
為了更容易理解,這是一個(gè)小示例:
## Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
## Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20,64,5)),
('relu2', nn.ReLU())
]))
class torch.nn.ModuleList(modules=None)?
將子模塊保存在列表中。
ModuleList
可以像常規(guī) Python 列表一樣被索引,但是其中包含的模塊已正確注冊(cè),并且對(duì)所有 Module
方法都是可見的。
Parameters
模塊(可迭代 , 可選)–可迭代的模塊
Example:
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])
def forward(self, x):
# ModuleList can act as an iterable, or be indexed using ints
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
append(module)?
將給定模塊附加到列表的末尾。
Parameters
模塊 (nn.Module)–要附加的模塊
extend(modules)?
將可迭代的 Python 模塊附加到列表的末尾。
Parameters
模塊(可迭代)–可迭代的模塊
insert(index, module)?
在列表中給定索引之前插入給定模塊。
Parameters
class torch.nn.ModuleDict(modules=None)?
將子模塊保存在字典中。
ModuleDict
可以像常規(guī)的 Python 字典一樣被索引,但是其中包含的模塊已正確注冊(cè),并且對(duì)所有 Module
方法都是可見的。
ModuleDict
是有序字典,
update()
中,OrderedDict
或另一個(gè) ModuleDict
的合并順序 (update()
的順序)。
請(qǐng)注意, update()
和其他無序映射類型(例如 Python 的普通dict
)不會(huì)保留合并映射的順序。
Parameters
模塊(可迭代 , 可選)–(字符串:模塊)的映射(字典)或鍵值對(duì)的可迭代 類型(字符串,模塊)
Example:
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.choices = nn.ModuleDict({
'conv': nn.Conv2d(10, 10, 3),
'pool': nn.MaxPool2d(3)
})
self.activations = nn.ModuleDict([
['lrelu', nn.LeakyReLU()],
['prelu', nn.PReLU()]
])
def forward(self, x, choice, act):
x = self.choices[choice](x)
x = self.activations[act](x)
return x
clear()?
從 ModuleDict 中刪除所有項(xiàng)目。
items()?
返回一個(gè)可迭代的 ModuleDict 鍵/值對(duì)。
keys()?
返回一個(gè)可迭代的 ModuleDict 鍵。
pop(key)?
從 ModuleDict 中刪除密鑰并返回其模塊。
Parameters
鍵(字符串)–從 ModuleDict 彈出的鍵
update(modules)?
使用來自映射或可迭代,覆蓋現(xiàn)有鍵的鍵值對(duì)更新 ModuleDict
。
Note
如果modules
是OrderedDict
, ModuleDict
或鍵值對(duì)的可迭代項(xiàng),則將保留其中的新元素順序。
Parameters
模塊(可迭代)–從字符串到 Module
的映射(字典),或鍵值對(duì)類型的可迭代(字符串, [] Module
])
values()?
返回一個(gè) ModuleDict 值的可迭代值。
class torch.nn.ParameterList(parameters=None)?
將參數(shù)保存在列表中。
ParameterList
可以像常規(guī) Python 列表一樣被索引,但是其中包含的參數(shù)已正確注冊(cè),并且將由所有 Module
方法可見。
Parameters
參數(shù)(可迭代的 , 可選)–可迭代的 Parameter
Example:
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.params = nn.ParameterList([nn.Parameter(torch.randn(10, 10)) for i in range(10)])
def forward(self, x):
# ParameterList can act as an iterable, or be indexed using ints
for i, p in enumerate(self.params):
x = self.params[i // 2].mm(x) + p.mm(x)
return x
append(parameter)?
在列表的末尾附加一個(gè)給定的參數(shù)。
Parameters
參數(shù) (nn.Parameter)–要附加的參數(shù)
extend(parameters)?
將可迭代的 Python 參數(shù)附加到列表的末尾。
Parameters
參數(shù)(可迭代)–可迭代的參數(shù)
class torch.nn.ParameterDict(parameters=None)?
將參數(shù)保存在字典中。
可以像常規(guī) Python 詞典一樣對(duì) ParameterDict 進(jìn)行索引,但是它包含的參數(shù)已正確注冊(cè),并且對(duì)所有 Module 方法都可見。
ParameterDict
是有序字典,
update()
中,OrderedDict
或另一個(gè) ParameterDict
的合并順序 (update()
的順序)。
請(qǐng)注意, update()
和其他無序映射類型(例如 Python 的普通dict
)不會(huì)保留合并映射的順序。
Parameters
參數(shù)(可迭代的 , 可選)–(字符串: Parameter
)的映射(字典) 或類型(字符串 Parameter
)的鍵值對(duì)的可迭代
Example:
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.params = nn.ParameterDict({
'left': nn.Parameter(torch.randn(5, 10)),
'right': nn.Parameter(torch.randn(5, 10))
})
def forward(self, x, choice):
x = self.params[choice].mm(x)
return x
clear()?
從 ParameterDict 中刪除所有項(xiàng)目。
items()?
返回一個(gè) ParameterDict 鍵/值對(duì)的可迭代對(duì)象。
keys()?
返回一個(gè)可迭代的 ParameterDict 鍵。
pop(key)?
從 ParameterDict 中刪除鍵并返回其參數(shù)。
Parameters
鍵(字符串)–從 ParameterDict 彈出的鍵
update(parameters)?
使用來自映射或可迭代,覆蓋現(xiàn)有鍵的鍵值對(duì)更新 ParameterDict
。
Note
如果parameters
是OrderedDict
, ParameterDict
或鍵值對(duì)的可迭代項(xiàng),則將保留其中的新元素順序。
Parameters
參數(shù)(可迭代的)–從字符串到 Parameter
的映射(字典),或鍵值對(duì)類型的可迭代(字符串, [] Parameter
])
values()?
返回 ParameterDict 值的可迭代值。
class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維卷積。
在最簡(jiǎn)單的情況下,具有輸入大小和輸出
的圖層的輸出值可以精確地描述為:
其中[是有效的]()[互相關(guān)]運(yùn)算符,
是批處理大小,
表示通道數(shù),![img])是信號(hào)序列的長(zhǎng)度。
stride
控制互相關(guān)的步幅,單個(gè)數(shù)字或一個(gè)元素的元組。padding
點(diǎn)數(shù),padding
控制兩側(cè)的隱式零填充量。dilation
控制內(nèi)核點(diǎn)之間的間距; 也稱為àtrous 算法。 很難描述,但是此鏈接很好地展示了dilation
的功能。groups
控制輸入和輸出之間的連接。 in_channels
和out_channels
必須都可以被groups
整除。 例如,
\> 在組= 1 時(shí),所有輸入都卷積為所有輸出。 >
\>
\> 在 groups = 2 時(shí),該操作等效于并排設(shè)置兩個(gè) conv 層,每個(gè) conv 層看到一半的輸入通道,并產(chǎn)生一半的輸出通道,并且隨后都將它們級(jí)聯(lián)。 >
\>
\> * 在 groups = in_channels
時(shí),每個(gè)輸入通道都與自己的大小為的一組濾波器卷積。
Note
根據(jù)內(nèi)核的大小,輸入的(最后)幾列可能會(huì)丟失,因?yàn)樗怯行У幕ハ嚓P(guān),而不是完整的互相關(guān) 。 由用戶決定是否添加適當(dāng)?shù)奶畛洹?/p>
Note
當(dāng)<cite>組== in_channels</cite> 和 <cite>out_channels == K * in_channels</cite> 時(shí),其中 <cite>K</cite> 是一個(gè)正整數(shù),此操作在文獻(xiàn)中也被稱為深度卷積。
換句話說,對(duì)于大小為的輸入,可以通過參數(shù)
構(gòu)造具有深度乘數(shù) <cite>K</cite> 的深度卷積。
Note
在某些情況下,將 CUDA 后端與 CuDNN 一起使用時(shí),該運(yùn)算符可能會(huì)選擇不確定的算法來提高性能。 如果不希望這樣做,則可以通過設(shè)置torch.backends.cudnn.deterministic = True
來使操作具有確定性(可能會(huì)降低性能)。 請(qǐng)參閱關(guān)于可再現(xiàn)性的注意事項(xiàng)作為背景。
Parameters
True
,則向輸出添加可學(xué)習(xí)的偏置。 默認(rèn)值:True
Shape:
Variables
bias
為True
,則這些權(quán)重的值將從例子:
>>> m = nn.Conv1d(16, 33, 3, stride=2)
>>> input = torch.randn(20, 16, 50)
>>> output = m(input)
class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')?
對(duì)由多個(gè)輸入平面組成的輸入信號(hào)應(yīng)用 2D 卷積。
在最簡(jiǎn)單的情況下,具有輸入大小和輸出
的圖層的輸出值可以精確地描述為:
其中是有效的 2D 互相關(guān)運(yùn)算符,
是批處理大小,
表示通道數(shù),
是輸入平面的高度(以像素為單位),并且[
是以像素為單位的寬度。
stride
控制互相關(guān)的步幅,單個(gè)數(shù)字或元組。padding
點(diǎn)數(shù),padding
控制兩側(cè)的隱式零填充量。dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.groups
controls the connections between inputs and outputs. in_channels
and out_channels
must both be divisible by groups
. For example,
\> At groups=1, all inputs are convolved to all outputs. >
\>
\> At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated. >
\>
\> * 在 groups = in_channels
時(shí),每個(gè)輸入通道都與自己的一組過濾器卷積,大小為。
參數(shù)kernel_size
,stride
,padding
和dilation
可以是:
- 單個(gè)
int
–在這種情況下,高度和寬度尺寸將使用相同的值
- 兩個(gè)整數(shù)的tuple
–在這種情況下,第一個(gè) <cite&int</cite& 用于高度尺寸,第二個(gè) <cite&int</cite& 用于寬度尺寸
Note
根據(jù)內(nèi)核的大小,輸入的(最后)幾列可能會(huì)丟失,因?yàn)樗怯行У幕ハ嚓P(guān),而不是完整的互相關(guān) 。 由用戶決定是否添加適當(dāng)?shù)奶畛洹?/p>
Note
When <cite>groups == in_channels</cite> and <cite>out_channels == K * in_channels</cite>, where <cite>K</cite> is a positive integer, this operation is also termed in literature as depthwise convolution.
換句話說,對(duì)于大小為的輸入,可以通過參數(shù)
構(gòu)造具有深度乘數(shù) <cite>K</cite> 的深度卷積。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
Parameters
True
, adds a learnable bias to the output. Default: True
Shape:
Variables
bias
為True
,則這些權(quán)重的值將從Examples:
>>> # With square kernels and equal stride
>>> m = nn.Conv2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> # non-square kernels and unequal stride and with padding and dilation
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
>>> input = torch.randn(20, 16, 50, 100)
>>> output = m(input)
class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 3D 卷積。
在最簡(jiǎn)單的情況下,具有輸入大小和輸出
的圖層的輸出值可以精確地描述為:
其中是有效的 3D 互相關(guān)運(yùn)算符
stride
控制互相關(guān)的步幅。padding
controls the amount of implicit zero-paddings on both sides for padding
number of points for each dimension.dilation
控制內(nèi)核點(diǎn)之間的間距; 也稱為àtrous 算法。 很難描述,但是此鏈接很好地展示了dilation
的功能。groups
controls the connections between inputs and outputs. in_channels
and out_channels
must both be divisible by groups
. For example,
\> At groups=1, all inputs are convolved to all outputs. >
\>
\> At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated. >
\>
\> * 在 groups = in_channels
時(shí),每個(gè)輸入通道都與自己的大小為的一組濾波器卷積。
The parameters kernel_size
, stride
, padding
, dilation
can either be:
- 單個(gè)
int
–在這種情況下,深度,高度和寬度尺寸使用相同的值
- 三個(gè)整數(shù)的tuple
–在這種情況下,第一個(gè) <cite&int</cite& 用于深度尺寸,第二個(gè) <cite&int</cite& 用于高度尺寸,第三個(gè) <cite&int</cite& 為寬度尺寸
Note
Depending of the size of your kernel, several (of the last) columns of the input might be lost, because it is a valid cross-correlation, and not a full cross-correlation. It is up to the user to add proper padding.
Note
When <cite>groups == in_channels</cite> and <cite>out_channels == K * in_channels</cite>, where <cite>K</cite> is a positive integer, this operation is also termed in literature as depthwise convolution.
換句話說,對(duì)于大小為的輸入,可以通過參數(shù)
構(gòu)造具有深度乘數(shù) <cite>K</cite> 的深度卷積。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
Parameters
True
, adds a learnable bias to the output. Default: True
Shape:
Variables
bias
為True
,則這些權(quán)重的值將從Examples:
>>> # With square kernels and equal stride
>>> m = nn.Conv3d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.Conv3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(4, 2, 0))
>>> input = torch.randn(20, 16, 10, 50, 100)
>>> output = m(input)
class torch.nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros')?
在由多個(gè)輸入平面組成的輸入圖像上應(yīng)用一維轉(zhuǎn)置的卷積運(yùn)算符。
該模塊可以看作是 Conv1d 相對(duì)于其輸入的梯度。 它也被稱為分?jǐn)?shù)步法卷積或反卷積(盡管它不是實(shí)際的反卷積運(yùn)算)。
stride
controls the stride for the cross-correlation.dilation * (kernel_size - 1) - padding
點(diǎn)數(shù),padding
控制兩側(cè)的隱式零填充量。 有關(guān)詳細(xì)信息,請(qǐng)參見下面的注釋。output_padding
控制添加到輸出形狀一側(cè)的附加尺寸。 有關(guān)詳細(xì)信息,請(qǐng)參見下面的注釋。dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.groups
controls the connections between inputs and outputs. in_channels
and out_channels
must both be divisible by groups
. For example,
\> At groups=1, all inputs are convolved to all outputs. >
\>
\> At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated. >
\>
\> * 在 groups = in_channels
時(shí),每個(gè)輸入通道都與自己的一組過濾器(大小為)卷積。
Note
Depending of the size of your kernel, several (of the last) columns of the input might be lost, because it is a valid cross-correlation, and not a full cross-correlation. It is up to the user to add proper padding.
Note
padding
參數(shù)有效地將dilation * (kernel_size - 1) - padding
的零填充量添加到兩種輸入大小。 進(jìn)行設(shè)置時(shí),以相同的參數(shù)初始化 Conv1d
和 ConvTranspose1d
時(shí),它們?cè)谳斎牒洼敵鲂螤罘矫姹舜讼喾础?但是,當(dāng)stride > 1
, Conv1d
將多個(gè)輸入形狀映射到相同的輸出形狀時(shí)。 提供output_padding
可通過有效地增加一側(cè)的計(jì)算輸出形狀來解決這種歧義。 請(qǐng)注意,output_padding
僅用于查找輸出形狀,而實(shí)際上并未向輸出添加零填充。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
Parameters
dilation * (kernel_size - 1) - padding
零填充將添加到輸入的兩側(cè)。 默認(rèn)值:0True
, adds a learnable bias to the output. Default: True
Shape:
Variables
bias
為True
,則這些權(quán)重的值將從class torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros')?
在由多個(gè)輸入平面組成的輸入圖像上應(yīng)用二維轉(zhuǎn)置卷積運(yùn)算符。
該模塊可以看作是 Conv2d 相對(duì)于其輸入的梯度。 它也被稱為分?jǐn)?shù)步法卷積或反卷積(盡管它不是實(shí)際的反卷積運(yùn)算)。
stride
controls the stride for the cross-correlation.padding
controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding
number of points. See note below for details.output_padding
controls the additional size added to one side of the output shape. See note below for details.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.groups
controls the connections between inputs and outputs. in_channels
and out_channels
must both be divisible by groups
. For example,
\> At groups=1, all inputs are convolved to all outputs. >
\>
\> At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated. >
\>
\> * At groups= in_channels
, each input channel is convolved with its own set of filters (of size ).
參數(shù)kernel_size
,stride
,padding
和output_padding
可以是:
- 單個(gè)
int
–在這種情況下,高度和寬度尺寸將使用相同的值
- atuple
of two ints – in which case, the first <cite&int</cite& is used for the height dimension, and the second <cite&int</cite& for the width dimension
Note
Depending of the size of your kernel, several (of the last) columns of the input might be lost, because it is a valid cross-correlation, and not a full cross-correlation. It is up to the user to add proper padding.
Note
padding
參數(shù)有效地將dilation * (kernel_size - 1) - padding
的零填充量添加到兩種輸入大小。 進(jìn)行設(shè)置時(shí),以相同的參數(shù)初始化 Conv2d
和 ConvTranspose2d
時(shí),它們?cè)谳斎牒洼敵鲂螤罘矫姹舜讼喾础?但是,當(dāng)stride > 1
, Conv2d
將多個(gè)輸入形狀映射到相同的輸出形狀時(shí)。 提供output_padding
可通過有效地增加一側(cè)的計(jì)算輸出形狀來解決這種歧義。 請(qǐng)注意,output_padding
僅用于查找輸出形狀,而實(shí)際上并未向輸出添加零填充。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
Parameters
dilation * (kernel_size - 1) - padding
零填充將添加到輸入中每個(gè)維度的兩側(cè)。 默認(rèn)值:0True
, adds a learnable bias to the output. Default: True
Shape:
Variables
bias
為True
,則值 這些權(quán)重來自Examples:
>>> # With square kernels and equal stride
>>> m = nn.ConvTranspose2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.ConvTranspose2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> input = torch.randn(20, 16, 50, 100)
>>> output = m(input)
>>> # exact output size can be also specified as an argument
>>> input = torch.randn(1, 16, 12, 12)
>>> downsample = nn.Conv2d(16, 16, 3, stride=2, padding=1)
>>> upsample = nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1)
>>> h = downsample(input)
>>> h.size()
torch.Size([1, 16, 6, 6])
>>> output = upsample(h, output_size=input.size())
>>> output.size()
torch.Size([1, 16, 12, 12])
class torch.nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1, padding_mode='zeros')?
在由多個(gè)輸入平面組成的輸入圖像上應(yīng)用 3D 轉(zhuǎn)置卷積運(yùn)算符。 轉(zhuǎn)置的卷積運(yùn)算符將每個(gè)輸入值逐個(gè)元素地乘以一個(gè)可學(xué)習(xí)的內(nèi)核,并對(duì)所有輸入特征平面的輸出求和。
該模塊可以看作是 Conv3d 相對(duì)于其輸入的梯度。 它也被稱為分?jǐn)?shù)步法卷積或反卷積(盡管它不是實(shí)際的反卷積運(yùn)算)。
stride
controls the stride for the cross-correlation.padding
controls the amount of implicit zero-paddings on both sides for dilation * (kernel_size - 1) - padding
number of points. See note below for details.output_padding
controls the additional size added to one side of the output shape. See note below for details.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.groups
controls the connections between inputs and outputs. in_channels
and out_channels
must both be divisible by groups
. For example,
\> At groups=1, all inputs are convolved to all outputs. >
\>
\> At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated. >
\>
\> * At groups= in_channels
, each input channel is convolved with its own set of filters (of size ).
The parameters kernel_size
, stride
, padding
, output_padding
can either be:
- 單個(gè)
int
–在這種情況下,深度,高度和寬度尺寸使用相同的值
- atuple
of three ints – in which case, the first <cite&int</cite& is used for the depth dimension, the second <cite&int</cite& for the height dimension and the third <cite&int</cite& for the width dimension
Note
Depending of the size of your kernel, several (of the last) columns of the input might be lost, because it is a valid cross-correlation, and not a full cross-correlation. It is up to the user to add proper padding.
Note
padding
參數(shù)有效地將dilation * (kernel_size - 1) - padding
的零填充量添加到兩種輸入大小。 進(jìn)行設(shè)置時(shí),以相同的參數(shù)初始化 Conv3d
和 ConvTranspose3d
時(shí),它們?cè)谳斎牒洼敵鲂螤罘矫姹舜讼喾础?但是,當(dāng)stride > 1
, Conv3d
將多個(gè)輸入形狀映射到相同的輸出形狀時(shí)。 提供output_padding
可通過有效地增加一側(cè)的計(jì)算輸出形狀來解決這種歧義。 請(qǐng)注意,output_padding
僅用于查找輸出形狀,而實(shí)際上并未向輸出添加零填充。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
Parameters
dilation * (kernel_size - 1) - padding
zero-padding will be added to both sides of each dimension in the input. Default: 0True
, adds a learnable bias to the output. Default: True
Shape:
Variables
bias
為True
,則值 這些權(quán)重來自Examples:
>>> # With square kernels and equal stride
>>> m = nn.ConvTranspose3d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.ConvTranspose3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(0, 4, 2))
>>> input = torch.randn(20, 16, 10, 50, 100)
>>> output = m(input)
class torch.nn.Unfold(kernel_size, dilation=1, padding=0, stride=1)?
從批處理輸入張量中提取滑動(dòng)局部塊。
考慮形狀為的成批
input
張量,其中為批尺寸,
為通道尺寸,
代表任意空間尺寸。 此操作將
input
的空間尺寸內(nèi)每個(gè)kernel_size
大小的滑動(dòng)塊壓平為形狀為的 3-D
output
張量的列(即最后一個(gè)尺寸),其中為總數(shù) 每個(gè)塊內(nèi)的值數(shù)量(一個(gè)塊具有
個(gè)空間位置,每個(gè)位置包含
通道矢量),
是此類塊的總數(shù):
其中由
input
(以上)的空間尺寸形成,而
在所有空間尺寸上。
因此,在最后一個(gè)維度(列維度)上索引output
將給出特定塊內(nèi)的所有值。
padding
,stride
和dilation
自變量指定如何檢索滑塊。
stride
控制滑塊的步幅。padding
控制每個(gè)維的padding
個(gè)點(diǎn)的兩側(cè)的隱式零填充量。dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.Parameters
kernel_size
,dilation
,padding
或stride
是長(zhǎng)度為 1 的 int 或元組,則它們的值將在所有空間維度上復(fù)制。im2col
。Note
Fold
通過對(duì)來自所有包含塊的所有值求和來計(jì)算所得大張量中的每個(gè)組合值。 Unfold
通過復(fù)制大張量來提取局部塊中的值。 因此,如果這些塊重疊,則它們不是彼此相反。
通常,折疊和展開操作如下相關(guān)。 考慮使用相同參數(shù)創(chuàng)建的 Fold
和 Unfold
實(shí)例:
>>> fold_params = dict(kernel_size=..., dilation=..., padding=..., stride=...)
>>> fold = nn.Fold(output_size=..., **fold_params)
>>> unfold = nn.Unfold(**fold_params)
然后,對(duì)于任何(受支持的)input
張量,以下等式成立:
fold(unfold(input)) == divisor * input
其中divisor
是僅取決于input
的形狀和 dtype 的張量:
>>> input_ones = torch.ones(input.shape, dtype=input.dtype)
>>> divisor = fold(unfold(input_ones))
當(dāng)divisor
張量不包含零元素時(shí),則fold
和unfold
運(yùn)算互為逆(最大除數(shù))。
Warning
當(dāng)前,僅支持 4D 輸入張量(像圖像一樣的批狀張量)。
Shape:
Examples:
>>> unfold = nn.Unfold(kernel_size=(2, 3))
>>> input = torch.randn(2, 5, 3, 4)
>>> output = unfold(input)
>>> # each patch contains 30 values (2x3=6 vectors, each of 5 channels)
>>> # 4 blocks (2x3 kernels) in total in the 3x4 input
>>> output.size()
torch.Size([2, 30, 4])
>>> # Convolution is equivalent with Unfold + Matrix Multiplication + Fold (or view to output shape)
>>> inp = torch.randn(1, 3, 10, 12)
>>> w = torch.randn(2, 3, 4, 5)
>>> inp_unf = torch.nn.functional.unfold(inp, (4, 5))
>>> out_unf = inp_unf.transpose(1, 2).matmul(w.view(w.size(0), -1).t()).transpose(1, 2)
>>> out = torch.nn.functional.fold(out_unf, (7, 8), (1, 1))
>>> # or equivalently (and avoiding a copy),
>>> # out = out_unf.view(1, 2, 7, 8)
>>> (torch.nn.functional.conv2d(inp, w) - out).abs().max()
tensor(1.9073e-06)
class torch.nn.Fold(output_size, kernel_size, dilation=1, padding=0, stride=1)?
將一系列滑動(dòng)局部塊組合成一個(gè)大型的張量。
考慮一個(gè)包含形狀的滑動(dòng)局部塊(例如圖像塊)的批處理
input
張量,其中是批處理尺寸,
是一個(gè)塊內(nèi)的值數(shù)(一個(gè)塊具有
每個(gè)包含
通道向量的空間位置),
是塊的總數(shù)。 (這與
Unfold
的輸出形狀完全相同。)此操作通過求和重疊值,將這些局部塊組合為形狀為的大
output
張量。 與 []()Unfold
類似,參數(shù)必須滿足
其中覆蓋所有空間尺寸。
output_size
描述了滑動(dòng)局部塊的大包含張量的空間形狀。 當(dāng)多個(gè)輸入形狀例如使用stride > 0
映射到相同數(shù)量的滑塊時(shí),解決歧義很有用。
The padding
, stride
and dilation
arguments specify how the sliding blocks are retrieved.
stride
controls the stride for the sliding blocks.padding
controls the amount of implicit zero-paddings on both sides for padding
number of points for each dimension before reshaping.dilation
controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation
does.Parameters
output.sizes()[2:]
)output_size
,kernel_size
,dilation
,padding
或stride
是長(zhǎng)度為 1 的整數(shù)或元組,則它們的值將在所有空間維度上復(fù)制。col2im
。Note
Fold
calculates each combined value in the resulting large tensor by summing all values from all containing blocks. Unfold
extracts the values in the local blocks by copying from the large tensor. So, if the blocks overlap, they are not inverses of each other.
In general, folding and unfolding operations are related as follows. Consider Fold
and Unfold
instances created with the same parameters:
>>> fold_params = dict(kernel_size=..., dilation=..., padding=..., stride=...)
>>> fold = nn.Fold(output_size=..., **fold_params)
>>> unfold = nn.Unfold(**fold_params)
Then for any (supported) input
tensor the following equality holds:
fold(unfold(input)) == divisor * input
where divisor
is a tensor that depends only on the shape and dtype of the input
:
>>> input_ones = torch.ones(input.shape, dtype=input.dtype)
>>> divisor = fold(unfold(input_ones))
When the divisor
tensor contains no zero elements, then fold
and unfold
operations are inverses of each other (upto constant divisor).
Warning
當(dāng)前,僅支持 4D 輸出張量(像圖像一樣的批狀張量)。
Shape:
Examples:
>>> fold = nn.Fold(output_size=(4, 5), kernel_size=(2, 2))
>>> input = torch.randn(1, 3 * 2 * 2, 12)
>>> output = fold(input)
>>> output.size()
torch.Size([1, 3, 4, 5])
class torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維最大池化。
在最簡(jiǎn)單的情況下,具有輸入大小和輸出
的圖層的輸出值可以精確地描述為:
如果padding
不為零,則對(duì)于padding
點(diǎn)的數(shù)量,輸入將在兩側(cè)隱式填充零。 dilation
控制內(nèi)核點(diǎn)之間的間距。 很難描述,但是此鏈接很好地展示了dilation
的功能。
Parameters
kernel_size
True
,將返回最大索引以及輸出。 以后對(duì) torch.nn.MaxUnpool1d
有用Shape:
Examples:
>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = torch.randn(20, 16, 50)
>>> output = m(input)
class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 最大合并。
在最簡(jiǎn)單的情況下,具有輸入大小,輸出
和
kernel_size
的圖層的輸出值可以精確地描述為:
If padding
is non-zero, then the input is implicitly zero-padded on both sides for padding
number of points. dilation
controls the spacing between the kernel points. It is harder to describe, but this link has a nice visualization of what dilation
does.
The parameters kernel_size
, stride
, padding
, dilation
can either be:
- a single
int
– in which case the same value is used for the height and width dimension
- atuple
of two ints – in which case, the first <cite&int</cite& is used for the height dimension, and the second <cite&int</cite& for the width dimension
Parameters
kernel_size
True
,將返回最大索引以及輸出。 以后對(duì) torch.nn.MaxUnpool2d
有用Shape:
Examples:
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
class torch.nn.MaxPool3d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 3D 最大池化。
在最簡(jiǎn)單的情況下,具有輸入大小,輸出
和
kernel_size
的圖層的輸出值可以精確地描述為:
If padding
is non-zero, then the input is implicitly zero-padded on both sides for padding
number of points. dilation
controls the spacing between the kernel points. It is harder to describe, but this link has a nice visualization of what dilation
does.
The parameters kernel_size
, stride
, padding
, dilation
can either be:
- a single
int
– in which case the same value is used for the depth, height and width dimension
- atuple
of three ints – in which case, the first <cite&int</cite& is used for the depth dimension, the second <cite&int</cite& for the height dimension and the third <cite&int</cite& for the width dimension
Parameters
kernel_size
True
,將返回最大索引以及輸出。 以后對(duì) torch.nn.MaxUnpool3d
有用Shape:
Examples:
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = torch.randn(20, 16, 50,44, 31)
>>> output = m(input)
class torch.nn.MaxUnpool1d(kernel_size, stride=None, padding=0)?
計(jì)算 MaxPool1d
的局部逆。
MaxPool1d
不能完全反轉(zhuǎn),因?yàn)闀?huì)丟失非最大值。
MaxUnpool1d
接收包括最大值索引在內(nèi)的 MaxPool1d
的輸出作為輸入,并計(jì)算一個(gè)部分逆,其中所有非最大值都設(shè)置為零。
Note
MaxPool1d
可以將多個(gè)輸入大小映射到相同的輸出大小。 因此,反轉(zhuǎn)過程可能會(huì)變得模棱兩可。 為了解決這個(gè)問題,您可以在前進(jìn)調(diào)用中提供所需的輸出大小作為附加參數(shù)output_size
。 請(qǐng)參閱下面的輸入和示例。
Parameters
kernel_size
。Inputs:
MaxPool1d
給出的指標(biāo)Shape:
或由呼叫運(yùn)營商中的output_size
給定
Example:
>>> pool = nn.MaxPool1d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool1d(2, stride=2)
>>> input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8.]]])
>>> # Example showcasing the use of output_size
>>> input = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8, 9]]])
>>> output, indices = pool(input)
>>> unpool(output, indices, output_size=input.size())
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8., 0.]]])
>>> unpool(output, indices)
tensor([[[ 0., 2., 0., 4., 0., 6., 0., 8.]]])
class torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)?
計(jì)算 MaxPool2d
的局部逆。
MaxPool2d
不能完全反轉(zhuǎn),因?yàn)闀?huì)丟失非最大值。
MaxUnpool2d
接收包括最大值索引在內(nèi)的 MaxPool2d
的輸出作為輸入,并計(jì)算一個(gè)部分逆,其中所有非最大值都設(shè)置為零。
Note
MaxPool2d
可以將多個(gè)輸入大小映射到相同的輸出大小。 因此,反轉(zhuǎn)過程可能會(huì)變得模棱兩可。 為了解決這個(gè)問題,您可以在前進(jìn)調(diào)用中提供所需的輸出大小作為附加參數(shù)output_size
。 請(qǐng)參閱下面的輸入和示例。
Parameters
kernel_size
by default.Inputs:
MaxPool2d
給出的指標(biāo)Shape:
or as given by output_size
in the call operator
Example:
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = torch.tensor([[[[ 1., 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[[ 0., 0., 0., 0.],
[ 0., 6., 0., 8.],
[ 0., 0., 0., 0.],
[ 0., 14., 0., 16.]]]])
>>> # specify a different output size than input size
>>> unpool(output, indices, output_size=torch.Size([1, 1, 5, 5]))
tensor([[[[ 0., 0., 0., 0., 0.],
[ 6., 0., 8., 0., 0.],
[ 0., 0., 0., 14., 0.],
[ 16., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]]])
class torch.nn.MaxUnpool3d(kernel_size, stride=None, padding=0)?
計(jì)算 MaxPool3d
的局部逆。
MaxPool3d
不能完全反轉(zhuǎn),因?yàn)闀?huì)丟失非最大值。 MaxUnpool3d
將包含最大值索引的 MaxPool3d
的輸出作為輸入,并計(jì)算將所有非最大值均設(shè)置為零的部分逆。
Note
MaxPool3d
可以將多個(gè)輸入大小映射到相同的輸出大小。 因此,反轉(zhuǎn)過程可能會(huì)變得模棱兩可。 為了解決這個(gè)問題,您可以在前進(jìn)調(diào)用中提供所需的輸出大小作為附加參數(shù)output_size
。 請(qǐng)參閱下面的輸入部分。
Parameters
kernel_size
by default.Inputs:
MaxPool3d
給出的指標(biāo)Shape:
or as given by output_size
in the call operator
Example:
>>> # pool of square window of size=3, stride=2
>>> pool = nn.MaxPool3d(3, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool3d(3, stride=2)
>>> output, indices = pool(torch.randn(20, 16, 51, 33, 15))
>>> unpooled_output = unpool(output, indices)
>>> unpooled_output.size()
torch.Size([20, 16, 51, 33, 15])
class torch.nn.AvgPool1d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維平均池。
在最簡(jiǎn)單的情況下,具有輸入大小,輸出
和
kernel_size
的圖層的輸出值可以精確地描述為:
如果padding
不為零,則對(duì)于padding
點(diǎn)的數(shù)量,輸入將在兩側(cè)隱式填充零。
參數(shù)kernel_size
,stride
和padding
可以分別是int
或一個(gè)元素元組。
Parameters
kernel_size
Shape:
Examples:
>>> # pool with window of size=3, stride=2
>>> m = nn.AvgPool1d(3, stride=2)
>>> m(torch.tensor([[[1.,2,3,4,5,6,7]]]))
tensor([[[ 2., 4., 6.]]])
class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 平均池。
In the simplest case, the output value of the layer with input size , output
and
kernel_size
can be precisely described as:
If padding
is non-zero, then the input is implicitly zero-padded on both sides for padding
number of points.
參數(shù)kernel_size
,stride
和padding
可以是:
- a single
int
– in which case the same value is used for the height and width dimension
- atuple
of two ints – in which case, the first <cite&int</cite& is used for the height dimension, and the second <cite&int</cite& for the width dimension
Parameters
kernel_size
Shape:
Examples:
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
class torch.nn.AvgPool3d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 3D 平均池。
In the simplest case, the output value of the layer with input size , output
and
kernel_size
can be precisely described as:
如果padding
不為零,則對(duì)于padding
點(diǎn)的數(shù)量,輸入將在所有三個(gè)側(cè)面隱式填充零。
參數(shù)kernel_size
和stride
可以是:
- a single
int
– in which case the same value is used for the depth, height and width dimension
- atuple
of three ints – in which case, the first <cite&int</cite& is used for the depth dimension, the second <cite&int</cite& for the height dimension and the third <cite&int</cite& for the width dimension
Parameters
kernel_size
Shape:
Examples:
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool3d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool3d((3, 2, 2), stride=(2, 1, 2))
>>> input = torch.randn(20, 16, 50,44, 31)
>>> output = m(input)
class torch.nn.FractionalMaxPool2d(kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 分?jǐn)?shù)最大池化。
Ben Graham 的論文 Fractional MaxPooling 中詳細(xì)描述了分?jǐn)?shù)最大池化
在區(qū)域中通過由目標(biāo)輸出大小確定的隨機(jī)步長(zhǎng)應(yīng)用最大合并操作。 輸出要素的數(shù)量等于輸入平面的數(shù)量。
Parameters
True
,則將返回索引以及輸出。 有助于傳遞給nn.MaxUnpool2d()
。 默認(rèn)值:False
例子
>>> # 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
>>> m = nn.FractionalMaxPool2d(3, output_ratio=(0.5, 0.5))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
class torch.nn.LPPool1d(norm_type, kernel_size, stride=None, ceil_mode=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維功率平均池。
在每個(gè)窗口上,計(jì)算的函數(shù)為:
Note
如果 <cite>p</cite> 的冪的和為零,則此函數(shù)的梯度不確定。 在這種情況下,此實(shí)現(xiàn)會(huì)將梯度設(shè)置為零。
Parameters
kernel_size
Shape:
Examples::
>>> # power-2 pool of window of length 3, with stride 2.
>>> m = nn.LPPool1d(2, 3, stride=2)
>>> input = torch.randn(20, 16, 50)
>>> output = m(input)
class torch.nn.LPPool2d(norm_type, kernel_size, stride=None, ceil_mode=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 功率平均池。
On each window, the function computed is:
The parameters kernel_size
, stride
can either be:
- a single
int
– in which case the same value is used for the height and width dimension
- atuple
of two ints – in which case, the first <cite&int</cite& is used for the height dimension, and the second <cite&int</cite& for the width dimension
Note
If the sum to the power of <cite>p</cite> is zero, the gradient of this function is not defined. This implementation will set the gradient to zero in this case.
Parameters
kernel_size
Shape:
Examples:
>>> # 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
>>> m = nn.LPPool2d(1.2, (3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
class torch.nn.AdaptiveMaxPool1d(output_size, return_indices=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維自適應(yīng)最大池化。
對(duì)于任何輸入大小,輸出大小均為 H。 輸出要素的數(shù)量等于輸入平面的數(shù)量。
Parameters
True
,則將返回索引以及輸出。 傳遞給 nn.MaxUnpool1d 很有用。 默認(rèn)值:False
Examples
>>> # target output size of 5
>>> m = nn.AdaptiveMaxPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)
class torch.nn.AdaptiveMaxPool2d(output_size, return_indices=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 自適應(yīng)最大池化。
對(duì)于任何輸入大小,輸出大小均為 H xW。 輸出要素的數(shù)量等于輸入平面的數(shù)量。
Parameters
int
或None
表示大小與輸入的大小相同。True
,則將返回索引以及輸出。 傳遞給 nn.MaxUnpool2d 很有用。 默認(rèn)值:False
Examples
>>> # target output size of 5x7
>>> m = nn.AdaptiveMaxPool2d((5,7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveMaxPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
class torch.nn.AdaptiveMaxPool3d(output_size, return_indices=False)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 3D 自適應(yīng)最大池化。
對(duì)于任何輸入大小,輸出大小均為 D xH xW。 輸出要素的數(shù)量等于輸入平面的數(shù)量。
Parameters
int
或None
,這意味著大小將與輸入的大小相同。True
,則將返回索引以及輸出。 傳遞給 nn.MaxUnpool3d 很有用。 默認(rèn)值:False
Examples
>>> # target output size of 5x7x9
>>> m = nn.AdaptiveMaxPool3d((5,7,9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveMaxPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveMaxPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
class torch.nn.AdaptiveAvgPool1d(output_size)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用一維自適應(yīng)平均池。
The output size is H, for any input size. The number of output features is equal to the number of input planes.
Parameters
output_size – the target output size H
Examples
>>> # target output size of 5
>>> m = nn.AdaptiveAvgPool1d(5)
>>> input = torch.randn(1, 64, 8)
>>> output = m(input)
class torch.nn.AdaptiveAvgPool2d(output_size)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 2D 自適應(yīng)平均池。
The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.
Parameters
output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a int
, or None
which means the size will be the same as that of the input.
Examples
>>> # target output size of 5x7
>>> m = nn.AdaptiveAvgPool2d((5,7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveAvgPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
class torch.nn.AdaptiveAvgPool3d(output_size)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用 3D 自適應(yīng)平均池。
The output is of size D x H x W, for any input size. The number of output features is equal to the number of input planes.
Parameters
output_size – D x H x W 形式的目標(biāo)輸出大小。可以是元組(D,H,W),也可以是多維數(shù)據(jù)集 D xD x D 的單個(gè)數(shù)字 D。D,H 和 W 可以是int
或None
,這意味著大小將與輸入的大小相同。
Examples
>>> # target output size of 5x7x9
>>> m = nn.AdaptiveAvgPool3d((5,7,9))
>>> input = torch.randn(1, 64, 8, 9, 10)
>>> output = m(input)
>>> # target output size of 7x7x7 (cube)
>>> m = nn.AdaptiveAvgPool3d(7)
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
>>> # target output size of 7x9x8
>>> m = nn.AdaptiveMaxPool3d((7, None, None))
>>> input = torch.randn(1, 64, 10, 9, 8)
>>> output = m(input)
class torch.nn.ReflectionPad1d(padding)?
使用輸入邊界的反射來填充輸入張量。
對(duì)于 <cite>N</cite> 維填充,請(qǐng)使用 torch.nn.functional.pad()
。
Parameters
填充 (python:int , 元組)–填充的大小。 如果為 <cite>int</cite> ,則在所有邊界中使用相同的填充。 如果 2- <cite>元組</cite>,則使用(,
)
Shape:
Examples:
>>> m = nn.ReflectionPad1d(2)
>>> input = torch.arange(8, dtype=torch.float).reshape(1, 2, 4)
>>> input
tensor([[[0., 1., 2., 3.],
[4., 5., 6., 7.]]])
>>> m(input)
tensor([[[2., 1., 0., 1., 2., 3., 2., 1.],
[6., 5., 4., 5., 6., 7., 6., 5.]]])
>>> # using different paddings for different sides
>>> m = nn.ReflectionPad1d((3, 1))
>>> m(input)
tensor([[[3., 2., 1., 0., 1., 2., 3., 2.],
[7., 6., 5., 4., 5., 6., 7., 6.]]])
class torch.nn.ReflectionPad2d(padding)?
Pads the input tensor using the reflection of the input boundary.
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
填充 (python:int , 元組)–填充的大小。 如果為 <cite>int</cite> ,則在所有邊界中使用相同的填充。 如果是 4- <cite>元組</cite>,則使用(,
,
和
)
Shape:
Examples:
>>> m = nn.ReflectionPad2d(2)
>>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
>>> input
tensor([[[[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]]]])
>>> m(input)
tensor([[[[8., 7., 6., 7., 8., 7., 6.],
[5., 4., 3., 4., 5., 4., 3.],
[2., 1., 0., 1., 2., 1., 0.],
[5., 4., 3., 4., 5., 4., 3.],
[8., 7., 6., 7., 8., 7., 6.],
[5., 4., 3., 4., 5., 4., 3.],
[2., 1., 0., 1., 2., 1., 0.]]]])
>>> # using different paddings for different sides
>>> m = nn.ReflectionPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[7., 6., 7., 8., 7.],
[4., 3., 4., 5., 4.],
[1., 0., 1., 2., 1.],
[4., 3., 4., 5., 4.],
[7., 6., 7., 8., 7.]]]])
class torch.nn.ReplicationPad1d(padding)?
使用輸入邊界的復(fù)制來填充輸入張量。
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
padding (python:int__, tuple) – the size of the padding. If is <cite>int</cite>, uses the same padding in all boundaries. If a 2-<cite>tuple</cite>, uses (,
)
Shape:
Examples:
>>> m = nn.ReplicationPad1d(2)
>>> input = torch.arange(8, dtype=torch.float).reshape(1, 2, 4)
>>> input
tensor([[[0., 1., 2., 3.],
[4., 5., 6., 7.]]])
>>> m(input)
tensor([[[0., 0., 0., 1., 2., 3., 3., 3.],
[4., 4., 4., 5., 6., 7., 7., 7.]]])
>>> # using different paddings for different sides
>>> m = nn.ReplicationPad1d((3, 1))
>>> m(input)
tensor([[[0., 0., 0., 0., 1., 2., 3., 3.],
[4., 4., 4., 4., 5., 6., 7., 7.]]])
class torch.nn.ReplicationPad2d(padding)?
Pads the input tensor using replication of the input boundary.
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
padding (python:int__, tuple) – the size of the padding. If is <cite>int</cite>, uses the same padding in all boundaries. If a 4-<cite>tuple</cite>, uses (,
,
,
)
Shape:
Examples:
>>> m = nn.ReplicationPad2d(2)
>>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
>>> input
tensor([[[[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]]]])
>>> m(input)
tensor([[[[0., 0., 0., 1., 2., 2., 2.],
[0., 0., 0., 1., 2., 2., 2.],
[0., 0., 0., 1., 2., 2., 2.],
[3., 3., 3., 4., 5., 5., 5.],
[6., 6., 6., 7., 8., 8., 8.],
[6., 6., 6., 7., 8., 8., 8.],
[6., 6., 6., 7., 8., 8., 8.]]]])
>>> # using different paddings for different sides
>>> m = nn.ReplicationPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[0., 0., 1., 2., 2.],
[0., 0., 1., 2., 2.],
[0., 0., 1., 2., 2.],
[3., 3., 4., 5., 5.],
[6., 6., 7., 8., 8.]]]])
class torch.nn.ReplicationPad3d(padding)?
Pads the input tensor using replication of the input boundary.
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
填充 (python:int , 元組)–填充的大小。 如果為 <cite>int</cite> ,則在所有邊界中使用相同的填充。 如果是 6-<cite>元組</cite>,則使用(,
,
,
,
,
)
Shape:
Examples:
>>> m = nn.ReplicationPad3d(3)
>>> input = torch.randn(16, 3, 8, 320, 480)
>>> output = m(input)
>>> # using different paddings for different sides
>>> m = nn.ReplicationPad3d((3, 3, 6, 6, 1, 1))
>>> output = m(input)
class torch.nn.ZeroPad2d(padding)?
用零填充輸入張量邊界。
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
padding (python:int__, tuple) – the size of the padding. If is <cite>int</cite>, uses the same padding in all boundaries. If a 4-<cite>tuple</cite>, uses (,
,
,
)
Shape:
Examples:
>>> m = nn.ZeroPad2d(2)
>>> input = torch.randn(1, 1, 3, 3)
>>> input
tensor([[[[-0.1678, -0.4418, 1.9466],
[ 0.9604, -0.4219, -0.5241],
[-0.9162, -0.5436, -0.6446]]]])
>>> m(input)
tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, -0.1678, -0.4418, 1.9466, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.9604, -0.4219, -0.5241, 0.0000, 0.0000],
[ 0.0000, 0.0000, -0.9162, -0.5436, -0.6446, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
>>> # using different paddings for different sides
>>> m = nn.ZeroPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[ 0.0000, -0.1678, -0.4418, 1.9466, 0.0000],
[ 0.0000, 0.9604, -0.4219, -0.5241, 0.0000],
[ 0.0000, -0.9162, -0.5436, -0.6446, 0.0000]]]])
class torch.nn.ConstantPad1d(padding, value)?
用恒定值填充輸入張量邊界。
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
填充 (python:int , 元組)–填充的大小。 如果為 <cite>int</cite> ,則在兩個(gè)邊界中使用相同的填充。 如果 2- <cite>元組</cite>,則使用(,
)
Shape:
Examples:
>>> m = nn.ConstantPad1d(2, 3.5)
>>> input = torch.randn(1, 2, 4)
>>> input
tensor([[[-1.0491, -0.7152, -0.0749, 0.8530],
[-1.3287, 1.8966, 0.1466, -0.2771]]])
>>> m(input)
tensor([[[ 3.5000, 3.5000, -1.0491, -0.7152, -0.0749, 0.8530, 3.5000,
3.5000],
[ 3.5000, 3.5000, -1.3287, 1.8966, 0.1466, -0.2771, 3.5000,
3.5000]]])
>>> m = nn.ConstantPad1d(2, 3.5)
>>> input = torch.randn(1, 2, 3)
>>> input
tensor([[[ 1.6616, 1.4523, -1.1255],
[-3.6372, 0.1182, -1.8652]]])
>>> m(input)
tensor([[[ 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000, 3.5000],
[ 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000, 3.5000]]])
>>> # using different paddings for different sides
>>> m = nn.ConstantPad1d((3, 1), 3.5)
>>> m(input)
tensor([[[ 3.5000, 3.5000, 3.5000, 1.6616, 1.4523, -1.1255, 3.5000],
[ 3.5000, 3.5000, 3.5000, -3.6372, 0.1182, -1.8652, 3.5000]]])
class torch.nn.ConstantPad2d(padding, value)?
Pads the input tensor boundaries with a constant value.
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
padding (python:int__, tuple) – the size of the padding. If is <cite>int</cite>, uses the same padding in all boundaries. If a 4-<cite>tuple</cite>, uses (,
,
,
)
Shape:
Examples:
>>> m = nn.ConstantPad2d(2, 3.5)
>>> input = torch.randn(1, 2, 2)
>>> input
tensor([[[ 1.6585, 0.4320],
[-0.8701, -0.4649]]])
>>> m(input)
tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[ 3.5000, 3.5000, 1.6585, 0.4320, 3.5000, 3.5000],
[ 3.5000, 3.5000, -0.8701, -0.4649, 3.5000, 3.5000],
[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
>>> # using different paddings for different sides
>>> m = nn.ConstantPad2d((3, 0, 2, 1), 3.5)
>>> m(input)
tensor([[[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[ 3.5000, 3.5000, 3.5000, 1.6585, 0.4320],
[ 3.5000, 3.5000, 3.5000, -0.8701, -0.4649],
[ 3.5000, 3.5000, 3.5000, 3.5000, 3.5000]]])
class torch.nn.ConstantPad3d(padding, value)?
Pads the input tensor boundaries with a constant value.
For <cite>N</cite>-dimensional padding, use torch.nn.functional.pad()
.
Parameters
padding (python:int__, tuple) – the size of the padding. If is <cite>int</cite>, uses the same padding in all boundaries. If a 6-<cite>tuple</cite>, uses (,
,
,
,
,
)
Shape:
Examples:
>>> m = nn.ConstantPad3d(3, 3.5)
>>> input = torch.randn(16, 3, 10, 20, 30)
>>> output = m(input)
>>> # using different paddings for different sides
>>> m = nn.ConstantPad3d((3, 3, 6, 6, 0, 1), 3.5)
>>> output = m(input)
class torch.nn.ELU(alpha=1.0, inplace=False)?
應(yīng)用逐元素函數(shù):
Parameters
False
Shape:
Examples:
>>> m = nn.ELU()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Hardshrink(lambd=0.5)?
逐個(gè)應(yīng)用硬收縮功能:
Parameters
lambd – Hardshrink 配方的值。 默認(rèn)值:0.5
Shape:
Examples:
>>> m = nn.Hardshrink()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Hardtanh(min_val=-1.0, max_val=1.0, inplace=False, min_value=None, max_value=None)?
逐個(gè)應(yīng)用 HardTanh 函數(shù)
HardTanh 定義為:
線性區(qū)域的范圍可以使用
min_val
和max_val
進(jìn)行調(diào)整。
Parameters
False
不推薦使用關(guān)鍵字參數(shù)min_value
和max_value
,而推薦使用min_val
和max_val
。
Shape:
Examples:
>>> m = nn.Hardtanh(-2, 2)
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.LeakyReLU(negative_slope=0.01, inplace=False)?
Applies the element-wise function:
要么
Parameters
False
Shape:
Examples:
>>> m = nn.LeakyReLU(0.1)
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.LogSigmoid?
Applies the element-wise function:
Shape:
Examples:
>>> m = nn.LogSigmoid()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.MultiheadAttention(embed_dim, num_heads, dropout=0.0, bias=True, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None)?
允許模型共同關(guān)注來自不同表示子空間的信息。 請(qǐng)參閱參考:注意就是您所需要的
Parameters
Examples:
>>> multihead_attn = nn.MultiheadAttention(embed_dim, num_heads)
>>> attn_output, attn_output_weights = multihead_attn(query, key, value)
forward(query, key, value, key_padding_mask=None, need_weights=True, attn_mask=None)?
Parameters
Shape:
class torch.nn.PReLU(num_parameters=1, init=0.25)?
Applies the element-wise function:
or
此處是可學(xué)習(xí)的參數(shù)。 當(dāng)不帶參數(shù)調(diào)用時(shí), <cite>nn.PReLU()</cite>在所有輸入通道上使用單個(gè)參數(shù)
。 如果使用 <cite>nn.PReLU(nChannels)</cite>進(jìn)行調(diào)用,則每個(gè)輸入通道將使用單獨(dú)的
。
Note
學(xué)習(xí)以獲得良好性能時(shí),不應(yīng)使用重量衰減。
Note
通道暗淡是輸入的第二暗淡。 當(dāng)輸入的亮度為< 2 時(shí),則不存在通道的亮度,并且通道數(shù)= 1。
Parameters
Shape:
Variables
?PReLU.weight (tensor)–可學(xué)習(xí)的形狀權(quán)重(num_parameters
)。
Examples:
>>> m = nn.PReLU()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.ReLU(inplace=False)?
將整流的線性單位函數(shù)按元素應(yīng)用:
Parameters
inplace – can optionally do the operation in-place. Default: False
Shape:
Examples:
>>> m = nn.ReLU()
>>> input = torch.randn(2)
>>> output = m(input)
An implementation of CReLU - https://arxiv.org/abs/1603.05201
>>> m = nn.ReLU()
>>> input = torch.randn(2).unsqueeze(0)
>>> output = torch.cat((m(input),m(-input)))
class torch.nn.ReLU6(inplace=False)?
Applies the element-wise function:
Parameters
inplace – can optionally do the operation in-place. Default: False
Shape:
Examples:
>>> m = nn.ReLU6()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.RReLU(lower=0.125, upper=0.3333333333333333, inplace=False)?
如本文所述,按元素應(yīng)用隨機(jī)泄漏的整流襯套單元功能:
卷積網(wǎng)絡(luò)中修正激活的經(jīng)驗(yàn)評(píng)估。
該函數(shù)定義為:
其中是從均勻分布
中隨機(jī)抽樣的。
Parameters
False
Shape:
Examples:
>>> m = nn.RReLU(0.1, 0.3)
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.SELU(inplace=False)?
按元素應(yīng)用,例如:
和
。
更多細(xì)節(jié)可以在論文自歸一化神經(jīng)網(wǎng)絡(luò)中找到。
Parameters
原位 (bool , 可選)–可以選擇就地進(jìn)行操作。 默認(rèn)值:False
Shape:
Examples:
>>> m = nn.SELU()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.CELU(alpha=1.0, inplace=False)?
Applies the element-wise function:
可以在論文連續(xù)微分指數(shù)線性單位中找到更多詳細(xì)信息。
Parameters
False
Shape:
Examples:
>>> m = nn.CELU()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.GELU?
應(yīng)用高斯誤差線性單位函數(shù):
其中是高斯分布的累積分布函數(shù)。
Shape:
Examples:
>>> m = nn.GELU()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Sigmoid?
Applies the element-wise function:
Shape:
Examples:
>>> m = nn.Sigmoid()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Softplus(beta=1, threshold=20)?
Applies the element-wise function:
SoftPlus 是 ReLU 函數(shù)的平滑近似,可用于將機(jī)器的輸出約束為始終為正。
為了獲得數(shù)值穩(wěn)定性,對(duì)于超過一定值的輸入,實(shí)現(xiàn)將恢復(fù)為線性函數(shù)。
Parameters
Shape:
Examples:
>>> m = nn.Softplus()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Softshrink(lambd=0.5)?
逐個(gè)應(yīng)用軟收縮功能:
Parameters
lambd –軟收縮配方的值。 默認(rèn)值:0.5
Shape:
Examples:
>>> m = nn.Softshrink()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Softsign?
Applies the element-wise function:
Shape:
Examples:
>>> m = nn.Softsign()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Tanh?
Applies the element-wise function:
Shape:
Examples:
>>> m = nn.Tanh()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Tanhshrink?
Applies the element-wise function:
Shape:
Examples:
>>> m = nn.Tanhshrink()
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Threshold(threshold, value, inplace=False)?
設(shè)置輸入張量的每個(gè)元素的閾值。
閾值定義為:
Parameters
False
Shape:
Examples:
>>> m = nn.Threshold(0.1, 20)
>>> input = torch.randn(2)
>>> output = m(input)
class torch.nn.Softmin(dim=None)?
將 Softmin 函數(shù)應(yīng)用于縮放后的 n 維輸入張量,以便 n 維輸出張量的元素在 <cite>[0,1]</cite> 范圍內(nèi),總和為 1。
Softmin 定義為:
Shape:
Parameters
dim (python:int )–將計(jì)算 Softmin 的維度(因此,沿著 dim 的每個(gè)切片的總和為 1)。
Returns
與輸入具有相同尺寸和形狀的張量,其值在[0,1]范圍內(nèi)
Examples:
>>> m = nn.Softmin()
>>> input = torch.randn(2, 3)
>>> output = m(input)
class torch.nn.Softmax(dim=None)?
將 Softmax 函數(shù)應(yīng)用于縮放后的 n 維輸入 Tensor,以使 n 維輸出 Tensor 的元素在[0,1]范圍內(nèi),總和為 1。
Softmax 定義為:
Shape:
Returns
與輸入具有相同尺寸和形狀的張量,其值在[0,1]范圍內(nèi)
Parameters
dim (python:int )–將計(jì)算 Softmax 的維度(因此,沿著 dim 的每個(gè)切片的總和為 1)。
Note
該模塊無法直接與 NLLLoss 配合使用,后者希望 Log 是在 Softmax 及其自身之間進(jìn)行計(jì)算的。 請(qǐng)改用 <cite>LogSoftmax</cite> (速度更快,并且具有更好的數(shù)值屬性)。
Examples:
>>> m = nn.Softmax(dim=1)
>>> input = torch.randn(2, 3)
>>> output = m(input)
class torch.nn.Softmax2d?
將 SoftMax 應(yīng)用于要素上的每個(gè)空間位置。
當(dāng)給定Channels x Height x Width
的圖像時(shí),它將 <cite>Softmax</cite> 應(yīng)用于每個(gè)位置
Shape:
Returns
a Tensor of the same dimension and shape as the input with values in the range [0, 1]
Examples:
>>> m = nn.Softmax2d()
>>> # you softmax over the 2nd dimension
>>> input = torch.randn(2, 3, 12, 13)
>>> output = m(input)
class torch.nn.LogSoftmax(dim=None)?
將功能應(yīng)用于 n 維輸入張量。 LogSoftmax 公式可以簡(jiǎn)化為:
Shape:
Parameters
暗淡的 (python:int )–用來計(jì)算 LogSoftmax 的尺寸。
Returns
與輸入具有相同尺寸和形狀的張量,其值在[-inf,0)范圍內(nèi)
Examples:
>>> m = nn.LogSoftmax()
>>> input = torch.randn(2, 3)
>>> output = m(input)
class torch.nn.AdaptiveLogSoftmaxWithLoss(in_features, n_classes, cutoffs, div_value=4.0, head_bias=False)?
如 Edouard Grave,Armand Joulin,MoustaphaCissé,David Grangier 和 HervéJégou 在中針對(duì) GPU 所述的高效 softmax 逼近。
自適應(yīng) softmax 是用于訓(xùn)練具有大輸出空間的模型的近似策略。 當(dāng)標(biāo)簽分布高度不平衡時(shí),例如在自然語言建模中,單詞頻率分布大致遵循 Zipf 定律時(shí),此方法最為有效。
自適應(yīng) softmax 根據(jù)標(biāo)簽的頻率將標(biāo)簽劃分為幾個(gè)簇。 這些集群每個(gè)可能包含不同數(shù)量的目標(biāo)。 此外,包含較少標(biāo)簽的群集將較低維的嵌入分配給這些標(biāo)簽,從而加快了計(jì)算速度。 對(duì)于每個(gè)小批量,僅評(píng)估至少存在一個(gè)目標(biāo)的集群。
這個(gè)想法是,頻繁訪問的集群(如第一個(gè)集群,包含最頻繁的標(biāo)簽),也應(yīng)該便宜計(jì)算-也就是說,包含少量分配的標(biāo)簽。
我們強(qiáng)烈建議您查看原始文件以了解更多詳細(xì)信息。
cutoffs
應(yīng)該是按升序排序的有序整數(shù)序列。 它控制集群的數(shù)量以及將目標(biāo)劃分為集群。 例如,設(shè)置cutoffs = [10, 100, 1000]
意味著第一個(gè) <cite>10 個(gè)</cite>目標(biāo)將分配給自適應(yīng) softmax 的“頭部”,目標(biāo) <cite>11、12,…,100 個(gè)</cite>將分配給第一個(gè)目標(biāo) 集群,目標(biāo) <cite>101、102,…,1000</cite> 將分配給第二個(gè)集群,而目標(biāo) <cite>1001、1002,…,n_classes-1</cite> 將分配給最后一個(gè),第三個(gè) 簇。div_value
用于計(jì)算每個(gè)附加聚類的大小,以head_bias
如果設(shè)置為 True,則會(huì)向自適應(yīng) softmax 的“頭部”添加一個(gè)偏差項(xiàng)。 有關(guān)詳細(xì)信息,請(qǐng)參見紙張。 在官方實(shí)現(xiàn)中設(shè)置為 False。Warning
傳遞給該模塊的標(biāo)簽應(yīng)根據(jù)其頻率進(jìn)行分類。 這意味著最頻繁的標(biāo)簽應(yīng)由索引 <cite>0</cite> 表示,最不頻繁的標(biāo)簽應(yīng)由索引 <cite>n_classes-1</cite> 表示。
Note
該模塊返回帶有output
和loss
字段的NamedTuple
。 有關(guān)詳細(xì)信息,請(qǐng)參見其他文檔。
Note
要計(jì)算所有類別的對(duì)數(shù)概率,可以使用log_prob
方法。
Parameters
True
,則向自適應(yīng) softmax 的“ head”添加一個(gè)偏差項(xiàng)。 默認(rèn)值:False
Returns
N
的張量,其中包含每個(gè)示例的計(jì)算目標(biāo)對(duì)數(shù)概率Return type
具有output
和loss
字段的NamedTuple
Shape:
Scalar
log_prob(input)?
計(jì)算所有的日志概率
Parameters
輸入 (tensor)–小批量示例
Returns
范圍內(nèi)每個(gè)類別
的對(duì)數(shù)概率,其中
是傳遞給
AdaptiveLogSoftmaxWithLoss
構(gòu)造函數(shù)的參數(shù)。
Shape:
predict(input)?
這等效于 <cite>self.log_pob(input).argmax(dim = 1)</cite>,但在某些情況下效率更高。
Parameters
input (Tensor) – a minibatch of examples
Returns
每個(gè)示例中概率最高的類別
Return type
輸出(張量)
Shape:
class torch.nn.BatchNorm1d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)?
如論文中所述,對(duì) 2D 或 3D 輸入(具有可選附加通道尺寸的 1D 輸入的微型批處理)應(yīng)用批歸一化:通過減少內(nèi)部協(xié)變量偏移加速深度網(wǎng)絡(luò)訓(xùn)練。
均值和標(biāo)準(zhǔn)偏差是在微型批次上按維度計(jì)算的,并且和
是大小為 <cite>C</cite> 的可學(xué)習(xí)參數(shù)矢量(其中 <cite>C</cite> 是輸入大小 )。 默認(rèn)情況下,
的元素設(shè)置為 1,
的元素設(shè)置為 0。
同樣默認(rèn)情況下,在訓(xùn)練過程中,該層會(huì)繼續(xù)對(duì)其計(jì)算的均值和方差進(jìn)行估算,然后將其用于評(píng)估期間的標(biāo)準(zhǔn)化。 運(yùn)行估計(jì)保持默認(rèn)值momentum
0.1。
如果track_running_stats
設(shè)置為False
,則此層將不保持運(yùn)行估計(jì),而是在評(píng)估期間也使用批處理統(tǒng)計(jì)信息。
Note
momentum
參數(shù)不同于優(yōu)化程序類中使用的參數(shù)以及傳統(tǒng)的動(dòng)量概念。 在數(shù)學(xué)上,此處用于運(yùn)行統(tǒng)計(jì)信息的更新規(guī)則為,其中
是估計(jì)的統(tǒng)計(jì)信息,
是新的觀測(cè)值。
由于批量歸一化是在 <cite>C</cite> 維度上完成的,因此要計(jì)算<cite>(N,L)</cite>切片的統(tǒng)計(jì)信息,因此通常將其稱為“時(shí)間批量歸一化”。
Parameters
None
以獲得累積移動(dòng)平均線(即簡(jiǎn)單平均線)。 默認(rèn)值:0.1True
時(shí),此模塊具有可學(xué)習(xí)的仿射參數(shù)。 默認(rèn)值:True
True
時(shí),此模塊跟蹤運(yùn)行平均值和方差;設(shè)置為False
時(shí),此模塊不跟蹤此類統(tǒng)計(jì)信息,并且始終使用批處理統(tǒng)計(jì)信息 訓(xùn)練和評(píng)估模式。 默認(rèn)值:True
Shape:
Examples:
>>> # With Learnable Parameters
>>> m = nn.BatchNorm1d(100)
>>> # Without Learnable Parameters
>>> m = nn.BatchNorm1d(100, affine=False)
>>> input = torch.randn(20, 100)
>>> output = m(input)
class torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)?
如論文中所述,對(duì) 4D 輸入(具有附加通道尺寸的 2D 輸入的微型批處理)應(yīng)用批歸一化:通過減少內(nèi)部協(xié)變量偏移來加速深度網(wǎng)絡(luò)訓(xùn)練。
The mean and standard-deviation are calculated per-dimension over the mini-batches and and
are learnable parameter vectors of size <cite>C</cite> (where <cite>C</cite> is the input size). By default, the elements of
are set to 1 and the elements of
are set to 0.
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default momentum
of 0.1.
If track_running_stats
is set to False
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.
Note
This momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where
is the estimated statistic and
is the new observed value.
由于批量歸一化是在 <cite>C</cite> 維度上完成的,因此要計(jì)算<cite>(N,H,W)</cite>切片的統(tǒng)計(jì)信息,因此通常將其稱為“空間批量歸一化”。
Parameters
None
for cumulative moving average (i.e. simple average). Default: 0.1True
, this module has learnable affine parameters. Default: True
True
, this module tracks the running mean and variance, and when set to False
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: True
Shape:
Examples:
>>> # With Learnable Parameters
>>> m = nn.BatchNorm2d(100)
>>> # Without Learnable Parameters
>>> m = nn.BatchNorm2d(100, affine=False)
>>> input = torch.randn(20, 100, 35, 45)
>>> output = m(input)
class torch.nn.BatchNorm3d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)?
如論文中所述,對(duì) 5D 輸入(具有附加通道尺寸的 3D 輸入的微型批處理)應(yīng)用批歸一化:通過減少內(nèi)部協(xié)變量偏移加速深度網(wǎng)絡(luò)訓(xùn)練。
The mean and standard-deviation are calculated per-dimension over the mini-batches and and
are learnable parameter vectors of size <cite>C</cite> (where <cite>C</cite> is the input size). By default, the elements of
are set to 1 and the elements of
are set to 0.
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default momentum
of 0.1.
If track_running_stats
is set to False
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.
Note
This momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where
is the estimated statistic and
is the new observed value.
由于批量歸一化是在 <cite>C</cite> 維度上完成的,因此要計(jì)算<cite>(N,D,H,W)</cite>切片的統(tǒng)計(jì)信息,因此通常將這種體積批量歸一化或時(shí)空稱為術(shù)語 批處理規(guī)范化。
Parameters
None
for cumulative moving average (i.e. simple average). Default: 0.1True
, this module has learnable affine parameters. Default: True
True
, this module tracks the running mean and variance, and when set to False
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: True
Shape:
Examples:
>>> # With Learnable Parameters
>>> m = nn.BatchNorm3d(100)
>>> # Without Learnable Parameters
>>> m = nn.BatchNorm3d(100, affine=False)
>>> input = torch.randn(20, 100, 35, 45, 10)
>>> output = m(input)
class torch.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True)?
如論文組歸一化中所述,將組歸一化應(yīng)用于微型輸入。
輸入通道分為num_groups
組,每個(gè)組包含num_channels / num_groups
通道。 均值和標(biāo)準(zhǔn)差在每個(gè)組中分別計(jì)算。 如果affine
為True
,則和
是大小為
num_channels
的可學(xué)習(xí)的每通道仿射變換參數(shù)矢量。
該層使用在訓(xùn)練和評(píng)估模式下從輸入數(shù)據(jù)中計(jì)算出的統(tǒng)計(jì)信息。
Parameters
True
時(shí),此模塊具有可學(xué)習(xí)的每通道仿射參數(shù),分別初始化為 1(用于權(quán)重)和零(用于偏差)。 默認(rèn)值:True
。Shape:
Examples:
>>> input = torch.randn(20, 6, 10, 10)
>>> # Separate 6 channels into 3 groups
>>> m = nn.GroupNorm(3, 6)
>>> # Separate 6 channels into 6 groups (equivalent with InstanceNorm)
>>> m = nn.GroupNorm(6, 6)
>>> # Put all 6 channels into a single group (equivalent with LayerNorm)
>>> m = nn.GroupNorm(1, 6)
>>> # Activating the module
>>> output = m(input)
class torch.nn.SyncBatchNorm(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, process_group=None)?
如論文[中所述,對(duì) N 維輸入(具有附加通道維的N-2] D 輸入的小批量)進(jìn)行批量歸一化批量歸一化:通過減少內(nèi)部協(xié)變量偏移加速深度網(wǎng)絡(luò)訓(xùn)練 。
均值和標(biāo)準(zhǔn)偏差是在同一過程組的所有微型批次中按維度計(jì)算的。 和
是大小為 <cite>C</cite> (其中 <cite>C</cite> 是輸入大小)的可學(xué)習(xí)參數(shù)向量。 默認(rèn)情況下,從
采樣
的元素,并將
的元素設(shè)置為 0。
Also by default, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default momentum
of 0.1.
If track_running_stats
is set to False
, this layer then does not keep running estimates, and batch statistics are instead used during evaluation time as well.
Note
momentum
參數(shù)不同于優(yōu)化程序類中使用的參數(shù)以及傳統(tǒng)的動(dòng)量概念。 在數(shù)學(xué)上,此處用于運(yùn)行統(tǒng)計(jì)信息的更新規(guī)則為,其中
是估計(jì)的統(tǒng)計(jì)信息,
是新的觀測(cè)值。
由于批量歸一化是在 <cite>C</cite> 維度上完成的,因此要計(jì)算<cite>(N,+)</cite>切片的統(tǒng)計(jì)信息,因此通常將其稱為體積批量歸一化或時(shí)空批量歸一化。
當(dāng)前,SyncBatchNorm 僅支持每個(gè)進(jìn)程具有單個(gè) GPU 的 DistributedDataParallel。 在使用 DDP 包裝網(wǎng)絡(luò)之前,使用 torch.nn.SyncBatchNorm.convert_sync_batchnorm()將 BatchNorm 層轉(zhuǎn)換為 SyncBatchNorm。
Parameters
None
for cumulative moving average (i.e. simple average). Default: 0.1True
, this module has learnable affine parameters. Default: True
True
, this module tracks the running mean and variance, and when set to False
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: True
Shape:
Examples:
>>> # With Learnable Parameters
>>> m = nn.SyncBatchNorm(100)
>>> # creating process group (optional)
>>> # process_ids is a list of int identifying rank ids.
>>> process_group = torch.distributed.new_group(process_ids)
>>> # Without Learnable Parameters
>>> m = nn.BatchNorm3d(100, affine=False, process_group=process_group)
>>> input = torch.randn(20, 100, 35, 45, 10)
>>> output = m(input)
>>> # network is nn.BatchNorm layer
>>> sync_bn_network = nn.SyncBatchNorm.convert_sync_batchnorm(network, process_group)
>>> # only single gpu per process is currently supported
>>> ddp_sync_bn_network = torch.nn.parallel.DistributedDataParallel(
>>> sync_bn_network,
>>> device_ids=[args.local_rank],
>>> output_device=args.local_rank)
classmethod convert_sync_batchnorm(module, process_group=None)?
輔助函數(shù)將模型中的 <cite>torch.nn.BatchNormND</cite> 層轉(zhuǎn)換為 <cite>torch.nn.SyncBatchNorm</cite> 層。
Parameters
默認(rèn)是整個(gè)世界
Returns
具有轉(zhuǎn)換后的 <cite>torch.nn.SyncBatchNorm</cite> 層的原始模塊
Example:
>>> # Network with nn.BatchNorm layer
>>> module = torch.nn.Sequential(
>>> torch.nn.Linear(20, 100),
>>> torch.nn.BatchNorm1d(100)
>>> ).cuda()
>>> # creating process group (optional)
>>> # process_ids is a list of int identifying rank ids.
>>> process_group = torch.distributed.new_group(process_ids)
>>> sync_bn_module = convert_sync_batchnorm(module, process_group)
class torch.nn.InstanceNorm1d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)?
如論文中所述,將實(shí)例標(biāo)準(zhǔn)化應(yīng)用于 3D 輸入(具有可選附加通道尺寸的 1D 輸入的微型批處理)實(shí)例標(biāo)準(zhǔn)化:快速樣式化的缺失成分。
微型批處理中每個(gè)對(duì)象的維數(shù)均值和標(biāo)準(zhǔn)差分別計(jì)算。 如果affine
為True
,則和
是大小為 <cite>C</cite> (其中 <cite>C</cite> 為輸入大小)的可學(xué)習(xí)參數(shù)矢量。
默認(rèn)情況下,該層使用在訓(xùn)練和評(píng)估模式下從輸入數(shù)據(jù)計(jì)算出的實(shí)例統(tǒng)計(jì)信息。
如果track_running_stats
設(shè)置為True
,則在訓(xùn)練過程中,此層將繼續(xù)對(duì)其計(jì)算的均值和方差進(jìn)行估算,然后將其用于評(píng)估期間的標(biāo)準(zhǔn)化。 運(yùn)行估計(jì)保持默認(rèn)值momentum
0.1。
Note
This momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where
is the estimated statistic and
is the new observed value.
Note
InstanceNorm1d
和 LayerNorm
非常相似,但有一些細(xì)微的差異。 InstanceNorm1d
應(yīng)用于多維數(shù)據(jù)序列之類的通道數(shù)據(jù)的每個(gè)通道,但是 LayerNorm
通常應(yīng)用于整個(gè)樣本,并且通常用于 NLP 任務(wù)。 另外, LayerNorm
應(yīng)用逐元素仿射變換,而 InstanceNorm1d
通常不應(yīng)用仿射變換。
Parameters
True
時(shí),此模塊具有可學(xué)習(xí)的仿射參數(shù),其初始化方式與批量標(biāo)準(zhǔn)化相同。 默認(rèn)值:False
。True
時(shí),此模塊跟蹤運(yùn)行平均值和方差;設(shè)置為False
時(shí),此模塊不跟蹤此類統(tǒng)計(jì)信息,并且始終使用批處理統(tǒng)計(jì)信息 訓(xùn)練和評(píng)估模式。 默認(rèn)值:False
Shape:
Examples:
>>> # Without Learnable Parameters
>>> m = nn.InstanceNorm1d(100)
>>> # With Learnable Parameters
>>> m = nn.InstanceNorm1d(100, affine=True)
>>> input = torch.randn(20, 100, 40)
>>> output = m(input)
class torch.nn.InstanceNorm2d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)?
如論文中所述,將實(shí)例標(biāo)準(zhǔn)化應(yīng)用于 4D 輸入(具有附加通道尺寸的 2D 輸入的小批量)實(shí)例標(biāo)準(zhǔn)化:快速樣式化的缺失成分。
The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch. and
are learnable parameter vectors of size <cite>C</cite> (where <cite>C</cite> is the input size) if
affine
is True
.
By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
If track_running_stats
is set to True
, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default momentum
of 0.1.
Note
This momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where
is the estimated statistic and
is the new observed value.
Note
InstanceNorm2d
和 LayerNorm
非常相似,但有一些細(xì)微的差異。 InstanceNorm2d
適用于 RGB 圖像之類的通道數(shù)據(jù)的每個(gè)通道,但是 LayerNorm
通常適用于整個(gè)樣本,并且通常用于 NLP 任務(wù)。 另外, LayerNorm
應(yīng)用逐元素仿射變換,而 InstanceNorm2d
通常不應(yīng)用仿射變換。
Parameters
True
, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default: False
.True
, this module tracks the running mean and variance, and when set to False
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: False
Shape:
Examples:
>>> # Without Learnable Parameters
>>> m = nn.InstanceNorm2d(100)
>>> # With Learnable Parameters
>>> m = nn.InstanceNorm2d(100, affine=True)
>>> input = torch.randn(20, 100, 35, 45)
>>> output = m(input)
class torch.nn.InstanceNorm3d(num_features, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)?
如論文中所述,將實(shí)例標(biāo)準(zhǔn)化應(yīng)用于 5D 輸入(具有附加通道尺寸的 3D 輸入的微型批處理)實(shí)例標(biāo)準(zhǔn)化:快速樣式化缺少的成分。
微型批處理中每個(gè)對(duì)象的維數(shù)均值和標(biāo)準(zhǔn)差分別計(jì)算。 如果affine
為True
,則和
是大小為 C 的可學(xué)習(xí)參數(shù)矢量(其中 C 為輸入大?。?/p>
By default, this layer uses instance statistics computed from input data in both training and evaluation modes.
If track_running_stats
is set to True
, during training this layer keeps running estimates of its computed mean and variance, which are then used for normalization during evaluation. The running estimates are kept with a default momentum
of 0.1.
Note
This momentum
argument is different from one used in optimizer classes and the conventional notion of momentum. Mathematically, the update rule for running statistics here is , where
is the estimated statistic and
is the new observed value.
Note
InstanceNorm3d
和 LayerNorm
非常相似,但有一些細(xì)微的差異。 InstanceNorm3d
適用于通道數(shù)據(jù)的每個(gè)通道,例如具有 RGB 顏色的 3D 模型,但 LayerNorm
通常適用于整個(gè)樣本,并且經(jīng)常用于 NLP 任務(wù)。 另外, LayerNorm
應(yīng)用逐元素仿射變換,而 InstanceNorm3d
通常不應(yīng)用仿射變換。
Parameters
True
, this module has learnable affine parameters, initialized the same way as done for batch normalization. Default: False
.True
, this module tracks the running mean and variance, and when set to False
, this module does not track such statistics and always uses batch statistics in both training and eval modes. Default: False
Shape:
Examples:
>>> # Without Learnable Parameters
>>> m = nn.InstanceNorm3d(100)
>>> # With Learnable Parameters
>>> m = nn.InstanceNorm3d(100, affine=True)
>>> input = torch.randn(20, 100, 35, 45, 10)
>>> output = m(input)
class torch.nn.LayerNorm(normalized_shape, eps=1e-05, elementwise_affine=True)?
如論文層規(guī)范化中所述,在小批量輸入上應(yīng)用層規(guī)范化。
平均值和標(biāo)準(zhǔn)偏差是在最后一定數(shù)量的尺寸上分別計(jì)算的,這些尺寸必須具有normalized_shape
指定的形狀。 如果elementwise_affine
為True
,則和
是
normalized_shape
的可學(xué)習(xí)仿射變換參數(shù)。
Note
與批量歸一化和實(shí)例歸一化不同,后者使用affine
選項(xiàng)對(duì)每個(gè)通道/平面應(yīng)用標(biāo)量縮放和偏置,而層歸一化則使用elementwise_affine
對(duì)每個(gè)元素縮放和偏置。
This layer uses statistics computed from input data in both training and evaluation modes.
Parameters
輸入尺寸的預(yù)期輸入
如果使用單個(gè)整數(shù),則將其視為一個(gè)單例列表,并且此模塊將在最后一個(gè)預(yù)期為該特定大小的維度上進(jìn)行規(guī)范化。
True
時(shí),此模塊具有可學(xué)習(xí)的按元素仿射參數(shù),分別初始化為 1(用于權(quán)重)和零(用于偏差)。 默認(rèn)值:True
。Shape:
Examples:
>>> input = torch.randn(20, 5, 10, 10)
>>> # With Learnable Parameters
>>> m = nn.LayerNorm(input.size()[1:])
>>> # Without Learnable Parameters
>>> m = nn.LayerNorm(input.size()[1:], elementwise_affine=False)
>>> # Normalize over last two dimensions
>>> m = nn.LayerNorm([10, 10])
>>> # Normalize over last dimension of size 10
>>> m = nn.LayerNorm(10)
>>> # Activating the module
>>> output = m(input)
class torch.nn.LocalResponseNorm(size, alpha=0.0001, beta=0.75, k=1.0)?
在由多個(gè)輸入平面組成的輸入信號(hào)上應(yīng)用本地響應(yīng)歸一化,其中通道占據(jù)第二維。 跨通道應(yīng)用標(biāo)準(zhǔn)化。
Parameters
Shape:
Examples:
>>> lrn = nn.LocalResponseNorm(2)
>>> signal_2d = torch.randn(32, 5, 24, 24)
>>> signal_4d = torch.randn(16, 5, 7, 7, 7, 7)
>>> output_2d = lrn(signal_2d)
>>> output_4d = lrn(signal_4d)
class torch.nn.RNNBase(mode, input_size, hidden_size, num_layers=1, bias=True, batch_first=False, dropout=0.0, bidirectional=False)?
flatten_parameters()?
重置參數(shù)數(shù)據(jù)指針,以便它們可以使用更快的代碼路徑。
現(xiàn)在,僅當(dāng)模塊在 GPU 上并且啟用 cuDNN 時(shí),此方法才有效。 否則,這是無人操作。
class torch.nn.RNN(*args, **kwargs)?
將具有或
非線性的多層 Elman RNN 應(yīng)用于輸入序列。
對(duì)于輸入序列中的每個(gè)元素,每一層都會(huì)計(jì)算以下功能:
其中是時(shí)間 <cite>t</cite> 的隱藏狀態(tài),
是時(shí)間 <cite>t</cite> 的輸入,而
是時(shí)間<cite>的上一層的隱藏狀態(tài) ] t-1</cite> 或時(shí)間 <cite>0</cite> 時(shí)的初始隱藏狀態(tài)。 如果
nonlinearity
為'relu'
,則使用 <cite>ReLU</cite> 代替 <cite>tanh</cite> 。
Parameters
num_layers=2
意味著將兩個(gè) RNN 堆疊在一起以形成<cite>堆疊的 RNN</cite> ,而第二個(gè) RNN 則接收第一個(gè) RNN 的輸出并計(jì)算最終結(jié)果。 默認(rèn)值:1'tanh'
或'relu'
。 默認(rèn)值:'tanh'
False
,則該層不使用偏置權(quán)重 <cite>b_ih</cite> 和 <cite>b_hh</cite> 。 默認(rèn)值:True
True
,則輸入和輸出張量以<cite>(批,序列,特征)</cite>的形式提供。 默認(rèn)值:False
dropout
。 默認(rèn)值:0True
成為雙向 RNN。 默認(rèn)值:False
Inputs: input, h_0
torch.nn.utils.rnn.pack_padded_sequence()
或 torch.nn.utils.rnn.pack_sequence()
。Outputs: output, h_n
torch.nn.utils.rnn.PackedSequence
作為輸入,則輸出也將是打包序列。
對(duì)于未包裝的情況,可以使用output.view(seq_len, batch, num_directions, hidden_size)
分隔方向,向前和向后分別是方向 <cite>0</cite> 和 <cite>1</cite> 。 同樣,在包裝好的情況下,方向也可以分開。
像輸出一樣,可以使用h_n.view(num_layers, num_directions, batch, hidden_size)
分離各層。
Shape:
Variables
Note
所有權(quán)重和偏差均從初始化,其中
Note
如果滿足以下條件:1)啟用 cudnn,2)輸入數(shù)據(jù)在 GPU 上 3)輸入數(shù)據(jù)具有 dtype torch.float16
4)使用 V100 GPU,5)輸入數(shù)據(jù)不是PackedSequence
格式的持久算法 可以選擇以提高性能。
Examples:
>>> rnn = nn.RNN(10, 20, 2)
>>> input = torch.randn(5, 3, 10)
>>> h0 = torch.randn(2, 3, 20)
>>> output, hn = rnn(input, h0)
class torch.nn.LSTM(*args, **kwargs)?
將多層長(zhǎng)短期記憶(LSTM)RNN 應(yīng)用于輸入序列。
For each element in the input sequence, each layer computes the following function:
其中是時(shí)間 <cite>t</cite> 的隱藏狀態(tài),
是時(shí)間 <cite>t</cite> 的單元狀態(tài),
是時(shí)間 <cite>t</cite> 的輸入 ,
是時(shí)間 <cite>t-1</cite> 時(shí)層的隱藏狀態(tài),或者是時(shí)間 <cite>0</cite> 時(shí)的初始隱藏狀態(tài),以及
,
,
,
分別是輸入,忘記,單元和輸出門。
是 S 型函數(shù),
是 Hadamard 乘積。
在多層 LSTM 中,第層(
)的輸入
是前一層的隱藏狀態(tài)
乘以壓降
,其中每個(gè)
是伯努利隨機(jī)變量 概率為
dropout
的是。
Parameters
num_layers=2
意味著將兩個(gè) LSTM 堆疊在一起以形成<cite>堆疊的 LSTM</cite> ,而第二個(gè) LSTM 則接收第一個(gè) LSTM 的輸出并計(jì)算最終結(jié)果。 默認(rèn)值:1False
, then the layer does not use bias weights <cite>b_ih</cite> and <cite>b_hh</cite>. Default: True
True
,則輸入和輸出張量按(batch,seq,feature)提供。 默認(rèn)值:False
dropout
。 默認(rèn)值:0True
變?yōu)殡p向 LSTM。 默認(rèn)值:False
Inputs: input, (h_0, c_0)
torch.nn.utils.rnn.pack_padded_sequence()
或 torch.nn.utils.rnn.pack_sequence()
。如果未提供<cite>(h_0,c_0)</cite>,則 h_0 和 c_0 均默認(rèn)為零。
Outputs: output, (h_n, c_n)
torch.nn.utils.rnn.PackedSequence
作為輸入,則輸出也將是打包序列。
For the unpacked case, the directions can be separated using output.view(seq_len, batch, num_directions, hidden_size)
, with forward and backward being direction <cite>0</cite> and <cite>1</cite> respectively. Similarly, the directions can be separated in the packed case.
像輸出一樣,可以使用h_n.view(num_layers, num_directions, batch, hidden_size)
分隔各層,并且對(duì)于 c_n 同樣。
Variables
Note
All the weights and biases are initialized from where
Note
If the following conditions are satisfied: 1) cudnn is enabled, 2) input data is on the GPU 3) input data has dtype torch.float16
4) V100 GPU is used, 5) input data is not in PackedSequence
format persistent algorithm can be selected to improve performance.
Examples:
>>> rnn = nn.LSTM(10, 20, 2)
>>> input = torch.randn(5, 3, 10)
>>> h0 = torch.randn(2, 3, 20)
>>> c0 = torch.randn(2, 3, 20)
>>> output, (hn, cn) = rnn(input, (h0, c0))
class torch.nn.GRU(*args, **kwargs)?
將多層門控循環(huán)單元(GRU)RNN 應(yīng)用于輸入序列。
For each element in the input sequence, each layer computes the following function:
其中是時(shí)間 <cite>t</cite> 的隱藏狀態(tài),
是時(shí)間 <cite>t</cite> 的輸入,
是時(shí)間 <cite>t 時(shí)層的隱藏狀態(tài) -1</cite> 或時(shí)間 <cite>0</cite> 時(shí)的初始隱藏狀態(tài),以及
,
和
分別是復(fù)位門,更新門和新門。
是 S 型函數(shù),
是 Hadamard 乘積。
在多層 GRU 中,第層(
)的輸入
是前一層的隱藏狀態(tài)
乘以壓降
,其中每個(gè)
是伯努利隨機(jī)變量 概率為
dropout
的是。
Parameters
num_layers=2
意味著將兩個(gè) GRU 堆疊在一起以形成<cite>堆疊的 GRU</cite> ,而第二個(gè) GRU 則接收第一個(gè) GRU 的輸出并計(jì)算最終結(jié)果。 默認(rèn)值:1False
, then the layer does not use bias weights <cite>b_ih</cite> and <cite>b_hh</cite>. Default: True
True
, then the input and output tensors are provided as (batch, seq, feature). Default: False
dropout
。 默認(rèn)值:0True
成為雙向 GRU。 默認(rèn)值:False
Inputs: input, h_0
torch.nn.utils.rnn.pack_padded_sequence()
。Outputs: output, h_n
torch.nn.utils.rnn.PackedSequence
,則輸出也將是打包序列。 對(duì)于未包裝的情況,可以使用output.view(seq_len, batch, num_directions, hidden_size)
分離方向,向前和向后分別是方向 <cite>0</cite> 和 <cite>1</cite> 。同樣,在包裝好的情況下,方向也可以分開。
Like output, the layers can be separated using h_n.view(num_layers, num_directions, batch, hidden_size)
.
Shape:
Variables
Note
All the weights and biases are initialized from where
Note
If the following conditions are satisfied: 1) cudnn is enabled, 2) input data is on the GPU 3) input data has dtype torch.float16
4) V100 GPU is used, 5) input data is not in PackedSequence
format persistent algorithm can be selected to improve performance.
Examples:
>>> rnn = nn.GRU(10, 20, 2)
>>> input = torch.randn(5, 3, 10)
>>> h0 = torch.randn(2, 3, 20)
>>> output, hn = rnn(input, h0)
class torch.nn.RNNCell(input_size, hidden_size, bias=True, nonlinearity='tanh')?
具有 tanh 或 ReLU 非線性的 Elman RNN 單元。
如果nonlinearity
為<cite>'relu'</cite>,則使用 ReLU 代替 tanh。
Parameters
False
, then the layer does not use bias weights <cite>b_ih</cite> and <cite>b_hh</cite>. Default: True
'tanh'
or 'relu'
. Default: 'tanh'
Inputs: input, hidden
Outputs: h'
Shape:
Variables
Note
All the weights and biases are initialized from where
Examples:
>>> rnn = nn.RNNCell(10, 20)
>>> input = torch.randn(6, 3, 10)
>>> hx = torch.randn(3, 20)
>>> output = []
>>> for i in range(6):
hx = rnn(input[i], hx)
output.append(hx)
class torch.nn.LSTMCell(input_size, hidden_size, bias=True)?
長(zhǎng)短期記憶(LSTM)單元。
其中是 S 型函數(shù),
是 Hadamard 乘積。
Parameters
False
,則該層不使用偏置權(quán)重 <cite>b_ih</cite> 和 <cite>b_hh</cite> 。 默認(rèn)值:True
Inputs: input, (h_0, c_0)
If <cite>(h_0, c_0)</cite> is not provided, both h_0 and c_0 default to zero.
Outputs: (h_1, c_1)
Variables
Note
All the weights and biases are initialized from where
Examples:
>>> rnn = nn.LSTMCell(10, 20)
>>> input = torch.randn(6, 3, 10)
>>> hx = torch.randn(3, 20)
>>> cx = torch.randn(3, 20)
>>> output = []
>>> for i in range(6):
hx, cx = rnn(input[i], (hx, cx))
output.append(hx)
class torch.nn.GRUCell(input_size, hidden_size, bias=True)?
門控循環(huán)單元(GRU)單元
where is the sigmoid function, and
is the Hadamard product.
Parameters
False
, then the layer does not use bias weights <cite>b_ih</cite> and <cite>b_hh</cite>. Default: True
Inputs: input, hidden
Outputs: h'
Shape:
Variables
Note
All the weights and biases are initialized from where
Examples:
>>> rnn = nn.GRUCell(10, 20)
>>> input = torch.randn(6, 3, 10)
>>> hx = torch.randn(3, 20)
>>> output = []
>>> for i in range(6):
hx = rnn(input[i], hx)
output.append(hx)
class torch.nn.Transformer(d_model=512, nhead=8, num_encoder_layers=6, num_decoder_layers=6, dim_feedforward=2048, dropout=0.1, activation='relu', custom_encoder=None, custom_decoder=None)?
變壓器模型。 用戶可以根據(jù)需要修改屬性。 該體系結(jié)構(gòu)基于論文“注意就是您所需要的”。 Ashish Vaswani,Noam Shazeer,Niki Parmar,Jakob Uszkoreit,Llion Jones,Aidan N Gomez,Lukasz Kaiser 和 Illia Polosukhin。 2017 年。您只需要關(guān)注即可。 《神經(jīng)信息處理系統(tǒng)的發(fā)展》,第 6000-6010 頁。 用戶可以使用相應(yīng)的參數(shù)構(gòu)建 BERT (https://arxiv.org/abs/1810.04805)模型。
Parameters
Examples::
>>> transformer_model = nn.Transformer(nhead=16, num_encoder_layers=12)
>>> src = torch.rand((10, 32, 512))
>>> tgt = torch.rand((20, 32, 512))
>>> out = transformer_model(src, tgt)
注意:中提供了將 nn.Transformer 模塊應(yīng)用于單詞語言模型的完整示例,網(wǎng)址為 https://github.com/pytorch/examples/tree/master/word_language_model
forward(src, tgt, src_mask=None, tgt_mask=None, memory_mask=None, src_key_padding_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None)?
接收并處理屏蔽的源/目標(biāo)序列。
Parameters
Shape:
注意:[src / tgt / memory] _mask 應(yīng)該用 float('-inf')表示被遮蓋的位置,而 float(0.0)表示其他。 這些掩碼可確保對(duì)位置 i 的預(yù)測(cè)僅取決于未掩碼的位置 j,并且對(duì)批次中的每個(gè)序列均應(yīng)用相同的預(yù)測(cè)。 [src / tgt / memory] _key_padding_mask 應(yīng)該是 ByteTensor,其中 True 值是應(yīng)該用 float('-inf')掩蓋的位置,而 False 值將保持不變。 此掩碼可確保在屏蔽后不會(huì)從位置 i 獲取任何信息,并且對(duì)于批次中的每個(gè)序列都有單獨(dú)的掩碼。
注意:由于轉(zhuǎn)換器模型中的多頭注意架構(gòu),轉(zhuǎn)換器的輸出序列長(zhǎng)度與解碼的輸入序列(即目標(biāo))長(zhǎng)度相同。
其中 S 是源序列長(zhǎng)度,T 是目標(biāo)序列長(zhǎng)度,N 是批處理大小,E 是特征編號(hào)
Examples
>>> output = transformer_model(src, tgt, src_mask=src_mask, tgt_mask=tgt_mask)
generate_square_subsequent_mask(sz)?
為該序列生成一個(gè)正方形蒙版。 屏蔽的位置填充有 float('-inf')。 未屏蔽的位置填充有 float(0.0)。
class torch.nn.TransformerEncoder(encoder_layer, num_layers, norm=None)?
TransformerEncoder 是 N 個(gè)編碼器層的堆棧
Parameters
Examples::
>>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8)
>>> transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)
>>> src = torch.rand(10, 32, 512)
>>> out = transformer_encoder(src)
forward(src, mask=None, src_key_padding_mask=None)?
將輸入依次通過編碼器層。
Parameters
Shape:
請(qǐng)參閱 Transformer 類中的文檔。
class torch.nn.TransformerDecoder(decoder_layer, num_layers, norm=None)?
TransformerDecoder 是 N 個(gè)解碼器層的堆棧
Parameters
Examples::
>>> decoder_layer = nn.TransformerDecoderLayer(d_model=512, nhead=8)
>>> transformer_decoder = nn.TransformerDecoder(decoder_layer, num_layers=6)
>>> memory = torch.rand(10, 32, 512)
>>> tgt = torch.rand(20, 32, 512)
>>> out = transformer_decoder(tgt, memory)
forward(tgt, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None)?
輸入(和掩碼)依次通過解碼器層。
Parameters
Shape:
see the docs in Transformer class.
class torch.nn.TransformerEncoderLayer(d_model, nhead, dim_feedforward=2048, dropout=0.1, activation='relu')?
TransformerEncoderLayer 由自檢和前饋網(wǎng)絡(luò)組成。 此標(biāo)準(zhǔn)編碼器層基于“注意就是您所需要的”一文。 Ashish Vaswani,Noam Shazeer,Niki Parmar,Jakob Uszkoreit,Llion Jones,Aidan N Gomez,Lukasz Kaiser 和 Illia Polosukhin。 2017 年。您只需要關(guān)注即可。 《神經(jīng)信息處理系統(tǒng)的發(fā)展》,第 6000-6010 頁。 用戶可以在應(yīng)用過程中以不同的方式修改或?qū)崿F(xiàn)。
Parameters
Examples::
>>> encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8)
>>> src = torch.rand(10, 32, 512)
>>> out = encoder_layer(src)
forward(src, src_mask=None, src_key_padding_mask=None)?
使輸入通過編碼器層。
Parameters
Shape:
see the docs in Transformer class.
class torch.nn.TransformerDecoderLayer(d_model, nhead, dim_feedforward=2048, dropout=0.1, activation='relu')?
TransformerDecoderLayer 由自組織,多頭組織和前饋網(wǎng)絡(luò)組成。 這個(gè)標(biāo)準(zhǔn)的解碼器層基于論文“注意就是全部”。 Ashish Vaswani,Noam Shazeer,Niki Parmar,Jakob Uszkoreit,Llion Jones,Aidan N Gomez,Lukasz Kaiser 和 Illia Polosukhin。 2017 年。您只需要關(guān)注即可。 《神經(jīng)信息處理系統(tǒng)的發(fā)展》,第 6000-6010 頁。 用戶可以在應(yīng)用過程中以不同的方式修改或?qū)崿F(xiàn)。
Parameters
Examples::
>>> decoder_layer = nn.TransformerDecoderLayer(d_model=512, nhead=8)
>>> memory = torch.rand(10, 32, 512)
>>> tgt = torch.rand(20, 32, 512)
>>> out = decoder_layer(tgt, memory)
forward(tgt, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None)?
將輸入(和掩碼)通過解碼器層。
Parameters
Shape:
see the docs in Transformer class.
class torch.nn.Identity(*args, **kwargs)?
對(duì)參數(shù)不敏感的占位符身份運(yùn)算符。
Parameters
Examples:
>>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
>>> input = torch.randn(128, 20)
>>> output = m(input)
>>> print(output.size())
torch.Size([128, 20])
class torch.nn.Linear(in_features, out_features, bias=True)?
對(duì)輸入數(shù)據(jù)應(yīng)用線性變換:
Parameters
False
,則該圖層將不會(huì)學(xué)習(xí)加法偏差。 默認(rèn)值:True
Shape:
Variables
bias
為True
,則從Examples:
>>> m = nn.Linear(20, 30)
>>> input = torch.randn(128, 20)
>>> output = m(input)
>>> print(output.size())
torch.Size([128, 30])
class torch.nn.Bilinear(in1_features, in2_features, out_features, bias=True)?
對(duì)輸入數(shù)據(jù)應(yīng)用雙線性變換:
Parameters
True
Shape:
Variables
bias
為True
,則從Examples:
>>> m = nn.Bilinear(20, 30, 40)
>>> input1 = torch.randn(128, 20)
>>> input2 = torch.randn(128, 30)
>>> output = m(input1, input2)
>>> print(output.size())
torch.Size([128, 40])
class torch.nn.Dropout(p=0.5, inplace=False)?
在訓(xùn)練期間,使用伯努利分布的樣本以概率p
將輸入張量的某些元素隨機(jī)歸零。 在每個(gè)前向呼叫中,每個(gè)通道將獨(dú)立清零。
如論文中所述,通過防止特征檢測(cè)器的共同適應(yīng)來改善神經(jīng)網(wǎng)絡(luò),這已被證明是一種有效的技術(shù),可用于規(guī)范化和防止神經(jīng)元的共同適應(yīng)。
此外,在訓(xùn)練期間,將輸出縮放為。 這意味著在評(píng)估期間,模塊僅計(jì)算身份函數(shù)。
Parameters
True
,將就地執(zhí)行此操作。 默認(rèn)值:False
Shape:
Examples:
>>> m = nn.Dropout(p=0.2)
>>> input = torch.randn(20, 16)
>>> output = m(input)
class torch.nn.Dropout2d(p=0.5, inplace=False)?
隨機(jī)將整個(gè)通道調(diào)零(通道是 2D 特征圖,例如,批輸入中第個(gè)樣本的第
個(gè)通道是 2D 張量
)。 使用伯努利分布中的樣本,每個(gè)信道將在每次前向呼叫中以概率
p
獨(dú)立清零。
通常,輸入來自nn.Conv2d
模塊。
如論文中所述,使用卷積網(wǎng)絡(luò)進(jìn)行有效的對(duì)象定位,如果特征圖中的相鄰像素高度相關(guān)(通常是早期卷積層的情況),則 i.i.d。 輟學(xué)不會(huì)使激活規(guī)律化,否則只會(huì)導(dǎo)致有效學(xué)習(xí)率下降。
在這種情況下,nn.Dropout2d()
將有助于促進(jìn)要素地圖之間的獨(dú)立性,應(yīng)改用nn.Dropout2d()
。
Parameters
True
,則將原位執(zhí)行此操作Shape:
Examples:
>>> m = nn.Dropout2d(p=0.2)
>>> input = torch.randn(20, 16, 32, 32)
>>> output = m(input)
class torch.nn.Dropout3d(p=0.5, inplace=False)?
隨機(jī)將整個(gè)通道調(diào)零(通道是 3D 特征圖,例如,批輸入中第個(gè)樣本的第
個(gè)通道是 3D 張量
)。 使用伯努利分布中的樣本,每個(gè)信道將在每次前向呼叫中以概率
p
獨(dú)立清零。
通常,輸入來自nn.Conv3d
模塊。
As described in the paper Efficient Object Localization Using Convolutional Networks , if adjacent pixels within feature maps are strongly correlated (as is normally the case in early convolution layers) then i.i.d. dropout will not regularize the activations and will otherwise just result in an effective learning rate decrease.
在這種情況下,nn.Dropout3d()
將有助于促進(jìn)要素地圖之間的獨(dú)立性,應(yīng)改用nn.Dropout3d()
。
Parameters
True
, will do this operation in-placeShape:
Examples:
>>> m = nn.Dropout3d(p=0.2)
>>> input = torch.randn(20, 16, 4, 32, 32)
>>> output = m(input)
class torch.nn.AlphaDropout(p=0.5, inplace=False)?
將 Alpha Dropout 應(yīng)用于輸入。
Alpha Dropout 是一種 Dropout,可以維持自我規(guī)范化屬性。 對(duì)于均值為零且單位標(biāo)準(zhǔn)差為零的輸入,Alpha Dropout 的輸出將保持輸入的原始均值和標(biāo)準(zhǔn)差。 Alpha Dropout 與 SELU 激活功能緊密結(jié)合,可確保輸出具有零均值和單位標(biāo)準(zhǔn)偏差。
在訓(xùn)練期間,它使用來自伯努利分布的樣本以概率 p 隨機(jī)掩蓋輸入張量的某些元素。 在每個(gè)前向調(diào)用中,要屏蔽的元素都會(huì)隨機(jī)化,并進(jìn)行縮放和移位以保持零均值和單位標(biāo)準(zhǔn)差。
在評(píng)估過程中,模塊僅計(jì)算身份函數(shù)。
More details can be found in the paper Self-Normalizing Neural Networks .
Parameters
True
, will do this operation in-placeShape:
Examples:
>>> m = nn.AlphaDropout(p=0.2)
>>> input = torch.randn(20, 16)
>>> output = m(input)
class torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None)?
一個(gè)簡(jiǎn)單的查找表,用于存儲(chǔ)固定字典和大小的嵌入。
該模塊通常用于存儲(chǔ)單詞嵌入并使用索引檢索它們。 模塊的輸入是索引列表,輸出是相應(yīng)的詞嵌入。
Parameters
padding_idx
處嵌入輸出以填充輸出(初始化為 零)。max_norm
的每個(gè)嵌入矢量將重新規(guī)范化為具有 規(guī)范max_norm
。max_norm
選項(xiàng)計(jì)算的 p 范數(shù)的 p。 默認(rèn)值2
。False
。True
,則梯度 w.r.t. weight
矩陣將是稀疏張量。 有關(guān)稀疏漸變的更多詳細(xì)信息,請(qǐng)參見注釋。
Variables
?Embedding.weight (tensor)–形狀模塊的可學(xué)習(xí)權(quán)重(num_embeddings,embedding_dim)從初始化
Shape:
Note
請(qǐng)記住,只有有限數(shù)量的優(yōu)化程序支持稀疏漸變:當(dāng)前為optim.SGD
(<cite>CUDA</cite> 和 <cite>CPU</cite> ),optim.SparseAdam
(<cite>CUDA</cite> 和[ <cite>CPU</cite> )和optim.Adagrad
(<cite>CPU</cite> )
Note
設(shè)置padding_idx
時(shí),padding_idx
的嵌入矢量初始化為全零。 但是,請(qǐng)注意,此向量可以在以后進(jìn)行修改,例如,使用定制的初始化方法,從而更改用于填充輸出的向量。 來自 Embedding
的矢量的梯度始終為零。
Examples:
>>> # an Embedding module containing 10 tensors of size 3
>>> embedding = nn.Embedding(10, 3)
>>> # a batch of 2 samples of 4 indices each
>>> input = torch.LongTensor([[1,2,4,5],[4,3,2,9]])
>>> embedding(input)
tensor([[[-0.0251, -1.6902, 0.7172],
[-0.6431, 0.0748, 0.6969],
[ 1.4970, 1.3448, -0.9685],
[-0.3677, -2.7265, -0.1685]],
[[ 1.4970, 1.3448, -0.9685],
[ 0.4362, -0.4004, 0.9400],
[-0.6431, 0.0748, 0.6969],
[ 0.9124, -2.3616, 1.1151]]])
>>> # example with padding_idx
>>> embedding = nn.Embedding(10, 3, padding_idx=0)
>>> input = torch.LongTensor([[0,2,0,5]])
>>> embedding(input)
tensor([[[ 0.0000, 0.0000, 0.0000],
[ 0.1535, -2.0309, 0.9315],
[ 0.0000, 0.0000, 0.0000],
[-0.1655, 0.9897, 0.0635]]])
classmethod from_pretrained(embeddings, freeze=True, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False)?
從給定的二維 FloatTensor 創(chuàng)建嵌入實(shí)例。
Parameters
num_embeddings
傳遞給嵌入,第二維作為embedding_dim
傳遞給 Embedding。True
,則張量不會(huì)在學(xué)習(xí)過程中更新。 等效于embedding.weight.requires_grad = False
。 默認(rèn)值:True
2
。False
。Examples:
>>> # FloatTensor containing pretrained weights
>>> weight = torch.FloatTensor([[1, 2.3, 3], [4, 5.1, 6.3]])
>>> embedding = nn.Embedding.from_pretrained(weight)
>>> # Get embeddings for index 1
>>> input = torch.LongTensor([1])
>>> embedding(input)
tensor([[ 4.0000, 5.1000, 6.3000]])
class torch.nn.EmbeddingBag(num_embeddings, embedding_dim, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, mode='mean', sparse=False, _weight=None)?
計(jì)算嵌入“袋”的總和或方法,而無需實(shí)例化中間嵌入。
對(duì)于恒定長(zhǎng)度且沒有per_sample_weights
的袋子,此類
mode="sum"
等于Embedding
,然后是torch.sum(dim=0)
,
-mode="mean"
等于Embedding
,然后是torch.mean(dim=0)
,
-mode="max"
的等效于Embedding
,然后是torch.max(dim=0)
。
但是, EmbeddingBag
比使用這些操作鏈要花費(fèi)更多的時(shí)間和內(nèi)存。
EmbeddingBag 還支持按樣本權(quán)重作為正向傳遞的參數(shù)。 在執(zhí)行mode
指定的加權(quán)縮減之前,這會(huì)縮放嵌入的輸出。 如果通過per_sample_weights``,則唯一支持的
mode是
"sum",它根據(jù)
per_sample_weights`計(jì)算加權(quán)和。
Parameters
max_norm
is renormalized to have norm max_norm
.max_norm
option. Default 2
.False
。 注意:mode="max"
時(shí)不支持此選項(xiàng)。"sum"
,"mean"
或"max"
。 指定減少袋子的方式。 "sum"
會(huì)考慮per_sample_weights
來計(jì)算加權(quán)總和。 "mean"
計(jì)算袋子中值的平均值,"max"
計(jì)算每個(gè)袋子中的最大值。 默認(rèn)值:"mean"
True
,則梯度 w.r.t. weight
矩陣將是稀疏張量。 有關(guān)稀疏漸變的更多詳細(xì)信息,請(qǐng)參見注釋。 注意:mode="max"
時(shí)不支持此選項(xiàng)。
Variables
?EmbeddingBag.weight (tensor)–形狀為<cite>的模塊的可學(xué)習(xí)權(quán)重(從初始化的 num_embeddings,embedding_dim)</cite> 。
Inputs: input (LongTensor), offsets (LongTensor, optional), and
per_index_weights
(張量,可選)
input
是形狀為<cite>(B,N)</cite>的二維,
它將被視為B
袋(序列),每個(gè)袋子的長(zhǎng)度都是固定長(zhǎng)度N
,這將返回B
值的匯總值取決于mode
。 在這種情況下,offsets
被忽略,必須為None
。
input
是形狀為<cite>(N)</cite>的 1D,
它將被視為多個(gè)包(序列)的串聯(lián)。 offsets
必須是一維張量,其中包含input
中每個(gè)包的起始索引位置。 因此,對(duì)于形狀為<cite>(B)</cite>的offsets
,input
將被視為具有B
袋。 空袋子(即長(zhǎng)度為 0 的袋子)將返回由零填充的向量。
per_sample_weights (Tensor, optional): a tensor of float / double weights, or None
表示所有權(quán)重應(yīng)為1
。 如果指定,per_sample_weights
必須具有與輸入完全相同的形狀,并且如果不是None
,則將其視為具有相同的offsets
。 僅支持mode='sum'
。
輸出形狀:<cite>(B,embedding_dim)</cite>
Examples:
>>> # an Embedding module containing 10 tensors of size 3
>>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')
>>> # a batch of 2 samples of 4 indices each
>>> input = torch.LongTensor([1,2,4,5,4,3,2,9])
>>> offsets = torch.LongTensor([0,4])
>>> embedding_sum(input, offsets)
tensor([[-0.8861, -5.4350, -0.0523],
[ 1.1306, -2.5798, -1.0044]])
classmethod from_pretrained(embeddings, freeze=True, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, mode='mean', sparse=False)?
從給定的二維 FloatTensor 創(chuàng)建 EmbeddingBag 實(shí)例。
Parameters
True
,則張量不會(huì)在學(xué)習(xí)過程中更新。 等效于embeddingbag.weight.requires_grad = False
。 默認(rèn)值:True
None
2
.False
."mean"
False
。Examples:
>>> # FloatTensor containing pretrained weights
>>> weight = torch.FloatTensor([[1, 2.3, 3], [4, 5.1, 6.3]])
>>> embeddingbag = nn.EmbeddingBag.from_pretrained(weight)
>>> # Get embeddings for index 1
>>> input = torch.LongTensor([[1, 0]])
>>> embeddingbag(input)
tensor([[ 2.5000, 3.7000, 4.6500]])
class torch.nn.CosineSimilarity(dim=1, eps=1e-08)?
傳回與
之間的余弦相似度(沿 dim 計(jì)算)。
Parameters
Shape:
Examples::
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim=1, eps=1e-6)
>>> output = cos(input1, input2)
class torch.nn.PairwiseDistance(p=2.0, eps=1e-06, keepdim=False)?
使用 p 范數(shù)計(jì)算向量和
之間的成對(duì)成對(duì)距離:
Parameters
Shape:
keepdim
為True
,則Examples::
>>> pdist = nn.PairwiseDistance(p=2)
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = pdist(input1, input2)
class torch.nn.L1Loss(size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),該標(biāo)準(zhǔn)測(cè)量輸入中的每個(gè)元素與目標(biāo)
中的平均絕對(duì)誤差(MAE)。
未減少的損失(即reduction
設(shè)置為'none'
)的損失可描述為:
其中是批次大小。 如果
reduction
不是'none'
(默認(rèn)為'mean'
),則:
和
是任意形狀的張量,每個(gè)張量共有
個(gè)元素。
求和運(yùn)算仍對(duì)所有元素進(jìn)行運(yùn)算,并除以。
如果一組reduction = 'sum'
可以避免被劃分。
Parameters
reduction
)。 默認(rèn)情況下,損失是批次中每個(gè)損失元素的平均數(shù)。 請(qǐng)注意,對(duì)于某些損失,每個(gè)樣本有多個(gè)元素。 如果將字段size_average
設(shè)置為False
,則每個(gè)小批量的損失總和。 當(dāng) reduce 為False
時(shí)將被忽略。 默認(rèn)值:True
reduction
)。 默認(rèn)情況下,取決于size_average
,對(duì)每個(gè)小批量的觀測(cè)值求平均或求和。 當(dāng)reduce
為False
時(shí),返回每批元素?fù)p失,并忽略size_average
。 默認(rèn)值:True
'none'
| 'mean'
| 'sum'
。 'none'
:不應(yīng)用任何減少量; 'mean'
:輸出的總和除以輸出中元素的數(shù)量; 'sum'
:將對(duì)輸出求和。 注意:size_average
和reduce
正在淘汰中,與此同時(shí),指定這兩個(gè) args 中的任何一個(gè)將覆蓋reduction
。 默認(rèn)值:'mean'
Shape:
reduction
為'none'
,則Examples:
>>> loss = nn.L1Loss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> output = loss(input, target)
>>> output.backward()
class torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),該標(biāo)準(zhǔn)測(cè)量輸入和目標(biāo)
中每個(gè)元素之間的均方誤差(L2 平方平方)。
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
where is the batch size. If
reduction
is not 'none'
(default 'mean'
), then:
and
are tensors of arbitrary shapes with a total of
elements each.
The sum operation still operates over all the elements, and divides by .
The division by can be avoided if one sets
reduction = 'sum'
.
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
Examples:
>>> loss = nn.MSELoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> output = loss(input, target)
>>> output.backward()
class torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')?
此標(biāo)準(zhǔn)將nn.LogSoftmax()
和nn.NLLLoss()
合并為一個(gè)類別。
在訓(xùn)練帶有 <cite>C</cite> 類的分類問題時(shí)很有用。 如果提供的話,可選參數(shù)weight
應(yīng)該是一維<cite>張量</cite>,為每個(gè)類分配權(quán)重。 當(dāng)您的訓(xùn)練集不平衡時(shí),此功能特別有用。
<cite>輸入</cite>預(yù)計(jì)將包含每個(gè)類別的原始,未標(biāo)準(zhǔn)化的分?jǐn)?shù)。
對(duì)于 <cite>K</cite> -維情況(稍后描述),<cite>輸入</cite>必須為或
和
大小的張量。
對(duì)于一個(gè)大小為 <cite>minibatch</cite> 的 1D 張量張量的每個(gè)值,此標(biāo)準(zhǔn)都希望在范圍內(nèi)的類別索引作為<cite>目標(biāo)</cite>; 如果指定了 <cite>ignore_index</cite> ,則此條件也接受該類索引(該索引可能不一定在類范圍內(nèi))。
損失可描述為:
或在指定weight
參數(shù)的情況下:
對(duì)于每個(gè)小批量,損失是通過觀察得出的平均值。
通過提供大小為
的輸入(其中
是尺寸的數(shù)量)和適當(dāng)形狀的目標(biāo),也可以用于更高尺寸的輸入(例如 2D 圖像)(請(qǐng)參見下文)。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
size_average
為True
時(shí),損耗是在不可忽略的目標(biāo)上平均的。reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
為'none'
,則與 K 尺寸相同:Examples:
>>> loss = nn.CrossEntropyLoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.empty(3, dtype=torch.long).random_(5)
>>> output = loss(input, target)
>>> output.backward()
class torch.nn.CTCLoss(blank=0, reduction='mean', zero_infinity=False)?
連接主義者的時(shí)間分類損失。
計(jì)算連續(xù)(非分段)時(shí)間序列與目標(biāo)序列之間的損失。 CTCLoss 將輸入與目標(biāo)進(jìn)行可能的對(duì)齊之和加起來,從而產(chǎn)生相對(duì)于每個(gè)輸入節(jié)點(diǎn)可微分的損耗值。 假設(shè)輸入與目標(biāo)的比對(duì)是“多對(duì)一”,這限制了目標(biāo)序列的長(zhǎng)度,因此它必須是輸入長(zhǎng)度的。
Parameters
'none'
| 'mean'
| 'sum'
。 'none'
:不應(yīng)用減少量,'mean'
:將輸出損失除以目標(biāo)長(zhǎng)度,然后取批次的平均值。 默認(rèn)值:'mean'
False
無限損失主要發(fā)生在輸入太短而無法與目標(biāo)對(duì)齊時(shí)。Shape:
torch.nn.functional.log_softmax()
獲得的概率)。target_n = targets[n,0:s_n]
。 每個(gè)長(zhǎng)度都必須為reduction
為'none'
,則為Example:
>>> T = 50 # Input sequence length
>>> C = 20 # Number of classes (including blank)
>>> N = 16 # Batch size
>>> S = 30 # Target sequence length of longest target in batch
>>> S_min = 10 # Minimum target length, for demonstration purposes
>>>
>>> # Initialize random batch of input vectors, for *size = (T,N,C)
>>> input = torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()
>>>
>>> # Initialize random batch of targets (0 = blank, 1:C = classes)
>>> target = torch.randint(low=1, high=C, size=(N, S), dtype=torch.long)
>>>
>>> input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.long)
>>> target_lengths = torch.randint(low=S_min, high=S, size=(N,), dtype=torch.long)
>>> ctc_loss = nn.CTCLoss()
>>> loss = ctc_loss(input, target, input_lengths, target_lengths)
>>> loss.backward()
Reference:
A. Graves 等人:連接主義者的時(shí)間分類:使用循環(huán)神經(jīng)網(wǎng)絡(luò)標(biāo)記未分段的序列數(shù)據(jù): https://www.cs.toronto.edu/~graves/icml_2006.pdf
Note
為了使用 CuDNN,必須滿足以下條件:targets
必須為級(jí)聯(lián)格式,所有input_lengths
都必須為 <cite>T</cite> 。 ,
target_lengths
,整數(shù)參數(shù)必須為 dtype
torch.int32
。
常規(guī)實(shí)現(xiàn)使用(在 PyTorch 中更常見) <cite>torch.long</cite> dtype。
Note
In some circumstances when using the CUDA backend with CuDNN, this operator may select a nondeterministic algorithm to increase performance. If this is undesirable, you can try to make the operation deterministic (potentially at a performance cost) by setting torch.backends.cudnn.deterministic = True
. Please see the notes on Reproducibility for background.
class torch.nn.NLLLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')?
負(fù)對(duì)數(shù)似然損失。 訓(xùn)練帶有 <cite>C</cite> 類的分類問題很有用。
如果提供,則可選參數(shù)weight
應(yīng)該是一維 Tensor,為每個(gè)類分配權(quán)重。 當(dāng)您的訓(xùn)練集不平衡時(shí),此功能特別有用。
通過前向調(diào)用給出的<cite>輸入</cite>預(yù)計(jì)將包含每個(gè)類的對(duì)數(shù)概率。 對(duì)于 <cite>K</cite> -維情況(稍后描述),<cite>輸入</cite>的張量必須為或
和
的大小。
通過在網(wǎng)絡(luò)的最后一層添加 <cite>LogSoftmax</cite> 層,可以輕松獲得神經(jīng)網(wǎng)絡(luò)中的對(duì)數(shù)概率。 如果您不想添加額外的圖層,則可以改用 <cite>CrossEntropyLoss</cite> 。
該損失預(yù)期的<cite>目標(biāo)</cite>應(yīng)該是范圍內(nèi)的類別索引,其中 <cite>C =類別數(shù)</cite>; 如果指定了 <cite>ignore_index</cite> ,則此丟失也將接受該類索引(該索引可能不一定在類范圍內(nèi))。
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
其中是批次大小。 如果
reduction
不是'none'
(默認(rèn)為'mean'
),則
通過提供大小為
的輸入(其中
是尺寸的數(shù)量)和適當(dāng)形狀的目標(biāo),也可以用于更高尺寸的輸入(例如 2D 圖像)(請(qǐng)參見下文)。 對(duì)于圖像,它計(jì)算每像素 NLL 損耗。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
size_average
為True
時(shí),損耗是在不可忽略的目標(biāo)上平均的。reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
為'none'
,則與 K 尺寸相同:Examples:
>>> m = nn.LogSoftmax(dim=1)
>>> loss = nn.NLLLoss()
>>> # input is of size N x C = 3 x 5
>>> input = torch.randn(3, 5, requires_grad=True)
>>> # each element in target has to have 0 <= value < C
>>> target = torch.tensor([1, 0, 4])
>>> output = loss(m(input), target)
>>> output.backward()
>>>
>>>
>>> # 2D loss example (used, for example, with image inputs)
>>> N, C = 5, 4
>>> loss = nn.NLLLoss()
>>> # input is of size N x C x height x width
>>> data = torch.randn(N, 16, 10, 10)
>>> conv = nn.Conv2d(16, C, (3, 3))
>>> m = nn.LogSoftmax(dim=1)
>>> # each element in target has to have 0 <= value < C
>>> target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C)
>>> output = loss(m(conv(data)), target)
>>> output.backward()
class torch.nn.PoissonNLLLoss(log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean')?
帶有目標(biāo)泊松分布的負(fù)對(duì)數(shù)似然損失。
The loss can be described as:
最后一項(xiàng)可以省略,也可以用斯特林公式近似。 逼近值用于大于 1 的目標(biāo)值。對(duì)于小于或等于 1 的目標(biāo)值,零添加到損耗中。
Parameters
True
的損失計(jì)算為False
的損失計(jì)算 是是否計(jì)算全部損失; i。 e。 添加斯特林近似項(xiàng)
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
log_input = False
時(shí)評(píng)估reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Examples:
>>> loss = nn.PoissonNLLLoss()
>>> log_input = torch.randn(5, 2, requires_grad=True)
>>> target = torch.randn(5, 2)
>>> output = loss(log_input, target)
>>> output.backward()
Shape:
reduction
為'none'
,則class torch.nn.KLDivLoss(size_average=None, reduce=None, reduction='mean')?
Kullback-Leibler 散度損失
KL 散度是用于連續(xù)分布的有用距離度量,并且在對(duì)(離散采樣)連續(xù)輸出分布的空間進(jìn)行直接回歸時(shí)通常很有用。
與 NLLLoss
一樣,給定的<cite>輸入</cite>預(yù)期包含對(duì)數(shù)概率,并且不限于 2D 張量。 目標(biāo)以概率給出(即,不取對(duì)數(shù))。
該標(biāo)準(zhǔn)要求<cite>目標(biāo)</cite> <cite>張量</cite>與<cite>輸入</cite> <cite>張量</cite>大小相同。
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
其中索引跨越
input
的所有維度,并且具有與
input
相同的形狀。 如果reduction
不是'none'
(默認(rèn)為'mean'
),則:
在默認(rèn)的reduction
模式'mean'
中,損耗是對(duì)觀察值和尺寸上的的每個(gè)小批量進(jìn)行平均的。 'batchmean'
模式給出正確的 KL 散度,其中損失僅在批次范圍內(nèi)平均。 在下一個(gè)主要版本中,'mean'
模式的行為將更改為與'batchmean'
相同。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'batchmean'
| 'sum'
| 'mean'
。 'none'
:不應(yīng)用減少。 'batchmean'
:輸出的總和將除以 batchsize。 'sum'
:將對(duì)輸出求和。 'mean'
:輸出將除以輸出中的元素?cái)?shù)。 默認(rèn)值:'mean'
Note
size_average
和reduce
正在棄用的過程中,與此同時(shí),指定這兩個(gè) args 中的任何一個(gè)將覆蓋reduction
。
Note
reduction
= 'mean'
未返回真實(shí)的 kl 散度值,請(qǐng)使用與 KL 數(shù)學(xué)定義一致的reduction
= 'batchmean'
。 在下一個(gè)主要版本中,'mean'
將更改為與'batchmean'
相同。
Shape:
reduction
為'none'
,則class torch.nn.BCELoss(weight=None, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)衡量目標(biāo)和輸出之間的二進(jìn)制交叉熵的標(biāo)準(zhǔn):
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
where is the batch size. If
reduction
is not 'none'
(default 'mean'
), then
這用于測(cè)量例如自動(dòng)編碼器中的重建誤差。 請(qǐng)注意,目標(biāo)應(yīng)為 0 到 1 之間的數(shù)字。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
為'none'
,則Examples:
>>> m = nn.Sigmoid()
>>> loss = nn.BCELoss()
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> output = loss(m(input), target)
>>> output.backward()
class torch.nn.BCEWithLogitsLoss(weight=None, size_average=None, reduce=None, reduction='mean', pos_weight=None)?
這種損失將<cite>乙狀結(jié)腸</cite>層和 <cite>BCELoss</cite> 合并為一個(gè)類別。 該版本比使用普通的 <cite>Sigmoid</cite> 和隨后的 <cite>BCELoss</cite> 在數(shù)值上更穩(wěn)定,因?yàn)橥ㄟ^將操作合并為一層,我們利用了 log-sum-exp 技巧進(jìn)行數(shù)值計(jì)算 穩(wěn)定性。
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
where is the batch size. If
reduction
is not 'none'
(default 'mean'
), then
這用于測(cè)量例如自動(dòng)編碼器中的重建誤差。 請(qǐng)注意,目標(biāo) <cite>t [i]</cite> 應(yīng)為 0 到 1 之間的數(shù)字。
通過在積極的例子中增加權(quán)重,可以權(quán)衡召回率和準(zhǔn)確性。 在多標(biāo)簽分類的情況下,損失可描述為:
其中,是類別編號(hào)(對(duì)于多標(biāo)簽二進(jìn)制分類,
;對(duì)于單標(biāo)簽二進(jìn)制分類,
),
是批次中樣品的數(shù)量,
是樣品的重量 課程
的肯定答案。
增加查全率,
增加精度。
例如,如果數(shù)據(jù)集包含一個(gè)類的 100 個(gè)正例和 300 個(gè)負(fù)例,則該類的 <cite>pos_weight</cite> 應(yīng)等于。 損失將好像數(shù)據(jù)集包含
陽性示例。
Examples:
>>> target = torch.ones([10, 64], dtype=torch.float32) # 64 classes, batch size = 10
>>> output = torch.full([10, 64], 0.999) # A prediction (logit)
>>> pos_weight = torch.ones([64]) # All weights are equal to 1
>>> criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
>>> criterion(output, target) # -log(sigmoid(0.999))
tensor(0.3135)
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
- 輸入:
其中
表示任意數(shù)量的附加尺寸
- Target:, same shape as the input
- Output: scalar. Ifreduction
is'none'
, then, same shape as input.
Examples:
>>> loss = nn.BCEWithLogitsLoss()
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> output = loss(input, target)
>>> output.backward()
class torch.nn.MarginRankingLoss(margin=0.0, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn)來測(cè)量給定輸入,
,兩個(gè) 1D 迷你批量<cite>張量</cite>和標(biāo)簽 1D 迷你批量張量
(包含 1 或-1)的損耗。
如果,則假定第一個(gè)輸入的排名應(yīng)高于第二個(gè)輸入(具有更大的值),反之亦然。
迷你批次中每個(gè)樣本的損失函數(shù)為:
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
是'none'
,則class torch.nn.HingeEmbeddingLoss(margin=1.0, size_average=None, reduce=None, reduction='mean')?
在輸入張量和標(biāo)簽張量
(包含 1 或-1)的情況下測(cè)量損耗。 通常用于測(cè)量?jī)蓚€(gè)輸入是否相似或不相似,例如 使用 L1 成對(duì)距離作為
,通常用于學(xué)習(xí)非線性嵌入或半監(jiān)督學(xué)習(xí)。
微型批次中第個(gè)樣本的損失函數(shù)為
總損失函數(shù)為
其中。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
為'none'
,則形狀與輸入相同class torch.nn.MultiLabelMarginLoss(size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),以優(yōu)化輸入(2D 微型批量<cite>張量</cite>)和輸出
(2D <cite>目標(biāo)類別索引的張量</cite>。 對(duì)于小批量中的每個(gè)樣品:
其中,,
,
和
都適用于
和
。
和
的大小必須相同。
該標(biāo)準(zhǔn)僅考慮從正面開始的非負(fù)目標(biāo)的連續(xù)塊。
這允許不同的樣本具有可變數(shù)量的目標(biāo)類別。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
is 'none'
, then Examples:
>>> loss = nn.MultiLabelMarginLoss()
>>> x = torch.FloatTensor([[0.1, 0.2, 0.4, 0.8]])
>>> # for target y, only consider labels 3 and 0, not after label -1
>>> y = torch.LongTensor([[3, 0, -1, 1]])
>>> loss(x, y)
>>> # 0.25 * ((1-(0.1-0.2)) + (1-(0.1-0.4)) + (1-(0.8-0.2)) + (1-(0.8-0.4)))
tensor(0.8500)
class torch.nn.SmoothL1Loss(size_average=None, reduce=None, reduction='mean')?
如果每個(gè)元素的絕對(duì)誤差小于 1,則創(chuàng)建一個(gè)使用平方項(xiàng)的條件,否則,則使用 L1 項(xiàng)。 它對(duì)異常值的敏感性不及 <cite>MSELoss</cite> ,并且在某些情況下可以防止爆炸梯度(例如,參見 Ross Girshick 的 <cite>Fast R-CNN</cite> 論文)。 也稱為胡貝爾損耗:
其中的計(jì)算公式為:
具有總共個(gè)元素的
和
任意形狀,求和運(yùn)算仍對(duì)所有元素進(jìn)行運(yùn)算,并除以
。
如果設(shè)置reduction = 'sum'
,則可以避免被劃分。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
is 'none'
, then class torch.nn.SoftMarginLoss(size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),以優(yōu)化輸入張量與目標(biāo)張量
(包含 1 或-1)之間的兩類分類邏輯損失。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
is 'none'
, then same shape as the inputclass torch.nn.MultiLabelSoftMarginLoss(weight=None, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),該標(biāo)準(zhǔn)基于大小的輸入
和目標(biāo)
之間的最大熵,優(yōu)化多標(biāo)簽“一對(duì)全”損失。 對(duì)于小批量中的每個(gè)樣品:
其中和
。
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
is 'none'
, then class torch.nn.CosineEmbeddingLoss(margin=0.0, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn)來測(cè)量給定輸入張量,
和<cite>張量</cite>標(biāo)簽
的損耗,其值為 1 或-1。 這用于使用余弦距離來測(cè)量?jī)蓚€(gè)輸入是否相似或不相似,并且通常用于學(xué)習(xí)非線性嵌入或半監(jiān)督學(xué)習(xí)。
每個(gè)樣本的損失函數(shù)為:
Parameters
margin
,則默認(rèn)值為reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
class torch.nn.MultiMarginLoss(p=1, margin=1.0, weight=None, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),以優(yōu)化輸入(2D 微型批處理<cite>張量</cite>)和輸出
(目標(biāo)的 1D 張量)之間的多類分類鉸鏈損耗(基于邊距的損耗) 類別索引
):
對(duì)于每個(gè)小批量樣品,一維輸入和標(biāo)量輸出
的損耗為:
其中和
。
(可選)您可以通過將 1D weight
張量傳遞到構(gòu)造函數(shù)中來對(duì)類進(jìn)行非相等加權(quán)。
損失函數(shù)將變?yōu)椋?/p>
Parameters
reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
class torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False, size_average=None, reduce=None, reduction='mean')?
創(chuàng)建一個(gè)標(biāo)準(zhǔn),該標(biāo)準(zhǔn)在輸入張量,
,
和值大于
的邊距下測(cè)量三重態(tài)損失。 這用于測(cè)量樣本之間的相對(duì)相似性。 一個(gè)三元組由 <cite>a</cite> , <cite>p</cite> 和 <cite>n</cite> 組成(即<cite>錨定</cite>,<cite>陽性示例</cite>和[HTG14 負(fù)面示例)。 所有輸入張量的形狀應(yīng)為
。
V. Balntas,E. Riba 等人的論文學(xué)習(xí)具有三重態(tài)損失的淺卷積特征描述符中詳細(xì)描述了距離交換。
The loss function for each sample in the mini-batch is:
哪里
Parameters
False
。reduction
). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample. If the field size_average
is set to False
, the losses are instead summed for each minibatch. Ignored when reduce is False
. Default: True
reduction
). By default, the losses are averaged or summed over observations for each minibatch depending on size_average
. When reduce
is False
, returns a loss per batch element instead and ignores size_average
. Default: True
'none'
| 'mean'
| 'sum'
. 'none'
: no reduction will be applied, 'mean'
: the sum of the output will be divided by the number of elements in the output, 'sum'
: the output will be summed. Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
Shape:
reduction
is 'none'
, then >>> triplet_loss = nn.TripletMarginLoss(margin=1.0, p=2)
>>> anchor = torch.randn(100, 128, requires_grad=True)
>>> positive = torch.randn(100, 128, requires_grad=True)
>>> negative = torch.randn(100, 128, requires_grad=True)
>>> output = triplet_loss(anchor, positive, negative)
>>> output.backward()
class torch.nn.PixelShuffle(upscale_factor)?
將形狀為的張量中的元素重新排列為形狀為
的張量中的元素。
這對(duì)于實(shí)現(xiàn)跨度為的有效子像素卷積很有用。
看論文:Shi 等人的使用高效的亞像素卷積神經(jīng)網(wǎng)絡(luò)進(jìn)行實(shí)時(shí)單圖像和視頻超分辨率。 al(2016)了解更多詳情。
Parameters
upscale_factor (python:int )–通過提高空間分辨率的因子
Shape:
Examples:
>>> pixel_shuffle = nn.PixelShuffle(3)
>>> input = torch.randn(1, 9, 4, 4)
>>> output = pixel_shuffle(input)
>>> print(output.size())
torch.Size([1, 1, 12, 12])
class torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None)?
上采樣給定的多通道 1D(時(shí)間),2D(空間)或 3D(體積)數(shù)據(jù)。
假定輸入數(shù)據(jù)的形式為<cite>微型批處理 x 通道 x [可選深度] x [可選高度] x 寬度</cite>。 因此,對(duì)于空間輸入,我們期望使用 4D 張量;對(duì)于體積輸入,我們期望使用 5D 張量。
可用于上采樣的算法分別是 3D,4D 和 5D 輸入張量的最近鄰和線性,雙線性,雙三次和三線性。
可以給出scale_factor
或目標(biāo)輸出size
來計(jì)算輸出大小。 (因?yàn)槟@鈨煽桑荒芡瑫r(shí)給出兩者)
Parameters
'nearest'
,'linear'
,'bilinear'
,[ 'bicubic'
和'trilinear'
。 默認(rèn)值:'nearest'
True
,則輸入和輸出張量的角像素對(duì)齊,因此 保留這些像素的值。 僅在mode
為'linear'
,'bilinear'
或'trilinear'
時(shí)才有效。 默認(rèn)值:False
Shape:
Warning
使用align_corners = True
時(shí),線性插值模式(<cite>線性</cite>,<cite>雙線性</cite>,<cite>雙三次</cite>和<cite>三線性</cite>)不會(huì)按比例對(duì)齊輸出 和輸入像素,因此輸出值可能取決于輸入大小。 這是這些模式(0.3.1 版之前)的默認(rèn)行為。 從那時(shí)起,默認(rèn)行為是align_corners = False
。 有關(guān)如何影響輸出的具體示例,請(qǐng)參見下文。
Note
如果要縮減采樣/調(diào)整大小,應(yīng)使用interpolate()
。
Examples:
>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[ 1., 2.],
[ 3., 4.]]]])
>>> m = nn.Upsample(scale_factor=2, mode='nearest')
>>> m(input)
tensor([[[[ 1., 1., 2., 2.],
[ 1., 1., 2., 2.],
[ 3., 3., 4., 4.],
[ 3., 3., 4., 4.]]]])
>>> m = nn.Upsample(scale_factor=2, mode='bilinear') # align_corners=False
>>> m(input)
tensor([[[[ 1.0000, 1.2500, 1.7500, 2.0000],
[ 1.5000, 1.7500, 2.2500, 2.5000],
[ 2.5000, 2.7500, 3.2500, 3.5000],
[ 3.0000, 3.2500, 3.7500, 4.0000]]]])
>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> m(input)
tensor([[[[ 1.0000, 1.3333, 1.6667, 2.0000],
[ 1.6667, 2.0000, 2.3333, 2.6667],
[ 2.3333, 2.6667, 3.0000, 3.3333],
[ 3.0000, 3.3333, 3.6667, 4.0000]]]])
>>> # Try scaling the same data in a larger tensor
>>>
>>> input_3x3 = torch.zeros(3, 3).view(1, 1, 3, 3)
>>> input_3x3[:, :, :2, :2].copy_(input)
tensor([[[[ 1., 2.],
[ 3., 4.]]]])
>>> input_3x3
tensor([[[[ 1., 2., 0.],
[ 3., 4., 0.],
[ 0., 0., 0.]]]])
>>> m = nn.Upsample(scale_factor=2, mode='bilinear') # align_corners=False
>>> # Notice that values in top left corner are the same with the small input (except at boundary)
>>> m(input_3x3)
tensor([[[[ 1.0000, 1.2500, 1.7500, 1.5000, 0.5000, 0.0000],
[ 1.5000, 1.7500, 2.2500, 1.8750, 0.6250, 0.0000],
[ 2.5000, 2.7500, 3.2500, 2.6250, 0.8750, 0.0000],
[ 2.2500, 2.4375, 2.8125, 2.2500, 0.7500, 0.0000],
[ 0.7500, 0.8125, 0.9375, 0.7500, 0.2500, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> # Notice that values in top left corner are now changed
>>> m(input_3x3)
tensor([[[[ 1.0000, 1.4000, 1.8000, 1.6000, 0.8000, 0.0000],
[ 1.8000, 2.2000, 2.6000, 2.2400, 1.1200, 0.0000],
[ 2.6000, 3.0000, 3.4000, 2.8800, 1.4400, 0.0000],
[ 2.4000, 2.7200, 3.0400, 2.5600, 1.2800, 0.0000],
[ 1.2000, 1.3600, 1.5200, 1.2800, 0.6400, 0.0000],
[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
class torch.nn.UpsamplingNearest2d(size=None, scale_factor=None)?
將二維最近鄰居上采樣應(yīng)用于由多個(gè)輸入通道組成的輸入信號(hào)。
要指定比例,請(qǐng)使用size
或scale_factor
作為構(gòu)造函數(shù)參數(shù)。
給定size
時(shí),它是圖像<cite>(h,w)</cite>的輸出大小。
Parameters
Warning
不推薦使用此類,而推薦使用interpolate()
。
Shape:
Examples:
>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[ 1., 2.],
[ 3., 4.]]]])
>>> m = nn.UpsamplingNearest2d(scale_factor=2)
>>> m(input)
tensor([[[[ 1., 1., 2., 2.],
[ 1., 1., 2., 2.],
[ 3., 3., 4., 4.],
[ 3., 3., 4., 4.]]]])
class torch.nn.UpsamplingBilinear2d(size=None, scale_factor=None)?
將二維雙線性上采樣應(yīng)用于由多個(gè)輸入通道組成的輸入信號(hào)。
To specify the scale, it takes either the size
or the scale_factor
as it's constructor argument.
When size
is given, it is the output size of the image <cite>(h, w)</cite>.
Parameters
Warning
不推薦使用此類,而推薦使用interpolate()
。 等效于nn.functional.interpolate(..., mode='bilinear', align_corners=True)
。
Shape:
Examples:
>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[ 1., 2.],
[ 3., 4.]]]])
>>> m = nn.UpsamplingBilinear2d(scale_factor=2)
>>> m(input)
tensor([[[[ 1.0000, 1.3333, 1.6667, 2.0000],
[ 1.6667, 2.0000, 2.3333, 2.6667],
[ 2.3333, 2.6667, 3.0000, 3.3333],
[ 3.0000, 3.3333, 3.6667, 4.0000]]]])
class torch.nn.DataParallel(module, device_ids=None, output_device=None, dim=0)?
在模塊級(jí)別實(shí)現(xiàn)數(shù)據(jù)并行性。
該容器通過按批處理維度中的組塊在指定設(shè)備之間劃分輸入來并行化給定module
的應(yīng)用程序(其他對(duì)象將每個(gè)設(shè)備復(fù)制一次)。 在前向傳遞中,模塊在每個(gè)設(shè)備上復(fù)制,每個(gè)副本處理輸入的一部分。 在向后傳遞過程中,每個(gè)副本的梯度被累加到原始模塊中。
批處理大小應(yīng)大于使用的 GPU 數(shù)量。
另請(qǐng)參見:使用 nn.DataParallel 而不是并行處理
允許將任意位置和關(guān)鍵字輸入傳遞到 DataParallel 中,但是某些類型經(jīng)過特殊處理。 張量將在指定的昏暗狀態(tài)(默認(rèn)值為 0)下分散為。 元組,列表和字典類型將被淺表復(fù)制。 其他類型將在不同線程之間共享,并且如果寫入模型的前向傳遞中,則可能會(huì)損壞。
運(yùn)行此 DataParallel
模塊之前,并行化的module
必須在device_ids[0]
上具有其參數(shù)和緩沖區(qū)。
Warning
在每個(gè)轉(zhuǎn)發(fā)中,module
是在每個(gè)設(shè)備上復(fù)制的,因此對(duì)forward
中正在運(yùn)行的模塊的任何更新都將丟失。 例如,如果module
具有在每個(gè)forward
中遞增的計(jì)數(shù)器屬性,則它將始終保持在初始值,因?yàn)楦率窃?code>forward之后銷毀的副本上進(jìn)行的。 但是, DataParallel
保證device[0]
上的副本具有與基本并行化module
共享存儲(chǔ)的參數(shù)和緩沖區(qū)。 因此,將記錄對(duì)device[0]
上參數(shù)或緩沖區(qū)的就地更新。 例如, BatchNorm2d
和 spectral_norm()
依賴此行為來更新緩沖區(qū)。
Warning
module
及其子模塊上定義的前向和后向掛鉤將被調(diào)用len(device_ids)
次,每次輸入都位于特定設(shè)備上。 特別地,僅保證掛鉤在相對(duì)應(yīng)的設(shè)備上的操作中以正確的順序執(zhí)行。 例如,不能保證在所有 len(device_ids)
forward()
調(diào)用<cite>之前執(zhí)行通過 register_forward_pre_hook()
設(shè)置的掛鉤。 在該設(shè)備的相應(yīng) forward()
調(diào)用之前執(zhí)行。</cite>
Warning
當(dāng)module
在forward()
中返回標(biāo)量(即 0 維張量)時(shí),此包裝器將返回一個(gè)長(zhǎng)度等于向量的設(shè)備,該矢量等于數(shù)據(jù)并行性中使用的設(shè)備數(shù),其中包含每個(gè)設(shè)備的結(jié)果。
Note
在包裹在 DataParallel
中的 Module
中使用pack sequence -> recurrent network -> unpack sequence
模式有個(gè)微妙之處。 有關(guān)詳細(xì)信息,請(qǐng)參見我的循環(huán)網(wǎng)絡(luò)不適用于數(shù)據(jù)并行性。部分。
Parameters
Variables
?DataParallel.module (模塊)–要并行化的模塊
Example:
>>> net = torch.nn.DataParallel(model, device_ids=[0, 1, 2])
>>> output = net(input_var) # input_var can be on any device, including CPU
class torch.nn.parallel.DistributedDataParallel(module, device_ids=None, output_device=None, dim=0, broadcast_buffers=True, process_group=None, bucket_cap_mb=25, find_unused_parameters=False, check_reduction=False)?
在模塊級(jí)別實(shí)現(xiàn)基于torch.distributed
包的分布式數(shù)據(jù)并行性。
該容器通過按批處理維度分塊指定設(shè)備之間的輸入來并行化給定模塊的應(yīng)用程序。 該模塊在每臺(tái)機(jī)器和每臺(tái)設(shè)備上復(fù)制,并且每個(gè)這樣的副本處理一部分輸入。 在向后傳遞過程中,將平均每個(gè)節(jié)點(diǎn)的梯度。
批處理大小應(yīng)大于本地使用的 GPU 數(shù)量。
另請(qǐng)參見:基礎(chǔ)知識(shí)和使用 nn.DataParallel 而不是并行處理。 對(duì)輸入的限制與 torch.nn.DataParallel
相同。
此類的創(chuàng)建要求通過調(diào)用 torch.distributed.init_process_group()
已初始化torch.distributed
。
DistributedDataParallel
可以通過以下兩種方式使用:
在這種情況下,將在每個(gè)主機(jī)/節(jié)點(diǎn)上生成一個(gè)進(jìn)程,并且每個(gè)進(jìn)程將在運(yùn)行該節(jié)點(diǎn)的節(jié)點(diǎn)的所有 GPU 上運(yùn)行。 要以這種方式使用DistributedDataParallel
,您可以簡(jiǎn)單地按以下方式構(gòu)建模型:
>>> torch.distributed.init_process_group(backend="nccl")
>>> model = DistributedDataParallel(model) # device_ids will include all GPU devices by default
強(qiáng)烈建議將DistributedDataParallel
與多個(gè)進(jìn)程配合使用,每個(gè)進(jìn)程都在單個(gè) GPU 上運(yùn)行。 這是目前使用 PyTorch 進(jìn)行數(shù)據(jù)并行訓(xùn)練的最快方法,適用于單節(jié)點(diǎn)(multi-GPU)和多節(jié)點(diǎn)數(shù)據(jù)并行訓(xùn)練。 對(duì)于單節(jié)點(diǎn)多 GPU 數(shù)據(jù)并行訓(xùn)練,它被證明比 torch.nn.DataParallel
快得多。
使用方法如下:在具有 N 個(gè) GPU 的每臺(tái)主機(jī)上,應(yīng)生成 N 個(gè)進(jìn)程,同時(shí)確保每個(gè)進(jìn)程在 0 至 N-1 的單個(gè) GPU 上可單獨(dú)運(yùn)行。 因此,您的工作是通過調(diào)用以下命令來確保您的訓(xùn)練腳本在單個(gè)給定的 GPU 上運(yùn)行:
>>> torch.cuda.set_device(i)
我從 0 到 N-1。 在每個(gè)過程中,應(yīng)參考以下內(nèi)容來構(gòu)造此模塊:
>>> torch.distributed.init_process_group(backend='nccl', world_size=4, init_method='...')
>>> model = DistributedDataParallel(model, device_ids=[i], output_device=i)
為了在每個(gè)節(jié)點(diǎn)上產(chǎn)生多個(gè)進(jìn)程,可以使用torch.distributed.launch
或torch.multiprocessing.spawn
Note
nccl
后端目前是與多進(jìn)程單 GPU 分布式訓(xùn)練一起使用的最快和強(qiáng)烈推薦的后端,這適用于單節(jié)點(diǎn)和多節(jié)點(diǎn)分布式訓(xùn)練
Note
該模塊還支持混合精度分布式訓(xùn)練。 這意味著您的模型可以具有不同類型的參數(shù),例如 fp16 和 fp32 的混合類型,對(duì)這些混合類型的參數(shù)進(jìn)行梯度降低將可以正常工作。 另請(qǐng)注意,nccl
后端是目前 fp16 / fp32 混合精度訓(xùn)練中最快,最受推薦的后端。
Note
如果在一個(gè)進(jìn)程上使用torch.save
檢查模塊,在其他進(jìn)程上使用torch.load
對(duì)其進(jìn)行恢復(fù),請(qǐng)確保為每個(gè)進(jìn)程都正確配置了map_location
。 沒有map_location
,torch.load
會(huì)將模塊恢復(fù)到保存模塊的設(shè)備。
Warning
該模塊僅適用于gloo
和nccl
后端。
Warning
構(gòu)造函數(shù),正向方法和輸出的微分(或此模塊輸出的函數(shù))是分布式同步點(diǎn)。 如果不同的進(jìn)程可能執(zhí)行不同的代碼,請(qǐng)考慮到這一點(diǎn)。
Warning
該模塊假定所有參數(shù)在創(chuàng)建時(shí)已在模型中注冊(cè)。 不應(yīng)添加或刪除參數(shù)。 同樣適用于緩沖區(qū)。
Warning
該模塊假定所有參數(shù)在模型中注冊(cè)的每個(gè)分布式過程都以相同的順序進(jìn)行。 模塊本身將按照模型注冊(cè)參數(shù)的相反順序進(jìn)行梯度全約。 換句話說,確保每個(gè)分布式過程具有完全相同的模型并因此具有完全相同的參數(shù)注冊(cè)順序是用戶的責(zé)任。
Warning
該模塊假定所有緩沖區(qū)和漸變都是密集的。
Warning
此模塊不適用于 torch.autograd.grad()
(即,僅當(dāng)要在.grad
參數(shù)的屬性中累積梯度時(shí)才適用)。
Warning
如果您打算將此模塊與nccl
后端或gloo
后端(使用 Infiniband)一起使用,以及使用多個(gè)工作程序的 DataLoader,請(qǐng)將并行處理啟動(dòng)方法更改為forkserver
(僅適用于 Python 3),或者 spawn
。 不幸的是,Gloo(使用 Infiniband)和 NCCL2 都不是安全的,如果不更改此設(shè)置,您可能會(huì)遇到死鎖。
Warning
除非在forward()
方法中初始化了掛鉤,否則將不再調(diào)用module
及其子模塊上定義的向前和向后掛鉤。
Warning
在用 DistributedDataParallel 包裝模型之后,您永遠(yuǎn)不要嘗試更改模型的參數(shù)。 換句話說,當(dāng)用 DistributedDataParallel 包裝模型時(shí),DistributedDataParallel 的構(gòu)造函數(shù)將在構(gòu)造時(shí)在模型本身的所有參數(shù)上注冊(cè)其他梯度減少函數(shù)。 如果在構(gòu)造 DistributedDataParallel 之后更改模型的參數(shù),則不支持此操作,并且可能會(huì)發(fā)生意外行為,因?yàn)榭赡懿粫?huì)調(diào)用某些參數(shù)的梯度減小函數(shù)。
Note
參數(shù)從不在進(jìn)程之間廣播。 該模塊對(duì)梯度執(zhí)行全縮減步驟,并假設(shè)優(yōu)化器將在所有過程中以相同方式修改它們。 緩沖區(qū)(例如 BatchNorm 統(tǒng)計(jì)信息)在每次迭代中從模塊廣播到第 0 級(jí)進(jìn)程,并廣播到系統(tǒng)中的所有其他副本。
Parameters
i``th :attr:
modulereplica is placed on ``device_ids[i]
。 對(duì)于多設(shè)備模塊和 CPU 模塊,device_ids 必須為 None 或?yàn)榭樟斜?,并且用于正向傳遞的輸入數(shù)據(jù)必須放置在正確的設(shè)備上。 (默認(rèn):?jiǎn)卧O(shè)備模塊的所有設(shè)備)True
)None
,將使用由torch.distributed.init_process_group
創(chuàng)建的默認(rèn)進(jìn)程組。 (默認(rèn):None
)bucket_cap_mb
控制存儲(chǔ)桶的大小(以兆字節(jié)(MB)為單位)(默認(rèn)值:25)forward
函數(shù)的返回值中包含的所有張量的自動(dòng)梯度圖。 在此圖表中未接收到漸變的參數(shù)會(huì)被搶先標(biāo)記為可以還原。 請(qǐng)注意,從模塊參數(shù)派生的所有forward
輸出必須參與計(jì)算損耗,然后再參與梯度計(jì)算。 如果沒有,該包裝器將掛起,等待 autograd 為這些參數(shù)生成梯度。 可以使用torch.Tensor.detach
將來自模塊參數(shù)的其他未使用的輸出從 autograd 圖中分離出來。 (默認(rèn):False
)True
時(shí),它使 DistributedDataParallel 自動(dòng)檢查在每次迭代的正向功能開始時(shí)是否成功發(fā)布了先前迭代的向后縮減。 通常您不需要啟用此選項(xiàng),除非您觀察到奇怪的現(xiàn)象,例如不同的等級(jí)得到不同的梯度,如果正確使用 DistributedDataParallel,則不會(huì)發(fā)生這種情況。 (默認(rèn):False
)
Variables
?DistributedDataParallel.module (Module)–要并行化的模塊
Example:
>>> torch.distributed.init_process_group(backend='nccl', world_size=4, init_method='...')
>>> net = torch.nn.DistributedDataParallel(model, pg)
no_sync()?
上下文管理器,用于禁用 DDP 進(jìn)程之間的梯度同步。 在此上下文中,梯度將累積在模塊變量上,稍后將在退出上下文的第一個(gè)向前-向后傳遞中進(jìn)行同步。
Example:
>>> ddp = torch.nn.DistributedDataParallel(model, pg)
>>> with ddp.no_sync():
... for input in inputs:
... ddp(input).backward() # no synchronization, accumulate grads
... ddp(another_input).backward() # synchronize grads
torch.nn.utils.clip_grad_norm_(parameters, max_norm, norm_type=2)?
裁剪參數(shù)可迭代的梯度范數(shù)。
范數(shù)是在所有梯度上一起計(jì)算的,就好像它們被串聯(lián)到單個(gè)矢量中一樣。 漸變就地修改。
Parameters
'inf'
。Returns
參數(shù)的總范數(shù)(視為單個(gè)向量)。
torch.nn.utils.clip_grad_value_(parameters, clip_value)?
將可迭代參數(shù)的梯度剪切為指定值。
漸變就地修改。
Parameters
torch.nn.utils.parameters_to_vector(parameters)?
將參數(shù)轉(zhuǎn)換為一個(gè)向量
Parameters
參數(shù)(可迭代 [ tensor ] )–張量的迭代器 模型的參數(shù)。
Returns
單個(gè)向量表示的參數(shù)
torch.nn.utils.vector_to_parameters(vec, parameters)?
將一個(gè)向量轉(zhuǎn)換為參數(shù)
Parameters
class torch.nn.utils.prune.BasePruningMethod?
用于創(chuàng)建新修剪技術(shù)的抽象基類。
為需要重寫諸如 compute_mask()
和 apply()
等方法的定制提供框架。
classmethod apply(module, name, *args, **kwargs)?
添加了前向預(yù)掛接,可進(jìn)行即時(shí)修剪,并根據(jù)原始張量和修剪蒙版對(duì)張量進(jìn)行重新參數(shù)化。
Parameters
module
中將對(duì)其進(jìn)行修剪的參數(shù)名稱。BasePruningMethod
子類的參數(shù)BasePruningMethod
子類的關(guān)鍵字參數(shù)apply_mask(module)?
只需處理要修剪的參數(shù)和生成的掩碼之間的乘法。 從模塊中獲取遮罩和原始張量,然后返回該張量的修剪版本。
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
輸入張量的修剪版本
Return type
pruned_tensor (Torch.Tensor)
abstract compute_mask(t, default_mask)?
計(jì)算并返回輸入張量t
的掩碼。 從基礎(chǔ)default_mask
(如果尚未修剪張量,應(yīng)為 1 的掩碼)開始,根據(jù)特定的修剪方法配方,生成一個(gè)隨機(jī)掩碼以應(yīng)用于default_mask
的頂部。
Parameters
t
相同。Returns
應(yīng)用于t
的遮罩,其亮度與t
相同
Return type
面罩(torch。張量)
prune(t, default_mask=None)?
根據(jù) compute_mask()
中指定的修剪規(guī)則,計(jì)算并返回輸入張量t
的修剪版本。
Parameters
default_mask
相同的尺寸)。Returns
張量的修剪版本t
。
remove(module)?
從模塊中刪除修剪重新參數(shù)化。 被修剪的名為name
的參數(shù)將被永久修剪,并且將從參數(shù)列表中刪除名為name+'_orig'
的參數(shù)。 同樣,從緩沖區(qū)中刪除名為name+'_mask'
的緩沖區(qū)。
Note
修剪本身不會(huì)撤消或撤消!
class torch.nn.utils.prune.PruningContainer(*args)?
容器,其中包含一系列用于迭代修剪的修剪方法。 跟蹤修剪方法的應(yīng)用順序,并處理合并的連續(xù)修剪調(diào)用。
接受 BasePruningMethod 的實(shí)例或它們的可迭代實(shí)例作為參數(shù)。
add_pruning_method(method)?
將子修剪method
添加到容器中。
Parameters
方法(BasePruningMethod 的子類)–要添加到容器的子修剪方法。
classmethod apply(module, name, *args, **kwargs)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.BasePruningMethod
BasePruningMethod
apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
compute_mask(t, default_mask)?
通過計(jì)算新的局部掩碼并返回其與default_mask
的組合來應(yīng)用最新的method
。 新的部分掩碼應(yīng)在default_mask
未歸零的條目或通道上計(jì)算。 將根據(jù)PRUNING_TYPE
(由類型處理程序處理)來計(jì)算新掩碼的張量t
的哪一部分:
- 對(duì)于“非結(jié)構(gòu)化”,蒙版將根據(jù)亂碼計(jì)算
非屏蔽條目列表;
- 對(duì)于“結(jié)構(gòu)化”,遮罩將根據(jù)非遮罩計(jì)算
張量中的通道;
- 對(duì)于“全局”,將在所有條目中計(jì)算掩碼。
Parameters
default_mask
尺寸相同)。Returns
合并了default_mask
和來自當(dāng)前修剪method
的新蒙版(與default_mask
和t
尺寸相同)的新蒙版的新蒙版。
Return type
mask (torch.Tensor)
prune(t, default_mask=None)?
根據(jù) compute_mask()
中指定的修剪規(guī)則,計(jì)算并返回輸入張量t
的修剪版本。
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.Identity?
實(shí)用修剪方法,不修剪任何單位,而是用一個(gè)“ 1”的掩碼生成修剪參數(shù)。
classmethod apply(module, name)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
prune(t, default_mask=None)?
根據(jù)compute_mask()
中指定的修剪規(guī)則,計(jì)算并返回輸入張量t
的修剪版本。
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.RandomUnstructured(amount)?
在張量中隨機(jī)修剪(當(dāng)前未修剪)單位。
Parameters
module
on which pruning will act.float
,則應(yīng)在 0.0 到 1.0 之間,并且代表要修剪的參數(shù)的分?jǐn)?shù)。 如果int
,則表示要修剪的參數(shù)的絕對(duì)數(shù)量。classmethod apply(module, name, amount)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
prune(t, default_mask=None)?
Computes and returns a pruned version of input tensor t
according to the pruning rule specified in compute_mask()
.
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.L1Unstructured(amount)?
通過將具有最低 L1 范數(shù)的單元?dú)w零,在張量中修剪(當(dāng)前未修剪的)單元。
Parameters
amount (python:int or python:float) – quantity of parameters to prune. If float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.
classmethod apply(module, name, amount)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
prune(t, default_mask=None)?
Computes and returns a pruned version of input tensor t
according to the pruning rule specified in compute_mask()
.
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.RandomStructured(amount, dim=-1)?
在張量中隨機(jī)修剪整個(gè)(當(dāng)前未修剪的)通道。
Parameters
float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.classmethod apply(module, name, amount, dim=-1)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
compute_mask(t, default_mask)?
計(jì)算并返回輸入張量t
的掩碼。 從基礎(chǔ)default_mask
(如果尚未修剪張量,應(yīng)為 1 的掩碼)開始,通過沿張量的指定暗部隨機(jī)清零通道,生成隨機(jī)掩碼以應(yīng)用于default_mask
頂部 。
Parameters
t
.Returns
mask to apply to t
, of same dims as t
Return type
mask (torch.Tensor)
Raises
IndexError –如果self.dim >= len(t.shape)
prune(t, default_mask=None)?
根據(jù) compute_mask()
中指定的修剪規(guī)則,計(jì)算并返回輸入張量t
的修剪版本。
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.LnStructured(amount, n, dim=-1)?
根據(jù)張量的 Ln 范數(shù)在張量中修剪整個(gè)(當(dāng)前未修剪的)通道。
Parameters
float
,則應(yīng)在 0.0 到 1.0 之間,并且代表要修剪的參數(shù)的分?jǐn)?shù)。 如果int
,則表示要修剪的參數(shù)的絕對(duì)數(shù)量。torch.norm()
中參數(shù)p
的條目。classmethod apply(module, name, amount, n, dim)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.p
in torch.norm()
.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
compute_mask(t, default_mask)?
計(jì)算并返回輸入張量t
的掩碼。 從基本default_mask
(如果尚未修剪張量,應(yīng)為 1 的掩碼)開始,通過將沿指定的暗角(具有最低 Ln 的通道)歸零,生成一個(gè)掩碼以應(yīng)用于default_mask
頂部 -規(guī)范。
Parameters
t
相同。Returns
mask to apply to t
, of same dims as t
Return type
mask (torch.Tensor)
Raises
IndexError – if self.dim >= len(t.shape)
prune(t, default_mask=None)?
根據(jù) compute_mask()
中指定的修剪規(guī)則,計(jì)算并返回輸入張量t
的修剪版本。
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
class torch.nn.utils.prune.CustomFromMask(mask)?
classmethod apply(module, name, mask)?
Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.
Parameters
module
on which pruning will act.apply_mask(module)?
Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.
Parameters
module (nn.Module) – module containing the tensor to prune
Returns
pruned version of the input tensor
Return type
pruned_tensor (torch.Tensor)
prune(t, default_mask=None)?
Computes and returns a pruned version of input tensor t
according to the pruning rule specified in compute_mask()
.
Parameters
default_mask
).Returns
pruned version of tensor t
.
remove(module)?
Removes the pruning reparameterization from a module. The pruned parameter named name
remains permanently pruned, and the parameter named name+'_orig'
is removed from the parameter list. Similarly, the buffer named name+'_mask'
is removed from the buffers.
Note
Pruning itself is NOT undone or reversed!
torch.nn.utils.prune.identity(module, name)?
將修剪重新參數(shù)化應(yīng)用于與module
中稱為name
的參數(shù)相對(duì)應(yīng)的張量,而無需實(shí)際修剪任何單位。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)name
的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Note
掩碼是一個(gè)張量。
Parameters
module
on which pruning will act.Returns
輸入模塊的修改(即修剪)版本
Return type
模塊 (nn.Module)
Examples
>>> m = prune.identity(nn.Linear(2, 3), 'bias')
>>> print(m.bias_mask)
tensor([1., 1., 1.])
torch.nn.utils.prune.random_unstructured(module, name, amount)?
通過刪除隨機(jī)選擇的(當(dāng)前未修剪的)單位的指定amount
來修剪與module
中稱為name
的參數(shù)相對(duì)應(yīng)的張量。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)<cite>名稱</cite>的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.Returns
modified (i.e. pruned) version of the input module
Return type
module (nn.Module)
Examples
>>> m = prune.random_unstructured(nn.Linear(2, 3), 'weight', amount=1)
>>> torch.sum(m.weight_mask == 0)
tensor(1)
torch.nn.utils.prune.l1_unstructured(module, name, amount)?
通過刪除 L1 范數(shù)最低的(當(dāng)前未修剪)單位的指定<cite>量</cite>,修剪與module
中稱為name
的參數(shù)相對(duì)應(yīng)的張量。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)name
的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.Returns
modified (i.e. pruned) version of the input module
Return type
module (nn.Module)
Examples
>>> m = prune.l1_unstructured(nn.Linear(2, 3), 'weight', amount=0.2)
>>> m.state_dict().keys()
odict_keys(['bias', 'weight_orig', 'weight_mask'])
torch.nn.utils.prune.random_structured(module, name, amount, dim)?
通過沿著隨機(jī)選擇的指定dim
移除(當(dāng)前未修剪的)通道的指定amount
來修剪與module
中稱為name
的參數(shù)相對(duì)應(yīng)的張量。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)name
的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.Returns
modified (i.e. pruned) version of the input module
Return type
module (nn.Module)
Examples
>>> m = prune.random_structured(
nn.Linear(5, 3), 'weight', amount=3, dim=1
)
>>> columns_pruned = int(sum(torch.sum(m.weight, dim=0) == 0))
>>> print(columns_pruned)
3
torch.nn.utils.prune.ln_structured(module, name, amount, n, dim)?
通過沿著具有最低 L''n`范數(shù)的指定
dim移除(當(dāng)前未修剪的)通道的指定
amount來修剪與
module中稱為
name的參數(shù)相對(duì)應(yīng)的張量。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為
name+'_mask'的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)
name的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)
name,而原始(未修剪)參數(shù)存儲(chǔ)在名為
name+'_orig'`的新參數(shù)中。
Parameters
module
on which pruning will act.float
, should be between 0.0 and 1.0 and represent the fraction of parameters to prune. If int
, it represents the absolute number of parameters to prune.p
in torch.norm()
.Returns
modified (i.e. pruned) version of the input module
Return type
module (nn.Module)
Examples
>>> m = prune.ln_structured(
nn.Conv2d(5, 3, 2), 'weight', amount=0.3, dim=1, n=float('-inf')
)
torch.nn.utils.prune.global_unstructured(parameters, pruning_method, **kwargs)?
通過應(yīng)用指定的pruning_method
全局修剪與parameters
中所有參數(shù)相對(duì)應(yīng)的張量。 通過以下方式修改模塊:1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)name
的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Parameters
nn.Module
類型,名稱必須為字符串。PRUNING_TYPE='unstructured'
的自定義函數(shù)。float
,則應(yīng)在 0.0 到 1.0 之間,并且代表要修剪的參數(shù)的分?jǐn)?shù)。 如果int
,則表示要修剪的參數(shù)的絕對(duì)數(shù)量。
Raises
TypeError –如果PRUNING_TYPE != 'unstructured'
Note
由于除非通過參數(shù)的大小對(duì)規(guī)范進(jìn)行規(guī)范化,否則全局結(jié)構(gòu)化修剪沒有多大意義,因此我們現(xiàn)在將全局修剪的范圍限制為非結(jié)構(gòu)化方法。
Examples
>>> net = nn.Sequential(OrderedDict([
('first', nn.Linear(10, 4)),
('second', nn.Linear(4, 1)),
]))
>>> parameters_to_prune = (
(net.first, 'weight'),
(net.second, 'weight'),
)
>>> prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=10,
)
>>> print(sum(torch.nn.utils.parameters_to_vector(net.buffers()) == 0))
tensor(10, dtype=torch.uint8)
torch.nn.utils.prune.custom_from_mask(module, name, mask)?
通過在mask
中應(yīng)用預(yù)先計(jì)算的掩碼,修剪與module
中稱為name
的參數(shù)相對(duì)應(yīng)的張量。 通過以下方式就地修改模塊(并返回修改后的模塊):1)添加一個(gè)名為name+'_mask'
的命名緩沖區(qū),該緩沖區(qū)與通過修剪方法應(yīng)用于參數(shù)name
的二進(jìn)制掩碼相對(duì)應(yīng)。 2)用已修剪版本替換參數(shù)name
,而原始(未修剪)參數(shù)存儲(chǔ)在名為name+'_orig'
的新參數(shù)中。
Parameters
module
on which pruning will act.Returns
modified (i.e. pruned) version of the input module
Return type
module (nn.Module)
Examples
>>> m = prune.custom_from_mask(
nn.Linear(5, 3), name='bias', mask=torch.Tensor([0, 1, 0])
)
>>> print(m.bias_mask)
tensor([0., 1., 0.])
torch.nn.utils.prune.remove(module, name)?
從模塊中刪除修剪重新參數(shù)化,并從前向掛鉤中刪除修剪方法。 被修剪的名為name
的參數(shù)將被永久修剪,并且將從參數(shù)列表中刪除名為name+'_orig'
的參數(shù)。 同樣,從緩沖區(qū)中刪除名為name+'_mask'
的緩沖區(qū)。
Note
Pruning itself is NOT undone or reversed!
Parameters
module
on which pruning will act.Examples
>>> m = random_pruning(nn.Linear(5, 7), name='weight', amount=0.2)
>>> m = remove_pruning(m, name='weight')
torch.nn.utils.prune.is_pruned(module)?
通過在從 BasePruningMethod
繼承的模塊中查找forward_pre_hooks
,檢查是否修剪了module
。
Parameters
模塊 (nn.Module)–已修剪或未修剪的對(duì)象
Returns
是否修剪module
的二進(jìn)制答案。
Examples
>>> m = nn.Linear(5, 7)
>>> print(prune.is_pruned(m))
False
>>> prune.random_pruning(m, name='weight', amount=0.2)
>>> print(prune.is_pruned(m))
True
torch.nn.utils.weight_norm(module, name='weight', dim=0)?
將權(quán)重歸一化應(yīng)用于給定模塊中的參數(shù)。
權(quán)重歸一化是將權(quán)重張量的大小與其方向解耦的重新參數(shù)化。 這用兩個(gè)參數(shù)替換了name
指定的參數(shù)(例如'weight'
):一個(gè)指定幅度(例如'weight_g'
)和一個(gè)指定方向(例如'weight_v'
)。 權(quán)重歸一化是通過一個(gè)掛鉤實(shí)現(xiàn)的,該掛鉤在每次forward()
調(diào)用之前從幅度和方向重新計(jì)算權(quán)重張量。
默認(rèn)情況下,使用dim=0
,將針對(duì)每個(gè)輸出通道/平面獨(dú)立計(jì)算范數(shù)。 要計(jì)算整個(gè)重量張量的范數(shù),請(qǐng)使用dim=None
。
參見 https://arxiv.org/abs/1602.07868
Parameters
Returns
帶有重量標(biāo)準(zhǔn)鉤的原始模塊
Example:
>>> m = weight_norm(nn.Linear(20, 40), name='weight')
>>> m
Linear(in_features=20, out_features=40, bias=True)
>>> m.weight_g.size()
torch.Size([40, 1])
>>> m.weight_v.size()
torch.Size([40, 20])
torch.nn.utils.remove_weight_norm(module, name='weight')?
從模塊中刪除權(quán)重歸一化重新參數(shù)化。
Parameters
例
>>> m = weight_norm(nn.Linear(20, 40))
>>> remove_weight_norm(m)
torch.nn.utils.spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None)?
將頻譜歸一化應(yīng)用于給定模塊中的參數(shù)。
頻譜歸一化通過使用權(quán)重矩陣計(jì)算的權(quán)重矩陣的頻譜范數(shù)重新調(diào)整權(quán)重張量,從而穩(wěn)定了生成對(duì)抗網(wǎng)絡(luò)(GAN)中鑒別器(批評(píng)家)的訓(xùn)練。 如果權(quán)重張量的尺寸大于 2,則在冪迭代方法中將其重塑為 2D 以獲得頻譜范數(shù)。 這是通過一個(gè)掛鉤實(shí)現(xiàn)的,該掛鉤在每次
forward()
調(diào)用之前計(jì)算頻譜范數(shù)并重新調(diào)整權(quán)重。
請(qǐng)參閱生成對(duì)抗網(wǎng)絡(luò)的頻譜歸一化。
Parameters
0
,除了模塊 是 ConvTranspose {1,2,3} d 的實(shí)例,當(dāng)它是1
時(shí)Returns
帶有頻譜范數(shù)掛鉤的原始模塊
Example:
>>> m = spectral_norm(nn.Linear(20, 40))
>>> m
Linear(in_features=20, out_features=40, bias=True)
>>> m.weight_u.size()
torch.Size([40])
torch.nn.utils.remove_spectral_norm(module, name='weight')?
從模塊中刪除頻譜歸一化重新參數(shù)化。
Parameters
Example
>>> m = spectral_norm(nn.Linear(40, 10))
>>> remove_spectral_norm(m)
torch.nn.utils.rnn.PackedSequence(data, batch_sizes=None, sorted_indices=None, unsorted_indices=None)?
保存打包序列的batch_sizes
的數(shù)據(jù)和列表。
所有 RNN 模塊都將打包序列作為輸入。
Note
此類的實(shí)例永遠(yuǎn)不要手動(dòng)創(chuàng)建。 它們應(yīng)通過 pack_padded_sequence()
之類的函數(shù)實(shí)例化。
批次大小代表批次中每個(gè)序列步驟的數(shù)量元素,而不是傳遞給 pack_padded_sequence()
的變化序列長(zhǎng)度。 例如,給定數(shù)據(jù)abc
和x
, PackedSequence
將包含數(shù)據(jù)axbc
和batch_sizes=[2,1,1]
。
Variables
PackedSequence
由序列構(gòu)建。Note
data
可以在任意設(shè)備上和任意 dtype 上。 sorted_indices
和unsorted_indices
必須是與data
在同一設(shè)備上的torch.int64
張量。
但是,batch_sizes
應(yīng)該始終是 CPU torch.int64
張量。
這個(gè)不變量在整個(gè) PackedSequence
類中保持不變,并且所有在 PyTorch 中構(gòu)造<cite>:class:PackedSequence</cite> 的函數(shù)都保持不變(即,它們僅傳遞符合此約束的張量)。
torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False, enforce_sorted=True)?
打包一個(gè) Tensor,其中包含可變長(zhǎng)度的填充序列。
input
的大小可以為T x B x *
,其中 <cite>T</cite> 是最長(zhǎng)序列的長(zhǎng)度(等于lengths[0]
),B
是批處理大小,*
是任意數(shù)量的尺寸 (包括 0)。 如果batch_first
為True
,則預(yù)期為B x T x *
input
。
對(duì)于未排序的序列,請(qǐng)使用 <cite>force_sorted = False</cite> 。 如果enforce_sorted
為True
,則序列應(yīng)按長(zhǎng)度降序排列,即input[:,0]
應(yīng)為最長(zhǎng)序列,而input[:,B-1]
為最短序列。 <cite>forcen_sorted = True</cite> 僅對(duì)于 ONNX 導(dǎo)出是必需的。
Note
此函數(shù)接受至少具有二維的任何輸入。 您可以將其應(yīng)用于包裝標(biāo)簽,并與它們一起使用 RNN 的輸出直接計(jì)算損失。 可以通過訪問 .data
屬性來從 PackedSequence
對(duì)象中檢索張量。
Parameters
True
,則輸入應(yīng)為B x T x *
格式。True
,則輸入應(yīng)包含按長(zhǎng)度降序排列的序列 。 如果為False
,則不檢查此條件。 默認(rèn)值:True
。Returns
PackedSequence
對(duì)象
torch.nn.utils.rnn.pad_packed_sequence(sequence, batch_first=False, padding_value=0.0, total_length=None)?
填充打包的可變長(zhǎng)度序列批次。
這是 pack_padded_sequence()
的逆運(yùn)算。
返回的 Tensor 數(shù)據(jù)大小為T x B x *
,其中 <cite>T</cite> 是最長(zhǎng)序列的長(zhǎng)度, <cite>B</cite> 是批處理大小。 如果batch_first
為 True,則數(shù)據(jù)將轉(zhuǎn)置為B x T x *
格式。
批處理元素將按其長(zhǎng)度順序減小。
Note
total_length
可用于在包裹在 DataParallel
中的 Module
中實(shí)現(xiàn)pack sequence -> recurrent network -> unpack sequence
模式。 有關(guān)詳細(xì)信息,請(qǐng)參見此常見問題解答部分。
Parameters
True
,則輸出為B x T x *
格式。None
,則輸出將被填充為長(zhǎng)度total_length
。 如果total_length
小于sequence
中的最大序列長(zhǎng)度,則此方法將拋出ValueError
。Returns
張量元組包含填充的序列,張量包含批處理中每個(gè)序列的長(zhǎng)度列表。
torch.nn.utils.rnn.pad_sequence(sequences, batch_first=False, padding_value=0)?
用padding_value
填充可變長(zhǎng)度張量的列表
pad_sequence
沿新維度堆疊張量列表,并將它們填充為相等的長(zhǎng)度。 例如,如果輸入是大小為L x *
的序列列表,并且 batch_first 為 False,否則為T x B x *
。
<cite>B</cite> 是批處理大小。 它等于sequences
中的元素?cái)?shù)。 <cite>T</cite> 是最長(zhǎng)序列的長(zhǎng)度。 <cite>L</cite> 是序列的長(zhǎng)度。 <cite>*</cite> 是任意數(shù)量的尾隨尺寸,包括無。
Example
>>> from torch.nn.utils.rnn import pad_sequence
>>> a = torch.ones(25, 300)
>>> b = torch.ones(22, 300)
>>> c = torch.ones(15, 300)
>>> pad_sequence([a, b, c]).size()
torch.Size([25, 3, 300])
Note
此函數(shù)返回張量為T x B x *
或B x T x *
的張量,其中 <cite>T</cite> 是最長(zhǎng)序列的長(zhǎng)度。 此函數(shù)假定序列中所有張量的尾隨尺寸和類型相同。
Parameters
B x T x *
,否則為T x B x *
Returns
如果batch_first
為False
,則大小為T x B x *
的張量。 否則大小為B x T x *
的張量
torch.nn.utils.rnn.pack_sequence(sequences, enforce_sorted=True)?
打包可變長(zhǎng)度張量的列表
sequences
應(yīng)該是大小為L x *
的張量的列表,其中 <cite>L</cite> 是序列的長(zhǎng)度, <cite>*</cite> 是任意數(shù)量的尾隨尺寸,包括零。
對(duì)于未排序的序列,請(qǐng)使用 <cite>force_sorted = False</cite> 。 如果enforce_sorted
為True
,則序列應(yīng)按長(zhǎng)度減小的順序排序。 enforce_sorted = True
僅對(duì)于 ONNX 導(dǎo)出是必需的。
Example
>>> from torch.nn.utils.rnn import pack_sequence
>>> a = torch.tensor([1,2,3])
>>> b = torch.tensor([4,5])
>>> c = torch.tensor([6])
>>> pack_sequence([a, b, c])
PackedSequence(data=tensor([ 1, 4, 6, 2, 5, 3]), batch_sizes=tensor([ 3, 2, 1]))
Parameters
True
,則檢查輸入是否包含按長(zhǎng)度排序的降序序列。 如果為False
,則不檢查此條件。 默認(rèn)值:True
。Returns
a PackedSequence
object
class torch.nn.Flatten(start_dim=1, end_dim=-1)?
將連續(xù)范圍的暗角展平為張量。 用于Sequential
。 :param start_dim:首先變暗到變平(默認(rèn)= 1)。 :param end_dim:最后變暗到變平(默認(rèn)= -1)。
Shape:
Examples::
>>> m = nn.Sequential(
>>> nn.Conv2d(1, 32, 5, 1, 1),
>>> nn.Flatten()
>>> )
量化是指用于執(zhí)行計(jì)算并以低于浮點(diǎn)精度的位寬存儲(chǔ)張量的技術(shù)。 PyTorch 支持每個(gè)張量和每個(gè)通道非對(duì)稱線性量化。 要了解更多如何在 PyTorch 中使用量化函數(shù)的信息,請(qǐng)參閱[量化]文檔。
更多建議: