FailedPreconditionError:GetNext()失败,因为迭代器尚未初始化

时间:2018-06-13 13:53:47

标签: tensorflow tensorboard

我是tensorflow的新手并试图创建一个简单的MLP。我的模型运行正常,但没有达到预期的性能。我试图创建摘要但现在收到此错误:

  

FailedPreconditionError:GetNext()失败,因为迭代器尚未初始化。确保在获取下一个元素之前已经为此迭代器运行了初始化操作。

我的代码:

def fc_layer(input, channels_in,channels_out, name = "fc"):
with tf.name_scope(name):
    W = tf.Variable(tf.zeros([channels_in, channels_out]), name="weights")
    clip_op = tf.assign(W, tf.clip_by_norm(W, 1, axes = None))
    b = tf.Variable(tf.zeros([channels_out]), name="biases")
    act = tf.matmul(input, W) + b
    tf.summary.histogram("weights", W)
    tf.summary.histogram("biases", b)
    tf.summary.histogram("activations", act)
    return act


# Setup placeholders, and reshape the data
y = tf.placeholder(tf.float32, shape=[None,128], name = 'y')
x = tf.placeholder(tf.float32, shape=[None,256], name = 'x')
dataset = tf.data.Dataset.from_tensor_slices((y, x)).batch(batch_size).repeat()
iter = dataset.make_initializable_iterator()
input_features, output_features = iter.get_next()

fc_1 = tf.nn.relu(fc_layer(input_features, 128,512, name = "fc1"))
fc_2 = tf.nn.relu(fc_layer(fc_1, 512,256, name = "fc1"))
out_layer = fc_layer(fc_2, 256,256, name = "out")

with tf.name_scope('loss'):
    loss_op = 
tf.sqrt(tf.reduce_mean(tf.squared_difference(out_layer,output_features)))
    tf.summary.scalar("loss", loss_op)
with tf.name_scope('train'):
    train_op = 
tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss_op)

#Summary writer
merged_summary = tf.summary.merge_all()
writer = tf.summary.FileWriter(r'C:\Users\Jaweria\Documents\Code_logs', 
graph=tf.get_default_graph())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # initialise iterator with train data
    sess.run(iter.initializer, feed_dict={ y: train_data[0], x: train_data[1], batch_size: Batch_Size})
    print('Training...')
    for i in range(training_epochs):
        tot_loss = 0
        for _ in range(n_batches):
            _, loss_value = sess.run([train_op, loss_op])
            tot_loss += loss_value
            s = sess.run(merged_summary)
            writer.add_summary(s,i*n_batches+ _)
        print("Iter: {}, Loss: {:.4f}".format(i, tot_loss / n_batches))
    # initialise iterator with test data
    sess.run(iter.initializer, feed_dict={ y: test_data[0], x: test_data[1], 
    batch_size: test_data[0].shape[0]})
    print('Test Loss: {:4f}'.format(sess.run(loss_op)))

0 个答案:

没有答案