TypeError: predict() takes 1 positional argument but 2 were given

I have tried to read the text on similar topics, but can’t seem to figure out whats wrong here.

This is the error:
Traceback (most recent call last):
File “”, line 1, in
TypeError: predict() takes 1 positional argument but 2 were given

Here is the code:

class NeuralNetwork(nn.Module):
   def __init__(self, ):
      super(NeuralNetwork, self).__init__()
      self.inputSize = 2
      self.outputSize = 1
      self.hiddenSize = 3

      self.W1 = torch.randn(self.inputSize, self.hiddenSize)
      self.W2 = torch.randn(self.hiddenSize, self.outputSize)

   def forward(self, X):
      self.z = torch.matmul(X, self.W1)
      self.z2 = self.sigmoid(self.z)
      self.z3 = torch.matmul(self.z2, self.W2)
      o = self.sigmoid(self.z3)
      return o

   def sigmoid(self, s):
      return 1 / (1 + torch.exp(-s))

   def predict(self, x_predicted):
      print('Predicted data based on trained weights: ')
      print('Input (scaled): \n' + str(x_predicted))
         print('Output: \n' + str(self.forward(x_predicted)))
from package_name.class_name import NeuralNetwork
xPredicted = torch.load('data/xPredicted.pt')
NN = NeuralNetwork()
NN.predict(xPredicted)

xPredicted is correctly loaded and defined. What I don’t understand is why predict() only takes one positional argument when it has both self and x_predicted? And how do I set this up so that it works?

I cannot reproduce the issue using your code snippet and get this output:

model = NeuralNetwork()
x = torch.randn(1, 2)
model.predict(x)
> Predicted data based on trained weights: 
Input (scaled): 
tensor([[1.2890, 0.3778]])
Output: 
tensor([[0.8106]])

Hi,

Not sure what was wrong, but now it works. Thank you!

Chris