Skip to content
Advertisement

How to avoid two variables refering to the same data? #Pytorch

During initializing, I tried to reduce the repeats in my code, so instead of:

JavaScript

I wrote:

JavaScript

However, I find that the second method is wrong.

If I assign the data to variables h,c, any change on h would also be applied to c

JavaScript

results of the test above:

JavaScript

Is there a more elegent way to initialize two indenpendent variables?

Advertisement

Answer

I agree that your initial line needs no modification but if you do want an alternative, consider:

JavaScript

The reason the other one (output = (z,z)) doesn’t work, as you’ve correctly discovered is that no copy is made. You’re only passing the same reference in each entry of the tuple to z

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement