mmalagocki
mmalagocki

Reputation: 39

Error during traning simple MLP network: mat1 and mat2 shapes cannot be multiplied

I'm trying to process with my network matrix that looks as following:

tensor([[0.],                                                                                                                                                                                                              | 0/6675 [00:00<?, ?it/s]
        [0.],
        [1.],
        ...,
        [0.],
        [1.],
        [1.]])

And receiving error:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (4267x4267 and 1x4267)

That doesn't make sense in mathematical point of view since the dimensions (m x n) (p x m) fits. Could you please give me any hint what I may be doing wrong?

Code which I'm executing:

def train_epoch_sparse(model, optimizer, device, graph, train_edges, batch_size, epoch, monet_pseudo=None):

    model.train()
    
    train_edges = train_edges.to(device)
    
    total_loss = total_examples = 0
    for perm in tqdm(DataLoader(range(train_edges.size(0)), batch_size, shuffle=True)):

        optimizer.zero_grad()

        graph = graph.to(device)
        x = graph.ndata['h'].to(device).float()
        e = graph.edata['h'].to(device).float()

        if monet_pseudo is not None:
            # Assign e as pre-computed pesudo edges for MoNet
            e = monet_pseudo.to(device)
        h = model(graph, x, e)
        # Positive samples
        edge = train_edges[perm].t()
        pos_out = model.edge_predictor( h[edge[0]], h[edge[1]] )
        # Just do some trivial random sampling
        edge = torch.randint(0, x.size(0), edge.size(), dtype=torch.long, device=x.device)

        neg_out = model.edge_predictor( h[edge[0]], h[edge[1]] )
        
        loss = model.loss(pos_out, neg_out)

        loss.backward()
        optimizer.step()

        num_examples = pos_out.size(0)
        total_loss += loss.detach().item() * num_examples
        total_examples += num_examples

    return total_loss/total_examples, optimizer

Upvotes: 0

Views: 35

Answers (0)

Related Questions