Hello @Tom_Bromley,
Do you have any suggestions to obtain better accuracies using qml.qnn.TorchLayers vs. original models? The code has been implemented into another classical notebook.
Thank you,
Reference:
Hello @Tom_Bromley,
Do you have any suggestions to obtain better accuracies using qml.qnn.TorchLayers vs. original models? The code has been implemented into another classical notebook.
Thank you,
Reference:
Hey @kevinkawchak!
What do you mean by âoriginal modelsâ? Are you trying to compare quantum / hybrid models to purely classical ones on the same task?
Yes, this is the case.
Thanks for clarifying! This is a question we get quite often, and unfortunately the answer isnât very satisfying . There are many things that go into making a machine learning model work well for a specific task, including the choice of optimizer, the choice of hyperparameters â learning rate, step size, batch size, etc â the cost function, the model architecture itself, and many more. Itâs a tedious task to tweak all of those things and find the right combination.
I also think itâs not good to assume that quantum / hybrid models should train better (better. accuracy, for instance) than a classical model. The question is much more nuanced there. Maria Schuld gave a nice talk on this topic recently, you should check it out!
Hello @isaacdevlugt,
I appreciate the resources. The issue appears to be with implementing the def forward() step from âCreating non-sequential modelsâ into other notebooks. For instance, the demo make_moons dataset for the first qlayer uses: x_1 = self.qlayer_1(x_1)
A hybrid model I am working on returns parameters in the run summary as âqlayer_1 | TorchLayer | 12â using self.qlayer_1 = qml.qnn.TorchLayer(qnode, weight_shapes)
I would need an analogous solution to âx_1â for the hybrid notebook for def forward(self, pixel_values) in order to fully incorporate the quantum circuit. Thank you.
References:
Can you reply back with some code that summarizes the issue youâre facing? That would help me understand the problem better
Hello @isaacdevlugt,
class ViTLightningModule(pl.LightningModule):
def __init__(self, num_labels=10):
super(ViTLightningModule, self).__init__()
self.vit = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224-in21k',
num_labels=10,
id2label=id2label,
label2id=label2id)
self.qlayer_1 = qml.qnn.TorchLayer(qnode, weight_shapes)
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
outputs = self.qlayer_1()
return outputs.logits
Some weights of ViTForImageClassification were not initialized from the model checkpoint at google/vit-base-patch16-224-in21k and are newly initialized: [âclassifier.weightâ, âclassifier.biasâ]
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
INFO:pytorch_lightning.utilities.rank_zero:GPU available: True (cuda), used: True
INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores
INFO:pytorch_lightning.utilities.rank_zero:IPU available: False, using: 0 IPUs
INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs
INFO:pytorch_lightning.accelerators.cuda:LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]
INFO:pytorch_lightning.callbacks.model_summary:
| Name | Type | Params
0 | vit | ViTForImageClassification | 85.8 M
1 | qlayer_1 | TorchLayer | 16
85.8 M Trainable params
0 Non-trainable params
85.8 M Total params
343.225 Total estimated model params size (MB)
Sanity Checking DataLoader 0: 0%
0/2 [00:00<?, ?it/s]
TypeError Traceback (most recent call last)
in <cell line: 15>()
13 model = ViTLightningModule()
14 trainer = Trainer(accelerator=âgpuâ, max_epochs=5) #, callbacks=[EarlyStopping(monitor=âvalidation_lossâ)])
â> 15 trainer.fit(model)
15 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
â 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = ,
TypeError: TorchLayer.forward() missing 1 required positional argument: âinputsâ
Thanks @kevinkawchak! I still donât quite understand what the issue youâre facing is. I canât run your code either since there are dependencies missing. However, your forward pass does look problematic:
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
outputs = self.qlayer_1()
return outputs.logits
qlayer_1
probably needs some inputs. You are also overriding what outputs
is after you calculate self.vit(pixel_values=pixel_values)
.
If this isnât the issue, having a complete code example that I can run would help
If youâre just trying to inject some âquantumnessâ into the code provided in that GitHub repository, then definitely thereâs something wrong with your forward pass:
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
outputs = self.qlayer_1()
return outputs.logits
qlayer_1
probably needs inputs, and you are also overriding what outputs
is after you calculate self.vit(pixel_values=pixel_values)
. If qlayer_1
comes after vit
, then youâd need to do something like this:
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
outputs = self.qlayer_1(outputs)
return outputs.logits
The output of vit
would then be the input to qlayer_1
. Also, Iâm not sure that outputs
with have a logits
attribute, so that might have to be changed as well. Let me know if that helps!
Thank you,
Is this what you are suggesting?
class ViTLightningModule(pl.LightningModule):
def init(inputs, self, num_labels=10):
super(ViTLightningModule, self).init()
self.vit = ViTForImageClassification.from_pretrained(âgoogle/vit-base-patch16-224-in21kâ,
num_labels=10,
id2label=id2label,
label2id=label2id)
self.qlayer_1 = qml.qnn.TorchLayer(inputs, qnode, weight_shapes)
Not quite â I think the key might be to just modify your forward
function to this
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
outputs = self.qlayer_1(outputs)
return outputs.logits