I cannot understand the code:" w = copy.deepcopy(w_in) # avoid modifying global w_in"
Why we should write it?Thanks
Hi!
That’s a good question because w gets assigned to w_in later in the method, but let me try to explain some rational behind using a deepcopy in general.
Python uses something called “Call by assignment”: Is Python call by reference or call by value - GeeksforGeeks.
This means that there are some types: numbers, strings, tuples, that are not able to be changed when passed into a function. However there are some types like List that are able to be changed in a method. This means if you append something to a List inside the method the List outside of the method also gets that value appended to it.
A deep copy is being done to create a new object w to store the information from w_in.
When that new object w is modified the global w_in object outside of the method is guaranteed not to be changed.
Also if the global w_in variable is changed the values in w are guaranteed not to be changed.
If you want more details on the copy.deepcopy method:
Please see the python docs on copy: copy — Shallow and deep copy operations — Python 3.10.7 documentation
Got it!Thank you so much!
Repeated. See Optional Lab-------Gradient Descent