Error making prediction with pruned neural network

Hayden_Johnson: <p>I have trained an MLP for regression with 18 output nodes. I want to remove 17 of these nodes (i.e. the weights and biases) so the MLP can output only for the one remaining node. I successfully removed these nodes, however the new MLP can only predict for one single example at a time (while the original MLP can predict for 10000 examples at a time). When I try to predict more than one example at a time I receive the following error - “RuntimeError: mat2 must be a matrix, got 1-D tensor”</p>
<p>My original network I trained was defined as follows:<br>
<strong>class NMR_Model(nn.Module):</strong><br>
**    def <strong>init</strong>(self):**<br>
**        super().<strong>init</strong>()**<br>
**        self.lin1 = nn.Linear(14000, 200)**<br>
**        self.relu1 = nn.ReLU()**<br>
**        self.lin2 = nn.Linear(200, 18)**<br>
**    def forward(self, input):**<br>
**        return (self.lin2(self.relu1(self.lin1(input))))**</p>
<p><strong>model = NMR_Model()</strong></p>
<p>After training, I made a second model and used the same weights and biases to create the MLP with only one output node. The following is the code for this:<br>
<strong>class NMR_Model2(nn.Module):</strong><br>
**    def <strong>init</strong>(self):**<br>
**        super().<strong>init</strong>()**<br>
**        self.lin1 = nn.Linear(14000, 200)**<br>
**        self.relu1 = nn.ReLU()**<br>
**        self.lin2 = nn.Linear(200, 1)**<br>
**    def forward(self, input):**<br>
**        return (self.lin2(self.relu1(self.lin1(input))))**</p>
<p><strong>model2 = NMR_Model2()</strong><br>
<strong>model2.lin1.weight.data = model.lin1.weight.data</strong><br>
<strong>model2.lin1.bias.data = model.lin1.bias.data</strong><br>
<strong>model2.lin2.weight.data = model.lin2.weight.data[0]</strong><br>
<strong>model2.lin2.bias.data = model.lin2.bias.data[0]</strong></p>
<p>The following works to make predictions with the first model, with outputs being the predictions for 10000 examples [i.e. the X_test variable]:<br>
<strong>outputs = model(X_test)</strong></p>
<p>This same code does not work for the pruned model, however the following works to make a prediction for a single example:<br>
<strong>outputs = model2(X_test[0])</strong></p>
<p>I am wondering why the pruned model only works for a single example, and how I might could modify my code to allow prediction for all 10000 examples using the pruned model.</p>

Hayden_Johnson: <p>Figured it out. I saw on this post (<a href="https://discuss.pytorch.org/t/runtimeerror-mat2-must-be-a-matrix-got-1-d-tensor/151404" class="inline-onebox">RuntimeError: mat2 must be a matrix, got 1-D tensor</a>) that someone had a similar error, and the solution suggested they needed to change the shape of their output tensor. I modified my code where I was copying the weights/biases from the first network, as follows:</p>
<p><strong>model3 = NMR_Model2()</strong><br>
<strong>model3.lin1.weight.data = model2.lin1.weight.data</strong><br>
<strong>model3.lin1.bias.data = model2.lin1.bias.data</strong><br>
<strong>model3.lin2.weight.data = model2.lin2.weight.data[:1]</strong><br>
<strong>model3.lin2.bias.data = model2.lin2.bias.data[:1]</strong></p>