3
votes

I am new to TensorFlow. I looked for examples on implementation of multi layer perceptron using tensorflow, but i am getting examples only on MNIST image data sets, apart from MNIST can i able to build the Neural Network model using same optimization and cost functions and train the data which is in number format,Means, Can I train my own number dataset using tensorflow.

Is there any example for training the new dataset?.

2

2 Answers

5
votes

Finally i got it. Building , Training and minimizing cost / loss of an Artificial Neural Network using Single Layer Perceptron with tensorflow, numpy , matplotlib packages. Data is used in the form of array instead of MNIST. Here is the code.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
learning_rate = 0.0008
training_epochs = 2000
display_step = 50
# taking input as array from numpy package and converting it into tensor
inputX = np.array([[  2,   3],
                  [  1,   3]])
inputY = np.array([[  2,   3],
                  [  1,   3]])
x = tf.placeholder(tf.float32, [None, 2])
y_ = tf.placeholder(tf.float32, [None, 2])

W = tf.Variable([[0.0,0.0],[0.0,0.0]])
b = tf.Variable([0.0,0.0])

layer1 = tf.add(tf.matmul(x, W), b)
y = tf.nn.softmax(layer1)

cost = tf.reduce_sum(tf.pow(y_-y,2))

optimizer =tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

avg_set = []
epoch_set = []

for i in range(training_epochs):
   sess.run(optimizer, feed_dict = {x: inputX, y_:inputY})

   #log training
   if i % display_step == 0:
       cc = sess.run(cost, feed_dict = {x: inputX, y_:inputY})
       #check what it thinks when you give it the input data
       print(sess.run(y, feed_dict = {x:inputX}))


       print("Training step:", '%04d' % (i), "cost=", "{:.9f}".format(cc))
       avg_set.append(cc)
       epoch_set.append(i + 1)

print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict = {x: inputX, y_: inputY})
print("Training cost = ", training_cost, "\nW=", sess.run(W),
      "\nb=", sess.run(b))
plt.plot(epoch_set,avg_set,'o',label = 'SLP Training phase')
plt.ylabel('cost')
plt.xlabel('epochs')
plt.legend()
plt.show()

Later by adding hidden layers it can be also implemented with Multi Layer Perceptron

1
votes

Modifying the TensorFlow multilayer perceptron example is easy. You will need to specify your input and output size here

n_input = # Input size (MNIST uses 28*28 size images = 784 pixels)
n_classes = # Output size (MNIST classifies into 0-9 digits)

Finally, you have to change the training cycle code to work with your dataset. I cannot help you more as I don't know anything about your dataset.