Add position encodings to :obj:`inputs`. Args: inputs: The inputs to encode. position: The single position to encode, to use when this layer is called step by step. Returns: A ``tf.Tensor`` whose shape depends on the configured ``reducer``.
(self, inputs, position=None)
| 32 | self.reducer = reducer |
| 33 | |
| 34 | def call(self, inputs, position=None): # pylint: disable=arguments-differ |
| 35 | """Add position encodings to :obj:`inputs`. |
| 36 | Args: |
| 37 | inputs: The inputs to encode. |
| 38 | position: The single position to encode, to use when this layer is called |
| 39 | step by step. |
| 40 | Returns: |
| 41 | A ``tf.Tensor`` whose shape depends on the configured ``reducer``. |
| 42 | """ |
| 43 | batch_size = tf.shape(inputs)[0] |
| 44 | timesteps = tf.shape(inputs)[1] |
| 45 | input_dim = inputs.get_shape().as_list()[-1] # return int |
| 46 | positions = tf.range(timesteps) + 1 if position is None else position |
| 47 | position_encoding = self._encode([positions], input_dim, dtype=inputs.dtype) |
| 48 | position_encoding = tf.tile(position_encoding, [batch_size, 1, 1]) |
| 49 | return self.reducer([inputs, position_encoding]) |
| 50 | |
| 51 | @abc.abstractmethod |
| 52 | def _encode(self, positions, depth, dtype): |