tzujan
tzujan

Reputation: 186

PyTorch RuntimeError: mat1 and mat2 shapes cannot be multiplied

Purely for learning, I'd like to get the following code to work, without a DataLoader. I use Huggingface transformers regularly yet I struggle with PyTorch dimensions all the time so I have started with some simple projects from the book "Deep Learning with PyTorch." One of the problems from the book suggested using a wine quality dataset on a super simple linear model. I have toiled with the dimensions of the data, which I think is the source of my error:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (3919x1 and 11x100)

Data is available here

import csv
from collections import OrderedDict

import numpy as np
import torch
import torch.optim as optim

import torch.nn as nn

wine_path = "winequality-white.csv"
wine_quality_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=";",
                         skiprows=1)

col_list = next(csv.reader(open(wine_path), delimiter=';'))

wineq = torch.from_numpy(wine_quality_numpy)

# print(wineq.shape, wineq.dtype)

data = wineq[:, :-1]
target = wineq[:, -1]
target = target.unsqueeze(1)

n_samples = wine_quality_numpy.shape[0]
n_val = int(0.2 * n_samples)

shuffled_indices = torch.randperm(n_samples)

train_indices = shuffled_indices[:-n_val]
val_indices = shuffled_indices[-n_val:]

target_train = target[train_indices]
data_train = data[train_indices]

target_val = target[val_indices]
data_val = data[val_indices]

seq_model = nn.Sequential(OrderedDict([
    ('hidden_linear', nn.Linear(11, 100)),
    ('hidden_activation', nn.Tanh()),
    ('output_linear', nn.Linear(100, 7))
]))

def training_loop(n_epochs, optimizer, model, loss_fn, target_train, target_val,
                  data_train, data_val):
    for epoch in range(1, n_epochs + 1):
        t_p_train = model(target_train) # <1>
        loss_train = loss_fn(t_p_train, data_train)

        t_p_val = model(t_u_val) # <1>
        loss_val = loss_fn(t_p_val, data_val)
        
        optimizer.zero_grad()
        loss_train.backward() # <2>
        optimizer.step()

        if epoch == 1 or epoch % 1000 == 0:
            print(f"Epoch {epoch}, Training loss {loss_train.item():.4f},"
                  f" Validation loss {loss_val.item():.4f}")


optimizer = optim.SGD(seq_model.parameters(), lr=1e-3) # <1>

training_loop(
    n_epochs = 5000, 
    optimizer = optimizer,
    model = seq_model,
    loss_fn = nn.MSELoss(),
    target_train = target_train,
    target_val = target_val, 
    data_train = data_train,
    data_val = data_val)

Thank you!

Upvotes: 1

Views: 4851

Answers (1)

tzujan
tzujan

Reputation: 186

In my haste I had the training data and labels swapped. Here is the fixed section.

seq_model = nn.Sequential(OrderedDict([
('hidden_linear', nn.Linear(11, 100)),
('hidden_activation', nn.Tanh()),
('output_linear', nn.Linear(100, 7))
]))

def training_loop(n_epochs, optimizer, model, loss_fn, target_train, target_val,
                  data_train, data_val):
    for epoch in range(1, n_epochs + 1):
        t_p_train = model(data_train) # <1>
        loss_train = loss_fn(t_p_train, target_train)

        t_p_val = model(data_val) # <1>
        loss_val = loss_fn(t_p_val, target_val)
        
        optimizer.zero_grad()
        loss_train.backward() # <2>
        optimizer.step()

        if epoch == 1 or epoch % 1000 == 0:
            print(f"Epoch {epoch}, Training loss {loss_train.item():.4f},"
                  f" Validation loss {loss_val.item():.4f}")

Upvotes: 0

Related Questions