├── CONTRIBUTING.md ├── HelloWorld.py ├── ImageClassification.py ├── LICENSE.md ├── LinearRegression.py ├── MNIST.py ├── MatrixMultiplication.py ├── NormalDistribution.py ├── README.md └── SoftmaxRegressionMNIST.py /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Hi Internet, 2 | 3 | Do contribute and participate here. 4 | 5 | If you know any cool & relevant learning code for TensorFlow, I would love if you contribute here. 6 | 7 | You can fork, star this project if you like. 8 | 9 | Happy Learning! :) 10 | -------------------------------------------------------------------------------- /HelloWorld.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | HelloWorld program using TensorFlow Library 5 | ''' 6 | 7 | import tensorflow as tf 8 | 9 | hello=tf.constant('Hello World') 10 | 11 | #Creating Sessions 12 | sess=tf.Session() 13 | 14 | print(sess.run(hello)) 15 | 16 | #Closing Session sess 17 | sess.close() 18 | -------------------------------------------------------------------------------- /ImageClassification.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials 4 | ''' 5 | 6 | """Simple image classification with Inception. 7 | 8 | Run image classification with Inception trained on ImageNet 2012 Challenge data 9 | set. 10 | 11 | This program creates a graph from a saved GraphDef protocol buffer, 12 | and runs inference on an input JPEG image. It outputs human readable 13 | strings of the top 5 predictions along with their probabilities. 14 | 15 | Change the --image_file argument to any jpg image to compute a 16 | classification of that image. 17 | 18 | Please see the tutorial and website for a detailed description of how 19 | to use this script to perform image recognition. 20 | 21 | https://tensorflow.org/tutorials/image_recognition/ 22 | """ 23 | 24 | from __future__ import absolute_import 25 | from __future__ import division 26 | from __future__ import print_function 27 | 28 | import os.path 29 | import re 30 | import sys 31 | import tarfile 32 | 33 | import numpy as np 34 | from six.moves import urllib 35 | import tensorflow as tf 36 | 37 | FLAGS = tf.app.flags.FLAGS 38 | 39 | # classify_image_graph_def.pb: 40 | # Binary representation of the GraphDef protocol buffer. 41 | # imagenet_synset_to_human_label_map.txt: 42 | # Map from synset ID to a human readable string. 43 | # imagenet_2012_challenge_label_map_proto.pbtxt: 44 | # Text representation of a protocol buffer mapping a label to synset ID. 45 | tf.app.flags.DEFINE_string( 46 | 'model_dir', '/tmp/imagenet', 47 | """Path to classify_image_graph_def.pb, """ 48 | """imagenet_synset_to_human_label_map.txt, and """ 49 | """imagenet_2012_challenge_label_map_proto.pbtxt.""") 50 | tf.app.flags.DEFINE_string('image_file', '', 51 | """Absolute path to image file.""") 52 | tf.app.flags.DEFINE_integer('num_top_predictions', 5, 53 | """Display this many predictions.""") 54 | 55 | # pylint: disable=line-too-long 56 | DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' 57 | # pylint: enable=line-too-long 58 | 59 | 60 | class NodeLookup(object): 61 | """Converts integer node ID's to human readable labels.""" 62 | 63 | def __init__(self, 64 | label_lookup_path=None, 65 | uid_lookup_path=None): 66 | if not label_lookup_path: 67 | label_lookup_path = os.path.join( 68 | FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt') 69 | if not uid_lookup_path: 70 | uid_lookup_path = os.path.join( 71 | FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt') 72 | self.node_lookup = self.load(label_lookup_path, uid_lookup_path) 73 | 74 | def load(self, label_lookup_path, uid_lookup_path): 75 | """Loads a human readable English name for each softmax node. 76 | 77 | Args: 78 | label_lookup_path: string UID to integer node ID. 79 | uid_lookup_path: string UID to human-readable string. 80 | 81 | Returns: 82 | dict from integer node ID to human-readable string. 83 | """ 84 | if not tf.gfile.Exists(uid_lookup_path): 85 | tf.logging.fatal('File does not exist %s', uid_lookup_path) 86 | if not tf.gfile.Exists(label_lookup_path): 87 | tf.logging.fatal('File does not exist %s', label_lookup_path) 88 | 89 | # Loads mapping from string UID to human-readable string 90 | proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines() 91 | uid_to_human = {} 92 | p = re.compile(r'[n\d]*[ \S,]*') 93 | for line in proto_as_ascii_lines: 94 | parsed_items = p.findall(line) 95 | uid = parsed_items[0] 96 | human_string = parsed_items[2] 97 | uid_to_human[uid] = human_string 98 | 99 | # Loads mapping from string UID to integer node ID. 100 | node_id_to_uid = {} 101 | proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines() 102 | for line in proto_as_ascii: 103 | if line.startswith(' target_class:'): 104 | target_class = int(line.split(': ')[1]) 105 | if line.startswith(' target_class_string:'): 106 | target_class_string = line.split(': ')[1] 107 | node_id_to_uid[target_class] = target_class_string[1:-2] 108 | 109 | # Loads the final mapping of integer node ID to human-readable string 110 | node_id_to_name = {} 111 | for key, val in node_id_to_uid.items(): 112 | if val not in uid_to_human: 113 | tf.logging.fatal('Failed to locate: %s', val) 114 | name = uid_to_human[val] 115 | node_id_to_name[key] = name 116 | 117 | return node_id_to_name 118 | 119 | def id_to_string(self, node_id): 120 | if node_id not in self.node_lookup: 121 | return '' 122 | return self.node_lookup[node_id] 123 | 124 | 125 | def create_graph(): 126 | """Creates a graph from saved GraphDef file and returns a saver.""" 127 | # Creates graph from saved graph_def.pb. 128 | with tf.gfile.FastGFile(os.path.join( 129 | FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f: 130 | graph_def = tf.GraphDef() 131 | graph_def.ParseFromString(f.read()) 132 | _ = tf.import_graph_def(graph_def, name='') 133 | 134 | 135 | def run_inference_on_image(image): 136 | """Runs inference on an image. 137 | 138 | Args: 139 | image: Image file name. 140 | 141 | Returns: 142 | Nothing 143 | """ 144 | if not tf.gfile.Exists(image): 145 | tf.logging.fatal('File does not exist %s', image) 146 | image_data = tf.gfile.FastGFile(image, 'rb').read() 147 | 148 | # Creates graph from saved GraphDef. 149 | create_graph() 150 | 151 | with tf.Session() as sess: 152 | # Some useful tensors: 153 | # 'softmax:0': A tensor containing the normalized prediction across 154 | # 1000 labels. 155 | # 'pool_3:0': A tensor containing the next-to-last layer containing 2048 156 | # float description of the image. 157 | # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG 158 | # encoding of the image. 159 | # Runs the softmax tensor by feeding the image_data as input to the graph. 160 | softmax_tensor = sess.graph.get_tensor_by_name('softmax:0') 161 | predictions = sess.run(softmax_tensor, 162 | {'DecodeJpeg/contents:0': image_data}) 163 | predictions = np.squeeze(predictions) 164 | 165 | # Creates node ID --> English string lookup. 166 | node_lookup = NodeLookup() 167 | 168 | top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1] 169 | for node_id in top_k: 170 | human_string = node_lookup.id_to_string(node_id) 171 | score = predictions[node_id] 172 | print('%s (score = %.5f)' % (human_string, score)) 173 | 174 | 175 | def maybe_download_and_extract(): 176 | """Download and extract model tar file.""" 177 | dest_directory = FLAGS.model_dir 178 | if not os.path.exists(dest_directory): 179 | os.makedirs(dest_directory) 180 | filename = DATA_URL.split('/')[-1] 181 | filepath = os.path.join(dest_directory, filename) 182 | if not os.path.exists(filepath): 183 | def _progress(count, block_size, total_size): 184 | sys.stdout.write('\r>> Downloading %s %.1f%%' % ( 185 | filename, float(count * block_size) / float(total_size) * 100.0)) 186 | sys.stdout.flush() 187 | filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress) 188 | print() 189 | statinfo = os.stat(filepath) 190 | print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') 191 | tarfile.open(filepath, 'r:gz').extractall(dest_directory) 192 | 193 | 194 | def main(_): 195 | maybe_download_and_extract() 196 | image = (FLAGS.image_file if FLAGS.image_file else 197 | os.path.join(FLAGS.model_dir, 'clouds.jpg')) 198 | run_inference_on_image(image) 199 | 200 | 201 | if __name__ == '__main__': 202 | tf.app.run() 203 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2016 Parampreet Singh 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /LinearRegression.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | 5 | LinearRegression Learning Algorithm using TensorFlow in Python 6 | ''' 7 | 8 | # Importing Libraries 9 | import tensorflow as tf 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | 13 | # Defining our training variables 14 | trX = np.linspace(-1,1,101) 15 | # Adding some noise to y 16 | trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 17 | 18 | # plotting our training x & y 19 | plt.plot(trX, trY) 20 | plt.show() 21 | 22 | 23 | 24 | # A placeholder is a value that we'll input when we ask TensorFlow to run a computation 25 | 26 | X = tf.placeholder("float") 27 | Y = tf.placeholder("float") 28 | 29 | # Defining our LinearRegression Model 30 | def model(X, W, b): 31 | return tf.add( tf.mul(X, W), b) 32 | 33 | W = tf.Variable(0.0, name="weights") 34 | b = tf.Variable(0.0, name="bias") 35 | 36 | y_model = model(X, W, b) 37 | 38 | # Defining our cost model 39 | cost = tf.square(Y - y_model) 40 | 41 | # Defining our optimising algorithm for LinearRegression 42 | # 0.01 is the learning rate 43 | train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 44 | 45 | # Initialising all variables and running session for execution 46 | # Launching the graph is sessions 47 | init = tf.initialize_all_variables() 48 | sess = tf.Session() 49 | sess.run(init) 50 | 51 | for i in range(100): 52 | for(x, y) in zip(trX, trY): 53 | sess.run(train_op, feed_dict={X: x, Y: y}) 54 | 55 | 56 | # Printing the optimised values of W and b 57 | print(sess.run(W)) 58 | print(sess.run(b)) 59 | 60 | # The value of W must be around 2 61 | # Run this program for many times to have great insight in LinearRegression. :) 62 | -------------------------------------------------------------------------------- /MNIST.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | 5 | Simple tutorial for understanding MNIST Data Sets 6 | ''' 7 | 8 | # Importing different libraries 9 | import tensorflow as tf 10 | import matplotlib.pyplot as plt 11 | import numpy as np 12 | import tensorflow.examples.tutorials.mnist.input_data as input_data 13 | 14 | # MNIST are the data sets of hand written images ranging from 0 to 9 15 | # The data is split into three parts: 55,000 data points of training data 16 | # 10,000 data points of test data 17 | # 5,000 of validation data 18 | # Each image is 28 pixel by 28 pixel 19 | mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) 20 | 21 | # Within each, we can access images, labels and num_examples 22 | print(mnist.train.num_examples, mnist.test.num_examples, mnist.validation.num_examples) 23 | 24 | # (55000, 784) (55000, 10) 25 | print(mnist.train.images.shape, mnist.train.labels.shape) 26 | 27 | # The ranges of values in each images is from 0-1 28 | print(np.min(mnist.train.images), np.max(mnist.train.images)) 29 | 30 | # We can visualize any one of the images by reshaping it to a 28*28 image 31 | plt.imshow(np.reshape(mnist.train.images[1000, :], (28, 28)), cmap='rainbow') 32 | plt.show() 33 | 34 | # try out with different data points and colors, we will get great insights. :) 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /MatrixMultiplication.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | 5 | Matrix Multiplication in Python using TensorFlow Libraries 6 | ''' 7 | 8 | import tensorflow as tf 9 | 10 | #Matrix1 of size 1*2 11 | a=tf.constant([[3.,3.]]) 12 | 13 | #Matrix2 of size 2*1 14 | b=tf.constant([[2.],[2.]]) 15 | 16 | #Matrix3 of size 1*1 17 | c=tf.matmul(a,b) 18 | sess1=tf.Session() 19 | print(sess1.run(c)) 20 | sess1.close() 21 | 22 | #Matrix4 of size 2*2 23 | d=tf.matmul(b,a) 24 | sess2=tf.Session() 25 | print(sess2.run(d)) 26 | sess2.close() 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /NormalDistribution.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | 5 | ~This program will help those having sound knowledge of Probability Theory 6 | 7 | Program to find The Normal Distribution Curve using TensorFlow Library 8 | ''' 9 | 10 | import tensorflow as tf 11 | import matplotlib.pyplot as plt 12 | 13 | n=64 14 | #Creating an array 15 | x=tf.linspace(-3.0,3.0,n) 16 | sess=tf.InteractiveSession() 17 | 18 | 19 | #Defining the parameters of a Standard Normal Random Variable 20 | sigma=1.0 21 | mean=0.0 22 | 23 | #Defining the probability distribution of Normal Random Variable 24 | z=(tf.exp(tf.neg(tf.pow(x - mean, 2.0) / (2.0 * tf.pow(sigma, 2.0)))) * (1.0 / (sigma * tf.sqrt(2.0 * 3.1415)))) 25 | 26 | #New Operations are added as Default Graph 27 | assert z.graph is tf.get_default_graph() 28 | 29 | #Executing graph and plotting it 30 | plt.plot(z.eval()) 31 | 32 | #Showing the plotted graph 33 | plt.show() 34 | 35 | #Similarly, its easy to find probability distribution curve. :) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow Tutorials 2 | 3 | ##### Hi Internet, 4 | 5 | ##### I like Python using TensorFlow library (that's why I made it of). 6 | 7 | ##### And I feel like making tutorials is the best way to understand things like ML algorithms and AI. 8 | 9 | ##### Hoping that you've installed TensorFlow in your system.(Installation) 10 | 11 | ##### Look at some easy codes in Python using this library. 12 | 13 | ### 1. Hello World (code) 14 | ### 2. Matrix Multiplication (code) 15 | ### 3. Normal Distribution Curve (code) 16 | ### 4. Linear Regression (code) 17 | ### 5. Understanding MNIST DataSets (code) 18 | ### 6. Softmax Regression on MNIST DataSets (code) 19 | ### 7. Image Classification on Inception Data (code) 20 | 21 | ##### And, more to come. 22 | 23 | ##### Hope It would help you. :) ;) 24 | -------------------------------------------------------------------------------- /SoftmaxRegressionMNIST.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author= 'Parampreet Singh' 3 | Project= 'https://github.com/paramsingh96/TensorFlow-Tutorials' 4 | 5 | Program to find the accuracy of prediction of digits by using SoftmaxRegression on MNIST data using TensorFlow Libraries 6 | ''' 7 | 8 | # Use Python3 for simplicity 9 | 10 | # Importing the TensorFlow 11 | import tensorflow as tf 12 | 13 | 14 | # Downloading the MNIST data(handwritten images data sets) 15 | from tensorflow.examples.tutorials.mnist import input_data 16 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 17 | 18 | # A placeholder is a value that we'll input when we ask TensorFlow to run a computation. 19 | # x is a 2-D Tensor of floating-point numbers with shape [None, 784] 20 | # None means that a dimension can be of any length 21 | x = tf.placeholder(tf.float32, [None, 784]) 22 | 23 | # A variable is a modifiable tensor that lives in TensorFlow's graph of interacting operations. 24 | # W and b are tensors full of zeros. 25 | # W has shape [784,10] because we want to multiply 784-dimensional image vectors by it to produce 10-dimensional vectors of evidence for different classes of b. 26 | # b has shape [10] so we can add it to the output. 27 | W = tf.Variable(tf.zeros([784, 10])) 28 | b = tf.Variable(tf.zeros([10])) 29 | 30 | # Our SoftmaxRegression Model 31 | 32 | # First, x is multiplied by W. Then it is flipped and added to b 33 | # y is the predicted probability distribution 34 | y = tf.nn.softmax(tf.matmul(x,W) + b) 35 | 36 | # Training 37 | 38 | # let y_ be the true probability distribution i.e. one_hot vector 39 | y_ = tf.placeholder(tf.float32, [None, 10]) 40 | 41 | # cross_entropy tells how inefficient our predictions are for describing the truth. 42 | # tf.reduce_sum adds the elements in the second dimension of y, due to the reduction_indices=[1] 43 | cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 44 | 45 | # Since TensorFlow knows the entire graph of our computations, it is automatically using ~BackpropagationAlgorithm~ to efficiently determine how our variables affect the cost we ask to minimize. 46 | train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 47 | # This is minimizing cross_entropy with learning rate 0.5 using gradient descent algorithm 48 | # Gradient descent is a simple procedure, where TensorFlow simply shifts each variable a little bit in the direction that reduces the cost. 49 | # TensorFlow, behind the scenes, adds new operation to our graph which implement ~backpropagation~ and ~GradientDescent~. 50 | # Then it gives back a single operation which do a step of gradient descent training by tweaking our variables to reduce the cost. 51 | 52 | # Initializing all variables 53 | init = tf.initialize_all_variables() 54 | 55 | # Launching the model in the Session 56 | sess = tf.Session() 57 | sess.run(init) 58 | 59 | # Training 60 | # Running the training step 1000 times 61 | for i in range(1000): 62 | batch_xs, batch_ys = mnist.train.next_batch(100) 63 | sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 64 | # Each step of loop, we get a "batch" of one hundred random data points from our training set. 65 | # We run train_step feeding in the batches data to replace the placeholders. 66 | 67 | # Evaluating our model 68 | # correct_prediction gives us a list of booleans 69 | # tf.argmax gives us the index of the highest entry in a tensor along some axis 70 | correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 71 | 72 | # accuracy of our data 73 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 74 | 75 | # percent accuracy 76 | print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) 77 | 78 | # This should be around 92% 79 | # ~~ Run several times this program, you'll get different accuracies. 80 | # And then, take the insights. :) 81 | --------------------------------------------------------------------------------