撰写时间:2017.8.16
tensorflow代码组织
tensorflow的源代码中有很多example,从那些大牛们的源代码中我们其实可以学到很多有关tensorflow的代码格式应该如何排版。但是由于tf项目组的人有很多,代码格式也是参差不齐,所以这篇博文也就是总结一些博主在实际使用tensorflow中常用的代码组织的结构。
该篇博文按照博主的代码格式变化的时间顺序组织
简单的代码组织
- tensorflow中将所有的操作都看做是节点,所有的节点组成一个前驱图,也就是tensorflow中提到的Graph计算图结构。
- tensorflow的另一个模块是session模块,通过session为之前建立好的Graph“喂(feed)数据”,然后从图的末端fetch需要的结果。
-
所以最简单的代码组织就是将这两个模块分开。先建立计算图,然后通过会话模块运行计算图。
#coding:utf-8 #######softmax import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data ######prepare data mnist = input_data.read_data_sets('MNIST_data/',one_hot = True); #######create the graph,定义图中所有的计算节点的依赖关系 x = tf.placeholder(tf.float32,shape = [None,784],name = 'x'); y_ = tf.placeholder(tf.float32,shape = [None,10],name = 'y_'); W = tf.Variable(tf.zeros([784,10])); b = tf.Variable(tf.zeros([10]),name = 'input_bias'); y = tf.nn.softmax(tf.matmul(x,W) + b) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32")) #set sess as default,定义会话 sess = tf.InteractiveSession(); init = tf.global_variables_initializer(); sess.run(init); ##通过会话运行图 for i in range(1000): batch = mnist.train.next_batch(100); sess.run(train_step,feed_dict={x: batch[0], y_: batch[1]}) if i % 50 == 0: pri