基于MNIST 了解Tensorflow 框架

Mnist.py

以下为完整代码:

import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()  
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10]) # not init ???


w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1) #
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x,w):
    return tf.nn.conv2d(x, w, strides=[1,1,1,1], padding= 'SAME')


def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')


w_conv1 = weight_variable([5,5,1,32]) # add noise to avoid derivate 0 and symetricq
b_conv1 = bias_variable([32])

x_image = tf.reshape(x, [-1,28,28,1])

h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)


w_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

w_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1)+ b_fc1)

keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

w_fc2 = weight_variable([1023, 10])
b_fc2 = bias_variable([10])

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)



cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) #caculate entropy, reduce_sum caculate all the output y's summary, not only one 
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
        x:batch[0], y_: batch[1], keep_prob: 1.0})
    print "step %d, training accuracy %g"%(i, train_accuracy)
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print "test accuracy %g"%accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})

mnist_dnn.py

import tensorflow as tf
import numpy as np

n_inputs = 28*28 # MNIST 
n_hidden1 = 300
n_hidden2 = 100
n_outputs = 10
x = tf.placeholder(tf.float32, shape=(None, n_inputs), name="x")
y = tf.placeholder(tf.int64, shape=(None), name="y")

def neuron_layer(x, n_neuron,name,activation=None):
        with tf.name_scope(name):
                n_inputs=int(x.get_shape()[1]) # return 
                stddev=2/np.sqrt(n_inputs)
                init=tf.truncated_normal((n_inputs,n_neuron),stddev=stddev)
                w=tf.Variable(init,name="weights")
                b=tf.Variable(tf.zeros([n_neuron]),name="bias")
                z=tf.matmul(x,w)+b
                if activation=='relu':
                        return tf.nn.relu(z)
                else:
                        return z

with tf.name_scope("dnn"):
        hidden1=neuron_layer(x,n_hidden1,"hidden1",activation="relu")
        hidden2=neuron_layer(hidden1,n_hidden2,"hidden2",activation="relu")
        logits=neuron_layer(hidden2,n_outputs,"outputs")

with tf.name_scope("loss"):
        xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)
        loss=tf.reduce_mean(xentropy,name="loss")

learning_rate=0.01

with tf.name_scope("train"):
        optimizer=tf.train.GradientDescentOptimizer(learning_rate)
        training_op=optimizer.minimize(loss)

with tf.name_scope("eval"):
        correct=tf.nn.in_top_k(logits,y,1)#return 1D tensor full of boolean values
        accuracy=tf.reduce_mean(tf.cast(correct,tf.float32))

init=tf.global_variables_initializer()
saver=tf.train.Saver()



#execute phase
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("tmp/data")

n_epochs=400
batch_size=50

with tf.Session() as sess:
        init.run()
        for epoch in range(n_epochs):
                for iteration in range(mnist.train.num_examples//batch_size):
                        x_batch,y_batch=mnist.train.next_batch(batch_size)
                        sess.run(training_op,feed_dict={x:x_batch,y:y_batch})
                acc_train=accuracy.eval(feed_dict={x:x_batch,y:y_batch})
                acc_test=accuracy.eval(feed_dict={x:mnist.test.images,y:mnist.test.labels})

        print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)
        save_path = saver.save( sess, "./my_model_final.ckpt" )
        tf.train.write_graph(session.graph_def, '', 'my_model_final.pb')

Low Level View

Tensor

  • rank: dimension
  • shape:array length of each dimension
3. # a rank 0 tensor; a scalar with shape [],
[1., 2., 3.] # a rank 1 tensor; a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

Graph

  • operation:描述消费或者生产一个tensor的计算动作
  • Tensors:1

Model save and restore

Checkpoint

与代码类型相关的tensorflow 模型数据格式

SaveModel

与代码类型无关的tensorflow 模型数据格式

tf.truncated_normal

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_shuffle(value, seed=None, name=None)
tf.set_random_seed(seed)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

Outputs random values from a truncated normal distribution.
The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.

猜你喜欢

转载自blog.csdn.net/huntershuai/article/details/80236498