Ejemplo n.º 1
0
    def construct(self, inputs, hx):
        hy, cy = lstm_cell(inputs, hx, self.weight_ih, self.weight_hh,
                           self.bias_ih, self.bias_hh)
        if self.cell_clip is not None:
            cy = P.clip_by_value(cy, -self.cell_clip, self.cell_clip)
        if self.proj_size is not None:
            hy = self.matmul(hy, self.proj_weight)
            if self.proj_clip is not None:
                hy = P.clip_by_value(hy, -self.proj_clip, self.proj_clip)

        return hy, cy
Ejemplo n.º 2
0
    def construct(self, x):
        """construct"""
        x_averaged = self.avg_pool(x, (2, 3))
        y = self.fc1(x_averaged)
        y = self.relu(y)
        y = self.fc2(y)

        mask_before = self.relu(y)
        mask_before = ops.clip_by_value(mask_before, self.clamp_min,
                                        self.clamp_max)
        tmp = ops.Greater()(mask_before, self.thre)
        mask = mask_before * tmp

        return mask
Ejemplo n.º 3
0
    def construct(self):
        """construct RelaPosMatrixGenerator"""
        range_vec_row_out = self.cast(ops.tuple_to_array(ops.make_range(self._length)), mstype.int32)
        range_vec_col_out = self.range_mat(range_vec_row_out, (self._length, -1))
        tile_row_out = self.tile(range_vec_row_out, (self._length,))
        tile_col_out = self.tile(range_vec_col_out, (1, self._length))
        range_mat_out = self.range_mat(tile_row_out, (self._length, self._length))
        transpose_out = self.range_mat(tile_col_out, (self._length, self._length))
        distance_mat = self.sub(range_mat_out, transpose_out)

        distance_mat_clipped = ops.clip_by_value(distance_mat,
                                                 self._min_relative_position,
                                                 self._max_relative_position)

        # Shift values to be >=0. Each integer still uniquely identifies a
        # relative position difference.
        final_mat = distance_mat_clipped + self._max_relative_position
        return final_mat
Ejemplo n.º 4
0
def _clip_grad(clip_type, clip_value, grad):
    """
    Clip gradients.

    Inputs:
        clip_type (int): The way to clip, 0 for 'value', 1 for 'norm'.
        clip_value (float): Specifies how much to clip.
        grad (tuple[Tensor]): Gradients.

    Outputs:
        tuple[Tensor], clipped gradients.
    """
    if clip_type != 0 and clip_type != 1:
        return grad
    dt = ops.dtype(grad)
    if clip_type == 0:
        new_grad = ops.clip_by_value(grad, ops.cast(ops.tuple_to_array((-clip_value,)), dt),
                                     ops.cast(ops.tuple_to_array((clip_value,)), dt))
    else:
        new_grad = nn.ClipByNorm()(grad, ops.cast(ops.tuple_to_array((clip_value,)), dt))
    return new_grad
Ejemplo n.º 5
0
 def construct(self, x):
     x = ops.clip_by_value(x, self._min, self._max)
     x = self.round(x)
     return x