简体   繁体   English

张量流中basic_lstm_cell_1层的LSTM输入0

[英]LSTM Input 0 of layer basic_lstm_cell_1 in tensorflow

I am working with lstm using tensor flow when I am running the code it is showing me the error. 当我运行代码时,我正在使用张量流使用lstm,它向我显示错误。 the code is running fine but when I am running the function tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float64) it is showing Value ERROR 代码运行正常,但是当我运行函数tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float64)它显示值ERROR

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)

How to resolve this error using tensor flow. 如何使用张量流解决此错误。

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]

the shape of the input_data is input_data的形状是

(24, 30, 1, 2)

and the shape of wordVector is 并且wordVector的形状是

(24053, 1, 2)

Since you have not provided a standalone code to reproduce the bug, i have a sample working code as shown below: 由于您尚未提供独立的代码来重现该错误,因此我有一个示例工作代码,如下所示:

VOCAB_SIZE = 128
HIDDEN_SIZE = 200

wordVectors = tf.Variable(tf.random_uniform([VOCAB_SIZE, HIDDEN_SIZE], -1, 1))

labels = tf.random_normal([batchSize, numClasses])
input_data = tf.random_uniform([batchSize, maxSeqLength], maxval=120, dtype=tf.int32)

data = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]),dtype=tf.float32)

data = tf.nn.embedding_lookup(wordVectors,input_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.float32)

I have changed the data type of tf.nn.dynamic_rnn to tf.float32 to fix the data type error. 我已将tf.nn.dynamic_rnn的数据类型tf.nn.dynamic_rnntf.float32以修复数据类型错误。

the label shape is 4 dimension because of you feed the wrong type of data to tf, 标签形状为4维,因为您向tf输入了错误的数据类型,

please try to use NumberPy array or List 请尝试使用NumberPy数组或列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM