TensorFlow developed by Google. This tutorial explains the TensorFlow with best examples. TensorFlow has been adapted and advanced by a huge open source community. It is therefore essential for the deep learning practitioners to at least master the basics.

**We’ll cover the ingredients of the TensorFlow core library as well as some high-level APIs that are available in the Python ecosystem.** Our discussion in this blog should help you understand the basic structures of the framework, allowing you to build your own deep learning models using TensorFlow.

Although we recommend using Keras if you are new to deep learning, learning the essentials of TensorFlow is quite useful, as Keras is also built on top of TensorFlow.

**TensorFlow is available both for Python 2 and Python 3. Since we’re using Python 3 in this blog,** we briefly cover how to install TensorFlow on your local computer. However, if you’re using the Docker file provided, TensorFlow is already installed for you.

**Before installing TensorFlow, it is important to make note of the computation units on your machine that can be used by TensorFlow. **You have two options to run your TensorFlow code: you can use the CPU or the GPU. Since GPUs are designed to run linear matrix operations faster than the CPUs, data scientists prefer to use GPUs when available.

**However, the Tensor Flow code you write will be the same (**except for the statement of your preference regarding the computation units you would like to use).

**Let’s start with the installation of the Tensor Flow.** In doing so, we make use of the pip package manager of Python. So, if Python 3 is the only installed version of Python in your machine, then the:

pip install –upgrade tensor flow

**the command would install Tensorflow for Python 3.** However, if both Python 2 and Python 3 are installed in your computer, then the command above might install the TensorFlow for Python 2. In that case, you can also use the following command to install TensorFlow for Python 3:

pip3 install –upgrade tensorflow

**The TensorFlow framework is now installed for you to explore. In your code, import the TensorFlow to use it:**

import tensorflow

**If you wish, you can rename it to “tf”. We will do this throughout the blog because it is the convention in the community:**

import tensorflow as tf

**TensorFlow is mathematical software and an open-source software library for Machine Intelligence, developed in 2011,** by Google Brain Team. The initial target of TensorFlow was to conduct research in machine learning and in deep neural networks.

In 2015, Google has open-sourced the TensorFlow and all of its reference implementation and made all the source code available on GitHub under the Apache 2.0 license.

**After that, TensorFlow has achieved wide adaption, from academia and research to industry and** following that recently the most stable version 1.0 has been released with a unified API.

**Keeping in mind your needs and based on all the latest and exciting features of TensorFlow 1.x, **this blog will give a description of the main TensorFlow's capabilities. The following topics will be discussed in this blog:

**The main features offered by the latest release of TensorFlow are as follows:**

Faster computing: The major versioning upgrade to TensorFlow 1.0 has made its capability incredibly faster including a 7.3x speedup on 8 GPUs for inception v3 and 58x speedup for distributed Inception (v3 training on 64 GPUs).

**Portability: TensorFlow runs on Windows, Linux, and Mac machines and on mobile computing platforms (that is, Android).**

Easy debugging: TensorFlow provides the Tensor Board tool for the analysis of the developed models.

**Unified API: TensorFlow offers you a very flexible architecture** that enables you to deploy computation to one or more CPUs or GPUs in a desktop, server or mobile device with a single API.

**Transparent use of GPU computing: Automating management and optimization of the same memory and the data used**. You can now use your machine for large-scale and data-intensive GPU computing with NVIDIA, cuDNN, and CUDA toolkits.

**Easy Use: TensorFlow is for everyone; it's for students, researchers, and deep learning practitioners **and also for readers of this blog. Production ready at scale: Recently it has been evolved as the neural network for machine translation, at production scale. TensorFlow 1.0 promises Python API stability making it easier to choose new features without worrying too much about breaking your existing code.

**Extensibility: TensorFlow is relatively new technology and it's still under active development. **However, it is extensible because it was released with the source code available on GitHub. And if you don't see the low-level data operator you need, you can write it in C++ and add it to the framework.

**Supported: There is a large community of developers and users working together to improve TensorFlow **both by providing feedback and by actively contributing to the source code.

**Wide adaption: Numerous tech giants are using TensorFlow to increase their business intelligence.** For example, ARM, Google, Intel, eBay, Qualcomm, SAM, DropBox, DeepMind, Airbnb, Twitter and so on.

**TensorFlow is designed as a distributed system by nature, so it is quite easy to run TensorFlow models** in distributed settings. The TensorFlow Distributed Execution Engine is responsible for handling this capability of TensorFlow.

**As we mentioned before, TensorFlow models can be run on top of CPUs and GPUs.** However, other computation units are also available to use. Recently, Google announced Tensor Processing Units (TPUs) that are designed to swiftly run TensorFlow models. You can even run TensorFlow in Android devices directly.

**Although Python is the most commonly used language with TensorFlow, you can use TensorFlow** with C++, Java, Julia, Go, R, and more. TensorFlow includes two relatively high-level abstraction modules called layers and datasets.

**The Layers module provides methods that simplify the creation of fully connected layers,** convolutional layers, pooling layers, and more. It also provides methods like adding activation functions or applying dropout regularization. The Datasets module includes capabilities to manage your datasets.

Higher-level APIs (like Keras or Estimators) are easier to use, and they provide the same functionality of these lower-level modules. Lastly, we should mention that TensorFlow includes some pre-trained models out of the box.

**To understand the core architecture of the TensorFlow framework, we introduce some basic concepts.**

First, let’s begin with a fundamental design principle of TensorFlow: TensorFlow is designed to work with “static graphs”. The computational flow of your model will be converted to a graphical representation in the framework before execution.

**The static graph in TensorFlow is the computational graph and not the data**. This means that before you run the code, you must define the computational flow of your data. After that, all of the data that is fed to the system will flow through this computational graph, even if the data changes from time to time.

**Let’s start with the basic concepts of the framework. The first concept you have to understand is the “tensor” **which is also included in the name of the framework.

Tensors are the units that hold the data. You can think of tensors as NumPy n-dimensional arrays. The rank of the tensor defines the dimension, and the shape defines the lengths of each dimension in a tuple form. So

[ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0] ] is a tensor that has rank 2 and shape (2,3).

**Another crucial concept of TensorFlow is the “directed graph”, which contains operations and tensors.** In this graph, operations are represented as nodes; tensors are represented as edges. Operations take tensors as input and produce tensors as output. Let’s give a simple example here:

# first, we have to import tensorflow Import tensorflow as tf # constants are the most basic type of operations x = tf.constant(1.0, dtype = tf.float32) y = tf.constant(2.0, dtype = tf.float32) z = x + y

In the code above, we define two tensors x and y by the tf.constant operation.

This operation takes 1.0 and 2.0 as inputs and just produces their tensor equivalents and nothing more. Then using x and y, we created another tensor called z. Now, what do you expect from this code below?

print(z) You are incorrect if you expect to see 3.0. Instead, you just see: Tensor(“add:0”, shape=(), dtype=float32)

**Defining graphs is different than executing the statements. For now, z is just a tensor object and has no value associated with it**. We somehow need to run the graph so that we can get 3.0 from the tensor z. This is where another concept in the TensorFlow comes in: the session.

**Sessions in TensorFlow are the objects that hold the state of the runtime where our graph will be executed.** We need to instantiate a session and then run the operations we have already defined:

sess = tf.Session() The code above instantiates the session object. Now, using that object, we can run our operations: print(sess.run(z))

**and we get 3.0 from the print statement! When we run an operation (namely a node in the graph),** the TensorFlow executes it by calculating the tensors that our operation takes as input.

This involves a backward calculation of the nodes and tensors until it reaches a natural starting point – just like in our tf.constant operations above.

**As you have already noticed, tf.constant simply provides constants as an operation; **it may not be suitable to work with external data. For these kinds of situations, TensorFlow provides another object called the placeholder. You can think of placeholders as arguments to a function.

**It is something that you’ll provide later on in your code! For example:**

k = tf.placeholder(tf.float32) l = tf.placeholder(tf.float32) m = k + l

This time we define k and las placeholders; we will assign some values to them when we run them in the session. Using the session above:

print(sess.run(m, feed_dict={k = 1.0, l = 2.0}))

**will print 3.0. Here we used a feed_dict object, which is a dictionary used to pass values to the placeholders**. Effectively, we pass 1.0 and 2.0 to k and l placeholders, respectively, in the runtime. You can also use the feed_dict parameter of the run method of the session to update values of the tf.constants.

**We have seen that constants and placeholders are useful TensorFlow constructs to store values**. Another useful construct is the TensorFlow variable.

One can think of a variable as something that lies between constants and placeholders. Like placeholders, variables do not have an assigned value. However, much like constants, they can have default values. Here is an example of a Tensor Flow variable:

v= tf.Variable([0], tf.float32)

**In the above line, we define a TensorFlow variable called v and set its default value as 0.** When we want to assign some value different than the default one, we can use the tf.assign method:

w= tf.assign(v, [-1.]) It is crucial to know that TensorFlow variables are not initialized when defined. Instead, we need to initialize them in the session like this: init = tf.global_variables_initializer() sess.run(init)

**The code above initializes all the variables! As a rule of thumb, you should use tf.constant to define constants,** tf.placeholder to hold the data fed to your model, and if. Variable to represent the parameters for your model.

**Now that we have learned the basic concepts of TensorFlow and demonstrated how to use them,** you are all set to use TensorFlow to build your own models.

**We’ll begin our TensorFlow exercises by implementing a deep learning classification model**, utilizing the elements of TensorFlow we covered in the last section.

**The datasets we use to demonstrate TensorFlow are the same synthetic datasets we used in the previous section**. We use them for classification and regression purposes in this blog.

Remember that those datasets– as well as the codes we go over in this section–are already provided with the Docker image distributed with this blog. You can run that Docker image to access the datasets and the source codes of this blog.

**Before we begin to implement our classifier, we need to import some libraries to use them.** Here are the libraries we need to import:

import numpy as np import pandas as pd import tensorflow as tf from sklearn.model_selection import train_test_split

**First, we should load the dataset and do a bit of preprocessing to format the data we’ll use in our model.** As usual, we load the data as a list:

# import the data with open(“../data/data1.csv”) as f: data_raw = f.read() # split the data into separate lines lines = data_raw.splitlines()

**Then, we separate the labels and the three features into lists, called “labels” and “features”:**

labels = [] features = [] for line in lines: tokens = line.split(‘,’) labels.append(int(tokens[-1])) x1,x2,x3 = float(tokens[0]), float(tokens[1]), float(tokens[2]) features.append([x1, x2, x3]) Next, we make dummy variables of the three label categories, using Pandas’ get_dummies function: labels = pd.get_dummies(pd.Series(labels))

**After this, the labels list should look like this:**

**The next step is to split our data into train and test sets. For this purpose**, we use the scikit-learn’s train_test_split function that we imported before:

X_train, X_test, y_train, y_test = train_test_split(features, \ labels, test_size=0.2, random_state=42)

**We’re now ready to build up our model using TensorFlow. **First, we define the hyperparameters of the model that are related to the optimization process:

# Parameters learning_rate = 0.1 epoch = 10 Next, we define the hyperparameters that are related with the structure of the model: # Network Parameters n_hidden_1 = 16 # 1st layer number of neurons n_hidden_2 = 16 # 2nd layer number of neurons num_input = 3 # data input num_classes = 3 # total classes Then we need the placeholders to store our data: # tf Graph input X = tf.placeholder(“float”, [None, num_input]) Y = tf.placeholder(“float”, [None, num_classes]) We will store the model parameters in two dictionaries: # weights and biases weights = { ‘h1’: tf.Variable(tf.random_normal([num_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, \ num_classes])) } 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([num_clas ses])) }

**We can now define our graph in TensorFlow. To that end, we provide a function:**

# Create model def neural_net(x): # Hidden fully connected layer with 16 neurons layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights[‘h1’]), \ biases[‘b1’])) # Hidden fully connected layer with 16 neurons layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, \ weights[‘h2’]), biases[‘b2’])) # Output fully connected layer with a neuron for each class out_layer = tf.add(tf.matmul(layer_2, weights[‘out’]), \ biases[‘out’]) # For visualization in TensorBoard tf.summary.histogram(‘output_layer’, out_laye r) return out_layer

**This function takes the input data as an argument. Using this data, it first constructs a hidden layer**. In this layer, each input data point is multiplied by the weights of the first layer and added to the bias terms.

Using the output of this layer, the function constructs another hidden layer. Similarly, this second layer multiplies the output of the first layer with the weights of its own and adds the result to the bias term.

Then the output of the second layer is fed into the last layer which is the output layer of the neural network. The output layer does the same thing as the previous layers. As a result, the function we define just returns the output of the last layer.

**After this, we can define our loss function, optimization algorithm, and the metric we will use to evaluate our model:**

# Construct model logits = neural_net(X) # Define loss and optimizer loss_op = tf.losses.softmax_cross_entropy(logit s=logits, \ onehot_labels=Y) # For visualization in TensorBoard tf.summary.scalar(‘loss_value’, loss_op) optimizer = tf.train.AdamOptimizer(learning_rate=learning _rate) train_op = optimizer.minimize(loss_op) # Evaluate model with test logits correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # For visualization in TensorBoard tf.summary.scalar(‘accuracy’, accuracy) #For TensorBoard merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(“events”) # Initialize the variables (assign their default value) init = tf.global_variables_initializer()

As our loss function, we use the cross-entropy loss with softmax. Apart from this, there are other loss functions that are pre-built in TensorFlow.

Some of them are: softmax, tanh, log_softmax, and weighted_cross_entropy_with_logits.

**Adam is one of the most commonly used optimization algorithms in the machine learning community**.

Some other optimizers available in TensorFlow are: GradientDescentOptimizer, AdadeltaOptimizer, AdagradOptimizer, MomentumOptimizer, FtrlOptimizer, and RMSPropOptimizer.

**Accuracy is our evaluation metric, as usual.**

Now it’s time to train our model!

with tf.Session() as sess: # Run the initializer sess.run(init) # For visualization of the graph in TensorBoard train_writer.add_graph(sess.graph) for step in range(0, epoch): # Run optimization sess.run(train_op, feed_dict={X: X_train, Y: y_train}) # Calculate loss and accuracy summary, loss, acc = sess.run([merged, loss_op, \ accuracy], feed_dict={X: X_train, Y: y_train}) # Add summary events for TensorBoard train_writer.add_summary(summary,step) print(“Step “ + str(step) + “, Loss= “ + \ “{:.4f}”.format(loss) + “, Training Accuracy= “+ \ “{:.3f}”.format(acc)) print(“Optimization Finished!”) # Calculate test accuracy acc = sess.run(accuracy, feed_dict= {X: X_test, Y: y_test}) print(“Testing Accuracy:”, acc) # close the FileWriter train_writer.close() After several iterations, you should see an output similar to this: Step 0, Loss= 0.4989, Training Accuracy= 0.821 Step 1, Loss= 0.2737, Training Accuracy= 0.898 Step 2, Loss= 0.2913, Training Accuracy= 0.873 Step 3, Loss= 0.3024, Training Accuracy= 0.864 Step 4, Loss= 0.2313, Training Accuracy= 0.892 Step 5, Loss= 0.1640, Training Accuracy= 0.933 Step 6, Loss= 0.1607, Training Accuracy= 0.943 Step 7, Loss= 0.1684, Training Accuracy= 0.938 Step 8, Loss= 0.1537, Training Accuracy= 0.944 Step 9, Loss= 0.1242, Training Accuracy= 0.956 Optimization Finished! Testing Accuracy: 0.95476

**Although today’s deep learning applications are quite successful in challenging classification tasks, TensorFlow** also enables us to build regression models in almost the same manner. In this section, we’ll show you how to predict a continuous outcome variable using regression.

**We import the same libraries that we imported for the classification task:**

import numpy as np import pandas as pd import tensorflow as tf from sklearn.model_selection import train_test_split

The dataset we use is the same synthetic set provided, with 20 features and 1 outcome variable. Below, we load the dataset and do some pre-processing to format the data we’ll use in our model:

import the data with open(“../data/data2.csv”) as f: data_raw = f.read() # split the data into separate lines lines = data_raw.splitlines()

Instead of calling the outcome variable as “labels”, we prefer to call it “outcomes” in this case as this seems more appropriate for regression models. As usual, we separate 20% of our dataset as our test data.

outcomes = [] features = [] for line in lines: tokens = line.split(‘,’) outcomes.append(float(tokens[-1])) features.append([float(x) for x in tokens[:-1]]) X_train, X_test, y_train, y_test = train_test_split(features, \ outcomes, test_size=0.2, random_state=42)

**We can now set the hyperparameters of the model regarding the optimization process, and define the structure of our model:**

# Parameters learning_rate = 0.1 epoch = 500 # Network Parameters n_hidden_1 = 64 # 1st layer number of neurons n_hidden_2 = 64 # 2nd layer number of neurons num_input = 20 # data input num_classes = 1 # total classes # tf Graph input X = tf.placeholder(“float”, [None, num_input]) Y = tf.placeholder(“float”, [None, num_classes])

, above. Next, we store the model parameters in two dictionaries as we did in the classification case:

# weights & biases weights = { ‘h1’: tf.Variable(tf.random_normal([num_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, \ num_classes])) } biases = {This time, our outcome is single-value in nature, and we have 20 features. We set the relevant parameters accordingly‘b1’: tf.Variable(tf.random_normal([n_hidden_ 1])), ‘b2’: tf.Variable(tf.random_normal([n_hidden_ 2])), ‘out’: tf.Variable(tf.random_normal([num_clas ses])) }

It’s time to define the structure of our model. The graph is exactly the same as the graph of the classification model we used in the previous part:

# Create model def neural_net(x): # Hidden fully connected layer with 64 neurons layer_1 = tf.add(tf.matmul(x, weights[‘h1’]), biases[‘b1’]) # Hidden fully connected layer with 64 neurons layer_2 = tf.add(tf.matmul(layer_1, weights[‘h2’]), \ biases[‘b2’]) # Output fully connected layer out_layer = tf.matmul(layer_2, weights[‘out’]) \ + biases[‘out’] return out_layer

**The difference between the classification model and the regression model is that the latter uses the L2 loss as a loss function**.

This is because the outcome of the regression model is continuous; as such, we must use a loss function that is capable of handling continues loss values. We also use the Adam optimization algorithm in this regression model.

# Construct model output = neural_net(X) # Define loss and optimizer loss_op = tf.nn.l2_loss(tf.subtract(Y, output)) optimizer = tf.train.AdamOptimizer(learning_rate=learning _rate) train_op = optimizer.minimize(loss_op)

**Another difference between our classification and regression models is the metric we use to evaluate our model**. For regression models, we prefer to use the R-squared metric; it is one of the most common metrics used to assess the performance of regression models:

# Evaluate model using R-squared total_error = tf.reduce_sum(tf.square(tf.subtra ct(Y, \ tf.reduce_mean(Y)))) unexplained_error = tf.reduce_sum(tf.square(tf. subtract(Y, \ output))) R_squared = tf.subtract(1.0,tf.div(unexplained_ error, \ total_error)) # Initialize the variables (assign their default values) init = tf.global_variables_initializer() We are all set to train our model: # Start training with tf.Session() as sess: # Run the initializer sess.run(init) for step in range(0, epoch): # Run optimization sess.run(train_op,feed_dict= \ {X: X_train, \ Y:np.array(y_train).reshape(200000,1)}) # Calculate batch loss and accuracy loss, r_sq = sess.run([loss_op, R_squared], \ feed_dict={X: X_train, \ Y: np.array(y_train).reshape(200000,1)}) print(“Step “ + str(step) + “, L2 Loss= “ + \ “{:.4f}”.format(loss) + “, Training R-squared= “ \ + “{:.3f}”.format(r_sq)) print(“Optimization Finished!”) # Calculate accuracy for MNIST test images print(“Testing R-squared:”, \ sess.run(R_squared, feed_dict={X: X_test, \ Y: np.array(y_test).reshape(50000,1)})) The outcome of the model should look like this: Step 497, L2 Loss= 81350.7812, Training R-squared= 0.992 Step 498, L2 Loss= 81342.4219, Training R-squared= 0.992 Step 499, L2 Loss= 81334.3047, Training R-squared= 0.992 Optimization Finished! Testing R-squared: 0.99210745

**So far, we’ve discussed the low-level structures of TensorFlow. We saw that we must build our own graph** and keep track of the session. However, TensorFlow also provides a high-level API, where the tedious works are handled automatically. This high-level API is called “Estimators”.

**Estimators API also provides pre-made estimators. You can use these estimators quickly**, and customize them if needed. Here are some of the advantages of this API, with respect to the low-level APIs of TensorFlow:

**With fewer lines of codes, you can implement the same model.**

Building the graph, opening and closing the session, and initializing the variables are all handled automatically.

**The same code runs in CPU, GPU, or TPU**

**Parallel computing is supported. As such, if multiple servers are available, **the code you write on this API can be run without any modification of the code you run on your local machine.

**When you are writing your code using this API, you basically follow four steps:**

- Reading the dataset.
- Defining the feature columns.
- Setting up a pre-defined estimator.
- Training and evaluating the estimator.

Now we will demonstrate each of these steps using our synthetic data for classification. First, we read the data from our .csv file, as usual:

# import the data with open(“../data/data1.csv”) as f: data_raw = f.read() lines = data_raw.splitlines() # split the data into separate lines labels = [] x1 = [] x2 = [] x3 = [] for line in lines: tokens = line.split(‘,’) labels.append(int(tokens[-1])-1) x1.append(float(tokens[0])) x2.append(float(tokens[1])) x3.append(float(tokens[2])) features = np.array([x1,x2,x3]).reshape(250000,3) labels = np.array(pd.Series(labels)) X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42) Second, we write a function that converts our features to a dictionary, and returns the features and labels for the model: def inputs(features,labels): features = {‘x1’: features[:,0], ‘x2’: features[:,1], ‘x3’: features[:,2]} labels = labels return features, labels Third, we write a function that transforms our data into a DataSet object: def train_input_fn(features, labels, batch_size): # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((dict(featur es), labels)) # Shuffle, repeat, and batch the examples. return dataset.shuffle(1000).repeat().batch(batch_size ) Defining our feature columns only requires a few lines of code: # Feature columns describe how to use the input. my_feature_columns = [] for key in [‘x1’,’x2’,’x3’]: my_feature_columns.append(tf.feature_column.n umeric_column(key=key))

**Before we run our model, we should select a pre-defined estimator that is suitable for our needs.** Since our task is classification, we use two fully-connected layers, as we did previously. For this, the estimator’s API provides a classifier called DNNClassifier:

# Build a DNN with 2 hidden layers and 256 nodes in each hidden layer. classifier = tf.estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 256 nodes each. hidden_units=[256, 256], # The model must choose between 3 classes. n_classes=3, optimizer=tf.train.AdamOptimizer( learning_rate=0.1))

**As before, we defined two dense layers of size 256, we set the learning rate to 0.1, and we set the number of classes to 3.**

Now, we are ready to train and evaluate our model. Training is as simple as:

classifier.train(input_fn=lambda:train_input_fn (inputs(X_train,y_train)[0], inputs(X_train,y_train)[1], 64), steps=500)

**We provided the function that we wrote above, which returns the DataSet object for the model as an argument **to the train() function. We also set training steps to 500, as usual. When you run the code above, you should see something like:

INFO:tensorflow:loss = 43.874107, step = 401 (0.232 sec) INFO:tensorflow:Saving checkpoints for 500 into /tmp/tmp8xv6svzr/model.ckpt. INFO:tensorflow:Loss for final step: 34.409817. <tensorflow.python.estimator.canned.dnn.DNNClas sifier at 0x7ff14f59b2b0> After this, we can evaluate the performance of our model in our test set: # Evaluate the model. eval_result = classifier.evaluate( input_fn=lambda:train_input_fn(inputs(X_test, y_test)[0], inputs(X_test,y_test)[1], 64), steps=1) print(‘Test set accuracy: {accuracy:0.3f}\n’.format(**eval_result))

**The output should look like this:**

INFO:tensorflow:Starting evaluation at 2018-04-07-12:11:21 INFO:tensorflow:Restoring parameters from /tmp/tmp8xv6svzr/model.ckpt-500 INFO:tensorflow:Evaluation [1/1] INFO:tensorflow:Finished evaluation at 2018-04- 07-12:11:21 INFO:tensorflow:Saving dict for global step 500: accuracy = 0.828125, average_loss = 0.6096449, global_step = 500, loss = 39.017273 Test set accuracy: 0.828

**Summary**

**TensorFlow is a deep learning framework initially developed by Google and now backed by a huge open source community.**

TensorFlow is by far the most popular deep learning framework. Even if you choose to use other frameworks, learning the basics of TensorFlow is beneficial; many of the codes you’ll encounter that are written by others will likely be written in TensorFlow.

- TensorFlow supports distributed computing by nature.
- TensorFlow models can be run on CPUs, GPUs, and TPUs.
- You can write TensorFlow code in Python, Java, Julia, C++, R, and more.
- Although you can use low-level structures of TensorFlow, there are also many high-level APIs that simplify the model building process.
- You can also use Keras on top of Theano or CNTK, but using it on top of TensorFlow is by far the most common usage in the industry.

**After installing TensorFlow, Keras can be installed via PyPl.11 Simply run this command:**

pip install keras

**After you run the command above, the Keras deep learning framework should be installed in your system.** After importing it, you can use

**Keras in your Python code as follows:**

import keras

**Keras abstracts away the low-level data structures of TensorFlow, replacing them with intuitive, easily** integrated, and extensible structures. When designing this framework, the developers followed these guiding principles:

**1. User friendliness: Keras makes human attention focus on the model and builds up the details around this structure.** In doing so, it reduces the amount of work done in common-use cases by providing relevant functionality by default.

**2. Modularity: In Keras, we can easily integrate the layers, optimizers, activation layers**, and other ingredients of a deep learning model together, as if they were modules.

**3. Easy extensibility: We can create new modules in Keras and integrate them into our existing models quite easily.** They can be used as objects or functions.

**The basic component in Keras is called the model. You can think of the Keras model as an abstraction of a deep learning model.** When we start to implement a deep learning model in Keras, we usually begin by creating a so-called model object. Of the many types of models in Keras, Sequential is the simplest and the most commonly used.

**Another basic structure in Keras is called the layer. Layer objects in Keras represent the actual layers in a deep learning model.** We can add layer objects to our model object by just defining the type of the layer, the number of units, and the input/output sizes. The most commonly used layer type is the Dense layer.

**And that is all! You might be surprised that the authors forgot to mention some other critical** parts of the Keras framework. However, as you’ll see below, you can now start to build up your model with what you’ve already learned so far!

**Now it’s time to see Keras in action. Remember that the datasets and the codes examined in this section are available **to you via the Docker image provided with the blog.

**The datasets used to demonstrate Keras is the same synthetic datasets used in the blog on TensorFlow.** We’ll again use them for classification and regression purposes.

**Before we begin to implement our classifier, we need to import some libraries in order to use them. **Here are the libraries we need to import:

import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import Dense from keras import optimizers from sklearn.model_selection import train_test_split

**First, we should load the dataset, and do a bit of pre-processing to format the data we’ll use in our model**. As usual, we load the data as a list:

# import the data with open(“../data/data1.csv”) as f: data_raw = f.read() lines = data_raw.splitlines() # split the data into separate lines

**Then, we separate the labels and the three features into lists, respectively called labels and features:**

labels = [] features = [] for line in lines: tokens = line.split(‘,’) labels.append(int(tokens[-1])) x1,x2,x3 = float(tokens[0]), float(tokens[1]), float(tokens[2]) features.append([x1, x2, x3]) Next, we make dummy variables of the three label categories using Pandas’ get_dummies function: labels = pd.get_dummies(pd.Series(labels))

**The next step is to split our data into train and test sets. For this purpose, we use the train_test_split** function that we imported before:

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)

**We’re now ready to build up our model using Keras. We first define our model and then add three layers; **the first two are the dense layers and the third is the output layer:

model = Sequential() model.add(Dense(units=16, activation=’relu’, input_dim=3)) model.add(Dense(units=16, activation=’relu’)) model.add(Dense(units=3, activation=‘softmax’))

**As you can see, building a graph in Keras is quite an easy task. In the code above, we first define a model object** (which is sequential, in this case). Then we add three fully-connected layers (called dense layers).

After we define our model and layers, we must choose our optimizer and compile our model. For the optimizer, we use Adam, setting its learning rate to 0.1:

sgd = optimizers.Adam(lr=0.1)

**Then we compile our model. In doing so, we define our loss function to be categorical cross-entropy, which **is one of the pre-defined loss functions in Keras.

For the metric to evaluate the performance of our model, we use accuracy, as usual. All these definitions can be implemented in a single line in Keras as seen here:

model.compile(loss=’categorical_crossentropy’, optimizer=sgd, metrics= [‘accuracy’])

Now, it’s time to train our model in a single line! We train our models by calling the fit function of the model object. As parameters, we provide our features and labels as NumPy arrays—the batch size and the epochs. We define the batch size as 10.000 and the epochs as 5:

model.fit(np.array(X_train), np.array(y_train), batch_size=10000, epochs = 5) Next, we evaluate the performance of the model in our test data: loss_and_metrics = model.evaluate(np.array(X_test), np.array(y_test), batch_size=100) print(loss_and_metrics) It should print out: [0.03417351390561089, 0.9865800099372863] So our model’s loss value is approximately 0.03 and the accuracy in the test set is about 0.99!

**If you don’t need any visuals, Keras can easily provide a textual summary of the layers of the model.** For this purpose, Keras provides a summary() function.

When called from a model, it returns the textual information about the model. By just printing the summary of a model using the code below, it is possible to check out the structure of the model:

print(model.summary())

**Of course, visualizations are not only more aesthetically pleasing**, but also can help you easily explain and share your findings with stakeholders and team members. Graphically visualizing the model in Keras is straightforward.

A module named keras.utils.vis_utils includes all the utilities for visualizing the graph using a library called graph viz.

Specifically, the plot_model() function is the basic tool for visualizing the model. The code below demonstrates how to create and save the graph visualization for a model:

from keras.utils import plot_model plot_model(model, to_file = “my_model.png”) The plot_model() function accepts two optional arguments: show_shapes: if True the graph shows the output shapes. The default setting is False.

**show_layer_names: if True the graph shows the names of the layers. The default setting is True.**

**Keras models turn into** TensorFlow Estimators, ready to be used. The function is called model_to_estimator() in the keras.estimator module, and looks like this:

estimator_model = keras.estimator.model_to_estimator(keras_model = model)

**Once we convert our Keras model into TensorFlow Estimator, we can use this estimator in TensorFlow code.**

**Before closing the blog, we encourage our users to read more about the Keras framework.** If you are using deep learning models for research purposes, Keras is probably the most convenient tool for you. Keras will save a lot of time in implementing the many models you’ll try.

If you’re a data science practitioner, Keras is one of the best choices for you both in prototyping and production. Hence, enhancing your understanding and expertise in Keras is beneficial regardless of your particular problem.

**Keras is a deep learning framework that provides a convenient **and easy-to-use abstraction layer on top of the sensor flow framework.

Keras brings a more user-friendly API to the TensorFlow framework. Along with easy extensibility and modularity, these are the key advantages of Keras over other frameworks.

The main structure in Keras is the model object, which represents the deep learning model to be used. The most commonly-used model type is the sequential model. Another important structure in Keras is the layer, which represents the layers in the model; the most common layer is the Dense layer.

Visualizing the model structure in Keras is accomplished with a single function call to plot_model(). It is a good idea to start building deep learning models in Keras instead of TensorFlow if you are new to the field.

Although Keras provides a very wide range of functionality, one may need to switch to TensorFlow to write some sophisticated functionality for non-standard deep learning models.

pretty = tf.placeholder([None, 784], tf.float32) softmax = (prettytensor.wrap(examples) .fully_connected(256, tf.nn.relu) .fully_connected(128, tf.sigmoid) .fully_connected(64, tf.tanh) .softmax(10))

**The Pretty Tensor installation is very simple; just use the pip installer:**

sudo pip install prettytensor

**Pretty Tensor has three modes of operation, which share the ability to chain methods.**

**In normal mode, every time a method is called, a new Pretty Tensor is created.** This allows for easy chaining, and yet you can still use any particular object multiple times. This makes it easy to branch your network.

**In sequential mode, an internal variable, the head, keeps track of the most recent output tensor,** thus allowing for the breaking of call chains into multiple statements.

Here is a quick example:

seq = pretty_tensor.wrap(input_data).sequential() seq.flatten() seq.fully_connected(200, activation_fn=tf.nn.relu) seq.fully_connected(10, activation_fn=None) result = seq.softmax(labels, name=softmax_name))

**In this example, we'll define and train either a two-layer model or a convolutional model in the style of LeNet 5:**

from six.moves import xrange import tensorflow as tf import prettytensor as pt from prettytensor.tutorial import data_utils tf.app.flags.DEFINE_string( 'save_path', None, 'Where to save the model checkpoints.') FLAGS = tf.app.flags.FLAGS BATCH_SIZE = 50 EPOCH_SIZE = 60000 // BATCH_SIZE TEST_SIZE = 10000 // BATCH_SIZE Since we are feeding our data as numpy arrays, we need to create placeholders in the graph. These must then be fed using the feed dict. image_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 28, 28, 1]) labels_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 10]) tf.app.flags.DEFINE_string('model', 'full', 'Choose one of the models, either full or conv') FLAGS = tf.app.flags.FLAGS

**We created the following function, multilayer_fully_connected. The first two layers are fully connected (100 neurons)**, and the final layer is a softmax result layer. Note that the chaining layer is a very simple operation:

def multilayer_fully_connected(images, labels): images = pt.wrap(images) with pt.defaults_scope\ (activation_fn=tf.nn.relu,l2loss=0.00001): return (images.flatten().\ fully_connected(100).\ fully_connected(100).\ softmax_classifier(10, labels))

**In the following, we'll build a multilayer convolutional network; the architecture is similar to that defined in LeNet 5**. Please change this to experiment with other architectures:

def lenet5(images, labels): images = pt.wrap(images) with pt.defaults_scope\ (activation_fn=tf.nn.relu, l2loss=0.00001): return (images.conv2d(5, 20).\ max_pool(2, 2).\ conv2d(5, 50).\ max_pool(2, 2). \ flatten().\ fully_connected(500).\ softmax_classifier(10, labels)) Since we are feeding our data as numpy arrays, we need to create placeholders in the graph. These must then be fed using the feed dict: def main(_=None): image_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 28, 28, 1]) labels_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 10]) Depending on FLAGS.model, we may have a two-layer classifier or a convolutional classifier, previously defined: def main(_=None): if FLAGS.model == 'full': result = multilayer_fully_connected\ (image_placeholder, labels_placeholder) elif FLAGS.model == 'conv': result = lenet5(image_placeholder, labels_placeholder) else: raise ValueError\ ('model must be full or conv: %s' % FLAGS.model) Then we define the accuracy function for the evaluated classifier: accuracy = result.softmax.evaluate_classifier\ (labels_placeholder,phase=pt.Phase.test) Next, we build the training and test sets: train_images, train_labels = data_utils.mnist(training=True) test_images, test_labels = data_utils.mnist(training=False)

We will use a gradient descent optimizer procedure and apply it to the graph. The pt.apply_optimizer function adds regularization losses and sets up a step counter:

optimizer = tf.train.GradientDescentOptimizer(0.01)\ train_op = pt.apply_optimizer (optimizer,losses=[result.loss]) We can set save_path in the running session to automatically checkpoint every so often. Otherwise, at the end of the session, the model will be lost: runner = pt.train.Runner(save_path=FLAGS.save_path) with tf.Session(): for epoch in xrange(10): Shuffle the training data: train_images, train_labels =\ data_utils.permute_data\ ((train_images, train_labels)) runner.train_model(train_op,result.\ loss,EPOCH_SIZE,\ feed_vars=(image_placeholder, \ labels_placeholder),\ feed_data=pt.train.\ feed_numpy(BATCH_SIZE,\ train_images,\ train_labels),\ print_every=100) classification_accuracy = runner.evaluate_model\ (accuracy,\ TEST_SIZE,\ feed_vars=(image_placeholder,\ labels_placeholder), \ feed_data=pt.train.\ feed_numpy(BATCH_SIZE,\ test_images,\ test_labels)) print('epoch' , epoch + 1) print('accuracy', classification_accuracy ) if __name__ == '__main__': tf.app.run() Running this example provides the following output: >>> Extracting /tmp/data/train-images-idx3-ubyte.gz Extracting tmp/data/train-labels-idx1-ubyte.gz Extracting /tmp/data/t10k-images-idx3-ubyte.gz Extracting /tmp/data/t10k-labels-idx1-ubyte.gz epoch = 1 Accuracy [0.8994] epoch = 2 Accuracy [0.91549999] epoch = 3 Accuracy [0.92259997] epoch = 4 Accuracy [0.92760003] epoch = 5 Accuracy [0.9303] epoch = 6 Accuracy [0.93870002] epoch = 7 epoch = 8 Accuracy [0.94700003] epoch = 9 Accuracy [0.94910002] epoch = 10 Accuracy [0.94980001]

**Source code for digit classifier**

**The following is the full source code for the digit classifier previously described:**

from six.moves import xrange import tensorflow as tf import prettytensor as pt from prettytensor.tutorial import data_utils tf.app.flags.DEFINE_string('save_path', None, 'Where to save the model checkpoints.') FLAGS = tf.app.flags.FLAGS BATCH_SIZE = 50 EPOCH_SIZE = 60000 // BATCH_SIZE TEST_SIZE = 10000 // BATCH_SIZE image_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 28, 28, 1]) labels_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 10]) tf.app.flags.DEFINE_string('model', 'full','Choose one of the models, either full or co FLAGS = tf.app.flags.FLAGS def multilayer_fully_connected(images, labels): images = pt.wrap(images) with pt.defaults_scope(activation_fn=tf.nn.relu,l2loss=0.000 return (images.flatten().\ fully_connected(100).\ fully_connected(100).\ softmax_classifier(10, labels)) def lenet5(images, labels): images = pt.wrap(images) with pt.defaults_scope\ (activation_fn=tf.nn.relu, l2loss=0.00001): return (images.conv2d(5, 20).\ max_pool(2, 2).\ conv2d(5, 50).\ max_pool(2, 2).\ flatten(). \ fully_connected(500).\ softmax_classifier(10, labels)) def main(_=None): image_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 28, 28, 1]) labels_placeholder = tf.placeholder\ (tf.float32, [BATCH_SIZE, 10]) if FLAGS.model == 'full': result = multilayer_fully_connected\ (image_placeholder,\ labels_placeholder) elif FLAGS.model == 'conv': result = lenet5(image_placeholder,\ labels_placeholder) else: raise ValueError\ ('model must be full or conv: %s' % FLAGS.model) accuracy = result.softmax.\ evaluate_classifier\ (labels_placeholder,phase=pt.Phase.test) train_images, train_labels = data_utils.mnist(training=True) test_images, test_labels = data_utils.mnist(training=False) optimizer = tf.train.GradientDescentOptimizer(0.01) train_op = pt.apply_optimizer(optimizer,losses=[result.loss]) runner = pt.train.Runner(save_path=FLAGS.save_path) with tf.Session(): for epoch in xrange(10): train_images, train_labels =\ data_utils.permute_data\ ((train_images, train_labels)) runner.train_model(train_op,result.\ loss,EPOCH_SIZE,\ feed_vars=(image_placeholder,\ labels_placeholder), feed_data=pt.train.\ feed_numpy(BATCH_SIZE,\ train_images,\ train_labels),\ print_every=100) classification_accuracy = runner.evaluate_model\ (accuracy,\ TEST_SIZE,\ feed_vars=(image_placeholder,\ labels_placeholder),\ feed_data=pt.train.\ feed_numpy(BATCH_SIZE,\ test_images,\ test_labels)) print('epoch' , epoch + 1) print('accuracy', classification_accuracy ) if __name__ == '__main__': tf.app.run()

Stress Relief and Stress Out (90+ New Stress Hacks 2019)

30 Best Research institutes & Universities in the World (2019)

Top 30+ Websites for Download Free E-Books (2019)

Automated Testing (Best Tutorial 2019)

Academic Writing Skills (Best Tutorial 2019)

How to a write Thesis Background (Best Tutorial 2019)

How to Write Thesis and Thesis Statement (Tutorial 2019)

Zoho CRM (Best Tutorial 2019)

Business Plans (70+ New Business Hacks 2019)

100+ New Business Planning Tips (2019)

20+ Best Options for Funding for Startups (2019)

What is a Thesis (Best Tutorial 2019)

How to start a Podcast (20+ Best Podcasts Hacks 2019)

What is Content Marketing (100+ Marketing Tips 2019)

20+ Coding Challenges in Software (2019)

Popular Articles

1. Top 20 Research Centers Labs in the World 2019

2. Top 12 Highest Salaries of Government Jobs in India 2019

3. Top 10 Space Research Organizations in the World 2019

4. Facebook Messenger Hack (2019)

5. 20+ Most Visited and Popular Websites in the World (2019)

6. Facebook Messages without Messenger (New Messenger Hacks 2019)

7. Top 30+ Websites for Download Free E-Books (2019)

8. Facebook Groups Secret (30+ New Facebook Hacks 2019)

9. Zoho CRM (Best Tutorial 2019)

10. Literature Review (Best Tutorial 2019)

11. Academic Writing Skills (Best Tutorial 2019)

12. 60+ On Page SEO Hacks (2019)

13. 30 Best Research institutes & Universities in the World (2019)

14. How to a write Thesis Background (Best Tutorial 2019)

15. Stress Relief and Stress Out (90+ New Stress Hacks 2019)

16. Java Exceptions (Best Tutorial 2019)

17. Robot Programming | Robot Operating System (ROS)

18. 55+ Best Google SEO Tips for Blogging (2019)

19. How to Write Thesis and Thesis Statement (Tutorial 2019)

20. Research Proposal and Null Hypothesis with Example (Tutorial 2019)

21. How to Build a 3D Printer (Best Tutorial 2019)

22. Qualitative Research Methods (Best Tutorial 2019)

23. Arduino LED Projects with code (Best Tutorial 2019)

24. How to Starting Blogging (100+ New Blogging Hacks 2019)

25. 30+ Largest Corporations in the World (2019)

26. Build your own Drone (Best Tutorial 2019)

27. Javafx Pane Gridpane and Animation (Best Tutorial 2019)

28. How Write Final Thesis (Best Tutorial 2019)

29. Thesis Definition (Best Thesis Tutorial 2019)

30. Quantitative vs Qualitative Research (Best Tutorial 2019)