Hi @_risto,
We are looking into resolving the issues in the demo. Depending on the specific solution needed, this could take some time but will get back as soon as it’s working. ![]()
Hi @_risto,
We are looking into resolving the issues in the demo. Depending on the specific solution needed, this could take some time but will get back as soon as it’s working. ![]()
Hi @antalszava
Thanks for the update
Hopefully comes soon, eager to try the experiment. As I remember it was intent to use it as a last layer of ResNet18?
Cheers,
Risto
Hi @_risto, yes you’re correct, the idea was to use this in ResNet18. The team is still working on resolving the issues in the demo, and we will let everyone here know when it’s up and running!
Hi. How does one find an address of a direct path in Linux?
Cheers,
Risto
Hi @_risto! Could you provide a few more details?
Hi @josh.
Sure. In previous steps we have specified our path to data as following: changing data_dir = "../_data/hymenoptera_data" line to: data_dir = os.path.abspath(r"C:\MyPath\pl_tutorials\_data\hymenoptera_data") where MyPath will be unique to our system.
Where in Linux can I find this direct link / address of the folder “r"C:\MyPath\pl_tutorials_data\hymenoptera_data”" in order to copy paste it into the code?
Cheers,
Risto
Ah, got it ![]()
If you navigate in a terminal to the directory with the data, you can run the command pwd. This will give you the location of the directory.
Hi again ![]()
How could I put this quantum layer in various ResNet networks? (50, 101,…)? Is it possible or is it structures only to work on ResNet18?
Best,
Risto
Hi @_risto,
Thanks for the question. As this post thread is now getting very long, would you be able to make a standalone post with your new question? This will help users find targeted questions & answers a bit easier. ![]()
Thanks!
Hi!, in the embedding layer what is the necessity of RY gates after Hadamard gates?
And also what is the significance of using (pi/2) as a constant scaling factor, I understand that it is used for converting to quantum data but why (pi/2) in particular?
Hi @mahesh,
the RY(x_j) gates are used to apply a quantum operation which depends on the input classical parameters x_j. As you said this can be used to convert classical data to quantum data: x_j --> |quantumstate(x_j) > .
The choice of the gate is quite arbitrary and other methods can be used: https://pennylane.readthedocs.io/en/stable/introduction/templates.htm
The choice of the pi/2 scaling is quite arbitrary too. The motivation was that a “typical” domain for classical data is often within [-1, 1], while a more natural domain for quantum rotation angles is [-pi/2, pi/2].
Hi !! , How is it beneficial to run on IBM’s real quantum device than Pennylane noiseless simulators? Ultimately only the final weights matter. Pennylane Simulators generate a model with better accuracy, so the generated weights can perform the classification.
Thanks for your question! Due to subject and the length of the existing thread, could you please make a new thread for your question and add a bit of context? This will help others with similar questions find the information more easily.
Thanks!
I don’t suppose there’s been any updates on the quantum embedding & metric learning demo code? I’ve had a quite a thorough look through it myself (both the data prep code and the embedding code) and tried retraining the paramaters for quite a high number of steps (1,500 and 10,000, where the latter shows no improvement over the former) but I can’t see anything obvious that’s wrong and I’m having no luck with classification (I’m seeing no decrease in cost function, nor a subsequent increase in the ability to classify).
When I do use the original pretrained parameters and the initial ants/bees txt files provided in earlier versions of the repo, the code does of course classify. However, in the initial provided txt files, the contents of the training and test data files are identical (which I’m sure shouldn’t be the case), and is of course the reason why the test data is classified well but the training data is not, when using the most recent versions of the provided ant/bee txt files.
I just can’t seem to replicate the code’s ability to classify when using a freshly generated, ‘correct’ set of ant/bee txt files (from image_to_resnet_output.py). This is the case both when I’m using newly trained parameters, but also with the provided pretrained ones.
I do think the problem may lie in the paramater training steps (the loop that uses the RMSPropOptimizer), however.
The reason is that even when I use the old ant/bee txt files (where the training/test files are identical) and run the training loop for 1500 steps, the program is still unable classify neither the training data, nor the test data (the training and test costs each still remain above 0.95). It feels as though the parameter training code provided in the demo must vary in some way from the original parameter training code used (which I of course do not have access to, if it does indeed vary) to generate the pretrained parameters.
One more thing to note is that I do get the following warning message when running the embedding code, although I’m not sure how important it is:
UserWarning: Attempted to differentiate a function with no trainable parameters. If this is unintended, please add trainable parameters via the ‘requires_grad’ attribute or ‘argnum’ keyword.
I can try playing around with the parameter training/optimizing code a bit and I could try debugging the warning message but other than that I feel like I’ve about exhausted the things I can try to get it to work.
Would be great to hear an update or to get some thoughts on the problem! ![]()
Thanks,
Jonathan
Hi @Jonathan_Kim1, welcome to the forum!
Thank you for asking this question here. There have been some strange behaviours regarding that demo as you can see here.
We will take a deeper look into the warning you’re getting and the specific details of the issue you’re facing. We will be back with an answer soon.
Thank you for bringing this to our attention!
Thanks for the reply.
Just thought it would be worth mentioning, the specific demo I am looking into is actually the embeddings & metric learnings one (which was actually removed from the repo around two years ago due to the problems in question), as opposed to the transfer learning one - Maria Schuld and _risto were discussing the problem in this thread back in September 2020 (even though this thread was originally about the transfer learning demo, not the embeddings & metric learning demo).
It is interesting however, that from the link you sent, there seems to be a better test performance than training performance within the transfer learning demo too. This is also observed within the embedding & metric learning demo, if one uses the correct (freshly generated) ant/bee txt files alongside the pretrained parameters provided by the demo. But in this case, it’s more that the training set is not classified at all, while the test set is classified perfectly. When the parameters are generated from scratch by using the code in the demo (instead of using the pretrained parameters), nothing classifies properly at all, however (neither training set nor test set).
Thanks,
Jonathan
Hi @Jonathan_Kim1,
About the issue of validation vs training loss, I just left a possible explanation at this link.
I don’t know instead the answer to your further questions.
Best wishes,
Andrea
Hi @Jonathan_Kim1,
Thank you for the clarification!
As far as I know there haven’t been any updates to the quantum embedding & metric learning demo code (which was removed a few years ago).
Thank you @andreamari for the explanation here!
Having looked into the problem further I’m pretty certain that the issue is associated with the aforementioned UserWarning: Attempted to differentiate a function with no trainable parameters. If this is unintended, please add trainable parameters via the ‘requires_grad’ attribute or ‘argnum’ keyword. error. The parameters are not changing at all between each iteration step. The associated part of the code is as below:
# generate initial parameters for circuit
init_pars_quantum = np.random.normal(loc=0, scale=0.1, size=(4, 3))
# generate initial parameters for linear layer
init_pars_classical = np.random.normal(loc=0, scale=0.1, size=(2, 512))
init_pars = [init_pars_classical, init_pars_quantum]
optimizer = qml.RMSPropOptimizer(stepsize=0.01)
batch_size = 5
pars = init_pars
selectA = np.random.choice(range(len(A)), size=(batch_size,), replace=True)
selectB = np.random.choice(range(len(B)), size=(batch_size,), replace=True)
A_batch = [A[s] for s in selectA]
B_batch = [B[s] for s in selectB]
for i in range(100):
# Sample a batch of training inputs from each class
selectA = np.random.choice(range(len(A)), size=(batch_size,), replace=True)
selectB = np.random.choice(range(len(B)), size=(batch_size,), replace=True)
A_batch = [A[s] for s in selectA]
B_batch = [B[s] for s in selectB]
print(selectA)
print(selectB)
print(A_batch[0][0])
print(B_batch[0][0])
# Walk one optimization step
pars = optimizer.step(lambda w: cost(w, A=A_batch, B=B_batch), pars)
print(pars)
print("Step", i, "done.")
# Print the validation cost every 5 steps
if i % 5 == 0 and i != 0:
cst = cost(pars, A=A_val[:20], B=B_val[:20])
print("Cost on validation set {:2f}".format(cst))
Specifically, the line causing the warning message is:
pars = optimizer.step(lambda w: cost(w, A=A_batch, B=B_batch), pars)
The warning message is mentioned within here, but I’m not really sure how to approach it.
Hi @Jonathan_Kim1,
What this warning means is that pars is never set as trainable. You could try defining pars = np.array(init_pars,requires_grad=True), although I’m not sure if this will fix all of your problems.
Please let me know if this does help! Otherwise please make sure to share the full code so that I can try to replicate the issue.