How do I do this

vimalkansal: <p>Hi,</p>
<p>I have trained a model. Now I want to use it to make predictions. using feed forward like model(x) wil do it only on single tensor. If I have to do it on a set of 20000 tensors, do I have to do it in a loop or like Keras there is a some method which can accept the complete set of tensors</p>

ptrblck: <p><code>model(x)</code> will take a whole batch of tensors with the batch size in dim0.<br>
E.g. if your model uses an <code>nn.Linear</code> module as the first layer, you might feed the complete dataset as <code>[20000, nb_features]</code> into your model.</p>

vimalkansal: <p>I get the error. My code is like this :<br>
X_train, X_test = train_test_split(transactionData, test_size=0.2, random_state=RANDOM_SEED)<br>
X_train = X_train[X_train.Class == 0]<br>
X_train = X_train.drop([‘Class’], axis=1)<br>
<span class="hashtag">#print</span>(type(X_train))<br>
y_test = X_test[‘Class’]<br>
X_test = X_test.drop([‘Class’], axis=1)</p>
<p>X_train = X_train.values<br>
<span class="hashtag">#print</span>(type(X_train))<br>
X_test = X_test.values<br>
y_test = y_test.values<br>
print(y_test.size)</p>
<p>and my model class is :</p>
<p>class Autoencoder(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Autoencoder, self).<strong>init</strong>()<br>
self.encoder = nn.Sequential(<br>
nn.Linear(29, 14),<br>
nn.Tanh(),<br>
nn.Linear(14, 7),<br>
nn.LeakyReLU(),<br>
)</p>
<pre><code>    self.decoder = nn.Sequential(
       nn.Linear(7, 7),
       nn.Tanh(),
       nn.Linear(7, 29),
       nn.LeakyReLU()
    )

def forward(self, x):
    x = self.encoder(x)
    x = self.decoder(x)
    return x
</code></pre>
<p>When i write :<br>
model.eval()<br>
with torch.no_grad():<br>
pred_losses = model([y_test.size, y_test])<br>
I get the following errors :</p>
<hr>
<p>AttributeError                            Traceback (most recent call last)<br>
 in <br>
10 #         <span class="hashtag">#print</span>(loss)<br>
11 #         pred_losses[‘pred_loss’].append(loss)<br>
—&gt; 12           pred_losses = model([y_test.size, y_test])<br>
13 reconstructionErrorDF = pd.DataFrame(pred_losses)<br>
14 reconstructionErrorDF[‘Class’] = y_test</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
491             result = self._slow_forward(*input, **kwargs)<br>
492         else:<br>
–&gt; 493             result = self.forward(*input, **kwargs)<br>
494         for hook in self._forward_hooks.values():<br>
495             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
17<br>
18     def forward(self, x):<br>
—&gt; 19         x = self.encoder(x)<br>
20         x = self.decoder(x)<br>
21         return x</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
491             result = self._slow_forward(*input, **kwargs)<br>
492         else:<br>
–&gt; 493             result = self.forward(*input, **kwargs)<br>
494         for hook in self._forward_hooks.values():<br>
495             hook_result = hook(self, input, result)</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)<br>
90     def forward(self, input):<br>
91         for module in self._modules.values():<br>
—&gt; 92             input = module(input)<br>
93         return input<br>
94</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
491             result = self._slow_forward(*input, **kwargs)<br>
492         else:<br>
–&gt; 493             result = self.forward(*input, **kwargs)<br>
494         for hook in self._forward_hooks.values():<br>
495             hook_result = hook(self, input, result)</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)<br>
90     <span class="mention">@weak_script_method</span><br>
91     def forward(self, input):<br>
—&gt; 92         return F.linear(input, self.weight, self.bias)<br>
93<br>
94     def extra_repr(self):</p>
<p>/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)<br>
1402         - Output: :math:<code>(N, *, out\_features)</code><br>
1403     “”"<br>
-&gt; 1404     if input.dim() == 2 and bias is not None:<br>
1405         # fused op is marginally faster<br>
1406         ret = torch.addmm(bias, input, weight.t())</p>
<p>AttributeError: ‘list’ object has no attribute ‘dim’</p>

ptrblck: <p>Try to pass <code>X_test</code> as a tensor to your model:</p>
<pre><code class="lang-python">X_test = torch.from_numpy(X_test).float()
output = model(X_test)
</code></pre>