import tensorflow as tf
# a placeholder just tells the system the type of the variable which will
a = tf.placeholder('int32')
b = tf.placeholder('int32')
# the mul is the multiplication
y = tf.multiply(a,b)
# start the session
sess = tf.Session()
# feed the placeholders in the model and have it run
sess.run(y,feed_dict={a:2,b:5})
It can be identified by 3 parameters: RANK, SHAPE, TYPE
import numpy as np
tensor_1d = np.array([1.3, 1, 4.0, 23.99])
print (tensor_1d)
print (tensor_1d[0])
print (tensor_1d[2])
# the indexing is the same as python
tensor_1d.ndim # number of dimensions, this is like the rank
tensor_1d.shape
tensor_1d.dtype
import numpy as np
my_numbers = np.array([1,2,3,4,5])
my_numbers.dtype
# convert the array to TF tensor
import tensorflow as tf
tf_tensor = tf.convert_to_tensor(tensor_1d,dtype=tf.float64)
print (tf_tensor)
# running the session we can then visualize the tensor and it's elements
with tf.Session() as sess:
print (sess.run(tf_tensor))
print (sess.run(tf_tensor[0]))
print (sess.run(tf_tensor[2]))
# let's build 2 integer arrays
matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype='int32')
matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype='int32')
# visualize them:
print ('matrix1 = ')
print (matrix1)
print ('matrix2 = ')
print (matrix2)
# defining matrix operations which will not be run until the run is called
matrix_product = tf.matmul(matrix1,matrix2) # matrix multiply
print (matrix_product)
matrix_sum = tf.add(matrix1,matrix2)
print (matrix_sum)
# new matrix to be used to compute a matrix determinant
matrix3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype='float32')
print ('matrix3 = ')
print (matrix3)
matrix_det = tf.matrix_determinant(matrix3)
matrix_det
with tf.Session() as sess:
result1 = sess.run(matrix_product)
result2 = sess.run(matrix_sum)
result3 = sess.run(matrix_det)
# print the results
print ('matrix1 * matrix2 = ')
print (result1)
print ('matrix1 + matrix2 = ')
print (result2)
print ('matrix3 determinant = ')
print (result3)
The first run throughout the Net is called feed forward and it will give a bad result usually. Then it takes the output and
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/',one_hot=True)
The data is stored in a vector format, although the original data was a 2d matrix with values representing how much pigment was at a certain location
type(mnist) # to find out the type of mnist
mnist.train.images.shape #to find the number of rows and cols we have in
sample = mnist.train.images[0].reshape(28,28)
%matplotlib inline
plt.imshow(sample,cmap='Greys')
We need to define (w) parameters from the stochastic gradient descent
- Learning Rate - how quickly to adjust the cost function, or how quickly the network forgets about older information
- Training Epochs - how many training cycles to go through (example, going through the 55K images above is an epoch)
- Batch Size - size of "batches" of training data (most of the Time all data will not fit into memory so it needs to be split in batches)
# parameters - they needs to be set up for
learning_rate = 0.001
training_epochs = 35
batch_size = 100
# Due to the fact that we are working with images and images are stored in
# Choose 256 but you can choose whatever you want
n_hidden_1 = 256 # first layer number of features
n_hidden_2 = 256 # second layer number of features
n_input = 784 # MNIST data input (image shape 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)
n_samples = mnist.train.num_examples # the number of images in the dataset (55000)
We will crete our model, we'll start with 2 hidden layers, whcih use the RELU activation function
def multilayer_perceptron(x, weights, biases):
'''
x: place Holder for Data Input
weights: Dictionary of weights
biases: Dictionary of biases
'''
layer_1 = tf.add(tf.matmul(x,weights['h1']),biases['b1'])
layer_1 = tf.nn.relu(layer_1)
layer_2 = tf.add(tf.matmul(layer_1,weights['h2']),biases['b2'])
layer_2 = tf.nn.relu(layer_2)
out_layer = tf.matmul(layer_2,weights['out']+biases['out'])
return out_layer
# In this case we are initializaing the weights with random ones
weights = {
'h1': tf.Variable(tf.random_normal([n_input,n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2,n_classes]))
}
# Same for the biases
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]))
}
# We are defining the input and output as placeholders
x = tf.placeholder('float',[None,n_input])
y = tf.placeholder('float',[None,n_classes])
# initialize the pred variable and pass into it the function we've defined
pred = multilayer_perceptron(x,weights,biases)
print (pred)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
# We are grabbing the first batch
Xsamp,ysamp = mnist.train.next_batch(1)
plt.imshow(Xsamp.reshape(28,28),cmap='Greys')
# Remember indexing starts at zero!
print (ysamp)
sess = tf.InteractiveSession()
sess.run(init)
for epoch in range(training_epochs):
avg_cost = 0.0
total_batch = int(n_samples/batch_size)
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer,cost],feed_dict={x:batch_x,y:batch_y})
avg_cost += c/total_batch
print ('Epoch: {} cost= {:.4f}'.format(epoch+1,avg_cost))
print ('Model has completed {} Epochs of Training'.format(training_epochs))
correct_predictions = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
print (correct_predictions[0])
correct_predictions = tf.cast(correct_predictions,'float')
print (correct_predictions[0])
accuracy = tf.reduce_mean(correct_predictions)
type(accuracy)
mnist.test.labels
mnist.test.images
print ('Accuracy: ',accuracy.eval({x:mnist.test.images,y:mnist.test.labels}))