tensorflow实战google深度学习框架第五章5.2.1minister数字识别源代码(代码片段)

a9999 a9999     2023-01-16     572

关键词:

  1 import os
  2 import tab
  3 import tensorflow as tf
  4 
  5 print "tensorflow 5.2 "
  6 
  7 from tensorflow.examples.tutorials.mnist import input_data
  8 
  9 ‘‘‘
 10 mnist = input_data.read_data_sets("/asky/tensorflow/mnist_data",one_hot=True)
 11 print "-------------------------------------"
 12 print "Training data size: ",mnist.train.num_examples
 13 print "-------------------------------------"
 14 print "Validating data size: ",mnist.validation.num_examples
 15 print "-------------------------------------"
 16 print "Testing data size: " ,mnist.test.num_examples
 17 print "-------------------------------------"
 18 print "Example training data: ",mnist.train.images[0]
 19 print "-------------------------------------"
 20 print "Example training data label: ",mnist.train.labels[0]
 21 
 22 batch_size = 100
 23 xs,ys=mnist.train.next_batch(batch_size)
 24 
 25 print "X shape:",xs.shape
 26 
 27 print "Y shape:",ys.shape
 28 
 29 
 30 print "Test Tezt"
 31 ‘‘‘
 32 
 33 INPUT_NODE = 784
 34 OUTPUT_NODE = 10
 35 
 36 LAYER1_NODE = 500
 37 
 38 BATCH_SIZE = 100
 39 
 40 LEARNING_RATE_BASE = 0.8
 41 LEARNING_RATE_DECAY = 0.99
 42 
 43 REGULARIZATION_RATE = 0.0001
 44 TRAINING_STEPS = 30000
 45 MOVING_AVERAGE_DECAY = 0.99
 46 
 47 def inference(input_tensor,avg_class,weights1,biases1,weights2,biases2):
 48     if avg_class == None:
 49         layer1 = tf.nn.relu(tf.matmul(input_tensor,weights1)+biases1)
 50         return tf.matmul(layer1,weights2)+biases2
 51     else:
 52         layer1 = tf.nn.relu(
 53             tf.matmul(input_tensor,avg_class.average(weights1))+
 54             avg_class.average(biases1))
 55         return tf.matmul(layer1,avg_class.average(weights2))+avg_class.average(biases2)
 56 
 57 def train(mnist):
 58     x = tf.placeholder(tf.float32,[None,INPUT_NODE],name=x-input)
 59     y_ = tf.placeholder(tf.float32,[None,OUTPUT_NODE],name=y-input)
 60     weights1 = tf.Variable(
 61         tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
 62     biases1 = tf.Variable( tf.constant(0.1,shape=[LAYER1_NODE]))
 63 
 64     weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE,OUTPUT_NODE],stddev=0.1))
 65     biases2 = tf.Variable(tf.constant(0.1,shape=[OUTPUT_NODE]))
 66 
 67     y = inference(x,None,weights1,biases1,weights2,biases2)
 68 
 69     global_step = tf.Variable(0,trainable=False)
 70 
 71     variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
 72 
 73     variables_averages_op = variable_averages.apply(tf.trainable_variables())
 74 
 75     average_y = inference(x,variable_averages,weights1,biases1,weights2,biases2)
 76 
 77     #cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, tf.argmax(y_, 1 ))
 78     cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y)
 79 
 80     cross_entropy_mean = tf.reduce_mean(cross_entropy)
 81 
 82     regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
 83 
 84     regularization = regularizer(weights1) + regularizer(weights2)
 85 
 86     loss = cross_entropy_mean + regularization
 87 
 88     learning_rate = tf.train.exponential_decay(
 89         LEARNING_RATE_BASE,
 90         global_step,
 91         mnist.train.num_examples/BATCH_SIZE,
 92         LEARNING_RATE_DECAY
 93     )
 94 
 95     train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
 96 
 97     with tf.control_dependencies([train_step,variables_averages_op]):
 98         train_op = tf.no_op(name=train)
 99 
100     correct_prediction = tf.equal(tf.argmax(average_y,1),tf.argmax(y_,1))
101     accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
102 
103     with tf.Session() as sess:
104         tf.global_variables_initializer().run()
105         validate_feed = x: mnist.validation.images,
106                          y_: mnist.validation.labels
107         test_feed = x: mnist.test.images, y_: mnist.test.labels 
108         for i in range(TRAINING_STEPS):
109             if i % 1000 ==0:
110                 validate_acc = sess.run(accuracy,feed_dict=validate_feed)
111                 print ("After %d training step(s),validation accuracy "
112                         "using average model is %g " %(i,validate_acc) )
113             xs, ys = mnist.train.next_batch(BATCH_SIZE)
114             sess.run(train_op,feed_dict=x: xs , y_ : ys)
115 
116         test_acc  = sess.run(accuracy,feed_dict=test_feed)
117         print ( "After %d training step(s),test accuracy using average "
118                 "model is %g " % (TRAINING_STEPS , test_acc)  )
119 
120 def main(argv=None) :
121     mnist = input_data.read_data_sets("/asky/tensorflow/mnist_data",one_hot=True)
122     train(mnist)
123 
124 if __name__ == __main__:
125     tf.app.run()

 

书籍推荐:《tensorflow实战google深度学习框架第2版》(高清pdf中文版)

内容简介:TensorFlow是谷歌2015年开源的主流深度学习框架,目前已得到广泛应用。《TensorFlow:实战Google深度学习框架(第2版)》为TensorFlow入门参考书,旨在帮助读者以快速、有效的方式上手TensorFlow和深度学习。书中省略了烦琐... 查看详情

分享《tensorflow实战google深度学习框架(第2版)》+pdf+源码+郑泽宇

...3-QPyHn5oCcVhcKjtTYFw更多分享资料:http://blog.51cto.com/14087171《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图: 查看详情

《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

下载:https://pan.baidu.com/s/1hltIZdjkET3CNxKWSixtZA《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图: 查看详情

《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

下载:https://pan.baidu.com/s/1aD1Y2erdtppgAbk8c63xEw《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图 查看详情

分享《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

下载:https://pan.baidu.com/s/1hltIZdjkET3CNxKWSixtZA《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图: 查看详情

《tensorflow实战google深度学习框架》pdf一套四本+源代码_高清_完整

 TensorFlow实战热门Tensorflow实战书籍PDF高清版一套共四本+源代码,包含《Tensorflow实战》、《Tensorflow:实战Google深度学习框架(完整版)》、《TensorFlow:实战Google深度学习框架(第2版)》与《TensorFlow技术解析与实战》,不能错过的... 查看详情

分享《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

下载:(https://pan.baidu.com/s/1mrEiGC2B3h6p1iOZuOkGlw)《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图: 查看详情

《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

...Y2erdtppgAbk8c63xEw更多最新的资料:http://blog.51cto.com/3215120《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图 查看详情

分享《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

...Y2erdtppgAbk8c63xEw更多最新的资料:http://blog.51cto.com/3215120《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图 查看详情

《tensorflow实战google深度学习框架(第2版)》中文版pdf和源代码

...xtZA更多分享资料:https://pan.baidu.com/s/1g4hv05UZ_w92uh9NNNkCaA《TensorFlow实战Google深度学习框架(第2版)》中文版PDF和源代码中文版PDF,带目录标签,文字可以复制,363页。配套源代码;经典书籍,讲解详细;如图: 查看详情

tensorflow实战google深度学习框架(代码片段)

第3章TensorFlow入门3.1TensorFlow计算模型-计算图3.1.1计算图的概念Tensorflow中所有计算都会被转化成计算图的一个节点,计算图上的边表示了他们之间的相互依赖关系。3.1.2计算图的使用Tensorflow的程序可以分成两个阶段:定义计算、... 查看详情

《tensorflow实战google深度学习框架(第二版)》学习笔记及书评(代码片段)

《TensorFlow实战Google深度学习框架(第二版)》学习笔记文章目录《TensorFlow实战Google深度学习框架(第二版)》学习笔记写在前面1.TensorFlow图像处理函数学习总结2.TensorFlow图像预处理完整样例3.TensorFlow多线程输入... 查看详情

tensorflow实战google深度学习框架

...就要入职。接下来准备在接下来的半个月时间内重温一下TensorFlow的用法。道友么,有感兴趣的,大家一起一起交流学习呀!争取每天更新~本书有十章,所以将分成十个部分介绍该系列来源于书籍《TensorFLow实战Google深度学习框... 查看详情

tensorflow实战google深度学习框架第五章5.2.1minister数字识别源代码(代码片段)

1importos2importtab3importtensorflowastf45print"tensorflow5.2"67fromtensorflow.examples.tutorials.mnistimportinput_data89‘‘‘10mnist=input_data.read_data_sets("/asky/tensorflow/mnist_data",one_hot=True 查看详情

求《tensorflow:实战google深度学习框架》全文免费下载百度网盘资源,谢谢~

《TensorFlow:实战Google深度学习框架》百度网盘pdf最新全集下载:链接:https://pan.baidu.com/s/1pSqb0GyJnrtt-CVG1DI2_g?pwd=zm65提取码:zm65简介:TensorFlow:实战Google深度学习框架pdf电子书,TensorFlow是一门新型技术学习框架,本书作者作为TensorFlo... 查看详情

tensorflow:实战google深度学习框架第三章

tensorflow的计算模型:计算图–tf.Graph tensorflow的数据模型:张量–tf.Tensor tensorflow的运行模型:会话–tf.Session tensorflow可视化工具:TensorBoard 通过集合管理资源:tf.add_to_collection、tf.get_collection Tensor主要三个属 查看详情

tensorflow实战google深度学习框架笔记摘要pone

《Tensorflow实战Google深度学习框架》前三章的摘要(没有简介和环境搭建的部分)摘要的内容是与tensorflow语句相关的知识如有违规之类的请通知我啊这个pdf一般bd有需要的可以私聊我或者留下扣扣邮箱欢迎指点注:本来是要直接... 查看详情

tensorflow实战-tensorflow和其他深度学习框架的对比-第2章

2TensorFlow和其他深度学习框架的对比182.1主流深度学习框架对比182.2各深度学习框架简介20TensorFlowTensorFlow是相对高阶的机器学习库,用户?方便地用它设计神经网络结构,而不必为了追求高效率的实现亲自写C++或者CUDA代码。TensorFlo... 查看详情