Normalize.py 542 Bytes
Newer Older
Rayyyyy's avatar
Rayyyyy committed
1
from typing import Dict
Rayyyyy's avatar
Rayyyyy committed
2

Rayyyyy's avatar
Rayyyyy committed
3
import torch.nn.functional as F
Rayyyyy's avatar
Rayyyyy committed
4
from torch import Tensor, nn
Rayyyyy's avatar
Rayyyyy committed
5
6
7


class Normalize(nn.Module):
Rayyyyy's avatar
Rayyyyy committed
8
    """This layer normalizes embeddings to unit length"""
Rayyyyy's avatar
Rayyyyy committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    def __init__(self):
        super(Normalize, self).__init__()

    def forward(self, features: Dict[str, Tensor]):
        features.update({"sentence_embedding": F.normalize(features["sentence_embedding"], p=2, dim=1)})
        return features

    def save(self, output_path):
        pass

    @staticmethod
    def load(input_path):
        return Normalize()