Exemplo n.º 1
0
    def __init__(self,
                 vocab: List[str],
                 hidden_features: int,
                 enc_layers=1,
                 dec_layers=1,
                 nhead=1,
                 dropout=0.1):
        super().__init__()

        self.letter_to_token, _ = tokenize_vocab(vocab)

        self.pos_encoder = PositionalEncoding(hidden_features, dropout)
        self.decoder = nn.Embedding(len(vocab), hidden_features)
        self.pos_decoder = PositionalEncoding(hidden_features, dropout)
        self.transformer = nn.Transformer(d_model=hidden_features,
                                          nhead=nhead,
                                          num_encoder_layers=enc_layers,
                                          num_decoder_layers=dec_layers,
                                          dim_feedforward=hidden_features * 4,
                                          dropout=dropout,
                                          activation='relu')

        self.src_mask = None
        self.trg_mask = None
        self.memory_mask = None
        self.fc = nn.Linear(hidden_features, len(vocab), bias=False)
        self._initialize_weights()
    def __init__(self, hidden_features, vocab, dropout_p=0.1):
        super(BahdanauAttnDecoderRNN, self).__init__()
        self.hidden_size = hidden_features
        self.output_size = len(vocab)
        self.letter_to_token, _ = tokenize_vocab(vocab)
        self.dropout_p = dropout_p

        self.embedding = nn.Linear(self.output_size,
                                   self.hidden_size,
                                   bias=False)
        self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
        self.dropout = nn.Dropout(self.dropout_p)
        self.gru = nn.GRU(self.hidden_size, self.hidden_size)
        self.out = nn.Linear(self.hidden_size, self.output_size)
        self.vat = nn.Linear(hidden_features, 1)
Exemplo n.º 3
0
    def __init__(
        self,
        vocab: List[str],
        sequence_size: int,
        backbone_cfg: CfgT = dict(),
        encoder_cfg: Optional[CfgT] = None,
        decoder_cfg: Optional[CfgT] = None,
        head_cfg: Optional[CfgT] = None,
        loss_cfgs: Optional[CfgT] = None,
        metric_cfgs: List[CfgT] = list(),
        train_transforms_cfg: CfgT = dict(),
        val_transforms_cfg: CfgT = dict(),
        train_dataset_cfg: CfgT = dict(),
        val_dataset_cfg: CfgT = dict(),
        train_dataloader_cfg: CfgT = dict(),
        val_dataloader_cfg: CfgT = dict(),
        optimizer_cfg: CfgT = dict(),
        scheduler_cfg: Optional[CfgT] = None,
        scheduler_update_params: Optional[CfgT] = None,
    ):
        super(BaseOCRPLModule, self).__init__()
        self.backbone_cfg = backbone_cfg
        self.head_cfg = head_cfg
        self.encoder_cfg = encoder_cfg
        self.decoder_cfg = decoder_cfg
        self.loss_cfgs = loss_cfgs
        self.metric_cfgs = metric_cfgs
        self.train_transforms_cfg = train_transforms_cfg
        self.val_transforms_cfg = val_transforms_cfg
        self.train_dataset_cfg = train_dataset_cfg
        self.val_dataset_cfg = val_dataset_cfg
        self.train_dataloader_cfg = train_dataloader_cfg
        self.val_dataloader_cfg = val_dataloader_cfg
        self.optimizer_cfg = optimizer_cfg
        self.scheduler_cfg = scheduler_cfg
        self.scheduler_update_params = scheduler_update_params
        self.save_hyperparameters()

        self.scheduler = None  # Can be useful for LR logging in the progress bar
        self.sequence_size = sequence_size
        self.letter_to_token, self.token_to_letter = tokenize_vocab(vocab)
        self._build_models()
Exemplo n.º 4
0
 def __init__(self, vocab, seq_size):
     super().__init__(always_apply=True, p=1)
     self.letter_to_token, _ = tokenize_vocab(vocab)
     self.seq_size = seq_size
Exemplo n.º 5
0
 def __init__(self, letters):
     super().__init__(compute_on_step=True)
     _, self.token_to_letter = tokenize_vocab(letters)
     self.add_state('dists', default=torch.tensor(0.), dist_reduce_fx="sum")
     self.add_state('total_seq', default=torch.tensor(0), dist_reduce_fx="sum")