2017-09-26 61 views
0

我正在嘗試編寫我自己的基本seq2seq分類器。我通過使用tf.nn.dynamic_rnn來做到這一點,代碼如下所示。但是,我發送給tf.nn.dynamic_rnn的張量的形狀似乎有問題。我這樣做的原因是因爲tensorflow的文檔涉及到seq2seq的時候非常多。seq2seq tf.nn.dynamic_rnn形狀錯誤

運行

import numpy as np 
source_batch = np.random.randint(x_letters, size=[batch_size, x_seq_length]) 
target_batch = np.random.randint(y_letters, size=[batch_size, y_seq_length+1]) 

sess.run(tf.global_variables_initializer()) 
loss = sess.run([loss], 
      feed_dict = {inputs: source_batch, 
         outputs: target_batch[:, :-1], 
         targets: target_batch[:, 1:]}) 

給我的錯誤:ValueError: Cannot feed value of shape (128, 10) for Tensor 'decoding/rnn/transpose:0', which has shape '(128, 10, 32)'

圖表如下所示:

import tensorflow as tf 

x_seq_length = 29 
y_seq_length = 10 

x_letters = 60 
y_letters = 13 

epochs = 2 
batch_size = 128 
nodes = 32 
embed_size = 10 

#################### 
# Tensorflow Graph 
#################### 
tf.reset_default_graph() 
sess = tf.InteractiveSession() 

inputs = tf.placeholder(tf.int32, (batch_size, x_seq_length), 'inputs') 
outputs = tf.placeholder(tf.int32, (batch_size, y_seq_length), 'output') 
targets = tf.placeholder(tf.int32, (batch_size, y_seq_length), 'targets') 

input_embedding = tf.Variable(tf.random_uniform((x_letters, embed_size), -1, 1), name='enc_embedding') 
output_embedding = tf.Variable(tf.random_uniform((y_letters, embed_size), -1, 1), name='dec_embedding') 

date_input_embed = tf.nn.embedding_lookup(input_embedding, inputs) 
date_output_embed = tf.nn.embedding_lookup(output_embedding, outputs) 

with tf.variable_scope("encoding") as encoding_scope: 
    lstm_enc = tf.contrib.rnn.BasicLSTMCell(nodes) 
    _, last_state = tf.nn.dynamic_rnn(lstm_enc, dtype=tf.float32,inputs=date_input_embed) 

with tf.variable_scope("decoding") as decoding_scope: 
    lstm_dec = tf.contrib.rnn.BasicLSTMCell(nodes) 
    outputs, _ = tf.nn.dynamic_rnn(lstm_dec, inputs=date_output_embed, initial_state=last_state) 

logits = tf.contrib.layers.fully_connected(outputs, num_outputs=y_letters, activation_fn=None) 

with tf.name_scope("optimization"): 
    loss = tf.contrib.seq2seq.sequence_loss(logits, targets, tf.ones([batch_size, y_seq_length])) 
    optimizer = tf.train.AdamOptimizer().minimize(loss) 

回答

2

你必須通過名稱outputs,即佔位符和所述解碼器輸出的兩個變量。更改一個變量的名稱。

+0

那真是尷尬。 –