Skip to main content
 首页 » 编程设计

python之tensorflow 层 basic_lstm_cell_1 的 LSTM 输入 0

2025年05月04日22thcjp

当我运行代码时,我正在使用 tensorflow 处理 lstm,它向我显示错误。代码运行正常,但是当我运行函数时 tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float64) 它显示值错误

import tensorflow as tf 
 
wordsList = np.load('urduwords.npy') 
wordVectors = np.load('urduwordsMatrix.npy') 
 
batchSize = 24 
lstmUnits = 64 
numClasses = 2 
iterations = 10000 
 
tf.reset_default_graph() 
 
 
labels = tf.placeholder(tf.float32, [batchSize, numClasses]) 
input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength]) 
 
print(labels) 
 
data = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]),dtype=tf.float32) 
print(data) 
 
 
data = tf.nn.embedding_lookup(wordVectors,input_data) 
print(data) 
 
 
lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits) 
lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, output_keep_prob=0.1) 
 
value, _ = tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float64) 

如何使用 tensorflow 解决此错误。

ValueError: Input 0 of layer basic_lstm_cell_1 is incompatible with the layer: expected ndim=2, found ndim=3. Full shape received: [24, 1, 2] 

input_data 的形状是

(24, 30, 1, 2) 

wordVector 的形状是

(24053, 1, 2) 

请您参考如下方法:

标签形状是 4 维的,因为你向 tf 提供了错误类型的数据,

请尝试使用 NumberPy 数组或 List