1

I have the body of the code, which should work fine, I think it doesn't because of something I'm messing up here, probably having to do with the embedding.

import torch.nn as nn
class MultilayerPerceptron(nn.Module):

  def __init__(self, input_size, hidden_size): # I removed output size
    # Call initializer function of the super class 
    super(MultilayerPerceptron, self).__init__()
    self.embedding = nn.Embedding(INPUT_DIM, EMBEDDING_DIM) #added this myself, maybe wrong
    #self.mlp = nn.MultilayerPerceptron(EMBEDDING_DIM, HIDDEN_DIM) #also added
    self.INPUT_DIM = INPUT_DIM
    self.HIDDEN_DIM = HIDDEN_DIM
    self.OUTPUT_DIM = OUTPUT_DIM
    self.EMBEDDING_DIM = EMBEDDING_DIM

    #whenever this model is called, those layers in the sequential block 
    #will be processed in the order given to the block. 
    self.model = nn.Sequential(
        #nn.Flatten(), # adding this hopefully it works (it didn't)
        #embeds = embedded.mean(dim=1) #god help
        nn.Linear(self.INPUT_DIM, self.HIDDEN_DIM), #later on, multiply by embedding dimensionality #I removed 
        nn.ReLU(),
        nn.Linear(self.HIDDEN_DIM, self.OUTPUT_DIM), #one layer neural network
        nn.ReLU(), # do I need this?
        nn.Sigmoid(),
    )
    
  def forward(self, x):
    embedded = self.embedding(x)
    #embedded = [sent len, batch size, emb dim]
    output, hidden = self.model(embedded) 
    output = self.model(x) #call the model defined above for forward propagation. 
    return output

INPUT_DIM = len(TEXT.vocab)
EMBEDDING_DIM = 100 #how do I fit this into the model??
HIDDEN_DIM = 256
OUTPUT_DIM = 1

model = MultilayerPerceptron(INPUT_DIM, HIDDEN_DIM) #MLP instead 

The error I get is "mat1 and mat2 shapes cannot be multiplied (50176x100 and 25002x256)".

  • You don't give the error message you are getting. But I notice you call `self.mlp()`, but haven't defined anything of that name in `__init__()` – Darren Cook May 31 '21 at 07:38
  • You're right, I've actually fixed that part since then. The error I get now is "mat1 and mat2 shapes cannot be multiplied". I know I should do something about the size of the embedding, I was told to add "embeds = embedded.mean(dim=1)", but I'm not sure how to call the variable or where to put it to actually make it run. – Flavio Spadavecchia Jun 01 '21 at 11:23
  • Can you update your question with latest code, and the full error message that is generated from it (it normally tells you the dimensions of the two matrices it cannot multiply). – Darren Cook Jun 01 '21 at 11:31
  • I've done that now, but I think that the dimensions actually change every time I run it... – Flavio Spadavecchia Jun 03 '21 at 18:03

0 Answers0