'Get embedding class shape in python

I have the following Embedding Model:

class MF(nn.Module):

def __init__(self, n_users: int, n_items: int, n_factors: int):
    """
    n_users - int - number of users.
    n_items - int - number of items.
    n_factors - int - dimensionality of the latent space.
    """
    
    super(MF, self).__init__()
    
    # TODO: YOUR IMPLEMENTATION.
    
    self.embedding_user = nn.Embedding(n_users, n_factors) 
    self.embedding_item = nn.Embedding(n_items, n_factors)
    
    
def forward(self, user: torch.Tensor, item: torch.Tensor) -> torch.Tensor:
    """
    We allow for some flexibility giving lists of ids as inputs:
    if the training data is small we can deal with it in a single forward pass,
    otherwise we could fall back to mini-batches, limiting users and items we pass
    every time.
    
    user - torch.Tensor - user_ids.
    item - torch.Tensor - item_ids.
    
    returns - torch.Tensor - Reconstructed Interaction matrix of shape (n_users, n_items).
    """
    # TODO: YOUR IMPLEMENTATION.
    
    res = self.embedding_user(user) @ self.embedding_item(item).T
    
            
    return res

model_128 = MF(train_data_inter.shape[0],train_data_inter.shape[1] ,n_factors=128) model.embedding_item gives me a result like the following: Embedding(412, 128)

Now I want to have the 412 of this as a float/int from this model directly.

What should I do in this case? Do I need to modify the model itself? Please advise. Thanks



Solution 1:[1]

you may use the following:

model_128.embedding_item.embedding_dim

this guide might help you in case you want any other parameters: https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hussam Knaany