MCPcopy Create free account
hub / github.com/MotrixLab/ADHMR / positionalencoding2d

Function positionalencoding2d

ADHMR/lib/models/hyponet.py:35–57  ·  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 from https://github.com/wzlxjtu/PositionalEncoding2D

(d_model, height, width)

Source from the content-addressed store, hash-verified

33 return emb
34
35def positionalencoding2d(d_model, height, width):
36 """
37 :param d_model: dimension of the model
38 :param height: height of the positions
39 :param width: width of the positions
40 :return: d_model*height*width position matrix
41 from https://github.com/wzlxjtu/PositionalEncoding2D
42 """
43 if d_model % 4 != 0:
44 raise ValueError("Cannot use sin/cos positional encoding with "
45 "odd dimension (got dim={:d})".format(d_model))
46 pe = torch.zeros(d_model, height, width)
47 # Each dimension use half of d_model
48 d_model = int(d_model / 2)
49 div_term = torch.exp(torch.arange(0., d_model, 2) *
50 -(math.log(10000.0) / d_model))
51 pos_w = torch.arange(0., width).unsqueeze(1)
52 pos_h = torch.arange(0., height).unsqueeze(1)
53 pe[0:d_model:2, :, :] = torch.sin(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
54 pe[1:d_model:2, :, :] = torch.cos(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
55 pe[d_model::2, :, :] = torch.sin(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
56 pe[d_model + 1::2, :, :] = torch.cos(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
57 return pe
58def nonlinearity(x):
59 # swish
60 return x*torch.sigmoid(x)

Callers 1

forwardMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected