pytorch nn functional

torch.nn.functional

该包提供了很多网络函数

convoludion functions

conv2d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

inputs = Variable(torch.randn(64,3,32,32))

filters1 = Variable(torch.randn(16,3,3,3))
output1 = F.conv2d(inputs,filters1)
print(output1.size())

filters2 = Variable(torch.randn(16,3,3,3))
output2 = F.conv2d(inputs,filters2,padding=1)
print(output2.size())

输出

torch.Size([64, 16, 30, 30])
torch.Size([64, 16, 32, 32])

relu functions

pooling functions

dropout functions

例子

1
2
3
4
5
6
7
8
import torch
import torch.nn.functional as F

x = torch.randn(1, 28, 28)
y = F.dropout(x, 0.5, True)
y = F.dropout2d(x, 0.5)

print(y)

注意$2$中说的问题,不过可能已经被改正了,注意一些就是了。

linear functions

loss functions

参考文献

1.https://pytorch.org/docs/stable/nn.html#torch-nn-functional
2.https://pytorch.org/docs/stable/nn.html#torch-nn-functional