Unexpected error: In the step function

I was following the Variational Classifier tutorial posted on Pennylane to teach myself the fundamentals of Pennylane. Unfortunately, received an unexpected error. Not sure if the recent development changed something or not.

Code

weights = weights_init
bias = bias_init
for it in range(100):
  batch_index = np.random.randint(0, len(x), (batch_size,))
  X_batch = X[batch_index]
  Y_batch = Y[batch_index]
  weights, bias = opt.step(cost, weights, bias, X=X_batch, Y=Y_batch)

  # compute accuracy
  predictions = [np.sign(variational_classifier(weights, bias, x)) for x in x]

  current_cost = cost(weights, bias, X, Y)
  acc = accuracy(Y, predictions)

  print(f"Iter: {it+1:4d} | Cost: {current_cost:0.7f} | Accuracy: {acc:0.7f}")

Produced Error

TypeError                                 Traceback (most recent call last)
<ipython-input-27-1016c24e2061> in <cell line: 3>()
      5   X_batch = X[batch_index]
      6   Y_batch = Y[batch_index]
----> 7   weights, bias = opt.step(cost, weights, bias, X=X_batch, Y=Y_batch)
      8 
      9   # compute accuracy

7 frames
/usr/local/lib/python3.10/dist-packages/autograd/wrap_util.py in unary_f(x)
     13                 else:
     14                     subargs = subvals(args, zip(argnum, x))
---> 15                 return fun(*subargs, **kwargs)
     16             if isinstance(argnum, int):
     17                 x = args[argnum]

TypeError: cost() got an unexpected keyword argument 'X'

Tutorial Following: Variational classifier | PennyLane Demos

Can someone help me? how to resolve this?

Hey @sleepingcat4,

I can’t try to replicate the error you’re seeing because your code snippet is missing crucial pieces for me to be able to run it. But, if you try

weights, bias = opt.step(cost, weights, bias, X, Y)

does that help?

I was following the default tutorial and then received the error. I will try out your code and let you know : )

If you’re following the default tutorial, make sure you’re using an up-to-date version of PennyLane :slight_smile:. I’m using v0.35.1 and that tutorial runs fine for me. You can update PennyLane via:

pip install --upgrade pennylane

Let me know if that helps!

1 Like

Hi @sleepingcat4 ,

Make sure your cost function is like that:

def cost(weights, bias, X, Y):
    predictions = [variational_classifier(weights, bias, x) for x in X]
    return square_loss(Y, predictions)

You error isn’t located in the optimizer step function but in the cost function. You probably changed X and Y names in your cost function.

1 Like