(batch_size, dim, pos)
| 30 | # batch_size, dimension and position |
| 31 | # output: (batch_size, dim) |
| 32 | def positional_encoding(batch_size, dim, pos): |
| 33 | assert batch_size == pos.shape[0] |
| 34 | positions_enc = np.array( |
| 35 | [[pos[j] / np.power(10000, (i - i % 2) / dim) for i in range(dim)] |
| 36 | for j in range(batch_size)], |
| 37 | dtype=np.float32) |
| 38 | positions_enc[:, 0::2] = np.sin(positions_enc[:, 0::2]) |
| 39 | positions_enc[:, 1::2] = np.cos(positions_enc[:, 1::2]) |
| 40 | return torch.from_numpy(positions_enc).float() |
| 41 | |
| 42 | |
| 43 | def get_padding_mask(batch_size, seq_len, cap_lens): |
nothing calls this directly
no outgoing calls
no test coverage detected