Model returns a tuple instead of tensor

I wrote a program in python using pytorch. My problem is:
my model returns tuple instead of tensor. How can I make/force my model to return tensor?

this my code:

import torch
from torch import nn
import numpy as np
from torch.optim import Adam

x = np.linspace(0, 1, 50).reshape((-1, 1)).astype('float32')
y = np.power(x, 2).reshape((-1, 1)).astype('float32')

x = torch.tensor(x)
y = torch.tensor(y)

model = nn.Sequential(
    nn.Linear(1, 1),
    nn.ReLU()
)

loss = nn.MSELoss()
opt = Adam(model.parameters(), lr=0.001)
model.train()
n_batch = 4
n_epoch = 100
for i in range(n_epoch):
    for b in range(0, len(x), n_batch):
        inp = x[b:b+n_batch]
        out = y[b:b+n_batch]
        opt.zero_grad()
        pred = model(inp),
        ls = loss(pred, out)
        ls.backward()
        opt.step()

This the last lines of error that I got:

File “D:\Projects\Sine_Wave_LSTM_Pytorch\venv\lib\site-packages\torch\nn\functional.py”, line 3355, in mse_loss
if not (target.size() == input.size()):
AttributeError: ‘tuple’ object has no attribute ‘size’

Remove the comma after model(inp):

1 Like