ParameterDict keys *must* be strings?

Is there a good reason for this? I would like to use general hashable objects as keys in my ParameterDict, just as I would in a normal Python dict.
Currently, the Pytorch documentation lies by claiming “ParameterDict can be indexed like a regular Python dictionary”, since the indexing must be done only with strings.

There is an issue about a similar topic on github (ModuleDict instead of ParameterDict).
If you like, you could describe your use case there.

Thanks, I’m also just going with the str(int) solution for now, it’s probably not that much of a performance-loss. Still a bit ugly is all.

Sure, I get it, and I think it’s a valid discussion, since there seem to be a few users noting this behavior. :wink:

The main reason is that we need to get a string key for the registered submodule, for purpose like saving state dict. Many things are hashable, but not all of them hash to same value when you run the same code in a new process.

1 Like

can we reopen this issue a dict with integer only keys will be very useful:
this is the use case:

Pytorch ParameterDict can only accept string type keys.

My model takes inputs that are large integers such as -18272849 and 12345678. These integers represent the murmur hash function of categorical features with low cardinality so the number of different values for the integers is small but the integers themselves are very large.

I want to use a ParameterDict OR an embedding layer for them without converting them to strings and having some standard embedding solution after that. How do I do that?