当前位置:网站首页>基于tensorflow的手写数字识别
基于tensorflow的手写数字识别
2022-06-26 18:08:00 【小狐狸梦想去童话镇】
import numpy as np
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() #解决tf.placeholder报错问题
import matplotlib.pyplot as plt
import input_data #使用的数据库是tensorflow内置数据库,可下载到本地
mnist = input_data.read_data_sets('data/',one_hot=True)
#network topologies 网络拓扑
n_hidden_1 = 256
n_hidden_2 = 128
n_input = 784
n_classes = 10
#inputs and outputs 输入 输出
x = tf.placeholder("float",[None,n_input])
y = tf.placeholder("float",[None,n_classes])
#network parameters 网络参数
stddev = 0.1
weights = {
'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),
'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),
'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev))
}
biases = {
'b1':tf.Variable(tf.random_normal([n_hidden_1])),
'b2':tf.Variable(tf.random_normal([n_hidden_2])),
'out':tf.Variable(tf.random_normal([n_classes]))
}
print("NETWORK READY")
def multilayer_perceptron(_X,_weights,_biases):
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['w1']),_biases['b1']))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,_weights['w2']),_biases['b2']))
return (tf.matmul(layer_2,_weights['out'])+_biases['out'])
#prediction
pred = multilayer_perceptron(x,weights,biases)
#loss and optimizer 损失函数及优化器
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
optm = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)
corr = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accr = tf.reduce_mean(tf.cast(corr,"float"))
#initializer
init = tf.global_variables_initializer()
print("FUNCTIONS READY")
#迭代
training_epochs = 20
batch_size = 100
display_step = 4
#launch the graph
sess = tf.Session()
sess.run(init)
#optimize
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
#iteration
for i in range(total_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
feeds = {
x:batch_xs,y:batch_ys}
sess.run(optm,feed_dict=feeds)
avg_cost +=sess.run(cost,feed_dict=feeds)
avg_cost = avg_cost/total_batch
#display
if (epoch+1)%display_step==0:
print("Epoch:%03d/%03d cost:%.9f"%(epoch,training_epochs,avg_cost))
feeds = {
x:batch_xs,y:batch_ys}
training_acc = sess.run(accr,feed_dict=feeds)
print("Train Accuracy:%.3f"%(training_acc))
feeds = {
x:mnist.test.images,y:mnist.test.labels}
test_acc = sess.run(accr,feed_dict=feeds)
print("Test Accuracy:%.3f"%(test_acc))
print("Optimization Finished")
边栏推荐
- How about opening a flush account? Is it safe? How to open a stock trading account
- vutils.make_grid()与黑白图像有关的一个小体会
- JVM entry Door (1)
- sql中的几种删除操作
- Static registration and dynamic registration of JNI
- DoS及攻擊方法詳解
- In and exceptions, count (*) query optimization
- ZCMU--1367: Data Structure
- Map and filter methods for processing scarce arrays
- 行锁分析和死锁
猜你喜欢
随机推荐
交叉编译环境出现.so链接文件找不到问题
Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
LM06丨仅用成交量构造抄底摸顶策略的奥秘
Discussion and generation of digital signature and analysis of its advantages
RSA concept explanation and tool recommendation - LMN
Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
PC端录制扫515地机器人/scan数据
Plt How to keep show() not closed
Properties file garbled
二分查找-2
如何将应用加入到deviceidle 白名单?
JS cast
Number of solutions for knapsack problem
Temporarily turn off MySQL cache
【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
properties文件乱码
零时科技 | 智能合约安全系列文章之反编译篇
临时关闭MySQL缓存
你了解如何比较两个对象吗
ROS查询话题具体内容常用指令









