-
Notifications
You must be signed in to change notification settings - Fork 0
/
gated_linear_unit.py
59 lines (45 loc) · 1.43 KB
/
gated_linear_unit.py
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
59
import torch
import torch.nn as nn
from utils.util_func import maybe_kwargs
from utils.network_block import NetworkBlock
class GatedLinearUnit(nn.Module):
def __init__(
self,
in_features,
out_features,
gate_kwargs=None,
block_kwargs=None,
):
super(GatedLinearUnit, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.gate = NetworkBlock(
in_features=in_features, out_features=out_features,
**maybe_kwargs(gate_kwargs, defaults=dict(
activation=nn.Sigmoid
))
)
self.block = NetworkBlock(
in_features=in_features, out_features=out_features,
**maybe_kwargs(block_kwargs, defaults=dict(
self_normalizing=True,
activation='auto',
use_layer_norm=True,
))
)
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
return self.block(inputs) * self.gate(inputs)
def main():
import numpy as np
batch_size = 1
feature_size = 4
out_features = 3
test_tensor = torch.tensor(np.random.random((batch_size, feature_size)), dtype=torch.float)
glu = GatedLinearUnit(
in_features=feature_size,
out_features=out_features,
)
print(test_tensor.shape)
print(glu(test_tensor).shape)
if __name__ == '__main__':
main()