How to use a trained regression network?

kavi14: <p>I’m a bit confused.<br>
I have trained my network to output float values for a given input (regression problem).<br>
How do I use this network now to output float values for a given input?</p>
<p>I followed this thread: <a href="https://discuss.pytorch.org/t/how-to-run-trained-model/21785/2" class="inline-onebox">How to run trained model? - #2 by ptrblck</a><br>
and did:</p>
<pre><code class="lang-auto"># 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)
</code></pre>
<p>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:</p>
<pre><code class="lang-auto">RuntimeError: Expected 4-dimensional input for 4-dimensional weight [12, 7, 5, 5], but got 3-dimensional input of size [7, 111, 111] instead
</code></pre>
<p>This is my network:</p>
<pre><code class="lang-auto">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
</code></pre>

ptrblck: <p>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:</p>
<pre><code class="lang-python">model = Net()
model.eval()
data = torch.randn(1, 3, 24, 24) # Load your data here, this is just dummy data
output = model(data)
&gt; 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
</code></pre>
<p>After changing the input channels to 7, you’ll get a shape mismatch error:</p>
<pre><code class="lang-python">data = torch.randn(1, 7, 24, 24) # Load your data here, this is just dummy data
output = model(data)
&gt; RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x4096 and 169744x120)
</code></pre>
<p>Changing the <code>in_features</code> to <code>self.fc1 = nn.Linear(4096, 120)</code> works.</p>

kavi14: <p>Thanks for trying to replicate the error.<br>
I did this as I realized a dimension was missing:<br>
First I saw that</p>
<pre><code class="lang-auto">(n_samples, channels, height, width) 
</code></pre>
<p>should be how the input should be given.</p>
<p>So to my input, I added a dimension and then it complained about a float so this is what worked for me:</p>
<pre><code class="lang-auto">input = input.unsqueeze(0).float()
</code></pre>
<p>The network output the following value:</p>
<pre><code class="lang-auto">tensor([[-2.2090]], grad_fn=&lt;AddmmBackward&gt;)
</code></pre>
<p>I’m trying to figure out how to get the float out of this Tensor type now…</p>

kavi14: <p>anndddd I just did input.item() and got the float out <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>