#### Good job! You’re all caught up

Topcoder Copilot Recruitment Challenge (3)
TOSCA Editor - Web Application Wireframe Challenge (1)
Earlier Dismiss All
Topcoder Copilot Recruitment Challenge (2)

## October 25, 2019 TensorFlow 2.0 Basics

This post will take you through the basics of using TensorFlow to build strong foundations before we start building neural networks.

For details about the environment setup and installation of TensorFlow please refer to this post.

Let’s first understand what a tensor is: a tensor is nothing but a multidimensional array.

To use the utilities and API of TF you have to first import TensorFlow library.

import tensorflow as tf #import tensorflow library under alias tf

All tensors have following properties:
shape
dimension
type (data type)

Let’s see how to declare a tensor constant in tf, value of a constant doesn’t change.

# create tf constants i.e 1D arrays with a list of values
x = tf.constant([1,3,5])
y = tf.constant([2,4,6])
# perform multiply operation on x and y , assigning the result to mul tensor variable
mul = tf.multiply(x,y)
print(mul)


Output:

Tensor("Mul:0", shape=(3,), dtype=int32)


The output will contain shape as (3,), since there is only 1 dimension and the length of dimension is 3. If the array was of 2D then the shape would contain 2 dimensions specifying rows and columns.

Example:

tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
[-1. -1. -1.]]


Output:

<tf.Tensor 'Const_3:0' shape=(2, 3) dtype=int32>

##### TensorFlow Rank and Shape
 Rank Shape Example 0 [] Scalar, s= 42 1 [D0] [1,2] 2 [D0,D1] [[1,2], [3,4]] 3 [D0,D1,D2] [[,,], [,,], [,,]] n [D0,D1,D2,….,Dn] A tensor with shape n-dimensions
##### TensorFlow Operations

TensorFlow contains all necessary mathematical operations such as add, subtract. Tensorflow operation takes one or more tensor(s) as input and produces zero or more tensor(s) as output. In the code example above we computed multiplication with 2 tensors x,y as input. For the list of operations supported by TensorFlow please refer here

##### Variables

TensorFlow variables are the way to hold and update the parameters when we train our model. Variables can be explicitly initialized and saved during and after training.

In TensorFlow variables are manipulated by tf.variable class. A tf.variable represents a tensor whose value can be modified by performing an operation on it. High level libraries such as tf.keras use tf.variable to store model parameters

Creating a variable:
When you create a variable you pass a tensor as it’s initial value to the variable constructor my_var = tf.variable(tf.zeros([1.,2.,3.]))

The above line of code will create a variable of 3 dimensions with shape [1, 2, 3] filled with zeros. The variable my_var will have the dtype tf.float32. If not specified, the dtype  is inferred from the initial value.

##### DataTypes

Every tensor has a data type. TensorFlow supports multiple data types such as float, int, string, bool etc. Details for the list of dtypes supported can be found here

The tf.as_dtype() function converts numpy types and string type names to a DType object.

##### Conclusion:

In this blog post we learned about the basics of TensorFlow. We will continue to learn more about TensorFlow and how to build neural networks using TensorFlow in next upcoming post. So stay tuned.

rkhemka

Guest Blogger

categories & Tags

UNLEASH THE GIG ECONOMY. START A PROJECT OR TALK TO SALES
Close