pytorch dropout自动调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function

import torch.nn as nn
import torch


class Linear(nn.Module):
def __init__(self, p_dropout=1):
super(Linear, self).__init__()
self.dropout = nn.Dropout(p_dropout)

def forward(self, x):
y = self.dropout(x)
return y


net = Linear()
a = torch.ones(4)

net.train()
print(net(a))

net.eval()
b = torch.ones(4)
print(net(b))
1
2
3
4
5
6
7
8
9
10
11
12
13
Variable containing:
0
0
0
0
[torch.FloatTensor of size 4]

Variable containing:
1
1
1
1
[torch.FloatTensor of size 4]
请作者喝一杯咖啡☕️