0

Dataset is FashionMNIST (784 input, 10 output). I'm trying to train logistic regression with Adam optimizer (coded it also):

weights = torch.randn(784, 10) / math.sqrt(784)
weights.requires_grad_()

bias = torch.zeros(10, requires_grad=True)

optimizer = Adam([weights, bias])
criterion = nn.CrossEntropyLoss()

The train function is:

def train_logistic_regression(weights, bias, batch, loss, optimizer):

    inputs, labels = batch

    inputs = inputs.view(inputs.shape[0], -1)

    optimizer.zero_grad()
    y_pred = torch.sigmoid(weights@inputs + bias) # there must be the problem
    loss = criterion(y_pred, labels)
    loss.backward()
    optimizer.step()


from IPython.display import clear_output


for epoch in range(1, 5):

    for batch in train_dataloader: # have to go with batches
      metrics = train_logistic_regression(weights, bias, batch, criterion, optimizer)

Every time I get the error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-161-408b80d71db1> in <module>()
      5 
      6     for batch in train_dataloader:
----> 7       metrics = train_logistic_regression(weights, bias, batch, criterion, optimizer)
      8 
      9 

<ipython-input-160-9c2f95ee56ee> in train_logistic_regression(weights, bias, batch, loss, optimizer)
      6 
      7     optimizer.zero_grad()
----> 8     y_pred = torch.sigmoid(weights@inputs + bias)
      9     # y_pred = model(inputs)
     10     loss = criterion(y_pred, labels)

RuntimeError: size mismatch, m1: [784 x 10], m2: [128 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

Would be very grateful if anyone could help me.

Sunny Duckling
  • 317
  • 1
  • 2
  • 13

1 Answers1

0

Instead of y_pred = torch.sigmoid(weights@inputs + bias) should be y_pred = torch.sigmoid(inputs.mm(weights) + bias)

Sunny Duckling
  • 317
  • 1
  • 2
  • 13