我正在尝试运行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)
答案 0 :(得分:0)
我已经简化了您报告的代码,以便让您了解如何在您的案例中使用字嵌入。此外,您还没有指定所有内容(请参阅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的查找操作!