# Transfer learning error

**URL:** <https://discuss.pennylane.ai/t/transfer-learning-error/1134>\
**Category:** PennyLane Help\
**Created:** [June 20, 2021, 9:06pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134 "2021-06-20T21:06:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 20, 2021, 9:06pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/1 "2021-06-20T21:06:20Z")

</div>

Hi.

When I try to run [https://pennylane.ai/qml/demos/tutorial\_quantum\_transfer\_learning.html](https://pennylane.ai/qml/demos/tutorial_quantum_transfer_learning.html) I get RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking arugment for argument mat2 in method wrapper\_mm)

This has not happened on previous computer, which had no GPU and no CUDA.

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [June 21, 2021, 6:36pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/2 "2021-06-21T18:36:03Z")

</div>

Hi @_risto,

This is an interesting one we haven’t seen before. Would you be able to a minimal code example that generated this error message for you, as well as the output of `qml.about()`? That will better help us diagnose. Thanks!

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 21, 2021, 8:14pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/3 "2021-06-21T20:14:38Z")

</div>

When running this line: model\_hybrid = train\_model(  
model\_hybrid, criterion, optimizer\_hybrid, exp\_lr\_scheduler, num\_epochs=num\_epochs  
)

I get:  
Training started:

C:\Users\risto\anaconda3\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at …\c10/core/TensorImpl.h:1156.)  
return torch.max\_pool2d(input, kernel\_size, stride, padding, dilation, ceil\_mode)

* * *

RuntimeError Traceback (most recent call last)  
 in   
----\> 1 model\_hybrid = train\_model(  
2 model\_hybrid, criterion, optimizer\_hybrid, exp\_lr\_scheduler, num\_epochs=num\_epochs  
3 )

in train\_model(model, criterion, optimizer, scheduler, num\_epochs)  
37 loss = criterion(outputs, labels)  
38 if phase == “train”:  
—\> 39 loss.backward()  
40 optimizer.step()  
41

~\anaconda3\lib\site-packages\torch\_tensor.py in backward(self, gradient, retain\_graph, create\_graph, inputs)  
253 create\_graph=create\_graph,  
254 inputs=inputs)  
–\> 255 torch.autograd.backward(self, gradient, retain\_graph, create\_graph, inputs=inputs)  
256  
257 def register\_hook(self, hook):

~\anaconda3\lib\site-packages\torch\autograd\__init_\_.py in backward(tensors, grad\_tensors, retain\_graph, create\_graph, grad\_variables, inputs)  
145 retain\_graph = create\_graph  
146  
–\> 147 Variable._execution\_engine.run\_backward(  
148 tensors, grad\_tensors_, retain\_graph, create\_graph, inputs,  
149 allow\_unreachable=True, accumulate\_grad=True) # allow\_unreachable flag

~\anaconda3\lib\site-packages\torch\autograd\function.py in apply(self, \*args)  
85 def apply(self, \*args):  
86 # \_forward\_cls is defined by derived class  
—\> 87 return self.\_forward\_cls.backward(self, \*args) # type: ignore[attr-defined]  
88  
89

~\anaconda3\lib\site-packages\pennylane\interfaces\torch.py in backward(ctx, dy)  
173 “”“Implements the backwards pass QNode vector-Jacobian product”""  
174 ctx.dy = dy  
–\> 175 vjp = dy.view(1, -1) @ ctx.jacobian.apply(ctx, \*ctx.saved\_tensors)  
176 vjp = torch.unbind(vjp.view(-1))  
177 return (None,) + tuple(vjp)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking arugment for argument mat2 in method wrapper\_mm)

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 22, 2021, 3:07am UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/4 "2021-06-22T03:07:47Z")

</div>

I have also tried to run some random network from github and I notice the error occuring, while running the following:

device\_name = “cuda:0:” if torch.cuda.is\_available() else “cpu”  
device = torch.device(device\_name)

RuntimeError: Invalid device string: ‘cuda:0:’

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [June 22, 2021, 1:44pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/5 "2021-06-22T13:44:45Z")

</div>

If your priority is just getting the code working, you could try

```auto
device_name = "cpu"

```

This won’t have a potential GPU speedup, but it should at least work.

Let me know if that helps 🙂

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 22, 2021, 1:57pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/6 "2021-06-22T13:57:43Z")

</div>

Hi @christina

Yes, I get the same error: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking arugment for argument mat2 in method wrapper\_mm).

I also get same error when running: device = torch.device(“cuda:0”)

Is it because I use RTX 3060 and those are not compatible with pennylane?  
I would like to scale the code for larger data an more complex resNet, thus would like to use it with GPU, if it would be possible.

---

<div class="post-metadata">

**Author:** ![christina](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/christina/32/583_2.png) [@christina](https://discuss.pennylane.ai/u/christina)\
**Post date:** [June 22, 2021, 4:06pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/7 "2021-06-22T16:06:34Z")

</div>

Looks like there’s a bug in computing the jacobian with a GPU. Thanks so much for bringing this problem up. 👍

A bugfix is currently in the works, so stay tuned.

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 22, 2021, 4:21pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/8 "2021-06-22T16:21:26Z")

</div>

Happy to be of any help. This company & group is doing really amazing things.

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 30, 2021, 2:53am UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/9 "2021-06-30T02:53:30Z")

</div>

Hi @christina

How is the bugfix going?

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [June 30, 2021, 12:06pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/10 "2021-06-30T12:06:53Z")

</div>

Hi @_risto,

The bug fix has now been merged into the master branch of PennyLane - try installing directly from there, and let us know how it goes. Just a heads up, depending on your setup, you might need to use the most recent stable version of torch, which is 1.9 (those of us on the team who tested the fix both have a GTX 1060, and had some card-related issues with 1.8.x that are resolved with 1.9).

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 30, 2021, 2:56pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/11 "2021-06-30T14:56:30Z")

</div>

> [@glassnotes](#):
>
> try installing directly from there

What do you mean by that?  
I was using 1.9 stable version of torch all along while I got the error.

---

<div class="post-metadata">

**Author:** ![theodor](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/theodor/32/178_2.png) [@theodor](https://discuss.pennylane.ai/u/theodor)\
**Post date:** [June 30, 2021, 5:00pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/12 "2021-06-30T17:00:08Z")

</div>

Hi @_risto,

The bug was in PennyLane, but has now been merged into the master branch of PennyLane (so you’d need to install it from there, which can be done by executing `pip install git+https://github.com/PennyLaneAI/pennylane.git ` or by downloading/cloning the repository and installing it directly from the downloaded folder using `pip install .`).

It’s recommended that you use torch v1.9 as well, since that version also resolved some issues that some members of the team had. 🙂

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [June 30, 2021, 11:04pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/13 "2021-06-30T23:04:27Z")

</div>

Hi @theodor

Did the installations of Pennylane. I have been using torch 1.9 all along. Still getting the error.

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [July 1, 2021, 1:35pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/14 "2021-07-01T13:35:55Z")

</div>

Hi @_risto,

Could you please post your error output? Is it exactly the same as before, or is the traceback pointing to a different part of the code now?

Also, could please post the output of running `qml.about()`?

Thanks!

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [July 1, 2021, 2:33pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/15 "2021-07-01T14:33:44Z")

</div>

Hi @glassnotes

So, first Powershell tells me pennylane 17 is installed

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/413e1bc61fedba9c3db56f749fbd633506346398.png)

But then when I run the demo I get that I am using pennylane 16

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/cca7a81e27aafa8f97f15b708f9e8364ee0b3259.png)

And then this error

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/158195756d97154f08fd378d407534a073a5904a.png)

I don’t understand why it says that pennylane 17 is installed, but then it shows 16 while running the code. 🤨

When I try to install it via `conda install git+https://github.com/PennyLaneAI/pennylane.git` I get:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/159002cc45a548b97a339ef2d2b3debe40a6031c.png)

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [July 2, 2021, 12:47am UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/16 "2021-07-02T00:47:15Z")

</div>

Hi @glassnotes

I have solved the issue. I have uninstalled anaconda all together and have installed and run jupyter notebook from pip. But I still get:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/549af7bdb22ce5695d389b11e19e5e047022b032.png)

What does that mean?

In addition, are the following parameters from the demo set to the optimal training performance?  
n\_qubits = 4 # Number of qubits  
step = 0.0004 # Learning rate  
batch\_size = 4 # Number of samples for each training step  
num\_epochs = 30 # Number of training epochs  
q\_depth = 6 # Depth of the quantum circuit (number of variational layers)  
gamma\_lr\_scheduler = 0.1 # Learning rate reduction applied every 10 epochs.  
q\_delta = 0.01 # Initial spread of random quantum weights  
start\_time = time.time() # Start of the computation timer

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [July 2, 2021, 10:22pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/17 "2021-07-02T22:22:01Z")

</div>

Hi @_risto,

The warning seems to be something that Torch raises internally when using functions like `max_pool1d`. The Torch team has been made aware of this happening and they have fixed it in their `master` version. See [their message here](https://github.com/pytorch/pytorch/issues/54846#issuecomment-868489638) from a week ago. According to the Torch team, it will be in the next release. It should be safe to be ignored until then.

As for the hyper-parameters, these were reported in the [original paper](https://arxiv.org/pdf/1912.08278.pdf) to provide high enough accuracy for transfer learning. Would you be interested in optimal training performance in terms of accuracy or execution time?

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [July 3, 2021, 3:12am UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/18 "2021-07-03T03:12:12Z")

</div>

Hi @antalszava

Thank you for your answer.  
Yes, I would be interested in setup for optimal performance. I am playing and changing parameters, but I don’t know how practical that would be.  
I am also trying to run some other models from torch (f.e.: resnext101\_32x8d).

But then it says Training started, but nothing is happening. However, my GPU usage is at 100%.

![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/00f68dbe3564c1a2e31257e04d1dcc83bc736b04.png)

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/680d3249d6774f344dd49f9069ad42afae28585e.png)

What does that mean?

Also, I have managed to run the algorithm on resnet152, both on a quantum and a classical one, with same parameters (batch size, epochs, etc.), on the same data and the classical one has better test/val acc. as well as better speed (the quantum model barely uses my GPU). So what would be an advantage of using the hybrid quantum model in compare to the classical one?

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [July 5, 2021, 10:15pm UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/19 "2021-07-05T22:15:56Z")

</div>

Hi @_risto,

The parameters in the paper present a scenario where the training seemed to work well, but there may be other configurations that also work nicely or even better. It’s worth exploring, but there aren’t many trivial approaches as hyperparameter optimization is an established problem in itself. There are some techniques that can be worth explored (e.g., doing a grid search of the parameters).

Was that something that came up specifically for resnext101\_32x8d? Naively, it would be an indication that the computation is still ongoing. Did that not occur when using only CPUs?

> So what would be an advantage of using the hybrid quantum model in compare to the classical one?

The advantage of using quantum machine learning algorithms compared to similar classical machine learning algorithms is an open research question. The paper on transfer learning presents proof of concept ideas and verifies that they produce accurate results on quantum hardware too.

---

<div class="post-metadata">

**Author:** ![\_risto](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/_risto/32/237_2.png) [@\_risto](https://discuss.pennylane.ai/u/_risto)\
**Post date:** [July 6, 2021, 1:12am UTC](https://discuss.pennylane.ai/t/transfer-learning-error/1134/20 "2021-07-06T01:12:31Z")

</div>

Hi @antalszava

I have run resnet152 both as classical and modified to this quantum demo with same parameters and same data. The result: classical has better validation / test accuracy and much faster (it took classical 2 min, whereas quantum one 20 min). Too bad, was hoping for an advantage of the quantum one.

[Next page](https://discuss.pennylane.ai/t/transfer-learning-error/1134.md?page=2)
