lekaha's blog Change the world     Wiki     Blog     Feed

03_net

In [2]:

import sys
sys.path.append('../')

In [3]:

#!/usr/bin/env python

import tensorflow as tf
import numpy as np
import input_data


def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, w_h, w_o):
    # this is a basic mlp, think 2 stacked logistic regressions
    h = tf.nn.sigmoid(tf.matmul(X, w_h)) 
    
    # note that we dont take the softmax at the end because our cost fn does that for us
    return tf.matmul(h, w_o) 


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])

w_h = init_weights([784, 625]) # create symbolic variables
w_o = init_weights([625, 10])

py_x = model(X, w_h, w_o)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) # compute costs
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer
predict_op = tf.argmax(py_x, 1)

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize all variables
    tf.initialize_all_variables().run()

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
        print(i, np.mean(np.argmax(teY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX, Y: teY})))
('Extracting', 'MNIST_data/train-images-idx3-ubyte.gz')
('Extracting', 'MNIST_data/train-labels-idx1-ubyte.gz')
('Extracting', 'MNIST_data/t10k-images-idx3-ubyte.gz')
('Extracting', 'MNIST_data/t10k-labels-idx1-ubyte.gz')
(0, 0.69189999999999996)
(1, 0.82230000000000003)
(2, 0.86160000000000003)
(3, 0.88090000000000002)
(4, 0.88800000000000001)
(5, 0.89380000000000004)
(6, 0.89780000000000004)
(7, 0.90029999999999999)
(8, 0.90369999999999995)
(9, 0.90539999999999998)
(10, 0.90720000000000001)
(11, 0.91010000000000002)
(12, 0.91139999999999999)
(13, 0.91239999999999999)
(14, 0.91400000000000003)
(15, 0.91469999999999996)
(16, 0.91490000000000005)
(17, 0.91610000000000003)
(18, 0.91720000000000002)
(19, 0.91810000000000003)
(20, 0.91859999999999997)
(21, 0.91900000000000004)
(22, 0.91910000000000003)
(23, 0.91949999999999998)
(24, 0.91990000000000005)
(25, 0.9204)
(26, 0.92100000000000004)
(27, 0.92159999999999997)
(28, 0.92179999999999995)
(29, 0.92259999999999998)
(30, 0.92320000000000002)
(31, 0.92359999999999998)
(32, 0.92420000000000002)
(33, 0.92420000000000002)
(34, 0.92410000000000003)
(35, 0.92469999999999997)
(36, 0.92510000000000003)
(37, 0.92559999999999998)
(38, 0.92589999999999995)
(39, 0.92659999999999998)
(40, 0.92689999999999995)
(41, 0.92710000000000004)
(42, 0.92759999999999998)
(43, 0.92810000000000004)
(44, 0.92910000000000004)
(45, 0.92959999999999998)
(46, 0.9304)
(47, 0.93100000000000005)
(48, 0.93140000000000001)
(49, 0.93259999999999998)
(50, 0.93300000000000005)
(51, 0.93389999999999995)
(52, 0.93469999999999998)
(53, 0.93510000000000004)
(54, 0.93569999999999998)
(55, 0.93600000000000005)
(56, 0.93689999999999996)
(57, 0.93720000000000003)
(58, 0.93759999999999999)
(59, 0.93830000000000002)
(60, 0.9385)
(61, 0.93920000000000003)
(62, 0.93979999999999997)
(63, 0.93999999999999995)
(64, 0.94069999999999998)
(65, 0.94120000000000004)
(66, 0.94189999999999996)
(67, 0.94230000000000003)
(68, 0.94289999999999996)
(69, 0.94340000000000002)
(70, 0.94359999999999999)
(71, 0.94410000000000005)
(72, 0.94410000000000005)
(73, 0.94499999999999995)
(74, 0.94569999999999999)
(75, 0.94650000000000001)
(76, 0.94679999999999997)
(77, 0.94730000000000003)
(78, 0.94769999999999999)
(79, 0.94830000000000003)
(80, 0.94840000000000002)
(81, 0.94899999999999995)
(82, 0.94930000000000003)
(83, 0.9496)
(84, 0.95009999999999994)
(85, 0.95069999999999999)
(86, 0.95120000000000005)
(87, 0.95140000000000002)
(88, 0.95220000000000005)
(89, 0.95289999999999997)
(90, 0.95340000000000003)
(91, 0.95340000000000003)
(92, 0.9536)
(93, 0.95379999999999998)
(94, 0.95409999999999995)
(95, 0.95440000000000003)
(96, 0.95440000000000003)
(97, 0.95450000000000002)
(98, 0.95509999999999995)
(99, 0.95530000000000004)

In [ ]: