# TensorFlow | TF tensor

In the previous article, we learnt how to use Google Colaboratory to get started with TensorFlow. In this tutorial we will cover tensors in TensorFlow.

### Introduction to Tensors

Tensors are multi-dimensional arrays having a uniform type that is called a `dtype`.

Similar to Python strings and numbers, tensors are immutable. A tensor's contents can never be changed, only a new tensor can be created.

### How to create a tensor

Let's get started with creating a tensor in TensorFlow. We would be using Google Colaboratory to write the code, if you don't know how to use Google Colaboratory refer tensorflow with google colab to get started.

Create a New notebook in Colaboratory. Write below code in the cell and run the cell.

``` ```

import tensorflow as tf
t1 = tf.constant(6)
t1
``` ```

#### Output ### Properties of a tensor

From the above output we can see a `tf.Tensor` has the following properties:
• a data type - tensor `t1` is having data type as `int32`
• a shape - tensor `t1` is having shape `t1.shape`, that means its a scalar or rank 0 tensor.
• numpy function (available in eager execution) - `numpy` function associated with tensor returns A NumPy array of the same shape and dtype.

### Tensor `DType's`

`dtype` property of Tensor represents the type of the elements in a Tensor. Following are some of the `DType's` defined in TensorFlow.

For a complete list of `DType's` refer this link.

• bfloat16
• float16
• float32
• float64
• int16
• int32
• int64
• int8
• string
• uint16
• uint32
• uint64
• uint8

### Tensor `DType's` attributes

Let's have a look to some os the important methods available in `tf.dtypes` module.

#### `tf.dtype.is_compatible_with()`

`tf.dtype.is_compatible_with` is used to check if a Tensor of the `other` `DType` will be implicitly converted to this `DType`. To understand this have a look to the below code snippet.

``` ```
import tensorflow as tf
import numpy as np

t1 = tf.constant(6, dtype=np.int16)
t2 = tf.constant(6, dtype=tf.int16)
t3 = tf.constant(6.0, dtype=tf.float16)

print(t1)
print(t2)
print(t3)

print(t1.dtype.is_compatible_with(t2.dtype))

ptint(t2.dtype.is_compatible_with(t3.dtype))
``` ```

#### Output #### `tf.dtype.limits`

`tf.dtype.limits` returns the tuple containing min, max intensity limits of `dtype`.

``` ```
import tensorflow as tf

t1 = tf.constant(6)
print(t1)

print(t1.dtype.limits)

``` ```

#### Output #### `tf.dtype.is_integer`

`tf.dtype.is_integer` returns `True` if the dtype of tensor is (non-quantized) integer type.

``` ```
import tensorflow as tf

t1 = tf.constant(6)
print(t1.dtype.is_integer)
``` ``` #### `tf.dtype.is_floating`

`tf.dtype.is_floating` returns `True` if the dtype of tensor is (non-quantized, real) floating point type.

``` ```
import tensorflow as tf

t1 = tf.constant(6.0)
print(t1.dtype.is_floating)
``` ``` #### `tf.dtype.is_bool`

`tf.dtype.is_bool` returns `True` if the dtype of tesnor is a boolean data type.

``` ```
import tensorflow as tf

t1 = tf.constant(True, dtype=tf.bool)
print(t1.dtype.is_bool)

t2 = tf.constant(False, dtype=tf.bool)
print(t2.dtype.is_bool)
``` ``` ###### www.gcptutorials.com

Portal for short tutorials and code snippets.

###### Contact

gcptutorials.com@gmail.com