我有一个函数可以检索batch of sentences
,从句子中的单词创建bag of words
,从单词包中的单词创建character id matrix
。
char_id_batch, word_id_batch, pos_id_batch = temp.retrieve_batch_sent(start, batch_size_counter)
我有三个占位符:
[length of bag of words, maximum length of word in bag of words]
的char_id形状为batch of sentences
。[batch of sentences, maximum length of sentence in batch of sentences]
的word_id形状为batch of sentences
。y
的形状与word_id相同。在调用retrieve
函数之前,我不知道形状。我可以使用retrieve
函数返回形状,但我不知道如何将它们作为char_id
,word_id
和y
的输入。我希望从feed_dict
推断形状,但它们不是。
部分代码:
char_id = tf.placeholder(dtype=tf.int32, shape=[None, None])
word_id = tf.placeholder(dtype=tf.int32, shape=[batch_size, None])
y = tf.placeholder(dtype=tf.int32, shape=[batch_size, None])
with tf.name_scope("CharacterLayer"):
with tf.variable_scope("CharacterLayer"):
char_embeddings = tf.Variable(tf.truncated_normal(shape=[char_codes, char_embed_size]))
char_lookup = tf.nn.embedding_lookup(char_embeddings, char_id)
char_train = tf.unstack(value=char_lookup, axis=1)
char_lstm_cell = rnn.BasicLSTMCell(word_embed_size, forget_bias=1)
output_words, _ = rnn.static_rnn(cell=char_lstm_cell, inputs=char_train, dtype=tf.float32)
for i in range(1, itr):
char_id_batch, word_id_batch, pos_id_batch = temp.retrieve_batch_sent(start, batch_size_counter)
start = batch_size_counter
batch_size_counter = batch_size + batch_size_counter
sess.run(train_op, feed_dict={char_id: char_id_batch, word_id: word_id_batch, y: pos_id_batch})
我知道如果参数未指定且不可推断,则tf.unstack
不起作用。这就是我收到错误的原因:ValueError: Cannot infer num from shape (?, ?, 10)
。
是否有方法可以提供shapes
,还是必须手动输入shapes
中的placeholders
?