I receive input ids like (7, 18, 6, …) as a pytorch tensor. However the embedding for 7 is in the first index of embedding, for 18 it is in second row, etc.
I want a map from these numbers to 1,2, 3… to access stored value in embedding.
It seems I can’t use a dictionary as follows
def forward(self,prompt_token_ids,pids=None):
prompt_token_ids = [self.id_map[x] for x in prompt_token_ids]
return self.embedding(prompt_token_ids)
you can convert the embedding to a numpy array using:
np_array = self.embedding.weight.detach().cpu().numpy()
Also, you need to create a new tensor with new indices using:
indices = torch.arange( # of elements in the input ids)
That would give you the flexibility to iterate over the whole thing. In the end, you can convert the np_array back to a tensor and then to the embedding.