(batch_size, dim, pos)
| 41 | # batch_size, dimension and position |
| 42 | # output: (batch_size, dim) |
| 43 | def positional_encoding(batch_size, dim, pos): |
| 44 | assert batch_size == pos.shape[0] |
| 45 | positions_enc = np.array([ |
| 46 | [pos[j] / np.power(10000, (i-i%2)/dim) for i in range(dim)] |
| 47 | for j in range(batch_size) |
| 48 | ], dtype=np.float32) |
| 49 | positions_enc[:, 0::2] = np.sin(positions_enc[:, 0::2]) |
| 50 | positions_enc[:, 1::2] = np.cos(positions_enc[:, 1::2]) |
| 51 | return torch.from_numpy(positions_enc).float() |
| 52 | |
| 53 | |
| 54 | def get_padding_mask(batch_size, seq_len, cap_lens): |
nothing calls this directly
no outgoing calls
no test coverage detected