What is the purpose of weights and biases in tensorflow word2vec example?

WarGoth

I'm trying to understand how word2vec example works and don't really understand what is the purpose of weights and biases passed into nse_loss function. There are two variable inputs into the function: weights (plus biases) and embedding.

# Look up embeddings for inputs.
embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)

# Construct the variables for the NCE loss
nce_weights = tf.Variable(
    tf.truncated_normal([vocabulary_size, embedding_size],
                        stddev=1.0 / math.sqrt(embedding_size)))
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

Both are randomly initialized and (as far as I understand) both are subject to updates during learning.

# Compute the average NCE loss for the batch.
loss = tf.reduce_mean(
  tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,
                 num_sampled, vocabulary_size))

I suppose both of them should represent trained model. However weights and biases are never used later on for similarity calculations. Instead, only one component is used:

# Compute the cosine similarity between minibatch examples and all embeddings.
norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
normalized_embeddings = embeddings / norm
valid_embeddings = tf.nn.embedding_lookup(
  normalized_embeddings, valid_dataset)
similarity = tf.matmul(
  valid_embeddings, normalized_embeddings, transpose_b=True)

So what about second component of the model? Why weighs and biases are being ignored?

Thank you.

jorgemf

In word2vec what you want is a vector representation of words. In order to do that you can use, among other things, a neural network. So you have inputs neurons, outputs and hidden layers. What you do to learn the vector representation is to have a hidden layer which number of neurons is the same as the dimension you want in your vectors. There is one input per word and one output per word. And then you train the network to learn the input from the output but in the middle you have a smaller layer which you can see it as a codification of the input in a vector. So here are the weights and biases. But you don't need them later, what you use for testing is a dictionary which contains the word and the vector which represents that word. This is faster than running the neural network to get the representation. That is why you don't see it later.

The last code you write about the cosine distance is to know which vectors are closed to your calculated vector. You have some words (vectors) you make some operations (like: king - man + woman) and then you have a vector that you want to convert in the result. This is the cosine function run among all the vectors (queen would have the minimum distance with the result vector of the operation).

To sum up, you don't see the weight and bias in the validation phase because you don't need them. You use the dictionary you have created in the training.

UPDATE s0urcer has explained better how the vector representation is created.

The input layer and the output layer of the networks represents words. It means the value is 0 if the word is not there and 1 if the word is there. First position is one word, second another one, etc. You have as input/output neurons as words.

The middle layer is the context, or you vector representation of the words.

Now you train the network with sentences or group of consecutive words. From this group you take one word and set it in the inputs and the other words are the outputs of the network. So basically the network learns how a word is related with other words in its context.

To get the vector representation of each word you set the input neuron of that word to 1 and see the values of the context layer (the middle layer). Those values are the values of the vector. As all the inputs are 0 except the word that is 1, those values are the weights of the connections of the input neuron with the context.

You don't use the network later because you don't need to calculate all the values of the context layer, that will be slower. You only need to check in your dictionary what are those values for the word.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What means stddev of nce_weights on word2vec?

From Dev

Save or export weights and biases in TensorFlow for non-Python replication

From Java

Does model.compile() initialize all the weights and biases in Keras (tensorflow backend)?

From Dev

TensorFlow word2vec tutorial input

From Java

What is the purpose of the Tensorflow Gradient Tape?

From Dev

What is the purpose of graph collections in TensorFlow?

From Dev

How to use the param of 'weights' of tensorflow function tf.contrib.legacy_seq2seq.sequence_loss_by_example?

From Dev

How to giving a specific word to word2vec model in tensorflow

From Dev

Error in PySpark trying to run Word2Vec example

From Dev

What is the purpose of an empty loop in this volatile var example?

From Dev

What is the purpose of using new operator in this example?

From Dev

What is the purpose of declaring a method synchronized in the following example

From Dev

What's the purpose of signal pt in this example

From Dev

what does the vector of a word in word2vec represents?

From Dev

What is vector for specific word in CBOW word2vec?

From Java

What is the purpose of the word 'self'?

From Dev

Fizzbuzz Example: What is the purpose of the equal-to operator in this example?

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

What meaning does the length of a Word2vec vector have?

From Dev

What should be used between Doc2Vec and Word2Vec when analyzing product reviews?

From Java

What is the purpose of a question mark after a type (for example: int? myVariable)?

From Dev

C++14 Variable Templates: what is their purpose? Any usage example?

From Dev

What's the purpose of the :printed keyword in this Clojure doto macro example?

From Dev

C++14 Variable Templates: what is their purpose? Any usage example?

From Java

Using a pre-trained word embedding (word2vec or Glove) in TensorFlow

From Dev

Proper use and purpose of mouse weights

From Dev

does word2vec tutorial example imply potential sub-optimal implementation?

From Dev

Spark word2vec example explanation and how to get similarity between strings

Related Related

  1. 1

    What means stddev of nce_weights on word2vec?

  2. 2

    Save or export weights and biases in TensorFlow for non-Python replication

  3. 3

    Does model.compile() initialize all the weights and biases in Keras (tensorflow backend)?

  4. 4

    TensorFlow word2vec tutorial input

  5. 5

    What is the purpose of the Tensorflow Gradient Tape?

  6. 6

    What is the purpose of graph collections in TensorFlow?

  7. 7

    How to use the param of 'weights' of tensorflow function tf.contrib.legacy_seq2seq.sequence_loss_by_example?

  8. 8

    How to giving a specific word to word2vec model in tensorflow

  9. 9

    Error in PySpark trying to run Word2Vec example

  10. 10

    What is the purpose of an empty loop in this volatile var example?

  11. 11

    What is the purpose of using new operator in this example?

  12. 12

    What is the purpose of declaring a method synchronized in the following example

  13. 13

    What's the purpose of signal pt in this example

  14. 14

    what does the vector of a word in word2vec represents?

  15. 15

    What is vector for specific word in CBOW word2vec?

  16. 16

    What is the purpose of the word 'self'?

  17. 17

    Fizzbuzz Example: What is the purpose of the equal-to operator in this example?

  18. 18

    What is the purpose of the tf.contrib module in Tensorflow?

  19. 19

    What is the purpose of the tf.contrib module in Tensorflow?

  20. 20

    What meaning does the length of a Word2vec vector have?

  21. 21

    What should be used between Doc2Vec and Word2Vec when analyzing product reviews?

  22. 22

    What is the purpose of a question mark after a type (for example: int? myVariable)?

  23. 23

    C++14 Variable Templates: what is their purpose? Any usage example?

  24. 24

    What's the purpose of the :printed keyword in this Clojure doto macro example?

  25. 25

    C++14 Variable Templates: what is their purpose? Any usage example?

  26. 26

    Using a pre-trained word embedding (word2vec or Glove) in TensorFlow

  27. 27

    Proper use and purpose of mouse weights

  28. 28

    does word2vec tutorial example imply potential sub-optimal implementation?

  29. 29

    Spark word2vec example explanation and how to get similarity between strings

HotTag

Archive