MCPcopy
hub / github.com/PaddlePaddle/PaddleOCR / positionalencoding2d

Function positionalencoding2d

ppocr/modeling/transforms/tbsrn.py:39–73  ·  view source on GitHub ↗

:param d_model: dimension of the model :param height: height of the positions :param width: width of the positions :return: d_model*height*width position matrix

(d_model, height, width)

Source from the content-addressed store, hash-verified

37
38
39def positionalencoding2d(d_model, height, width):
40 """
41 :param d_model: dimension of the model
42 :param height: height of the positions
43 :param width: width of the positions
44 :return: d_model*height*width position matrix
45 """
46 if d_model % 4 != 0:
47 raise ValueError(
48 "Cannot use sin/cos positional encoding with "
49 "odd dimension (got dim={:d})".format(d_model)
50 )
51 pe = paddle.zeros([d_model, height, width])
52 # Each dimension use half of d_model
53 d_model = int(d_model / 2)
54 div_term = paddle.exp(
55 paddle.arange(0.0, d_model, 2, dtype="int64") * -(math.log(10000.0) / d_model)
56 )
57 pos_w = paddle.arange(0.0, width, dtype="float32").unsqueeze(1)
58 pos_h = paddle.arange(0.0, height, dtype="float32").unsqueeze(1)
59
60 pe[0:d_model:2, :, :] = (
61 paddle.sin(pos_w * div_term).transpose([1, 0]).unsqueeze(1).tile([1, height, 1])
62 )
63 pe[1:d_model:2, :, :] = (
64 paddle.cos(pos_w * div_term).transpose([1, 0]).unsqueeze(1).tile([1, height, 1])
65 )
66 pe[d_model::2, :, :] = (
67 paddle.sin(pos_h * div_term).transpose([1, 0]).unsqueeze(2).tile([1, 1, width])
68 )
69 pe[d_model + 1 :: 2, :, :] = (
70 paddle.cos(pos_h * div_term).transpose([1, 0]).unsqueeze(2).tile([1, 1, width])
71 )
72
73 return pe
74
75
76class FeatureEnhancer(nn.Layer):

Callers 1

forwardMethod · 0.85

Calls 2

formatMethod · 0.80
logMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…