File: position_weighted.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (64 lines) | stat: -rw-r--r-- 2,066 bytes parent folder | download | duplicates (2)
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
60
61
62
63
64
## @package position_weighted
# Module caffe2.python.layers.position_weighted





import logging
import numpy as np

from caffe2.python import schema
from caffe2.python.layers.layers import (
    get_categorical_limit,
    ModelLayer,
)

from caffe2.python.layers.tags import Tags

logger = logging.getLogger(__name__)


class PositionWeighted(ModelLayer):
    def __init__(self, model, input_record, weight_optim=None,
                 name="position_weights"):
        super(PositionWeighted, self).__init__(model, name, input_record)

        assert isinstance(input_record, schema.List), "Incorrect input type"
        length_metadata = input_record.lengths.metadata
        max_length = (length_metadata.categorical_limit if length_metadata is
                      not None else None)
        if max_length is not None:
            self.shape = max_length
        else:
            self.shape = get_categorical_limit(input_record)
            logger.warning(
                '{}: categorical_limit of lengths is not available, using '
                'categorical_limit of the keys: {}'.format(
                    str(input_record.lengths()), self.shape))

        self.pos_w = self.create_param(param_name='pos_w',
                                       shape=[self.shape, ],
                                       initializer=('ConstantFill', {'value': 1.0}),
                                       optimizer=weight_optim)

        self.output_schema = schema.Struct(
            ('position_weights',
                schema.Scalar((np.float32, self.shape),
                              self.get_next_blob_reference("pos_w_gather")))
        )

        self.tags.update({Tags.HANDLE_AS_SPARSE_LAYER})

    def get_memory_usage(self):
        return self.shape

    def add_ops(self, net):
        inc_seq = net.LengthsRangeFill(
            [self.input_record.lengths()],
            self.input_record.lengths() + '_pos_w_seq'
        )

        net.Gather(
            [self.pos_w, inc_seq],
            self.output_schema.position_weights.field_blobs())