Created
January 10, 2017 03:46
-
-
Save sjchoi86/1757dd2fadba31393ab65c9f43af19ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import scipy.io | |
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
from tensorflow.examples.tutorials.mnist import input_data | |
%matplotlib inline | |
print ("PACKAGES LOADED") | |
mnist = input_data.read_data_sets('data/', one_hot=True) | |
# NETWORK TOPOLOGIES | |
n_hidden_1 = 256 | |
n_hidden_2 = 128 | |
n_input = 784 | |
n_classes = 10 | |
# INPUTS AND OUTPUTS | |
x = tf.placeholder("float", [None, n_input]) | |
y = tf.placeholder("float", [None, n_classes]) | |
# NETWORK PARAMETERS | |
stddev = 0.1 | |
weights = { | |
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)), | |
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)), | |
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], stddev=stddev)) | |
} | |
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])) | |
} | |
print ("NETWORK READY") | |
def mlp(_X, _weights, _biases): | |
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1'])) | |
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2'])) | |
return (tf.matmul(layer_2, _weights['out']) + _biases['out']) | |
# PREDICTION | |
pred = mlp(x, weights, biases) | |
# LOSS AND OPTIMIZER | |
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) | |
# optm = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost) | |
optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) | |
corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) | |
accr = tf.reduce_mean(tf.cast(corr, "float")) | |
# INITIALIZER | |
init = tf.global_variables_initializer() | |
print ("FUNCTIONS READY") | |
# PARAMETERS | |
training_epochs = 20 | |
batch_size = 100 | |
display_step = 4 | |
# LAUNCH THE GRAPH | |
sess = tf.Session() | |
sess.run(init) | |
# OPTIMIZE | |
for epoch in range(training_epochs): | |
avg_cost = 0. | |
total_batch = int(mnist.train.num_examples/batch_size) | |
# ITERATION | |
for i in range(total_batch): | |
batch_xs, batch_ys = mnist.train.next_batch(batch_size) | |
feeds = {x: batch_xs, y: batch_ys} | |
sess.run(optm, feed_dict=feeds) | |
avg_cost += sess.run(cost, feed_dict=feeds) | |
avg_cost = avg_cost / total_batch | |
# DISPLAY | |
if (epoch+1) % display_step == 0: | |
print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost)) | |
feeds = {x: batch_xs, y: batch_ys} | |
train_acc = sess.run(accr, feed_dict=feeds) | |
print ("TRAIN ACCURACY: %.3f" % (train_acc)) | |
feeds = {x: mnist.test.images, y: mnist.test.labels} | |
test_acc = sess.run(accr, feed_dict=feeds) | |
print ("TEST ACCURACY: %.3f" % (test_acc)) | |
print ("OPTIMIZATION FINISHED") | |
# SAVE | |
trainimg = mnist.train.images | |
trainlabel = mnist.train.labels | |
testimg = mnist.test.images | |
testlabel = mnist.test.labels | |
w1 = sess.run(weights['h1']) | |
w2 = sess.run(weights['h2']) | |
wout = sess.run(weights['out']) | |
b1 = sess.run(biases['b1']) | |
b2 = sess.run(biases['b2']) | |
bout = sess.run(biases['out']) | |
# SAVE TO MAT FILE | |
savepath = './data/mlp.mat' | |
scipy.io.savemat(savepath | |
, mdict={'trainimg': trainimg, 'trainlabel': trainlabel | |
, 'testimg': testimg, 'testlabel': testlabel | |
, 'w1': w1, 'w2': w2, 'wout': wout, 'b1': b1, 'b2': b2, 'bout': bout}) | |
print ("%s SAVED." % (savepath)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment