2

I'm trying to use tf.nn.embedding_lookup() and I get the following warning:

UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.

I read in this link that to avoid this issue we should ensure that the params input to tf.nn.embedding_lookup() is a tf.Variable

But what I pass to tf.nn.embedding_lookup() is already a tensor that is the output of another operation, and I imagine I cannot use it to initialize the tf.Variable.

Is there a way to convert a tensor to tf.Variable or initialize one with another tensor?

1
  • 1
    Hi, does my answer solve your doubt? Commented Jun 24, 2019 at 10:03

1 Answer 1

2

A tf.Variable represents a tensor whose value can be changed by running ops on it.

Internally, a tf.Variable stores a persistent tensor.

Source: https://www.tensorflow.org/guide/variables

You can initialize a tf.Variable with a Tensor object: tf.Tensor

# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)

initial_value: A Tensor, or Python object convertible to a Tensor, which is the initial value for the Variable.

The initial value must have a shape specified unless validate_shape is set to False. Can also be a callable with no argument that returns the initial value when called. In that case, dtype must be specified.

(Note that initializer functions from init_ops.py must first be bound to a shape before being used here.)

In summary, you can initialize a tf.Variable with another Tensor object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.