Python tf.nn.seq2seq中的动态形状误差
我正在尝试编写自己的基本seq2seq分类器。我使用Python tf.nn.seq2seq中的动态形状误差,python,tensorflow,Python,Tensorflow,我正在尝试编写自己的基本seq2seq分类器。我使用tf.nn.dynamic\u rnn来实现这一点,代码如下所示。然而,我发送到tf.nn.dynamic\u rnn的张量的形状似乎有问题。我这样做的原因是因为tensorflow关于seq2seq的文档到处都是 运行 import numpy as np source_batch = np.random.randint(x_letters, size=[batch_size, x_seq_length]) target_batch = np
tf.nn.dynamic\u rnn
来实现这一点,代码如下所示。然而,我发送到tf.nn.dynamic\u rnn
的张量的形状似乎有问题。我这样做的原因是因为tensorflow关于seq2seq的文档到处都是
运行
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:无法为具有shape'(128,10,32)
的张量'decoding/rnn/transpose:0'提供shape(128,10)的值
图表如下所示:
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)
名为
的输出有两个变量,即占位符和解码器输出。更改一个变量的名称。您有两个名为输出的变量,即占位符和解码器输出。更改一个变量的名称。那是令人尴尬的。那是令人尴尬的。