我是tensorflow的新手,我收到一個錯誤當我運行此:Tensorflow錯誤:形狀'(?,181,192)''張量u'Placeholder_3:0'無法送入形狀的值(100,177,203)'
init = tf.global_variables_initializer()
init
n_epochs = 1000
batch_size = 100
n_batches = len(train_sequences) // batch_size
print(n_batches)
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
train_sequences, train_y, train_lengths = shuffle(train_sequences, train_y, train_lengths)
for iteration in range(n_batches):
start = iteration*batch_size
end = start+batch_size
X_batch = train_sequences[start:end]
y_batch = train_y[start:end]
seq_length_batch = train_lengths[start:end]
if iteration % 20 == 0:
train_summary_str = train_summary_op.eval(
feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
)
step = epoch * n_batches + iteration
train_writer.add_summary(train_summary_str, step)
train_writer.flush()
if iteration % 200 == 0:
summary_str = eval_summary_op.eval(
feed_dict = {X: test_sequences, y: test_y, seq_length: test_lengths}
)
step = epoch * n_batches + iteration
eval_writer.add_summary(summary_str, step)
eval_writer.flush()
sess.run(
training_op,
feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
)
acc_train = accuracy.eval(
feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
)
acc_test = accuracy.eval(
feed_dict = {X: test_sequences, y: test_y, seq_length: test_lengths}
)
print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)
save_path = saver.save(sess, "/home/ubuntu/tensorflow/model.ckpt")
train_writer.close()
eval_writer.close()
我收到的錯誤是:
ValueError: Cannot feed value of shape (100, 177, 203) for Tensor u'Placeholder_3:0', which has shape '(?, 181, 192)'
看起來這是在獲取生成錯誤:
train_summary_str = train_summary_op.eval(
feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
)
更新: 我通過在定義tf佔位符的同時更改尺寸來修正了錯誤。
tf.reset_default_graph()
n_steps = 177
n_inputs = 203
n_neurons = 100
n_outputs = 2
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.int32)
seq_length = tf.placeholder(tf.int32, [None])
basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32, sequence_length=seq_length)
可以顯示完整的代碼;特別是你在哪裏定義你的佔位符?和數據加載 –