我是Tensorflow的新手,请耐心等待我,我正在关注this教程并遇到一个我不知道如何解决的错误。有人可以帮我吗?提前谢谢。
这是我尝试执行的代码:
def train():
model = Model()
with tf.Graph().as_default():
images, val_images, labels, val_labels = load_training_data()
x = tf.placeholder(shape=[FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 1], dtype=tf.float32, name='x')
y = tf.placeholder(shape=[FLAGS.batch_size, NUM_LABELS], dtype=tf.float32, name='y')
keep_prob = tf.placeholder(tf.float32, name='dropout_prob')
global_step = tf.contrib.framework.get_or_create_global_step()
logits = model.inference(x, keep_prob=keep_prob)
loss = model.loss(logits=logits, labels=y)
accuracy = model.accuracy(logits, y)
summary_op = tf.summary.merge_all()
train_op = model.train(loss, global_step=global_step)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
writer = tf.summary.FileWriter(FLAGS.summary_dir, sess.graph)
print(writer)
sess.run(init)
for i in range(FLAGS.num_iter):
offset = (i * FLAGS.batch_size) % (len(images) - FLAGS.batch_size)
batch_x, batch_y = images[offset:(offset + FLAGS.batch_size), :], labels[
offset:(offset + FLAGS.batch_size), :]
_, cur_loss, summary = sess.run([train_op,
loss,
summary_op],
feed_dict={
x: batch_x,
y: batch_y,
keep_prob: 0.5
})
writer.add_summary(summary, i)
print(i, cur_loss)
if i % 1000 == 0:
validation_accuracy = accuracy.eval(feed_dict={x: val_images, y: val_labels, keep_prob: 1.0})
print('Iter {} Accuracy: {}'.format(i, validation_accuracy))
if i == FLAGS.num_iter - 1:
saver.save(sess, FLAGS.checkpoint_file_path, global_step)
这是错误日志:
TypeError Traceback (most recent call last)
<ipython-input-72-8d21e7b24473> in <module>()
49 saver.save(sess, FLAGS.checkpoint_file_path, global_step)
50
---> 51 train()
<ipython-input-72-8d21e7b24473> in train()
36 x: batch_x,
37 y: batch_y,
---> 38 keep_prob: 0.5
39 })
40
我还需要补充一点,我目前正在使用Kaggle笔记本电脑,所以如果这影响了一些事情,请告诉我。
答案 0 :(得分:0)
看起来其中一个train_op
,loss
或summary_op
为无。
您没有提供完整的代码,所以我不能多说。检查Model
班级的相应功能。
此外,如果在Notebook环境中工作,请确保在进行一些更改后执行了所有相关单元格。例如,您可以尝试从头开始执行所有单元格。