如何正确使用tf.nn.embedding_lookup并查看Tensor对象的具体值?
Hey there, let's work through your TensorFlow 1.x embedding issue step by step—you're trying to inspect tensor values and use tf.nn.embedding_lookup correctly, right? Let's break down what's going wrong and fix it.
First, let's unpack your errors
- The
tf.constant([1][0])syntax is invalid:[1][0]evaluates to the integer1, so trying to do[0][0][1]is like trying to index an integer, which throwsTypeError: 'int' object is not subscriptable. Similarly,[0][1][0]fails because the list[0]only has one element, so index1doesn't exist—hence theIndexError. - You're misusing
tf.nn.embedding_lookup: this function takes an embedding weight matrix and a tensor of indices to look up, not a pre-computed embedding tensor likeX_embedded. - You forgot to initialize variables: In TensorFlow 1.x, all trainable variables (like the weights in your Embedding layer) need to be initialized before you can evaluate their values in a session.
Fixed code example
Let's rewrite your code to correctly build the embedding, use tf.nn.embedding_lookup, and inspect tensor values. I'll assume pos is your positional embedding tensor, and X is your input sequence of word indices:
import tensorflow as tf from tensorflow import keras # Your parameter dictionary (vocab size 30, embedding dim 128) params = {'30': 30, '128': 128} # 1. Define your input index tensor (example: 1 sample, 25 tokens) # Replace this with your actual input indices X = tf.constant([list(range(25))], dtype=tf.int32) # 2. Build word embedding layer and generate embeddings word_embedding_layer = keras.layers.Embedding( input_dim=params['30'], output_dim=params['128'], embeddings_initializer='glorot_normal' ) word_embeddings = word_embedding_layer(X) # 3. Add positional embedding (replace with your actual pos tensor) # Example positional embedding matching shape (1,25,128) pos = tf.random.normal(shape=(1, 25, 128)) final_embeddings = word_embeddings + pos # 4. Correct usage of tf.nn.embedding_lookup # Grab the embedding weight matrix from the layer embedding_weights = word_embedding_layer.weights[0] # Define indices you want to look up (e.g., indices 5 and 10) lookup_indices = tf.constant([5, 10], dtype=tf.int32) lookup_results = tf.nn.embedding_lookup(embedding_weights, lookup_indices) # 5. Evaluate tensors in a session with tf.Session() as sess: # Initialize all variables first! sess.run(tf.global_variables_initializer()) # Inspect the final embedding tensor values final_embeds_val = sess.run(final_embeddings) print("Final Embeddings Shape:", final_embeds_val.shape) print("First token's embedding from the first sample:\n", final_embeds_val[0][0]) # Inspect the embedding lookup results lookup_val = sess.run(lookup_results) print("\nEmbeddings for indices 5 and 10:\n", lookup_val)
Key takeaways to remember
- Always initialize variables: Call
sess.run(tf.global_variables_initializer())before evaluating any tensors that depend on trainable variables (like Embedding weights). - Use
tf.nn.embedding_lookupproperly: Pass the embedding weight matrix (fromEmbeddingLayer.weights[0]) as the first argument, and your target indices as the second argument. - Inspect tensors correctly: Use
sess.run(tensor)ortensor.eval(session=sess)to get the actual numeric values of a tensor—just make sure all dependencies are initialized. - Avoid invalid tensor construction: Don't chain list indices inside
tf.constant()—if you need a constant tensor with values like[0,0,1], writetf.constant([[0,0,1]])directly.
内容的提问来源于stack exchange,提问作者Carl Choi
相关产品推荐
相关产品推荐

