遇到了一个报错,大家来帮帮忙

来源:3-7 TensorFlow结合mnist进行卷积模型训练(4)

Noah_________

2018-06-10 12:54

https://img1.mukewang.com/5b1cae050001579b10890537.jpg


各位有遇到这个报错的情况吗?

import os
import model
import tensorflow as tf
import input_data

data = input_data.read_data_sets('MNIST_data', one_hot=True)

# model
with tf.variable_scope("convolutional"):
    x = tf.placeholder(tf.float32, [None, 784], name='x')
    keep_prob = tf.placeholder(tf.float32)
    y, variables = model.convolutional(x, keep_prob)

# train
y_ = tf.placeholder(tf.float32, [None, 10], name='y')
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver(variables)

with tf.Session() as sess:
    merged_summary_op = tf.summary.merge_all()
    summary_writer = tf.summary.FileWriter('/tmp/mnist_log/1', sess.graph)
    summary_writer.add_graph(sess.graph)
    sess.run(tf.global_variables_initializer())

    for i in range(20000):
        batch = data.train.next_batch(50)
        if i % 100 == 0:
                # 发现了,是这里末尾少写参数了,加上 < , keep_prob: 1.0 >就ok啦
            train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
            print("Step %d, training accuracy %g" % (i, train_accuracy))
        sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    print(sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels, keep_prob: 1.0}))

    path = saver.save(
        sess, os.path.join(os.path.dirname(__file__), 'data_convolutional', 'convolutional.ckpt'),
        write_meta_graph=False, write_state=False)

    print("Saved: ", path)


写回答 关注

2回答

  • Noah_________
    2018-06-29 16:09:52
    import os
    import model
    import tensorflow as tf
    import input_data
    
    data = input_data.read_data_sets('MNIST_data', one_hot=True)
    
    # model
    with tf.variable_scope("convolutional"):
        x = tf.placeholder(tf.float32, [None, 784], name='x')
        keep_prob = tf.placeholder(tf.float32)
        y, variables = model.convolutional(x, keep_prob)
    
    # train
    y_ = tf.placeholder(tf.float32, [None, 10], name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    saver = tf.train.Saver(variables)
    
    with tf.Session() as sess:
        merged_summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter('/tmp/mnist_log/1', sess.graph)
        summary_writer.add_graph(sess.graph)
        sess.run(tf.global_variables_initializer())
    
        for i in range(20000):
            batch = data.train.next_batch(50)
            if i % 100 == 0:
                train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})
                print("Step %d, training accuracy %g" % (i, train_accuracy))
            sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
    
        print(sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels, keep_prob: 1.0}))
    
        path = saver.save(
            sess, os.path.join(os.path.dirname(__file__), 'data_convolutional', 'convolutional.ckpt'),
            write_meta_graph=False, write_state=False)
    
        print("Saved: ", path)


  • minip
    2018-06-23 17:20:32

    show me the code

    Noah__...

    找到问题根源了,少写了一个参数(o(╯□╰)o)

    2018-06-29 16:33:00

    共 2 条回复 >

TensorFlow与Flask结合打造手写体数字识别

TensorFlow和flask结合识别自己的手写体数字

20438 学习 · 107 问题

查看课程

相似问题