How do I do this

Hi,

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

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

I get the error. My code is like this :
X_train, X_test = train_test_split(transactionData, test_size=0.2, random_state=RANDOM_SEED)
X_train = X_train[X_train.Class == 0]
X_train = X_train.drop([‘Class’], axis=1)
#print(type(X_train))
y_test = X_test[‘Class’]
X_test = X_test.drop([‘Class’], axis=1)

X_train = X_train.values
#print(type(X_train))
X_test = X_test.values
y_test = y_test.values
print(y_test.size)

and my model class is :

class Autoencoder(nn.Module):
def init(self):
super(Autoencoder, self).init()
self.encoder = nn.Sequential(
nn.Linear(29, 14),
nn.Tanh(),
nn.Linear(14, 7),
nn.LeakyReLU(),
)

    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

When i write :
model.eval()
with torch.no_grad():
pred_losses = model([y_test.size, y_test])
I get the following errors :


AttributeError Traceback (most recent call last)
in
10 # #print(loss)
11 # pred_losses[‘pred_loss’].append(loss)
—> 12 pred_losses = model([y_test.size, y_test])
13 reconstructionErrorDF = pd.DataFrame(pred_losses)
14 reconstructionErrorDF[‘Class’] = y_test

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
–> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

in forward(self, x)
17
18 def forward(self, x):
—> 19 x = self.encoder(x)
20 x = self.decoder(x)
21 return x

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
–> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
—> 92 input = module(input)
93 return input
94

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
–> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
90 @weak_script_method
91 def forward(self, input):
—> 92 return F.linear(input, self.weight, self.bias)
93
94 def extra_repr(self):

/data/anaconda/envs/vimal_pytorch/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
1402 - Output: :math:(N, *, out\_features)
1403 “”"
-> 1404 if input.dim() == 2 and bias is not None:
1405 # fused op is marginally faster
1406 ret = torch.addmm(bias, input, weight.t())

AttributeError: ‘list’ object has no attribute ‘dim’

Try to pass X_test as a tensor to your model:

X_test = torch.from_numpy(X_test).float()
output = model(X_test)