import tensorflow.compat.v1 as tf
import numpy as np
import random
import time
import matplotlib.pyplot as plt
tf.disable_v2_behavior()
tf.set_random_seed(777) # for reproducibility
from tensorflow.examples.tutorials.mnist import input_data
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
mnist = input_data.read_data_sets("D:/Downloads/", one_hot=True)
sizeof_x = 784
nb_classes = 10
nb_branch = 256
epoch = 15
batch_size = 100
alg = 4
val_keep_prob = 1.0
num_iterations = int(mnist.train.num_examples / batch_size)
x_data = tf.placeholder(tf.float32, [None, sizeof_x])
y_data = tf.placeholder(tf.float32, [None, nb_classes])
keep_prob = tf.placeholder(tf.float32)
if alg == 0 :
print("Using single network.. softmax + GradientOptimizer")
W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")
logits = tf.matmul(x_data, W1) + B1
H = tf.nn.softmax(logits)
cost = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(H), axis=1))
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
if alg == 1 :
print("Using single network.. softmax + AdamOptimizer1")
W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")
logits = tf.matmul(x_data, W1) + B1
H = tf.nn.softmax(logits)
cost = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(H), axis=1))
train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
if alg == 2 :
print("Using single network.. softmax + AdamOptimizer2")
W1 = tf.Variable(tf.random_normal([sizeof_x, nb_classes]), name="weight1")
B1 = tf.Variable(tf.random_normal([nb_classes]), name="bias1")
H = tf.matmul(x_data, W1) + B1
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=y_data))
train = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
elif alg == 3:
print("Using neural network.. relu + AdamOptimizer")
W1 = tf.Variable(tf.random_normal([sizeof_x, nb_branch]), name="weight1")
B1 = tf.Variable(tf.random_normal([nb_branch]), name="bias1")
W2 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight2")
B2 = tf.Variable(tf.random_normal([nb_branch]), name="bias2")
W3 = tf.Variable(tf.random_normal([nb_branch, nb_classes]), name="weight3")
B3 = tf.Variable(tf.random_normal([nb_classes]), name="bias3")
L1 = tf.nn.relu(tf.matmul(x_data, W1) + B1)
L2 = tf.nn.relu(tf.matmul(L1, W2) + B2)
H = tf.matmul(L2, W3) + B3
# cross entropy
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=tf.stop_gradient(y_data)))
train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
elif alg == 4:
nb_branch = 512
val_keep_prob = 0.7
print("Using Deep neural network.. dropout")
W1 = tf.Variable(tf.random_normal([sizeof_x, nb_branch]), name="weight1")
B1 = tf.Variable(tf.random_normal([nb_branch]), name="bias1")
W2 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight2")
B2 = tf.Variable(tf.random_normal([nb_branch]), name="bias2")
W3 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight3")
B3 = tf.Variable(tf.random_normal([nb_branch]), name="bias3")
W4 = tf.Variable(tf.random_normal([nb_branch, nb_branch]), name="weight4")
B4 = tf.Variable(tf.random_normal([nb_branch]), name="bias4")
W5 = tf.Variable(tf.random_normal([nb_branch, nb_classes]), name="weight5")
B5 = tf.Variable(tf.random_normal([nb_classes]), name="bias5")
L1 = tf.nn.relu(tf.matmul(x_data, W1) + B1)
L1= tf.nn.dropout(L1, keep_prob=keep_prob)
L2 = tf.nn.relu(tf.matmul(L1, W2) + B2)
L2= tf.nn.dropout(L2, keep_prob=keep_prob)
L3 = tf.nn.relu(tf.matmul(L2, W3) + B3)
L3= tf.nn.dropout(L3, keep_prob=keep_prob)
L4 = tf.nn.relu(tf.matmul(L3, W4) + B4)
L4 = tf.nn.dropout(L4, keep_prob=keep_prob)
H = tf.matmul(L4, W5) + B5
# cross entropy
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=H, labels=tf.stop_gradient(y_data)))
train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
is_correct = tf.equal(tf.argmax(y_data, 1), tf.argmax(H, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for e in range(epoch):
avg_cost = 0
for it in range(num_iterations):
x_batch, y_batch = mnist.train.next_batch(batch_size)
val_cost, acc, _ = sess.run([cost, accuracy, train], feed_dict={x_data:x_batch, y_data:y_batch, keep_prob:val_keep_prob})
avg_cost = avg_cost + val_cost / num_iterations
print("Epoch: {:04d}, Cost: {:.9f}".format(e + 1, avg_cost))
print("training finished")
val_keep_prob = 1.0
print("Accuracy: ", accuracy.eval(session=sess, feed_dict={x_data: mnist.test.images, y_data: mnist.test.labels, keep_prob:val_keep_prob}))
for i in range(1):
r = random.randint(0, mnist.test.num_examples - 1)
label= sess.run( tf.argmax( mnist.test.labels[r:r+1], 1 ))
image= mnist.test.images[r:r+1]
predict= sess.run(tf.argmax(H, 1), feed_dict={x_data: image, keep_prob:val_keep_prob})
print("{} {} Label={} Predict={}".format(i, label==predict, label, predict))
plt.imshow(image.reshape(28, 28), cmap="Greys",
interpolation="nearest"
)
plt.show()
time.sleep(3)