吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

时间:2023-02-23 17:40:42

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 # 输入节点
OUTPUT_NODE = 10 # 输出节点
LAYER1_NODE = 500 # 隐藏层数 BATCH_SIZE = 100 # 每次batch打包的样本个数 # 模型相关的参数
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARAZTION_RATE = 0.0001
TRAINING_STEPS = 5000
MOVING_AVERAGE_DECAY = 0.99 def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):
# 不使用滑动平均类
if avg_class == None:
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)
return tf.matmul(layer1, weights2) + biases2 else:
# 使用滑动平均类
layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1))
return tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2) def train(mnist):
x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
# 生成隐藏层的参数。
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))
# 生成输出层的参数。
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) # 计算不含滑动平均类的前向传播结果
y = inference(x, None, weights1, biases1, weights2, biases2) # 定义训练轮数及相关的滑动平均类
global_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2) # 计算交叉熵及其平均值
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy) # 损失函数的计算
regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)
regularaztion = regularizer(weights1) + regularizer(weights2)
loss = cross_entropy_mean + regularaztion # 设置指数衰减的学习率。
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY,
staircase=True) # 优化损失函数
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) # 反向传播更新参数和更新每一个参数的滑动平均值
with tf.control_dependencies([train_step, variables_averages_op]):
train_op = tf.no_op(name='train') # 计算正确率
correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 初始化会话,并开始训练过程。
with tf.Session() as sess:
tf.global_variables_initializer().run()
validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
test_feed = {x: mnist.test.images, y_: mnist.test.labels} # 循环的训练神经网络。
for i in range(TRAINING_STEPS):
if i % 1000 == 0:
validate_acc = sess.run(accuracy, feed_dict=validate_feed)
print("After %d training step(s), validation accuracy using average model is %g " % (i, validate_acc)) xs,ys=mnist.train.next_batch(BATCH_SIZE)
sess.run(train_op,feed_dict={x:xs,y_:ys}) test_acc=sess.run(accuracy,feed_dict=test_feed)
print(("After %d training step(s), test accuracy using average model is %g" %(TRAINING_STEPS, test_acc))) def main(argv=None):
mnist = input_data.read_data_sets("../../../datasets/MNIST_data", one_hot=True)
train(mnist) if __name__=='__main__':
main()

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import os BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = "MNIST_model/"
MODEL_NAME = "mnist_model" def train(mnist):
# 定义输入输出placeholder。
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
y = mnist_inference.inference(x, regularizer)
global_step = tf.Variable(0, trainable=False) # 定义损失函数、学习率、滑动平均操作以及训练过程。
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY,
staircase=True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
with tf.control_dependencies([train_step, variables_averages_op]):
train_op = tf.no_op(name='train') # 初始化TensorFlow持久化类。
saver = tf.train.Saver()
with tf.Session() as sess:
tf.global_variables_initializer().run() for i in range(TRAINING_STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) def main(argv=None):
mnist = input_data.read_data_sets("../../../datasets/MNIST_data", one_hot=True)
train(mnist) if __name__ == '__main__':
main()

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train # 加载的时间间隔。
EVAL_INTERVAL_SECS = 10 def evaluate(mnist):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')
validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} y = mnist_inference.inference(x, None)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore) while True:
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score))
else:
print('No checkpoint file found')
return
time.sleep(EVAL_INTERVAL_SECS) def main(argv=None):
mnist = input_data.read_data_sets("../../../datasets/MNIST_data", one_hot=True)
evaluate(mnist) if __name__ == '__main__':
main()

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题

吴裕雄--天生自然python Google深度学习框架:MNIST数字识别问题的更多相关文章

  1. 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  2. 吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型

    import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABEL ...

  3. 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络

  4. 吴裕雄--天生自然python Google深度学习框架:深度学习与深层神经网络

  5. 吴裕雄--天生自然python Google深度学习框架:TensorFlow实现神经网络

    http://playground.tensorflow.org/

  6. 吴裕雄--天生自然python Google深度学习框架:Tensorflow基础应用

    import tensorflow as tf a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2.0, 3.0], ...

  7. 吴裕雄--天生自然python Google深度学习框架:人工智能、深度学习与机器学习相互关系介绍

  8. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:Bellman函数、贪心算法与增强性学习网络开发实践

    !pip install gym import random import numpy as np import matplotlib.pyplot as plt from keras.layers ...

  9. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:使用TensorFlow和Keras开发高级自然语言处理系统——LSTM网络原理以及使用LSTM实现人机问答系统

    !mkdir '/content/gdrive/My Drive/conversation' ''' 将文本句子分解成单词,并构建词库 ''' path = '/content/gdrive/My D ...

随机推荐

  1. 如何在Eclipse下查看JDK源代码

    在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 "window"-> "Preferences" -> "Java&quo ...

  2. C++ URLDecode和URLEncode实现——仅限gb2312,非utf8

    转载--http://blog.163.com/zhangjie_0303/blog/static/9908270620148251658993/   #include <iostream&gt ...

  3. How to regress out unwanted vectors

    Source: http://stats.stackexchange.com/questions/117840/how-to-regress-out-some-variables Answer in ...

  4. struts2和spring3&period;2的整合 详细演示

    1.首先我们新建一个Web工程,如下: 2.导入Spring和Struts2的jar包. 其中,struts2-spring-plugin-2.1.8.jar是struts2.spring整合的关键. ...

  5. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  6. &lbrack;Security&rsqb; Automatically adding CSRF tokens to ajax calls when using jQuery--转

    地址:http://erlend.oftedal.no/blog/?blogid=118 When building a ajax based application, you want to pro ...

  7. MySQL增删改查的常用操作指令总结

    总结: 1.数据库操作: 创建库: create database db_name; 查询库: show databases; //显示所有的数据库 show create databases db_ ...

  8. Android中px、dp、sp的区别

    px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guid ...

  9. Android Studio 2&period;1&period;x 关联SDK API Source

    问题: 看图=>,当在android studio里ctrl+鼠标左键查看例如: TextUtils.isEmpty(content);这段代码的isEmpty方法的实现的时候经常就跑到如图所示 ...

  10. 基于FPGA摄像头图像采集显示系统

    本系统主要由FPGA主控模块.图像采集模块.图像存储模块以及图像显示模块等模块组成.其中图像采集模块选择OV7670摄像头模块,完成对视频图像的采集和解码功能,并以RGB565标准输出RGB 5:6: ...