TensorFlow Variables and Placeholders Tutorial With Example is today’s topic. TensorFlow is an open source machine learning framework developed by Google which can be used to the build neural networks and perform a variety of all machine learning tasks. TensorFlow works on data flow graphs where nodes are the mathematical operations, and the edges are the data […]

The post TensorFlow Variables and Placeholders Tutorial With Example appeared first on AppDividend.

]]>TensorFlow Variables and Placeholders Tutorial With Example is today’s topic. TensorFlow is an open source machine learning framework developed by Google which can be used to the build neural networks and perform a variety of all machine learning tasks. TensorFlow works on data flow graphs where nodes are the mathematical operations, and the edges are the data in the for tensors, hence the name Tensor-Flow.

A tensor is a central unit of data in TensorFlow. It consists of primitive values stored in the shape of a multidimensional array. The number of dimensions a tensor has is called its **rank**.

A rank 0 tensor is just a **scalar**. To keep things simple, we can say that a tensor in TensorFlow is instead a fancy name of an array and now we call dimension number as rank. One dimensional array or list is rank one tensor, and two-dimensional array or list is two rank tensor.

When we train the model, we need to assign some weights and biases throughout the session.

TensorFlow variables can hold the values of biases and weights throughout the session.

You need to one thing keep in mind thatTensorFlow variables need to be initialized.

In TensorFlow variables are of great use when we are training models. As constants, we have to call a constructor to initialize a variable; the initial value can be passed in as an argument.

Variables can easily be added to the computational graph by calling a constructor.

TensorFlow placeholders are initially empty and are used to feed in the actual training examples.

If we want to inject the data into a computation graph, we have to use the mechanism named as a placeholder. Placeholders are bound inside some expressions. The syntax of the placeholder is following.

placeholder(dtype, shape=None, name=None)

Placeholders allow us to not to provide the data in advance for operations and computation graphs, and the data can be added in runtime from external sources as we train the Machine Learning models.

TensorFlow Placeholder does need to declare as a float32 datatype within an optional shape parameter.

Okay, we have covered enough theory, let’s see some practical example of TensorFlow Variables and Placeholders in Python Jupyter Notebook.

You can find the guide about how to install TensorFlow on Mac on this article.

Also, you can find the basics of TesorFlow post.

Now, fire up the Jupyter Notebook and import the TensorFlow.

import tensorflow as tf

You can run the cell by keyboard shortcut **Ctrl + Enter.**

In the next cell, we will write the following code.

sess = tf.InteractiveSession()

The only difference with a regular **Session** is that an **InteractiveSession** installs itself as the default session on construction. We do not need to write that with** tf.Session() as sess** code whenever we need to perform some operations.

Once we run the above code, we do not need to start the session again for that Jupyter Notebook file.

Now, let’s define a random tensor using the following code.

tensorA = tf.random_uniform((4, 4), 1, 2) tensorA

Here, we have defined the **4*4 **matrix between the value 1 and 2. The values are random between 1 to 2.

When we try to display the tensorA, we will get the following output.

Here, you can see that the datatype of tensorA is **float32**.

Now, in the next step, we will define a TensorFlow variable called **tensor_var_A.**

tensor_var_A = tf.Variable(initial_value=tensorA)

Okay, now run the **tensor_var_A **variable.

sess.run(tensor_var_A)

You will get an error like below.

So, the error is saying that **FailedPreconditionError: Attempting to use uninitialized value Variable.**

That means, we need to first initialize the TensorFlow variable and then we can run that variable.

So, let’s do that first. Write the following code in the next cell.

init = tf.global_variables_initializer()

Run the above cell and then write the following code in the next cell.

sess.run(init)

Run the above cell, and all the variables are initialized. Now, we write that failed code again, and now you can see the 4*4 matrix.

sess.run(tensor_var_A)

See the output below.

Now, let’s create a TensorFlow Placeholder Example.

Define one placeholder using the following code in the next cell.

tfph = tf.placeholder(tf.float32, shape=(None, 5))

The above code creates a TensorFlow placeholder, and its datatype is float32, and here **None **is the placeholder’s initial value of data. As time goes and our machine learning mode starts training, the data is filled in the placeholder. But, at the starting point, it is None.

We can use another example of TensorFlow Placeholder, which is the following code.

a = tf.placeholder(tf.float32, name='a') b = tf.placeholder(tf.float32, name='b') c = tf.add(a, b, name='c') sess.run(c, feed_dict={a: 2.1, b: 1.9})

Here, we have defined two placeholders and then create the third node to add both placeholders and run the operation. Remember, we are using Interactive Session. The output is following.

So, this is how you can create TensorFlow Variables and Placeholders.

Finally, TensorFlow Variables and Placeholders Tutorial With Example is over.

The post TensorFlow Variables and Placeholders Tutorial With Example appeared first on AppDividend.

]]>In this article, we will see Tensorflow Basics Tutorial With Example | Getting Started With Tensorflow. Machine learning is the complex discipline. But implementing the machine learning models is far less daunting and difficult than it used to be, thanks to the machine learning frameworks—such as Google’s TensorFlow: that ease the process of acquiring data, training models, serving […]

The post Tensorflow Basics Tutorial | Getting Started With Tensorflow appeared first on AppDividend.

]]>In this article, we will see Tensorflow Basics Tutorial With Example | Getting Started With Tensorflow. Machine learning is the complex discipline. But implementing the machine learning models is far less daunting and difficult than it used to be, thanks to the machine learning frameworks—such as Google’s TensorFlow: that ease the process of acquiring data, training models, serving predictions, and future results.

TensorFlow is an open source library for numerical computation and large-scale machine learning. TensorFlow bundles together the slew of machine learning and deep learning (also known as neural networking) models and algorithms and makes them useful by way of the common metaphor. It uses Python to provide the convenient front-end API for building applications with the framework while executing those applications in high-performance C++.

For this example, I am using Jupyter Notebook to perform some Tensorflow practicals. So, if your notebook has not installed the Tensorflow library then you can install it using Anaconda Navigator and find the environment section of Anaconda Navigator. You will see something like this.

Here, if a tensorflow package is uninstalled on your machine, then you can install it from here.

Now, launch a Jupyter Notebook and create a new book and verify that you have installed the **tensorflow** using the following code in the first cell of Jupyter Notebook.

import tensorflow as tf tf.__version__

Run the cell using the Ctrl + Enter command and see the output.

Let’s define the Tensorflow constants using the following code. You need to write that code in the next cell.

app = tf.constant('app') type(app)

Here, I have defined the app tensorflow constant and also we have checked the type of that constant. Run the cell See the below output.

Let’s define one more tensorflow constant and see its datatype as well.

dividend = tf.constant('dividend') type(dividend)

See the output below.

Now, print the constant and see the output.

print(app)

See the output below.

See the datatype of the constant is String. The object is Tensor.

You can not run any operation in Tensorflow without using the sessions. If you try to add any integers or concat the strings, you need to start a session and then add the operational code and then run that operation. Otherwise, nothing will have happened. Let’s concat the above two strings we have defined earlier. See the following example.

with tf.Session() as sess: result = sess.run(app + dividend) print(result)

Here, we have used the **tf.Session() **function to start a session and perform the concat operation between two strings and store that output in the result variable and then print that variable. See the output.

Here, in the output **b **represents the byte literal.

Let’s perform the addition operation computation in Tensorflow. Write the following code in the next cell.

a = tf.constant(10) b = tf.constant(10) with tf.Session() as sess: result = sess.run(a + b) print(result)

Here, we have defined the two constants, which are integers and then we have performed the addition operation inside the tensorflow session and store the output in result variable and display that variable. See the following output.

We can create a Matrix using Tensorflow functions like, we have created using NumPy library.

We can create the matrix using **tf.fill() **method provided by the tensorflow framework. See the following example.

mat = tf.fill((5,5), 21) with tf.Session() as sess: op = sess.run(mat) op

See the output below.

In the above code, we have created the 5*5 matrix with the 21 values filled inside each element. Remember, we need to perform any operation inside the tensorflow sessions.

So these are some basics of Tensorflow.

Finally, Tensorflow Basics Tutorial | Getting Started With Tensorflow is over.

The post Tensorflow Basics Tutorial | Getting Started With Tensorflow appeared first on AppDividend.

]]>In this tutorial, I will show you How To Build Simple Model In Tensorflow. Tensorflow Framework is the popular framework to design a neural network in Machine Learning. Tensorflow is created at Google. It is an open source machine learning framework for everyone. TensorFlow is an open source library for high-performance numerical computation. Tensorflow has a flexible […]

The post How To Build Simple Model In Tensorflow appeared first on AppDividend.

]]>In this tutorial, I will show you How To Build Simple Model In Tensorflow. Tensorflow Framework is the popular framework to design a neural network in Machine Learning. Tensorflow is created at Google. It is an open source machine learning framework for everyone. TensorFlow is an open source library for high-performance numerical computation. Tensorflow has a flexible architecture that allows easy deployment of calculation across a variety of platforms like CPUs, GPUs, TPUs, and from desktops to clusters of servers to mobile and edge devices. It is initially developed by researchers and engineers from the Google Brain team within Google’s AI organization, and it comes with strong support for machine learning and deep learning, and the flexible numerical computation core is used across many other scientific domains.

If we want to work with Tensorflow then first, we need to define the tensorflow graph. In this model, we will define two tensors or nodes, and then we add those nodes. Now, let’s dive into what is tensors.

TensorFlow, as the name indicates, is the framework to define and run computations involving tensors. The **tensor** is the generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.

When writing a TensorFlow program, the primary object you manipulate and pass around is the **tf.Tensor**. The **tf.Tensor **object represents a partially defined computation that will eventually produce a value. TensorFlow programs work by first building a graph of **tf.Tensor** objects, detailing how each tensor is computed based on the other available tensors and then by running parts of this graph to achieve the desired results.

TensorFlow programs use the tensor data structure to represent all data only tensors are passed between operations in the computation graph. You can think of the TensorFlow tensor as an n-dimensional array or list.

For example, a scaler is a tensor, a vector is a tensor, and a matrix is a tensor. A tensor has a rank, a shape, and a static type so that a tensor can be represented as the multidimensional array of numbers.

Okay, now, if you do not know how to install and configure the Tensorflow on virtualenv then check out my post on how to install tensorflow on this blog.

Okay, now we will create a file called **tflow.py **and write the following code inside that file.

# app.py import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' X = tf.placeholder(tf.int32, name='X') Y = tf.placeholder(tf.int32, name='Y') add = tf.add(X, Y, name='add') with tf.Session() as session: result = session.run(add, feed_dict={ X: [19, 1, 2], Y: [21, 4, 3] }) print(result)

In the above code, first, we have imported the **tensorflow **and **os module **from the Python library.

We have used the **os **module’s **environ attribute **to disable the logs which are unnecessary for this demo model. If you want that log in the output, then you can comment that statement.

Then we have created the computational graph with two nodes using **a placeholder **and defined their datatypes and names.

So, we have two tensors up to now which is X and Y.

Then we are adding those tensors using tensorflow’s add method and get the third node which is an **add node.**

If we want to execute operation in the Graph, we need to use sessions.

We can create a session in tensorflow using **with tf.Session as session **code.

Next step is that we have called the run method on the session, which takes the two arguments.

- add node
- data

We have used the dictionary data, which has two nodes, X and Y. So we will add those and generate a new node with the session.

Go to the terminal and run the following command.

python3 tflow.py

The output of the above code is following.

So, we have built a tensorflow model, which can add two nodes and gives the output node.

Here, the computation is to add the nodes and nothing complicated. But in real life application, there are lots of variables and iterables to go through and finally predict the future value.

Finally, How To Build Simple Model In Tensorflow Tutorial With Example is over.

The post How To Build Simple Model In Tensorflow appeared first on AppDividend.

]]>In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed Python 3 in your mac machine. If not then go to python.org website and install version 3 of Python. Other then that, you need to have an editor like Visual Studio Code. You can always welcome with PyCharm IDE as well. […]

The post How To Install Tensorflow on Mac Tutorial From Scratch appeared first on AppDividend.

]]>In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed **Python 3 **in your mac machine. If not then go to **python.org **website and install version 3 of Python. Other then that, you need to have an editor like Visual Studio Code. You can always welcome with PyCharm IDE as well. I am using VSCode and have installed Python extension.

I have installed version 3 of the Python. You can check it by going to the terminal and see the output by **python3 -v **command.

If you don’t have still Python 3 and other packages like Otherwise, install Python, the pip package manager, and Virtualenv then you can follow the below steps.

First, Install using the Homebrew package manager.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then add a global path. Modify and add the following line inside **.bash_profile **or** .zshrc **file.

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

The hit the one by one following command.

brew update brew install python # Python 3 sudo pip3 install -U virtualenv

I have already created a Virtual Environment, but you can also create by following steps.

Create a new virtual environment by choosing a Python interpreter and making a **./pythonenv** directory to hold it. Type the following command then.

virtualenv --system-site-packages -p python3 ./pythonenv

Go inside that folder.

cd pythonenv

Activate the virtual environment using a shell-specific command.

source bin/activate

If you have installed virtual environment perfectly then virtualenv is active, your shell prompt is prefixed with (**pythonenv**). See the below image.

Install packages within a virtual environment without affecting the host system setup. Start by upgrading **pip. **See the following command to upgrade pip.

pip install --upgrade pip

After that, you can see all the packages by typing the following command.

pip list

You can see that, I have already installed the **TensorFlow package.**

If your system has not tensorflow package then you can install it using the following command.

pip install tensorflow

Basically, package dependencies are already installed, if you have not the newer version of tensorflow then you can upgrade using the following command.

pip install --upgrade tensorflow

So, finally, the Tensorflow is installed on your machine.

Let’s check if it has installed or not.

Create a new folder inside the **pythonenv **folder called **tflow **and inside that, create a new file called **tflow.py **and add the following code inside it.

import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' hello = tf.constant('Kudos, TensorFlow!') sess = tf.Session() print(sess.run(hello))

In the above code, we have imported the **tensorflow** as well as the **os** module.

If you don’t import the os module and use the **environ **function then it will give us a warning and avoid the warning, we have applied the setting export TF_CPP_MIN_LOG_LEVEL=2.

For more information, you can check out this StackOverflow link.

Finally, run the **tflow.py **file using the following command and see the output.

python3 tflow.py

If you have seen like this then congratulations!! You have successfully installed Tensorflow on your mac.

Finally, How To Install Tensorflow on Mac Tutorial From Scratch is over.

The post How To Install Tensorflow on Mac Tutorial From Scratch appeared first on AppDividend.

]]>