In [1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)


Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz

In [3]:
learning_rate = 0.001
batch_size = 100
display_step = 1
model_path = "./data/model.ckpt"

n_hidden_1 = 256
n_hidden_2 = 256
n_input =784
n_classes = 10

X = tf.placeholder(tf.float32, [None, n_input])
Y = tf.placeholder(tf.float32, [None, n_classes])

# Create model
def multilayer_perceptron(X, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    # Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
pred = multilayer_perceptron(X, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Initializing the variables
init = tf.initialize_all_variables()

Save


In [4]:
# 'Saver' op to save and restore all the variables
saver = tf.train.Saver()

In [5]:
with tf.Session() as sess:
    sess.run(init)
    
    for epoch in range(100):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        
        for i in range(batch_size):
            batch_X, batch_Y  = mnist.train.next_batch(batch_size)
            _, c = sess.run([optimizer, cost], feed_dict={X:batch_X, Y:batch_Y})
            
            avg_cost += c/total_batch
        if epoch % display_step == 0:
            print( "Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
    print("First Optimization Finished!")
    
    correct_prediction = tf.equal(tf.argmax(pred, 1 ), tf.argmax(y,1))
    
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy : "+ str(accuracy.eval({X:mnist.test.images, Y:mnist.test.labels})) )
    
    save_path = saver.save(sess,model_path)
    print("Model saved in file : %s" % save_path)


Epoch: 0001 cost= 108.836407498
Epoch: 0002 cost= 30.739126670
Epoch: 0003 cost= 21.507855863
Epoch: 0004 cost= 15.947324760
Epoch: 0005 cost= 14.108594177
Epoch: 0006 cost= 10.141938653
Epoch: 0007 cost= 10.082922783
Epoch: 0008 cost= 8.599552193
Epoch: 0009 cost= 7.916485622
Epoch: 0010 cost= 7.450386595
Epoch: 0011 cost= 6.868772612
Epoch: 0012 cost= 5.539112077
Epoch: 0013 cost= 5.567978056
Epoch: 0014 cost= 5.313154310
Epoch: 0015 cost= 4.911020572
Epoch: 0016 cost= 4.769210415
Epoch: 0017 cost= 4.223326237
Epoch: 0018 cost= 3.631510315
Epoch: 0019 cost= 3.788965478
Epoch: 0020 cost= 3.328496356
Epoch: 0021 cost= 3.681523117
Epoch: 0022 cost= 3.365954922
Epoch: 0023 cost= 2.619539668
Epoch: 0024 cost= 2.854094573
Epoch: 0025 cost= 2.401501827
Epoch: 0026 cost= 2.808115716
Epoch: 0027 cost= 2.427668705
Epoch: 0028 cost= 2.187976044
Epoch: 0029 cost= 1.994007990
Epoch: 0030 cost= 2.182865797
Epoch: 0031 cost= 1.909010120
Epoch: 0032 cost= 1.644456389
Epoch: 0033 cost= 2.027595317
Epoch: 0034 cost= 1.574618207
Epoch: 0035 cost= 1.341240444
Epoch: 0036 cost= 1.448604215
Epoch: 0037 cost= 1.539782290
Epoch: 0038 cost= 1.371522809
Epoch: 0039 cost= 1.266875405
Epoch: 0040 cost= 1.215893294
Epoch: 0041 cost= 0.916586773
Epoch: 0042 cost= 1.093212824
Epoch: 0043 cost= 1.288075718
Epoch: 0044 cost= 1.041984087
Epoch: 0045 cost= 0.713760991
Epoch: 0046 cost= 0.773545033
Epoch: 0047 cost= 0.855657026
Epoch: 0048 cost= 0.914503218
Epoch: 0049 cost= 0.819455729
Epoch: 0050 cost= 0.737717363
Epoch: 0051 cost= 0.693877363
Epoch: 0052 cost= 0.558006277
Epoch: 0053 cost= 0.657101778
Epoch: 0054 cost= 0.608070918
Epoch: 0055 cost= 0.665338250
Epoch: 0056 cost= 0.487164423
Epoch: 0057 cost= 0.499384636
Epoch: 0058 cost= 0.417588220
Epoch: 0059 cost= 0.496351095
Epoch: 0060 cost= 0.553822043
Epoch: 0061 cost= 0.433717270
Epoch: 0062 cost= 0.358853584
Epoch: 0063 cost= 0.395721364
Epoch: 0064 cost= 0.416529171
Epoch: 0065 cost= 0.313038622
Epoch: 0066 cost= 0.378131208
Epoch: 0067 cost= 0.182569908
Epoch: 0068 cost= 0.283222805
Epoch: 0069 cost= 0.273369903
Epoch: 0070 cost= 0.304327300
Epoch: 0071 cost= 0.329852967
Epoch: 0072 cost= 0.259328344
Epoch: 0073 cost= 0.190560621
Epoch: 0074 cost= 0.237003728
Epoch: 0075 cost= 0.202705845
Epoch: 0076 cost= 0.216561911
Epoch: 0077 cost= 0.254399115
Epoch: 0078 cost= 0.165276233
Epoch: 0079 cost= 0.169544764
Epoch: 0080 cost= 0.192430339
Epoch: 0081 cost= 0.248759141
Epoch: 0082 cost= 0.191404709
Epoch: 0083 cost= 0.186678057
Epoch: 0084 cost= 0.155714316
Epoch: 0085 cost= 0.107239100
Epoch: 0086 cost= 0.189494407
Epoch: 0087 cost= 0.144931069
Epoch: 0088 cost= 0.178824373
Epoch: 0089 cost= 0.099904080
Epoch: 0090 cost= 0.076279207
Epoch: 0091 cost= 0.056546210
Epoch: 0092 cost= 0.168103006
Epoch: 0093 cost= 0.159206276
Epoch: 0094 cost= 0.112134664
Epoch: 0095 cost= 0.087191524
Epoch: 0096 cost= 0.094286026
Epoch: 0097 cost= 0.118514578
Epoch: 0098 cost= 0.089320543
Epoch: 0099 cost= 0.141592914
Epoch: 0100 cost= 0.101450808
First Optimization Finished!
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-c6112e65b811> in <module>()
     15     print("First Optimization Finished!")
     16 
---> 17     correct_prediction = tf.equal(tf.argmax(pred, 1 ), tf.argmax(y,1))
     18 
     19     accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

NameError: name 'y' is not defined

Restore


In [ ]:
print("restore!")

with tf.Session() as sess:
    sess.run(init)
    
    load_path = save.restore(sess, model_path)
    
    for epoch in range(10):