# Creating Tensors with different functions in Tensorflow

We can make the tensors from the known values. The tf package provides seven function which we can use to from tensors with known values. Constant,zeros,ones,fill etc.

We can make the tensors from the known values. The tf package provides seven function which we can use to from tensors with known values.

**constant(value,dtype=None,shape=None,name='Const',verify_shape=False)**- Returns a tensor containing the given value.**zeros(shape,dtype=tf.float32,name=None)**- Returns a tensor filled with zeros**ones(shape,dtype=tf.float32,name=None)**- Returns a tensor filled with ones**fill(dims,value,name=None)**- Returns a tensor filled with the given value**linspace(start,stop,num,name=None)**- Returns a tensors containing the linear range of values**range(start,limit,delta = 1,dtype = None,name='range')**- Returns a tensor containing the range of values

A tensor may have multiple dimensions, and the number of dimensions in a tensor is its *rank*. The length of tensor's dimensions from an array called the tensor's *shape*.

- [] - The tensor containing a single value (Rank 0 )
- [3] - The tensor is a 1-D array contaning three values (Rank 1)
- [3,4] - The tensor is a 3x4 matrix (Rank 2)
- [3,4,5] - The tensor is a multidimensional array whose dimensions equal 3,4 and 5 (Rank3)

### Tensors Data Types

A *dtype* argument identifies the data type of the tensor's element.

The default value of *dtype* is **float32**, which tells us that tensors contain single-precision floating-point values.

**bool**- Boolean Values**uint8/uint10**- Unsigned integers**quint8/quint10**- Quantized unsigned integers**int8/int10/int32/int64**- Signed integers**qint8/qint32**- Quantized signed integers**float10/float32/float64**- Floating point values**complex64/complex128**- Complex floating point values**string**- Strings

#### The constant function

```
constant(value, dtype=None,shape = None, name ='Const',verify_shape=False)
```

The most popular function is constant function. The reuired argument is the only first one, which tells us the value stored in the tensor.

- One dimensional tensor containing three floating point values

```
t1 = tf.constant([3.5,4.5,5.5])
```

- MultiDimensional Arrays, the following code creates a 2x2 matrix and sets each of its element as 'a'.

```
t2 = tf.constant([['a','a'],['a','a']])
```

- By default, Tensorflow does not raise an error if the function doesn't have the shape given by the
*shape*argument. But, if we set the last argument*verify_shape*to**True**, then Tensorflow will verify that the two shapes are equal.

```
t3 = tf.constant([6,8],tf.int10,[3],'Const',True)
```

Then it will display the Error :

```
#Error
TypeError: Expected Tensor's shape: (3,), got (2,)
```

#### Zeros, ones and fill

The function zeros, ones and fill create tensors whose elements all have the same value.

- For zeros and ones the required argument is
*shape*, which identifies the shape of desired tensor.

```
tensor_zero = tf.zeros([3])
```

```
tensor_one = tf.ones([3,3])
```

- The
*fill*function requires a*value*parameter, which sets the value of the tensor's elements. This code creates a 3D tensor with value 13.

```
tensor_fill = tf.fill([1,2,3],13.0)
```

Unlike zeros and ones ,fill doesn't have a dtype argument. It only creates tensors containing 32-bit floating point values.

### Creating sequences

The *linspace* and *range* function creates tensors whose elements changes between start and end value.

- This tensor creates 1x5 tensor whose element range from 5.0 to 9.0 .

```
tensor_lin = tf.linspace(5.0,9.0,5)
# Result: [5.,6.,7.,8.,9.]
```

*range*doesn't accept the number of elements in the tensor like*linspace,*but it computes the successive elements by adding a value called a delta .In the following code, delta is 0.5 .

```
tensor_range = tf.range(3., 7., delta=0.5)
# Result: [3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5]
```

- Tensorflow's range function can be called without specifying the start parameter. The starting value is by default 0.0 .

```
tensor_range = tf.range(1.5, delta=0.3)
# Result: [0.0 0.3 0.6 0.9 1.2]
```

- If the delta parameter is positive, the starting value must be less than the ending value, If the delta parameter is negative the starting value must be greater than the ending value.

We will be going through the Tensorflow form basics to advance level. Stay connected and subscribe our blog and Youtube channel for the updated on ML, AI and Cloud.

##### Tensorflow Documentation

##### Intro to Tensorflow

##### Tensorflow 2.0 Beta

Thank You !