MCPcopy Create free account
hub / github.com/BIT-MCS/DRL-eFresh / SpatialBasis

Class SpatialBasis

methods/SpatialAttGRU.py:121–148  ·  view source on GitHub ↗

NOTE: The `height` and `weight` depend on the inputs' size and its resulting size after being processed by the vision network.

Source from the content-addressed store, hash-verified

119
120
121class SpatialBasis:
122 # TODO: Implement Spatial.
123 """
124 NOTE: The `height` and `weight` depend on the inputs' size and its resulting size
125 after being processed by the vision network.
126 """
127
128 def __init__(self, height=8, width=8, channels=64):
129 h, w, d = height, width, channels
130
131 p_h = torch.mul(torch.arange(1, h + 1).unsqueeze(1).float(), torch.ones(1, w).float()) * (np.pi / h)
132 p_w = torch.mul(torch.ones(h, 1).float(), torch.arange(1, w + 1).unsqueeze(0).float()) * (np.pi / w)
133
134 # NOTE: I didn't quite see how U,V = 4 made sense given that the authors form the spatial
135 # basis by taking the outer product of the values. Still, I think what I have is aligned with what
136 # they did, but I am less confident in this step.
137 U = V = 8 # size of U, V.
138 u_basis = v_basis = torch.arange(1, U + 1).unsqueeze(0).float()
139 a = torch.mul(p_h.unsqueeze(2), u_basis)
140 b = torch.mul(p_w.unsqueeze(2), v_basis)
141 out = torch.einsum('hwu,hwv->hwuv', torch.cos(a), torch.cos(b)).reshape(h, w, d)
142 self.S = out
143
144 def __call__(self, X):
145 # Stack the spatial bias (for each batch) and concat to the input.
146 batch_size = X.size()[0]
147 S = torch.stack([self.S] * batch_size).to(X.device)
148 return torch.cat([X, S], dim=3)
149
150
151def spatial_softmax(A):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected