如何将形状动态输入到占位符

时间:2017-12-03 18:25:22

标签: python-3.x tensorflow placeholder

我有一个函数可以检索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)

我有三个占位符:

  1. char_id:每个[length of bag of words, maximum length of word in bag of words]的char_id形状为batch of sentences
  2. word_id:每个[batch of sentences, maximum length of sentence in batch of sentences]的word_id形状为batch of sentences
  3. 输出:y的形状与word_id相同。
  4. 在调用retrieve函数之前,我不知道形状。我可以使用retrieve函数返回形状,但我不知道如何将它们作为char_idword_idy的输入。我希望从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

0 个答案:

没有答案