一 训练神经网络
注意在神经网络中输入x为“x-input”,输出layer2为“0”,在之后保存图结构的时候需要用到这两个名字
x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
layer2 = tf.add(tf.matmul(layer1, w2), b2, name="O")
保存图结构的两种方法
1.以变量的形式保存图,这里的output_node_names的名字是之前layer2的名字
output_graph_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_node_names=['O'])
with tf.gfile.FastGFile("MNIST_data/model-graph2.pb", "wb") as f:
f.write(output_graph_def.SerializeToString())
2.以常量的形式保存图,这里需要用到冻结图
以MNIST手写字体识别为例,代码如下
import numpy as np
import sklearn.preprocessing as prep
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
n_samples = int(mnist.train.num_examples)
training_epochs = 10000
batch_size = 100
REGULARIZATION_RATE = 0.0001
MOVING_AVERAGE_DECAY = 0.99
INPUT_NODE = 784
OUTPUT_NODE = 10
hidden_1 = 500
hidden_2 = 10
x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
y = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
w1 = tf.Variable(tf.truncated_normal([INPUT_NODE, hidden_1], stddev=0.1), name="w1")
w2 = tf.Variable(tf.truncated_normal([hidden_1, hidden_2], stddev=0.1), name="w2")
b1 = tf.Variable(tf.constant(0.1, shape=[hidden_1]), name="b1")
b2 = tf.Variable(tf.constant(0.1, shape=[hidden_2]), name="b2")
global_step = tf.Variable(0, trainable=False)
layer1 = tf.nn.relu(tf.add(tf.matmul(x, w1), b1), name="l1")
layer2 = tf.add(tf.matmul(layer1, w2), b2, name="O")
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=layer2, labels=tf.argmax(y, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)