首页   注册   登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python 学习手册
Python Cookbook
Python 基础教程
Python Sites
PyPI - Python Package Index
http://www.simple-is-better.com/
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
V2EX  ›  Python

求助 Python 大神关于 tensorflow 预测手写数字

  •  
  •   lvming6816077 · 30 天前 · 576 次点击

    本人属于新手入门,憋了几天尝试写了一个 demo,模型是可以训练出来,但是不知道如何将自己写的数字进行预测,代码在这里,总是报错。。 https://github.com/lvming6816077/pythontensorflow/blob/master/tensor9.py

    4 回复  |  直到 2019-04-21 16:02:01 +08:00
        1
    wuyifar   30 天前
    把报错的内容贴出来吧
        2
    lvming6816077   30 天前
    You must feed a value for placeholder tensor 'y' with dtype int64
        3
    lvming6816077   30 天前
    关键代码
    y = tf.placeholder(tf.int64, shape=(None), name = 'y')
    prediction = tf.argmax(y, 1)
    predint = prediction.eval(feed_dict={X: result}, session=sess)
    print(result)
        4
    lvming6816077   28 天前
    # import tensorflow as tf
    # dnn 神经网络
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    # x = tf.Variable(3, name="x")
    # y = tf.Variable(4, name="y")
    # f = x*x*y + y + 2

    # # way1
    # sess = tf.Session()
    # sess.run(x.initializer)
    # sess.run(y.initializer)
    # result = sess.run(f)

    # print(result)
    # sess.close()

    from tensorflow.examples.tutorials.mnist import input_data
    import tensorflow as tf
    from sklearn.metrics import accuracy_score
    import numpy as np

    if __name__ == '__main__':
    n_inputs = 28 * 28
    n_hidden1 = 300
    n_hidden2 = 100
    n_outputs = 10

    mnist = input_data.read_data_sets("tmp/data/")

    X_train = mnist.train.images
    X_test = mnist.test.images
    y_train = mnist.train.labels.astype("int")
    y_test = mnist.test.labels.astype("int")

    X = tf.placeholder(tf.float32, shape= (None, n_inputs), name='X')
    y = tf.placeholder(tf.int64, shape=(None), name = 'y')

    with tf.name_scope('dnn'):
    hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.relu
    ,name= 'hidden1')

    hidden2 = tf.layers.dense(hidden1, n_hidden2, name='hidden2',
    activation= tf.nn.relu)

    logits = tf.layers.dense(hidden2, n_outputs, name='outputs')

    with tf.name_scope('loss'):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,
    logits = logits)
    loss = tf.reduce_mean(xentropy, name='loss')#所有值求平均

    learning_rate = 0.01

    with tf.name_scope('train'):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    training_op = optimizer.minimize(loss)

    with tf.name_scope('eval'):
    correct = tf.nn.in_top_k(logits ,y ,1)#是否与真值一致 返回布尔值
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) #tf.cast 将数据转化为 0,1 序列

    init = tf.global_variables_initializer()

    n_epochs = 20
    batch_size = 50

    # with tf.Session() as sess:
    # saver = tf.train.Saver()
    # init.run()
    # for epoch in range(n_epochs):
    # for iteration in range(mnist.train.num_examples // batch_size):
    # X_batch, y_batch = mnist.train.next_batch(batch_size)
    # sess.run(training_op,feed_dict={X:X_batch,
    # y: y_batch})
    # acc_train = accuracy.eval(feed_dict={X:X_batch,
    # y: y_batch})
    # acc_test = accuracy.eval(feed_dict={X: mnist.test.images,
    # y: mnist.test.labels})
    # print(X_batch.shape)
    # print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)

    # # # saver.restore(sess, "./my_model_final_mnist.ckpt") # or better, use save_path
    # save_path = saver.save(sess, "./tensor9/my_model_final.ckpt")


    from PIL import Image, ImageFilter
    # import tensorflow as tf

    def imageprepare():
    file_name = './5.png' # 图片路径
    myimage = Image.open(file_name).convert('L') # 转换成灰度图
    tv = list(myimage.getdata()) # 获取像素值
    # 转换像素范围到[0 1], 0 是纯白 1 是纯黑
    tva = [(255-x)*1.0/255.0 for x in tv]
    # print(tva)
    tva = np.array(tva)
    # print(tva)
    return tva

    result = imageprepare().reshape(1,784)
    print(mnist.test.images.shape)
    print(result.reshape(1,784).shape)
    # init = tf.global_variables_initializer()
    # saver = tf.train.Saver

    with tf.Session() as sess:
    sess.run(init)
    saver = tf.train.import_meta_graph('./tensor9/my_model_final.ckpt.meta') # 载入模型结构
    saver.restore(sess, './tensor9/my_model_final.ckpt') # 载入模型参数

    y = tf.nn.softmax(y) # 为了打印出预测值,我们这里增加一步通过 softmax 函数处理后来输出一个向量
    # y = tf.cast(y, tf.int64)
    # result = sess.run(y, feed_dict={X: result})

    # graph = tf.get_default_graph() # 计算图
    # # x = graph.get_tensor_by_name("x:0") # 从模型中获取张量 x
    # y = graph.get_tensor_by_name("y:0") # 从模型中获取张量 y
    # y = tf.placeholder(tf.int64, shape=(None), name = 'y')
    X = tf.placeholder(tf.float32, shape= (None, n_inputs), name='X')
    # y =
    prediction = tf.argmax(y, 1)
    predint = prediction.eval(feed_dict={X: result}, session=sess)
    print(result)
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2386 人在线   最高记录 5043   ·  
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 17ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    ♥ Do have faith in what you're doing.
    沪ICP备16043287号-1