无效参数异常索引不在 TensorFlow 中的词嵌入向量中

Invalid Argument Exception Indices Not in Vectors in Word Embeddings in TensorFlow

我正在尝试 运行 LSTM 代码并为此尝试连接 word2Vec 词嵌入输入,但在获取嵌入查找时出错。

代码如下:

batchSize = 24
lstmUnits = 64
numClasses = 2
iterations = 100000
maxSeqLength = 250
numDimensions = 128    
import tensorflow as tf
tf.reset_default_graph()

labels = tf.placeholder(tf.float32, [batchSize, numClasses])
input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength])

data = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]),dtype=tf.float32)
# word Vector Shape = (13277, 128)
data = tf.nn.embedding_lookup(wordVectors,input_data)




saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
try:
    for i in range(iterations):
   #nextBatch shape is (24, 250)
        nextBatch, nextBatchLabels = getTrainBatch()
        sess.run(optimizer, feed_dict={input_data: nextBatch, labels: nextBatchLabels})
except Exception as ex:
    print(ex)

我可能遗漏了一小步。它可以是什么。 当我 运行 代码时,我得到异常:

我已经简化了您报告的代码,以便您了解如何在您的案例中使用词嵌入。此外,您还没有指定所有内容(请参阅 optimizer 变量),因此无法完全重现您的代码。

我在这里报告一个简单的片段,它允许您从形状为 (batchSize, maxSeqLength) 的输入矩阵中获取词嵌入。

batchSize = 24
lstmUnits = 64
numClasses = 2
iterations = 100000
maxSeqLength = 250
numDimensions = 128
numTokens = 50

import tensorflow as tf
import numpy as np

session = tf.InteractiveSession()
input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength])
# you should NOT use tf.Variable() but tf.get_variable() instead
embeddings_weights = tf.get_variable("embeddings_weights",  initializer=tf.random_normal_initializer(0, 1), shape=(numTokens, numDimensions))
input_embeddings = tf.nn.embedding_lookup(embeddings_weights, input_data)
result = session.run(input_embeddings, feed_dict={input_data: np.random.randint(0, numTokens, (batchSize, maxSeqLength))})
print(result.shape)
// should print (24, 250, 300)

如果您想了解收到该错误的原因,您应该调试代码并查看指定的训练数据中是否存在无效索引。在我的代码片段中,通过使用 np.random.randint(),我强制输出元素在 (0, numTokens) 范围内,以避免出现错误。发生这种情况是因为 TensorFlow 无法完成超出范围的 ID 的查找操作!