How to use a trained regression network?

I’m a bit confused.
I have trained my network to output float values for a given input (regression problem).
How do I use this network now to output float values for a given input?

I followed this thread: How to run trained model? - #2 by ptrblck
and did:

# Train your model
...
# After training
model.eval()
data = torch.randn(1, 3, 24, 24) # Load your data here, this is just dummy data
output = model(data)
prediction = torch.argmax(output)

For the data variable, I put the same tensor i used as input for training but without the target value, but it is complaining about the following:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [12, 7, 5, 5], but got 3-dimensional input of size [7, 111, 111] instead

This is my network:

class Net(nn.Module):
    def __init__(self, bounds):
        super().__init__()
        self.bounds = bounds
        # input channels, output no. of features, kernel size
        self.conv1 = nn.Conv2d(7, 12, 5)
#         self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(12, 16, 5)
        self.fc1 = nn.Linear(16 * 103 * 103, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 1)
#         self.double()

    def forward(self, x):
#         x = self.pool(F.relu(self.conv1(x)))
#         x = self.pool(F.relu(self.conv1(x)))
        
#         x = self.pool(F.relu(self.conv2(x)))
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
     #   x = self.pool(F.relu(self.conv2(x)))
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

I’m unable to reproduce the posted error message, but your current code won’t be working since the number of input channels is wrong:

model = Net()
model.eval()
data = torch.randn(1, 3, 24, 24) # Load your data here, this is just dummy data
output = model(data)
> RuntimeError: Given groups=1, weight of size [12, 7, 5, 5], expected input[1, 3, 24, 24] to have 7 channels, but got 3 channels instead

After changing the input channels to 7, you’ll get a shape mismatch error:

data = torch.randn(1, 7, 24, 24) # Load your data here, this is just dummy data
output = model(data)
> RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x4096 and 169744x120)

Changing the in_features to self.fc1 = nn.Linear(4096, 120) works.

Thanks for trying to replicate the error.
I did this as I realized a dimension was missing:
First I saw that

(n_samples, channels, height, width) 

should be how the input should be given.

So to my input, I added a dimension and then it complained about a float so this is what worked for me:

input = input.unsqueeze(0).float()

The network output the following value:

tensor([[-2.2090]], grad_fn=<AddmmBackward>)

I’m trying to figure out how to get the float out of this Tensor type now…

anndddd I just did input.item() and got the float out :slight_smile: