forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.pooling.py
More file actions
58 lines (45 loc) · 1.27 KB
/
05.pooling.py
File metadata and controls
58 lines (45 loc) · 1.27 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description:
"""
import torch
from torch import nn
def pool2d(X, pool_size, mode="max"):
X = X.float()
p_h, p_w = pool_size
Y = torch.zeros(X.shape[0] - p_h + 1, X.shape[1] - p_w + 1)
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
if mode == 'max':
Y[i, j] = X[i:i + p_h, j:j + p_w].max()
elif mode == 'mean':
Y[i, j] = X[i:i + p_h, j:j + p_w].mean()
return Y
X = torch.tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
c = pool2d(X, (2, 2))
print(c)
# mean
c = pool2d(X, (2, 2), mode='mean')
print(c)
# other, mean
X = torch.tensor([[0, 1, 2], [3, 4, 5], [6, 7, 9]])
c = pool2d(X, (2, 2), mode='mean')
print(c)
X = torch.arange(32, dtype=torch.float).view((1, 2, 4, 4)) # view, bathsize, channels, height, width
print(X)
X = torch.arange(32, dtype=torch.float).view((2, 1, 4, 4)) # view, bathsize, channels, height, width
print(X)
X = torch.arange(16, dtype=torch.float).view((1, 1, 4, 4)) # view, bathsize, channels, height, width
print(X)
pool2d = nn.MaxPool2d(3)
c = pool2d(X)
print(c)
pool2d = nn.MaxPool2d((3, 3))
c = pool2d(X)
print(c)
# 多通道
X = torch.cat((X, X + 1), dim=1)
print(X)
pool2d = nn.MaxPool2d(2)
print(pool2d(X))