-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreshape_layer.py
More file actions
26 lines (21 loc) · 910 Bytes
/
Copy pathreshape_layer.py
File metadata and controls
26 lines (21 loc) · 910 Bytes
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
import torch
import torch.nn as nn
from functools import reduce
def prod(iterable, start=1):
return reduce(lambda a, b: a * b, iterable, start)
class Reshape(nn.Module):
def __init__(self, input_shape, output_shape):
super(Reshape, self).__init__()
assert isinstance(input_shape, tuple) or isinstance(input_shape, torch.Size)
assert isinstance(output_shape, tuple) or isinstance(output_shape, torch.Size)
assert prod(input_shape) == prod(output_shape)
self._input_shape = input_shape
self._output_shape = output_shape
def forward(self, x):
assert isinstance(x, torch.Tensor)
B = x.shape[0]
if x.shape[1:] != self._input_shape:
raise Exception(
f"Reshape: expected input size {self._input_shape} but got {tuple(x.shape[1:])} instead"
)
return x.view(B, *self._output_shape)