MCPcopy Create free account
hub / github.com/MarcCoru/locationencoder / forward

Method forward

locationencoder/pe/grid_and_sphere.py:94–247  ·  view source on GitHub ↗
(self, coords)

Source from the content-addressed store, hash-verified

92 self.freq_mat = np.repeat(freq_mat, 2, axis=1)
93
94 def forward(self, coords):
95 device = coords.device
96 dtype = coords.dtype
97 N = coords.size(0)
98
99 # add 1 context point dimension (unused here)
100 coords = coords[:, None, :]
101
102 # coords_mat: shape (batch_size, num_context_pt, 2)
103 coords_mat = np.asarray(coords.cpu())
104 batch_size = coords_mat.shape[0]
105 num_context_pt = coords_mat.shape[1]
106 # coords_mat: shape (batch_size, num_context_pt, 2, 1)
107 coords_mat = np.expand_dims(coords_mat, axis=3)
108 # coords_mat: shape (batch_size, num_context_pt, 2, 1, 1)
109 coords_mat = np.expand_dims(coords_mat, axis=4)
110 # coords_mat: shape (batch_size, num_context_pt, 2, frequency_num, 1)
111 coords_mat = np.repeat(coords_mat, self.frequency_num, axis=3)
112 # coords_mat: shape (batch_size, num_context_pt, 2, frequency_num, 2)
113 coords_mat = np.repeat(coords_mat, 2, axis=4)
114 # spr_embeds: shape (batch_size, num_context_pt, 2, frequency_num, 2)
115 spr_embeds = coords_mat * self.freq_mat
116
117 if self.name == "grid":
118 # eq 3 in https://arxiv.org/pdf/2201.10489.pdf
119 # code from https://github.com/gengchenmai/space2vec/blob/a29793336e6a1ebdb497289c286a0b4d5a83079f/spacegraph/spacegraph_codebase/SpatialRelationEncoder.py#L135
120
121 spr_embeds[:, :, :, :, 0::2] = np.sin(spr_embeds[:, :, :, :, 0::2]) # dim 2i
122 spr_embeds[:, :, :, :, 1::2] = np.cos(spr_embeds[:, :, :, :, 1::2]) # dim 2i+1
123
124 elif self.name == "spherec":
125 # eq 4 in https://arxiv.org/pdf/2201.10489.pdf
126 # lambda: longitude, theta=latitude
127
128 #sin_lon, sin_lat = np.sin(spr_embeds[:, 0, :, :, 0]).transpose(1, 0, 2)
129 #cos_lon, cos_lat = np.cos(spr_embeds[:, 0, :, :, 1]).transpose(1, 0, 2)
130
131 # eq 4
132 # sin theta, cos_theta * cos_lambda, cos_theta * sin_lambda
133 # sin lat, cos_lat cos_lon, cos_lat sin_lon
134 #spr_embeds = np.stack([sin_lat, cos_lat*cos_lon, cos_lat*sin_lon], axis=-1)
135
136 spr_embeds = spr_embeds# * math.pi / 180
137
138 # lon, lat: shape (batch_size, num_context_pt, 1, frequency_num, 1)
139 lon = np.expand_dims(spr_embeds[:, :, 0, :, :], axis=2)
140 lat = np.expand_dims(spr_embeds[:, :, 1, :, :], axis=2)
141
142 # make sinuniod function
143 # lon_sin, lon_cos: shape (batch_size, num_context_pt, 1, frequency_num, 1)
144 lon_sin = np.sin(lon)
145 lon_cos = np.cos(lon)
146
147 # lat_sin, lat_cos: shape (batch_size, num_context_pt, 1, frequency_num, 1)
148 lat_sin = np.sin(lat)
149 lat_cos = np.cos(lat)
150
151 # spr_embeds_: shape (batch_size, num_context_pt, 1, frequency_num, 3)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected