23. Deep Neural Network for XOR and MNIST

Deep Neural Network or XOR and MNIST

tensorflow

TOC

XOR with 2 Layer Neural Network

  • This is default 2 layer neural network for XOR.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# From reproducibility
tf.set_random_seed(777)

# Learning rate
learning_rate = 0.1

# Inputs data
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
# Labels
y_data = [[0],
          [1],
          [1],
          [0]]

# Inputs array
x_data = np.array(x_data, dtype=np.float32)
# Labels array
y_data = np.array(y_data, dtype=np.float32)

# Placeholder for Inputs and Labels
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

# Weight and bias for the first layer
W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')
b1 = tf.Variable(tf.random_normal([2]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)

# Weight and bias for the second layer
W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')
b2 = tf.Variable(tf.random_normal([1]), name='bias2')
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)

# Cost function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))

# Optimizer
train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

# Set threshold.
#  True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

# Accuracy
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

costs= []
accs = []

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
        # Train
        sess.run(train, feed_dict={X: x_data, Y: y_data})
        _cost = sess.run(cost, feed_dict={
                X: x_data, Y: y_data})
        costs.append(_cost)
        _acc = sess.run(accuracy, feed_dict={X: x_data, Y: y_data})
        accs.append(_acc)

    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)

steps = [i for i in range(len(costs))]

plt.plot(steps, costs)
plt.title("Costs")
plt.xlabel("Steps")
plt.ylabel("Cost")
plt.show()

plt.plot(steps, accs)
plt.title("Accuracies")
plt.xlabel("Steps")
plt.ylabel("Accuracy")
plt.show()
Hypothesis:  [[ 0.01272806]
 [ 0.98249799]
 [ 0.9886651 ]
 [ 0.01084627]] 
Correct:  [[ 0.]
 [ 1.]
 [ 1.]
 [ 0.]] 
Accuracy:  1.0
Image 1. Cost for XOR with 2 layer neural network
Image 2. Accuracy for XOR with 2 layer neural network

XOR with 2 Layer Wide Neural Network

  • From default 2 layer neural network, it is possible to make it wide to increase accuracy.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# From reproducibility
tf.set_random_seed(777)

# Learning rate
learning_rate = 0.1

# Inputs data
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
# Labels
y_data = [[0],
          [1],
          [1],
          [0]]

# Inputs array
x_data = np.array(x_data, dtype=np.float32)
# Labels array
y_data = np.array(y_data, dtype=np.float32)

# Placeholder for Inputs and Labels
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

# Weight and bias for the first layer
#  Weight: 2 x 10
#  Bias: 1 x 10
W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1')
b1 = tf.Variable(tf.random_normal([10]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)

# Weight and bias for the second layer
#  Weight:10 x 1
#  Bias: 1 x 1
W2 = tf.Variable(tf.random_normal([10, 1]), name='weight2')
b2 = tf.Variable(tf.random_normal([1]), name='bias2')
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)

# Cost function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))

# Optimizer
train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

# Set threshold.
#  True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

# Accuracy
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

costs= []
accs = []

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
        # Train
        sess.run(train, feed_dict={X: x_data, Y: y_data})

        _cost = sess.run(cost, feed_dict={
                X: x_data, Y: y_data})
        costs.append(_cost)
        _acc = sess.run(accuracy, feed_dict={X: x_data, Y: y_data})
        accs.append(_acc)

    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)

steps = [i for i in range(len(costs))]

plt.plot(steps, costs)
plt.title("Costs")
plt.xlabel("Steps")
plt.ylabel("Cost")
plt.show()

plt.plot(steps, accs)
plt.title("Accuracies")
plt.xlabel("Steps")
plt.ylabel("Accuracy")
plt.show()
Hypothesis:  [[ 0.00504063]
 [ 0.99114835]
 [ 0.99255556]
 [ 0.01169373]] 
Correct:  [[ 0.]
 [ 1.]
 [ 1.]
 [ 0.]] 
Accuracy:  1.0
Image 3. Cost for XOR with 2 layer wide neural network
Image 4. Accuracy for XOR with 2 layer wide neural network

XOR with Deep Neural Network

  • It is possible to insert more layers to increase accuracy.
  • This model is Deep Neural Network (DNN).

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# From reproducibility
tf.set_random_seed(777)

# Learning rate
learning_rate = 0.1

# Inputs data
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
# Labels
y_data = [[0],
          [1],
          [1],
          [0]]

# Inputs array
x_data = np.array(x_data, dtype=np.float32)
# Labels array
y_data = np.array(y_data, dtype=np.float32)

# Placeholder for Inputs and Labels
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])

# Weight and bias for the first layer
#  Weight: 2 x 10
#  Bias: 1 x 10
W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1')
b1 = tf.Variable(tf.random_normal([10]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)

# Weight and bias for the second layer
#  Weight:10 x 10
#  Bias: 1 x 10
W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2')
b2 = tf.Variable(tf.random_normal([10]), name='bias2')
layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2)

# Weight and bias for the third layer
#  Weight:10 x 10
#  Bias: 1 x 10
W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3')
b3 = tf.Variable(tf.random_normal([10]), name='bias3')
layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3)

# Weight and bias for the fourth layer
#  Weight:10 x 1
#  Bias: 1 x 1
W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4')
b4 = tf.Variable(tf.random_normal([10]), name='bias4')
hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)

# Cost function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
                       tf.log(1 - hypothesis))

# Optimizer
train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

# Set threshold.
#  True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

# Accuracy
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

costs= []
accs = []

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
        # Train
        sess.run(train, feed_dict={X: x_data, Y: y_data})

        _cost = sess.run(cost, feed_dict={
                X: x_data, Y: y_data})
        costs.append(_cost)
        _acc = sess.run(accuracy, feed_dict={X: x_data, Y: y_data})
        accs.append(_acc)

    h, c, a = sess.run([hypothesis, predicted, accuracy],
                       feed_dict={X: x_data, Y: y_data})
    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)

steps = [i for i in range(len(costs))]

plt.plot(steps, costs)
plt.title("Costs")
plt.xlabel("Steps")
plt.ylabel("Cost")
plt.show()

plt.plot(steps, accs)
plt.title("Accuracies")
plt.xlabel("Steps")
plt.ylabel("Accuracy")
plt.show()
Hypothesis:  [[ 0.00130236]
 [ 0.99811018]
 [ 0.99866831]
 [ 0.00167277]] 
Correct:  [[ 0.]
 [ 1.]
 [ 1.]
 [ 0.]] 
Accuracy:  1.0
Image 5. Cost for XOR with deep neural network
Image 6. Accuracy for XOR with deep neural network

MNIST with Deep Neural Network

  • For MNIST data set, DNN can be applied.
import tensorflow as tf
# Tensorflow already incldues MNNIST data set
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import random

# Get input data as one_hot encoding format
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# Labels: 0 ~ 9
nb_labels = 10

# MNIST data image of shape 28 x 28 = 784
X = tf.placeholder(tf.float32, [None, 784])
# 0 ~ 9 digits recofnition = 10 labels
Y = tf.placeholder(tf.float32, [None, nb_labels])
# Weight
W = tf.Variable(tf.random_normal([784, nb_labels]))
# Bias
b = tf.Variable(tf.random_normal([nb_labels]))


# Weight and bias for the first layer
#  Weight: 2 x 10
#  Bias: 1 x 10
W1 = tf.Variable(tf.random_normal([784, 100]), name='weight1')
b1 = tf.Variable(tf.random_normal([100]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)

# Weight and bias for the second layer
#  Weight:10 x 10
#  Bias: 1 x 10
W2 = tf.Variable(tf.random_normal([100, 100]), name='weight2')
b2 = tf.Variable(tf.random_normal([100]), name='bias2')
layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2)

# Weight and bias for the third layer
#  Weight:10 x 10
#  Bias: 1 x 10
W3 = tf.Variable(tf.random_normal([100, 100]), name='weight3')
b3 = tf.Variable(tf.random_normal([100]), name='bias3')
layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3)

# Weight and bias for the fourth layer
#  Weight:10 x 1
#  Bias: 1 x 1
W4 = tf.Variable(tf.random_normal([100, 10]), name='weight4')
b4 = tf.Variable(tf.random_normal([10]), name='bias4')
hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)

# Hypothesis - softmax
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)

# Cost
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis =1))
# Optimizer
optimizer = tf.train.GradientDescentOptimizer(\
                    learning_rate=0.1).minimize(cost)

# Test model
is_correct = tf.equal(tf.argmax(hypothesis, 1), \
                        tf.argmax(Y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

# Epoch - How many times data will be trained
training_epochs = 15
# Batch - How many data will be trained at once
batch_size = 100

with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    # training cycle
    for epoch in range(training_epochs):
        avg_cost = 0
        # Iteration - (the number of data) / (batch size).
        max_iteration = int(mnist.train.num_examples / batch_size)

        for itr in range(max_iteration):
            batch_xs, batch_ys = \
                    mnist.train.next_batch(batch_size)
            c, _ = sess.run([cost, optimizer],
                        feed_dict={X: batch_xs, Y: batch_ys})
            avg_cost += c / max_iteration

        print("Epoch: {0:4d}, Cost: {1:0.9f}".format(\
                        epoch + 1, avg_cost))

    print("Learning finished")

    # Test the model using test sets
    # accuracy.eval() == sess.run()
    print("Accuracy: ", accuracy.eval(session=sess, \
                        feed_dict={X: mnist.test.images, \
                                   Y: mnist.test.labels}))

    # Get on and predict
    r = random.randint(0, mnist.test.num_examples - 1)
    # mnist.test.labels are one-hot encoded
    print("Label: {0}".format(\
            sess.run(tf.argmax(mnist.test.labels[r:r+1], 1))))
    print("Prediction: {0}".format(\
            sess.run(tf.argmax(hypothesis, 1), \
            feed_dict = {X: mnist.test.images[r:r+1]})))
    plt.imshow(mnist.test.images[r:r+1].reshape(28, 28),
               cmap="Greys", interpolation="nearest")
    plt.show()
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
Epoch:    1, Cost: 2.728260803
Epoch:    2, Cost: 1.027512100
Epoch:    3, Cost: 0.826937820
Epoch:    4, Cost: 0.730932239
Epoch:    5, Cost: 0.669988185
Epoch:    6, Cost: 0.626827917
Epoch:    7, Cost: 0.594122318
Epoch:    8, Cost: 0.568072515
Epoch:    9, Cost: 0.546891481
Epoch:   10, Cost: 0.528336101
Epoch:   11, Cost: 0.512735213
Epoch:   12, Cost: 0.498853235
Epoch:   13, Cost: 0.486702925
Epoch:   14, Cost: 0.475801280
Epoch:   15, Cost: 0.465662283
Learning finished
Accuracy:  0.8877
Label: [8]
Prediction: [8]
Image 7. MNIST inference

COMMENTS

Name

0 weights,1,abstract class,1,active function,3,adam,2,Adapter,1,affine,2,argmax,1,back propagation,3,binary classification,3,blog,2,Bucket list,1,C++,11,Casting,1,cee,1,checkButton,1,cnn,3,col2im,1,columnspan,1,comboBox,1,concrete class,1,convolution,2,cost function,6,data preprocessing,2,data set,1,deep learning,31,Design Pattern,12,DIP,1,django,1,dnn,2,Don't Repeat Your code,1,drop out,2,ensemble,2,epoch,2,favicon,1,fcn,1,frame,1,gradient descent,5,gru,1,he,1,identify function,1,im2col,1,initialization,1,Lab,9,learning rate,2,LifeLog,1,linear regression,6,logistic function,1,logistic regression,3,logit,3,LSP,1,lstm,1,machine learning,31,matplotlib,1,menu,1,message box,1,mnist,3,mse,1,multinomial classification,3,mutli layer neural network,1,Non Virtual Interface,1,normalization,2,Note,21,numpy,4,one-hot encoding,3,OOP Principles,2,Open Close Principle,1,optimization,1,overfitting,1,padding,2,partial derivative,2,pooling,2,Prototype,1,pure virtual function,1,queue runner,1,radioButton,1,RBM,1,regularization,1,relu,2,reshape,1,restricted boltzmann machine,1,rnn,2,scrolledText,1,sigmoid,2,sigmoid function,1,single layer neural network,1,softmax,6,softmax classification,3,softmax cross entropy with logits,1,softmax function,2,softmax regression,3,softmax-with-loss,2,spinBox,1,SRP,1,standardization,1,sticky,1,stride,1,tab,1,Template Method,1,TensorFlow,31,testing data,1,this,2,tkinter,5,tooltip,1,Toplevel,1,training data,1,vanishing gradient,1,Virtual Copy Constructor,1,Virtual Destructor,1,Virtual Function,1,weight decay,1,xavier,2,xor,3,
ltr
item
Universe In Computer: 23. Deep Neural Network for XOR and MNIST
23. Deep Neural Network for XOR and MNIST
Deep Neural Network or XOR and MNIST
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s0/
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s72-c/
Universe In Computer
https://kunicom.blogspot.com/2017/07/23-deep-neural-network-for-xor-and-mnist.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2017/07/23-deep-neural-network-for-xor-and-mnist.html
true
2543631451419919204
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy