TensorFlow Tutorial: 10 Minute TensorFlow Beginner Tutorial [Translation]

Hello, Habr! I present to you the translation of the article "TensorFlow Tutorial: 10 minutes Practical TensorFlow lesson for quick learners" by Ankit Sachan.







This TensorFlow tutorial is for anyone who has a basic understanding of machine learning and is trying to get started with TensorFlow.







First of all, you must have TensorFlow installed. You can install it in this guide. This lesson is divided into two parts: in the first part we explain the basics with a working example, in the second part we build a linear regression model.







Part 1. TensorFlow Basics



TensorFlow is a numerical library in which data passes through a graph. Data in TensorFlow is represented by n-dimensional arrays - tensors. The graph is composed of data (tensors) and mathematical operations.







In the nodes of the graph are mathematical operations.







The edges of the graph represent tensors that "flow" between operations.







There is another aspect in which TensorFlow is very different from any other programming language. In TensorFlow, you first need to create a project of what you want to create. Initially, when creating a graph, variables have no meaning. Later, when you created a complete graph, you run it in a session and only then the variables will take on any values.







Let's start learning by doing. Run python and import TensorFlow:







~$ python Python 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>>import tensorflow as tf
      
      





A graph is used to define operations, and all operations are performed in a session. Graphs and sessions are created independently of each other. Treat the graph as a construction project and the session as a construction site.







1.1. Counts in TensorFlow



A graph is the basis of TensorFlow, all calculations (operations), variables are on the graph. Everything that happens in the code is on the default graph provided by TensorFlow. You can access this graph as follows:







 graph = tf.get_default_graph()
      
      





And so you can get a list of all operations in this column:







 graph.get_operations()
      
      





Your graph is now empty. If you need to print the name of each operation, run the following code:







 for op in graph.get_operations(): print(op.name)
      
      





The output will again be blank. In the future, after adding operations to the graph, we will use this code to display their names.







1.2. Sessions at TensorFlow



The graph only defines the calculations (creates the project), but has no variables, no values, unless we run the graph or part of the graph in the session.







You can create a session like this:







 sess=tf.Session() ... your code ... ... your code ... sess.close()
      
      





Do not forget that when you start a session you need to end it or use the with ... as block, for example like this:







 with tf.Session() as sess: sess.run(f)
      
      





The advantage of using this approach is that the session will be closed automatically at the end of the with block







1.3. Tensors in TensorFlow



TensorFlow stores data in tensors - multidimensional arrays very similar to numPy arrays.







a) Constants are values ​​that cannot be changed. They are defined as follows:







 a=tf.constant(1.0)
      
      





however, when you try to output, you get the following:







 print(a) <tf.Tensor'Const:0' shape=() dtype=float32> print(a) Tensor("Const:0", shape=(), dtype=float32)
      
      





As you can see, this is different from other programming languages, such as python. You cannot print or access the constant until you start the session. Let's do this:







 with tf.Session() as sess: print(sess.run(a))
      
      





This code will output 1.0







b) Variables are also tensors that are like variables in other programming languages:







 >>>b = tf.Variable(2.0,name="test_var") >>>b <tensorflow.python.ops.variables.Variable object at 0x7f37ebda1990>
      
      





Unlike constants, variables can change their contents. However, variables in TensorFlow must be initialized in a separate initialization operation. It may be time consuming to initialize all variables, but TensorFlow provides a mechanism to initialize all variables at a time:







 init_op = tf.global_variables_initializer()
      
      





Now you need to start the initialization operation before you try to access the variable:







 with tf.Session() as sess: sess.run(init_op) print(sess.run(b))
      
      





This code will output 2.0

If you now try to print operations in the graph







 graph = tf.get_default_graph() for op in graph.get_operations(): print(op.name)
      
      





you will get the following output:







 Const test_var/initial_value test_var test_var/Assign test_var/read init
      
      





As you can see, we declared β€œa” as Const and it was added to the chart. Similarly, for the variable b, many test_var states were added to the TensorFlow graph, such as test_var / initial_value, test_var / read, etc. You can visualize the entire network using TensorBoard, which is a tool for visualizing the TensorFlow graph and the learning process .







c) Placeholders are tensors waiting to be initialized with data. They are used for data that will be provided only when the code is actually executed in the session. What is transferred to the placeholder is a dictionary that contains the names of placeholders and their meanings:







 a = tf.placeholder("float") b = tf.placeholder("float") y = tf.multiply(a, b) fd = {a: 2, b: 3} with tf.Session() as sess: print(sess.run(y, fd))
      
      





This code will output 6.0







1.4. Devices in TensorFlow



TensorFlow has very powerful built-in features to run your code on GPU, CPU or clusters. It gives you the opportunity to choose the device on which you want to run your code. However, this is not something to think about when you are just starting out with TF.

So here is a complete picture of how the calculations in TensorFlow work:

image







Part 2. TensorFlow Tutorial with a Simple Example



In this part, we will look at the code for running linear regression. Before that, let's look at some of the basic TensorFlow functions that we will use in the code.







Creating a random normal distribution:







Use random_normal to create random values ​​from the normal distribution. In this example, w is a variable of dimension 784 * 10 with random values ​​with a standard deviation of 0.01.







 w=tf.Variable(tf.random_normal([784, 10], stddev=0.01))
      
      





Reduce_mean - Calculates the average value of an array







 b = tf.Variable([10,20,30,40,50,60],name='t') with tf.Session() as sess: sess.run(tf.initialize_all_variables()) sess.run(tf.reduce_mean(b))
      
      





This code will output 35







argmax - very similar to argmax in Python. Gets the maximum value from the tensor along the specified axis.







 a=[ [0.1, 0.2, 0.3 ], [20, 2, 3 ] ] b = tf.Variable(a,name='b') with tf.Session() as sess: sess.run(tf.initialize_all_variables()) sess.run(tf.argmax(b,1))
      
      





Print an array ([2, 0]), which shows the index of the maximum value in each of the lines a.







Linear regression problem



Statement of the problem: there are a large number of data points, trying to match a straight line with them. For this example, we will create 100 data points and try to match the line with them.







2.1. Create a training dataset



trainX contains values ​​between -1 and 1, and trainY contains 3 times the value of trainX plus some random variable:







 import tensorflow as tf import numpy as np trainX = np.linspace(-1, 1, 101) trainY = 3 * trainX + np.random.randn(*trainX.shape) * 0.33
      
      





2.2. Placeholders



 X = tf.placeholder("float") Y = tf.placeholder("float")
      
      





2.3. Modeling



The linear regression model has the form





$$ display $$ Ymodel = w * x $$ display $$







- we must calculate the value of w. Let's initialize w to zero and create a model to solve this problem. We define the cost function as

$$ display $$ (Y-y__model) ^ 2 $$ display $$







TensorFlow comes with many optimizers that calculate and update gradients after each iteration, trying to minimize the specified cost function. We are going to define a learning operation using GradientDescentOptimizer in order to minimize our cost function at a learning speed of 0.01. Then we run this learning operation in a loop.







 w = tf.Variable(0.0, name="weights") y_model = tf.multiply(X, w) cost = (tf.pow(Y-y_model, 2)) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
      
      





2.4. Training



Up to this point, we have only defined a graph. No calculations have occurred, none of the TensorFlow variables matter. To run this graph, we need to create a session and execute it. Before that, we need to create an initialization operation for all variables:







 init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for i in range(100): for (x, y) in zip(trainX, trainY): sess.run(train_op, feed_dict={X: x, Y: y}) print(sess.run(w))
      
      





Note that the first thing that was done was to initialize the variables by calling init inside session.run () . Later we run train_op , feeding her feed_dict . Finally, we print the value of w (again inside sess.run ()), which should be around 3.







The exercise:



If you create a new session after this code and try to print w, what will be output?







 with tf.Session() as sess: sess.run(init) print(sess.run(w))
      
      





Yes, you understood correctly, 0.0 will be displayed. As soon as we left the session created earlier, all operations cease to exist.







Hope this tutorial gives you a quick start with TensorFlow. Please feel free to ask your questions in the comments. The full code can be downloaded here .








All Articles