parameter
官網API
其可以將普通張量轉變為模型參數的一部分。Parameters是Tensor的一個子類,當用於Module時具有非常特殊的屬性,當其被賦予為模塊的屬性時,他們自動地添加到模塊參數列表中,且將會出現在如parameters()迭代器中。如果賦予一個普通張量則沒有這樣的效果。這是由於人們可能想要在模型中緩存一些臨時狀態,如RNN中的上一個隱層狀態。如果沒有像Parameter這樣的類,這些緩存狀態也將被注冊(只需要緩存,而不是反復注冊)。
Answer 1
I will break it down for you. Tensors, as you might know, are multi dimensional matrices. Parameter, in its raw form, is a tensor i.e. a multi dimensional matrix. It sub-classes the Variable class.
The difference between a Variable and a Parameter comes in when associated with a module. When a Parameter is associated with a module as a model attribute, it gets added to the parameter list automatically and can be accessed using the 'parameters' iterator.
Initially in Torch, a Variable (which could for example be an intermediate state) would also get added as a parameter of the model upon assignment. Later on there were use cases identified where a need to cache the variables instead of having them added to the parameter list was identified.
One such case, as mentioned in the documentation is that of RNN, where in you need to save the last hidden state so you don't have to pass it again and again. The need to cache a Variable instead of having it automatically register as a parameter to the model is why we have an explicit way of registering parameters to our model i.e. nn.Parameter class.
最初在Torch中,Variable(例如可以是中間狀態)也會在賦予時作為模型的參數添加。 后來,確定了需要緩存變量而不是將它們添加到參數列表中的情況。這種情況下,如文檔中提到的RNN,您需要保存最后一個隱藏狀態,這樣就不必一次又一次地傳遞它。需要緩存一個變量,而不是讓它自動注冊為模型的參數,這就是為什么我們有一個顯式的方式注冊parameter到我們的模型,即nn.Parameter類
Answer 2
Recent PyTorch releases just have Tensors, it came out the concept of the Variable has deprecated. (Variable已被棄用)
Parameters are just Tensors limited to the module they are defined (in the module constructor __init__
method).
They will appear inside module.parameters()
. This comes handy when you build your custom modules, that learn thanks to these parameters gradient descent.
Anything that is true for the PyTorch tensors is true for parameters, since they are tensors.
Additionally, if module goes to GPU, parameters goes as well. If module is saved parameters will also be saved.
There is a similar concept to model parameters called buffers.
These are named tensors inside the module, but these tensors are not meant to learn via gradient descent, instead you can think these are like variables. You will update your named buffers inside module forward()
as you like.
For buffers, it is also true they will go to GPU with the module, and they will be saved together with the module.
Are Parameter
s only limited being used in __init__()
? No, but it is most common to define them inside the __init__
method.
https://stackoverflow.com/questions/50935345/understanding-torch-nn-parameter
https://www.jianshu.com/p/d8b77cc02410
parameter和variable的區別
https://www.jianshu.com/p/cb739922ce88
https://blog.csdn.net/u014244487/article/details/104372441
parameter和buffer的區別
一個基本的區別是parameter會被更新,而buffer不會。
There is a similar concept to model parameters called buffers. These are named tensors inside the module, but these tensors are not meant to learn via gradient descent, instead you can think these are like variables. You will update your named buffers inside module forward()
as you like.
https://www.cnblogs.com/jiangkejie/p/13154058.html
https://zhuanlan.zhihu.com/p/89442276