#002 TF 2.0 An Introduction to TensorFlow 2.0
Highlights: In this post we are going to talk more about what are TensorFlow data model elements. Those are elements like Constants and Variables.
So let’s see how we can create an operation like \(c = a*b\) and run it with the following lines of code.
TensorFlow data model elements
There are certain programming elements in TensorFlow that are essential for writing any TensorFlow code like Constants and Variables. These data model elements are used to assign and store values.
Tensors can be created in the following way:
- By defining constants, operations, and variables, and passing the values to their constructor.
- By converting Python objects such as scalar values, lists, and NumPy arrays with the tf.convert_to_tensor() function.
Let’s show how we can create the constant value tensors using the tf.constant() function. Here, we can also define the type and the name of our tensor.
TensorFlow provides for us many operations that can be applied to Tensors. An operation is defined by passing values and/or assigning the output to another tensor.
Each operation in a graph is given a unique name. This name is independent of the names the objects are assigned to in Python. Tensors are named after the operation that produces them followed by an output index, as “mul:0” and “add:0” in the following code.
Creating tensors from Python objects
TensorFlow enables us to create tensors from Python objects such as lists and NumPy arrays, using the tf.convert_to_tensor() operation with the following signature:
tf.convert_to_tensor( value, dtype=None, name=None, preferred_dtype=None )
First, let’s see how we can create a tensor from a number.
TensorFlow can seamlessly convert NumPy ndarray to TensorFlow tensor and vice-versa. So, let’s see how we can create an operation like the following graph and run it with the following lines of code.
In TensorFlow, variables are tensor objects that hold values that can be modified during the execution of the program.
When you train a model you use variables to hold and update parameters. Variables are in-memory buffers containing tensorsTensorFlow Docs.
A variable can be created with tf.Variable() function. Let’s see an example for a basic linear model: \(y= wx + b\).
Here we are going to do this in two stages. First, we call the tf.Variable() function in order to create a Variable and define what value it will be initialized with.
After initializing the variables, we will run our model to give the output for values of \(x = [1,2,3,4]\).
The graph that corresponds to the following piece of code
To sum it up, this was an introduction to TensorFlow data types, and now it is time to think about how TensorFlow evaluates our graphs.
So for that reason, in the next post we are going to talk about what is an Eager execution and how it is utilized in TensorFlow 2.x.