Different methods for initializing embedding layer weights in Pytorch

gil.fernandes

There seem to be two ways of initializing embedding layers in Pytorch 1.0 using an uniform distribution.

For example you have an embedding layer:

self.in_embed = nn.Embedding(n_vocab, n_embed)

And you want to initialize its weights with an uniform distribution. The first way you can get this done is:

self.in_embed.weight.data.uniform_(-1, 1)

And another one would be:

nn.init.uniform_(self.in_embed.weight, -1.0, 1.0)

My question is: what is the difference between the first and second initialization form. Do both methods do the same thing?

mujjiga

Both are same

torch.manual_seed(3)
emb1 = nn.Embedding(5,5)
emb1.weight.data.uniform_(-1, 1)

torch.manual_seed(3)
emb2 = nn.Embedding(5,5)
nn.init.uniform_(emb2.weight, -1.0, 1.0)

assert torch.sum(torch.abs(emb1.weight.data - emb2.weight.data)).numpy() == 0

Every tensor has a uniform_ method which initializes it with the values from the uniform distribution. Also, the nn.init module has a method uniform_ which takes in a tensor and inits it with values from uniform distribution. Both are same expect first one is using the member function and the second is using a general utility function.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to invert a PyTorch Embedding?

分類Dev

How to input a list to the embedding layer?

分類Dev

Tensorflow returns same weights in hidden layer

分類Dev

pytorch custom layer "is not a Module subclass"

分類Dev

how to build Sequence-to-sequence autoencoder in keras with embedding layer?

分類Dev

How to use keras embedding layer with 3D tensor input?

分類Dev

Instance methods Vs Static Methods in the business layer

分類Dev

Return layer activations and weights from Tensorflow model in separate threads

分類Dev

Keras layer.weightsとlayer.get_weights()は異なる値を与えます

分類Dev

PyTorch: How to write a neural network that only returns the weights?

分類Dev

Why are there two different ways of initializing a pointer in C

分類Dev

Initializing a smart pointer to an instance of class and accessing its methods

分類Dev

Not using @Transactional and calling persistence layer methods

分類Dev

`layer.get_weights()`は何を返しますか?

分類Dev

How can I load the weights of some layer of a trained network in a new network in returnn?

分類Dev

How do I copy specific layer weights from pretrained models using Tensorflow Keras api?

分類Dev

Get parameters/weights for each layer of the model using the c++ API on OpenVINO

分類Dev

MVC: Passage of the methods in different classes

分類Dev

Calculate time execution for different methods

分類Dev

PyTorch LSTM-nn.Embedding()の代わりに単語の埋め込みを使用

分類Dev

PyTorchのtorch.embeddingの定義はどこにありますか?

分類Dev

Tensorflow: how to restore a inception v3 pre trained network weights after having expanded the graph with new final layer

分類Dev

Single struct adjusting methods to different data types

分類Dev

Reading same input twice, in two different methods

分類Dev

Passing multiple arguments with tidyeval - different methods?

分類Dev

Common methods for different page objects in Cucumber

分類Dev

Dictionary to switch between methods with different arguments

分類Dev

Spring Security: Different authentication methods depending on entity

分類Dev

Java - Separating Class Methods into Different Files

Related 関連記事

  1. 1

    How to invert a PyTorch Embedding?

  2. 2

    How to input a list to the embedding layer?

  3. 3

    Tensorflow returns same weights in hidden layer

  4. 4

    pytorch custom layer "is not a Module subclass"

  5. 5

    how to build Sequence-to-sequence autoencoder in keras with embedding layer?

  6. 6

    How to use keras embedding layer with 3D tensor input?

  7. 7

    Instance methods Vs Static Methods in the business layer

  8. 8

    Return layer activations and weights from Tensorflow model in separate threads

  9. 9

    Keras layer.weightsとlayer.get_weights()は異なる値を与えます

  10. 10

    PyTorch: How to write a neural network that only returns the weights?

  11. 11

    Why are there two different ways of initializing a pointer in C

  12. 12

    Initializing a smart pointer to an instance of class and accessing its methods

  13. 13

    Not using @Transactional and calling persistence layer methods

  14. 14

    `layer.get_weights()`は何を返しますか?

  15. 15

    How can I load the weights of some layer of a trained network in a new network in returnn?

  16. 16

    How do I copy specific layer weights from pretrained models using Tensorflow Keras api?

  17. 17

    Get parameters/weights for each layer of the model using the c++ API on OpenVINO

  18. 18

    MVC: Passage of the methods in different classes

  19. 19

    Calculate time execution for different methods

  20. 20

    PyTorch LSTM-nn.Embedding()の代わりに単語の埋め込みを使用

  21. 21

    PyTorchのtorch.embeddingの定義はどこにありますか?

  22. 22

    Tensorflow: how to restore a inception v3 pre trained network weights after having expanded the graph with new final layer

  23. 23

    Single struct adjusting methods to different data types

  24. 24

    Reading same input twice, in two different methods

  25. 25

    Passing multiple arguments with tidyeval - different methods?

  26. 26

    Common methods for different page objects in Cucumber

  27. 27

    Dictionary to switch between methods with different arguments

  28. 28

    Spring Security: Different authentication methods depending on entity

  29. 29

    Java - Separating Class Methods into Different Files

ホットタグ

アーカイブ