Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pytorch Without Pytorch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Falguni Ghosh
Pytorch Without Pytorch
Commits
248d0d2d
Commit
248d0d2d
authored
1 year ago
by
Falguni Ghosh
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
b49adae5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
3_RNN/Pooling.py
+51
-0
51 additions, 0 deletions
3_RNN/Pooling.py
with
51 additions
and
0 deletions
3_RNN/Pooling.py
0 → 100644
+
51
−
0
View file @
248d0d2d
from
.Base
import
BaseLayer
import
numpy
as
np
class
Pooling
(
BaseLayer
):
kernels
=
None
weights
=
None
def
__init__
(
self
,
stride_shape
,
pooling_shape
):
super
().
__init__
()
self
.
stride_shape
=
stride_shape
self
.
pooling_shape
=
pooling_shape
self
.
input_tensor
=
None
self
.
input_tensor_shape
=
None
self
.
error_tensor
=
None
self
.
max_indices
=
None
def
forward
(
self
,
input_tensor
):
self
.
input_tensor
=
input_tensor
self
.
input_tensor_shape
=
input_tensor
.
shape
op_tensor_num_rows
=
int
(
np
.
floor
((
input_tensor
.
shape
[
2
]
-
self
.
pooling_shape
[
0
])
/
self
.
stride_shape
[
0
]
+
1
))
op_tensor_num_cols
=
int
(
np
.
floor
((
input_tensor
.
shape
[
3
]
-
self
.
pooling_shape
[
1
])
/
self
.
stride_shape
[
1
]
+
1
))
output_tensor
=
np
.
empty
((
input_tensor
.
shape
[
0
],
input_tensor
.
shape
[
1
],
op_tensor_num_rows
,
op_tensor_num_cols
))
max_indices
=
[]
for
i
in
np
.
arange
(
input_tensor
.
shape
[
0
]):
# iterate through batch
for
j
in
np
.
arange
(
input_tensor
.
shape
[
1
]):
# iterate through channels
for
k
in
np
.
arange
(
output_tensor
.
shape
[
2
]):
for
l
in
np
.
arange
(
output_tensor
.
shape
[
3
]):
subblock_mat
=
input_tensor
[
i
][
j
][
k
*
self
.
stride_shape
[
0
]
:
k
*
self
.
stride_shape
[
0
]
+
self
.
pooling_shape
[
0
],
l
*
self
.
stride_shape
[
1
]
:
l
*
self
.
stride_shape
[
1
]
+
self
.
pooling_shape
[
1
]]
output_tensor
[
i
][
j
][
k
][
l
]
=
np
.
max
(
subblock_mat
)
c
,
d
=
np
.
unravel_index
(
subblock_mat
.
argmax
(),
subblock_mat
.
shape
)
max_indices
.
append
([
i
,
j
,
k
*
self
.
stride_shape
[
0
]
+
c
,
l
*
self
.
stride_shape
[
1
]
+
d
])
self
.
max_indices
=
max_indices
return
output_tensor
def
backward
(
self
,
error_tensor
):
self
.
error_tensor
=
error_tensor
output_error_tensor
=
np
.
zeros
(
self
.
input_tensor_shape
)
for
i
in
np
.
arange
(
error_tensor
.
shape
[
0
]):
# iterate through batch
for
j
in
np
.
arange
(
error_tensor
.
shape
[
1
]):
# iterate through channels
for
k
in
np
.
arange
(
error_tensor
.
shape
[
2
]):
for
l
in
np
.
arange
(
error_tensor
.
shape
[
3
]):
index
=
self
.
max_indices
[
0
]
del
(
self
.
max_indices
[
0
])
output_error_tensor
[
index
[
0
]][
index
[
1
]][
index
[
2
]][
index
[
3
]]
+=
error_tensor
[
i
][
j
][
k
][
l
]
return
output_error_tensor
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment