-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoders.py
36 lines (27 loc) · 1.32 KB
/
encoders.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import TransformerEncoder, TransformerEncoderLayer
Linear = nn.Linear
def get_normalized_uniform_encoder(encoder_creator):
"""
This can be used to wrap an encoder that is fed uniform samples in [0,1] and normalizes these to 0 mean and 1 std.
For example, it can be used as `encoder_creator = get_normalized_uniform_encoder(encoders.Linear)`, now this can
be initialized with `encoder_creator(feature_dim, in_dim)`.
:param encoder:
:return:
"""
return lambda in_dim, out_dim: nn.Sequential(Normalize(.5, math.sqrt(1/12)), encoder_creator(in_dim, out_dim))
class CanEmb(nn.Embedding):
def __init__(self, num_features, num_embeddings: int, embedding_dim: int, *args, **kwargs):
assert embedding_dim % num_features == 0
embedding_dim = embedding_dim // num_features
super().__init__(num_embeddings, embedding_dim, *args, **kwargs)
def forward(self, x):
x = super().forward(x)
return x.view(*x.shape[:-2], -1)
def get_Canonical(num_classes):
return lambda num_features, emsize: CanEmb(num_features, num_classes, emsize)
def get_Embedding(num_embs_per_feature=100):
return lambda num_features, emsize: EmbeddingEncoder(num_features, emsize, num_embs=num_embs_per_feature)