lambeq icon indicating copy to clipboard operation
lambeq copied to clipboard

Error whem training Classical Pipeline with Spider Ansatz

Open PeppeGoodhelp opened this issue 2 years ago • 3 comments

Hi,

I have an issue when I try to train a classical pipelien on a standard Sentiment Analysis (binary classifcation task). Basically, after the conversion of the training diagrams into IQP ansatz I set up the trainer:

from lambeq import PytorchModel all_circuits = train_circuits# + dev_circuits + test_circuits model = PytorchModel.from_diagrams(all_circuits) model.initialise_weights()

and then:

`from lambeq import PytorchTrainer

trainer = PytorchTrainer( model=model, loss_function=torch.nn.BCEWithLogitsLoss(), optimizer=torch.optim.AdamW, # type: ignore learning_rate=LEARNING_RATE, epochs=EPOCHS, evaluate_functions={"acc": accuracy}, evaluate_on_train=False, verbose='text', seed=SEED)`

Finally: `from lambeq import Dataset

train_dataset = Dataset( train_circuits, train_label, batch_size=BATCH_SIZE)

trainer.fit(train_dataset, log_interval=5)`

I get the following error:

TypeError: moveaxis() received an invalid combination of arguments - got (Tensor, range, list), but expected one of:

  • (Tensor input, int source, int destination) didn't match because some of the arguments have invalid types: (Tensor, !range!, !list of [int, int]!)
  • (Tensor input, tuple of ints source, tuple of ints destination) didn't match because some of the arguments have invalid types: (Tensor, !range!, !list of [int, int]!)

Attached, a screen of the error stated above.

Any idea about how to fix this? Screenshot from 2023-11-22 11-34-19

PeppeGoodhelp avatar Nov 22 '23 12:11 PeppeGoodhelp

Hi @PeppeGoodhelp, thank you for reporting this issue.

I have a couple of questions regarding your training setup.

  • What dataset are you using for your classification task?
  • How did you create your IQPAnsatz instance? My initial impression is you are reusing this classical pipeline which used SpiderAnsatz but switched to IQPAnsatz.

It would be great if you could provide your full code here and a couple of sentences you use for your training task.

Thank you very much.

neiljdo avatar Nov 23 '23 15:11 neiljdo

Hi @PeppeGoodhelp, thank you for reporting this issue.

I have a couple of questions regarding your training setup.

* What dataset are you using for your classification task?

* How did you create your `IQPAnsatz` instance? My initial impression is you are reusing this [classical pipeline](https://cqcl.github.io/lambeq/examples/classical_pipeline.html) which used `SpiderAnsatz` but switched to `IQPAnsatz`.

It would be great if you could provide your full code here and a couple of sentences you use for your training task.

Thank you very much.

Hi and thanks for your answer. I am basically using a standard Cola (acceptability judgement) dataset. It is quite extensive so we are shrinking it a little bit.

Concerning the ansatz thats what I did:

`from lambeq import AtomicType, IQPAnsatz, remove_cups,CircuitAnsatz,MPSAnsatz, BaseAnsatz,SpiderAnsatz from discopy.tensor import Dim #AtomicType.CONJUNCTION :0, AtomicType.PREPOSITIONAL_PHRASE:1, #ans1=MPSAnsatz({AtomicType.NOUN: Dim(4), AtomicType.SENTENCE: Dim(3),AtomicType.PREPOSITIONAL_PHRASE: Dim(1)}, bond_dim=5)

ansatz = SpiderAnsatz({AtomicType.NOUN: Dim(2), AtomicType.SENTENCE: Dim(2),AtomicType.PREPOSITIONAL_PHRASE: Dim(2),AtomicType.CONJUNCTION: Dim(2)})

train_circuits = [ansatz(diagram) for diagram in train_diagrams]`

As you can see this is basically a standard classical pipeline. There is a further issue when I try the IQP for the quantum pipeline. I can't remove cups. I cant wrap my head around

PeppeGoodhelp avatar Nov 29 '23 09:11 PeppeGoodhelp

Hi @PeppeGoodhelp,

If you can, can you provide a dataset containing 5 to 10 sample sentences that, if used with the code you provided, generate the same error message? Since the code you are using is unchanged from the docs, it's most likely that the dataset is causing the issue. Thanks!

neiljdo avatar Dec 05 '23 10:12 neiljdo

This will be closed due to inactivity.

dimkart avatar Apr 05 '24 14:04 dimkart