码迷,mamicode.com
首页 > 编程语言 > 详细

mnist 缩减版 练手 tensorflow python

时间:2019-05-23 21:02:22      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:window   show   保存   normal   显示图片   amp   nis   保存图像   ast   

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘] = ‘3 ‘

import numpy as np
import cv2 as cv
import wx
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("C:\\Users\\HHQ\Desktop\\tangjun\\minist\\minist_data\\minist", one_hot=True)
import tensorflow as tf
x = tf.placeholder(dtype=tf.float32,shape=[None,784])
W = tf.Variable(tf.zeros([784,10]),dtype=tf.float32)
b = tf.Variable(tf.zeros([10]),tf.float32)
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder( dtype=tf.float32,shape=[None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #损失函数

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) #优化器

#定义测试的准确率 #ragmaax()0表示按列,1表示按行,输出该列或行的最大值的下标值;equal()表示相等返回值为True或False
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) #执行测试样本的准确率(全部的样本),计算相等值,为bool值,则为1和0
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将全部的bool型转换为float32类型,在求平均值


init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for i in range(300):
batch_xs, batch_ys = mnist.train.next_batch(50)
if i % 10==0:
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
retr =sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys})
print("step %d, training accuracy %g" % (i, retr))
else:
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
retr1 = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
# print(retr1)
#显示图片
check_img=cv.imread("C:\\Users\\HHQ\Desktop\\tangjun\\minist\\minist_data\\minist_jpg\\read\\read.jpg",0) #测试数字保存图像
# cv.namedWindow(‘训练图片‘)
# cv.imshow(‘训练图片‘,a)
# cv.waitKey(0)
check_st=check_img.reshape(1,784) #将其转换所需要的数组
result=sess.run(y,feed_dict={x:check_st})
result_end=np.argmax(result)
#显示在界面上
app=wx.App()
win = wx.Frame(None,title = "图片预测分类显示", id=2,size=(410,335),pos=(0,0),name=‘nihao‘)
loadButton = wx.Button(win, label = str(result_end),pos = (0,0),size = (410,335))
font = wx.Font(150, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False)
loadButton.SetFont(font)
win.Show()
app.MainLoop()












mnist 缩减版 练手 tensorflow python

标签:window   show   保存   normal   显示图片   amp   nis   保存图像   ast   

原文地址:https://www.cnblogs.com/tangjunjun/p/10914204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!