mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Closes https://github.com/caffe2/caffe2/pull/1316 Differential Revision: D6026557 Pulled By: Yangqing fbshipit-source-id: 95c634872ac02be721257169e38c8fead04cd66b
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
# Copyright (c) 2016-present, Facebook, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
##############################################################################
|
|
|
|
## @package position_weighted
|
|
# Module caffe2.python.layers.position_weighted
|
|
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
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
|
|
|
|
logging.basicConfig()
|
|
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})
|
|
self.tags.update({Tags.GRADIENT_FROM_PS})
|
|
|
|
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())
|