首页   注册   登录

lvming6816077

V2EX 第 70501 号会员,加入于 2014-08-11 13:43:15 +08:00
ok
求助 Python 大神关于 tensorflow 预测手写数字
Python  •  lvming6816077  •  233 天前  •  最后回复来自 lvming6816077
4
自荐一门移动 web 实战课程
程序员  •  lvming6816077  •  355 天前  •  最后回复来自 lvming6816077
2
郑州 web 前端求工作!
求职  •  lvming6816077  •  2018-04-04 19:23:35 PM  •  最后回复来自 levy
15
React Native 下拉刷新组件
前端开发  •  lvming6816077  •  2016-05-15 20:01:48 PM  •  最后回复来自 maomaomao001
13
求助 关于 svn 服务器搭建
程序员  •  lvming6816077  •  2015-05-05 00:17:01 AM  •  最后回复来自 bdnet
6
自己写了一个全屏滚动效果的 js 库 大家看看哈
程序员  •  lvming6816077  •  2015-03-19 14:36:16 PM  •  最后回复来自 lvming6816077
21
Canvas 喷泉小球
JavaScript  •  lvming6816077  •  2015-02-01 20:29:17 PM  •  最后回复来自 Kai
1
canvas 重力小球效果
 •  lvming6816077  •  2015-02-01 19:15:35 PM  •  最后回复来自 lvming6816077
5
canvas 重力小效果
 •  lvming6816077  •  2015-02-01 19:00:44 PM
lvming6816077 最近回复了
@dog 还是需要多打磨一下~
@berumotto 可以隐藏。。
@yazoox 为慕课网写的专栏,一个全栈的项目
各位 V 友看看能测出什么 bug 不
132 天前
回复了 WriteCloser 创建的主题 程序员 各位大佬,谈谈你们在做的副业
写一篇慕课网专栏:
https://www.imooc.com/read/42#catalog
# 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)
关键代码
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)
You must feed a value for placeholder tensor 'y' with dtype int64
355 天前
回复了 lvming6816077 创建的主题 程序员 自荐一门移动 web 实战课程
Ding
关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   4128 人在线   最高记录 5043   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.3 · 26ms · UTC 08:10 · PVG 16:10 · LAX 00:10 · JFK 03:10
♥ Do have faith in what you're doing.