Compile author | Orhan g. Yalc ı n | source of vitamin k | forward Datas of Science
If you’re reading this, I’m sure we have similar interests and are/will be in similar industries.
In this article we will delve into the details of Tensorflow Tensor. We’re going to cover all the themes of Tensorflow’s Tensor in five simple steps:
-
Step 1: the definition of tensors → what is a tensor?
-
Step 2: Create the tensor → create the function of the tensor object
-
Step 3: Characteristics of the tensor object
-
Step 4: Tensor operation → index, basic tensor operation, shape operation, broadcast
-
Step 5: Special tensors
Definition of tensors: What is a tensor
Tensors are uniform multidimensional arrays of TensorFlow. They are very similar to NumPy arrays, and they are immutable, which means they cannot be changed once created. New copies can only be created using edits.
Let’s see how tensors work with code examples. But first, to use TensorFlow objects, we need to import the TensorFlow library. We often use NumPy with TensorFlow, so we can also import NumPy with the following line:
import tensorflow as tf
import numpy as np
Copy the code
Tensor creation: Creates a tensor object
There are several ways to create tF.tensor objects. Let’s start with a few examples. Multiple TensorFlow functions can be used to create tensor objects, as shown in the following example:
You can create tF.tensor objects with the 'tf.constant' function:
x = tf.constant([[1.2.3.4 ,5]])
You can create tF.tensor objects with the 'tF.ones' function:
y = tf.ones((1.5))
You can create tf.tensor objects with the 'tf.zeros' function:
z = tf.zeros((1.5))
You can create tf.tensor objects with the 'tf.range' function:
q = tf.range(start=1, limit=6, delta=1)
print(x)
print(y)
print(z)
print(q)
Copy the code
Output: tf. Tensor ([[1 2 3 4 5]], shape=(1.5), dtype=int32)
tf.Tensor([[1. 1. 1. 1. 1.]], shape=(1.5), dtype=float32)
tf.Tensor([[0. 0. 0. 0. 0.]], shape=(1.5), dtype=float32)
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
Copy the code
As you can see, we used three different functions to create a tensor object of shape (1,5) and a fourth tensor object of shape (5,) using the tf.range() function. Note that tf.ones and tf.zeros accept shapes as required parameters because their element values are predetermined.
Characteristics of tensor objects
Tf. Tensor creates objects that have several characteristics. First, they have a number of dimensions. Second, they have a shape, a list of the lengths of the dimensions. All tensors have a magnitude, which is the total number of elements in the tensor. Finally, their elements are recorded in a unified datatype (datatype). Let’s take a closer look at these features.
The dimension
Tensors are classified according to their dimensions:
-
1. A tensor (dimension 0) containing a single value and having no axis;
-
1. A tensor containing a list of uniaxial (one-dimensional) values;
-
1. A tensor containing two axes (2 dimensions); As well as
-
Rank-n tensor: a tensor (three-dimensional) containing the n-axis.
For example, we can create a rank-3 tensor by passing tf.constant a three-tiered nested list object. For this example, we can split the numbers into a nested list of 3 levels, each with 3 elements:
three_level_nested_list = [[[0.1.2],
[3.4.5]],
[[6.7.8],
[9.10.11]] ]
rank_3_tensor = tf.constant(three_level_nested_list)
print(rank_3_tensor)
Copy the code
Output:
tf.Tensor( [[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]],
shape=(2.2.3), dtype=int32)
Copy the code
We can see how many dimensions the “Rank_3_tensor” object currently has a “.ndim “property.
tensor_ndim = rank_3_tensor.ndim
print("The number of dimensions in our Tensor object is", tensor_ndim)
Copy the code
Output:
The number of dimensions in our Tensor object is 3
Copy the code
The shape of
The shape characteristic is another property that every tensor has. It shows the size of each dimension as a list. We can look at the shape of the Rank_3_tensor object created with the.shape property as follows:
tensor_shape = rank_3_tensor.shape
print("The shape of our Tensor object is", tensor_shape)
Copy the code
Output:
The shape of our Tensor object is (2.2.3)
Copy the code
As you can see, our tensor has two elements at the first level, two elements at the second level, and three elements at the third level.
The size of the
Size is another property of tensors, it means how many elements there are in a tensor. We cannot use properties of tensor objects to measure size. Instead, we need to use the tf.size function. Finally, we’ll use the instance function. NumPy () converts the output to NumPy for a more readable result:
tensor_size = tf.size(rank_3_tensor).numpy()
print("The size of our Tensor object is", tensor_size)
Copy the code
Output:
The size of our Tensor object is 12
Copy the code
The data type
Tensors typically contain numeric data types, such as floating point and integer, but may also contain many other data types, such as complex numbers and strings.
However, each tensor object must store all its elements in a unified data type. Therefore, we can also use the.dtype property to see the data type selected for a particular tensor object, as follows:
tensor_dtype = rank_3_tensor.dtype
print("The data type selected for this Tensor object is", tensor_dtype)
Copy the code
Output:
The data type selected for this Tensor object is <dtype: 'int32'>
Copy the code
Tensor operation
The index
An index is a numerical representation of an item’s position in a sequence. This sequence can refer to many things: a list, a string, or an arbitrary sequence of values.
TensorFlow also follows standard Python indexing rules, which are similar to list indexing or NumPy array indexing.
Some rules about indexes:
-
The index starts at zero (0).
-
A negative index (” -n “) value indicates counting backwards from the end.
-
The colon (” : “) is used for slices: Start: stop: step.
-
The comma (“, “) is used to go deeper.
Let’s create rank_1_tensor with the following lines:
single_level_nested_list = [0.1.2.3.4.5.6.7.8.9.10.11]
rank_1_tensor = tf.constant(single_level_nested_list)
print(rank_1_tensor)
Copy the code
Output:
tf.Tensor([ 0 1 2 3 4 5 6 7 8 9 10 11],
shape=(12,), dtype=int32)
Copy the code
Test our rules 1,2,3:
# rule 1, index starts at 0
print("First element is:",
rank_1_tensor[0].numpy())
# rule 2, negative index
print("Last element is:",
rank_1_tensor[-1].numpy())
# Rule 3, slice
print("Elements in between the 1st and the last are:",
rank_1_tensor[1: -1].numpy())
Copy the code
Output:
First element is: 0
Last element is: 11
Elements in between the 1st and the last are: [ 1 2 3 4 5 6 7 8 9 10]
Copy the code
Now let’s create rank_2_tensor with this code:
two_level_nested_list = [ [0.1.2.3.4.5], [6.7.8.9.10.11] ]
rank_2_tensor = tf.constant(two_level_nested_list)
print(rank_2_tensor)
Copy the code
Output:
tf.Tensor( [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]], shape=(2.6), dtype=int32)
Copy the code
Test rule 4 with a few examples:
print("The 1st element of the first level is:",
rank_2_tensor[0].numpy())
print("The 2nd element of the first level is:",
rank_2_tensor[1].numpy())
# Rule 4, commas stand for deeper
print("The 1st element of the second level is:",
rank_2_tensor[0.0].numpy())
print("The 3rd element of the second level is:",
rank_2_tensor[0.2].numpy())
Copy the code
Output:
The first element of the first level is: [0 1 2 3 4 5]
The second element of the first level is: [ 6 7 8 9 10 11]
The first element of the second level is: 0
The third element of the second level is: 2
Copy the code
Now that we’ve covered the basics of indexes, let’s look at the basic operations we can do with tensors.
Basic operations of tensors
You can easily perform basic mathematical operations on tensors, such as:
-
add
-
Elements multiplication
-
Matrix multiplication
-
Find the maximum or minimum
-
Find the index of the Max element
-
Calculate the Softmax value
Let’s look at these operations. We will create two tensor objects and apply these operations.
a = tf.constant([[2.4],
[6.8]], dtype=tf.float32)
b = tf.constant([[1.3],
[5.7]], dtype=tf.float32)
Copy the code
We can start with addition.
# We can use the 'tf.add()' function and pass the tensor as an argument.
add_tensors = tf.add(a,b)
print(add_tensors)
Copy the code
Output:
tf.Tensor( [[ 3. 7.]
[11. 15.]], shape=(2.2), dtype=float32)
Copy the code
The multiplication
# We can use the 'tf.multiply()' function and pass the tensor as a parameter.
multiply_tensors = tf.multiply(a,b)
print(multiply_tensors)
Copy the code
Output:
tf.Tensor( [[ 2. 12.]
[30. 56.]], shape=(2.2), dtype=float32)
Copy the code
Matrix multiplication:
# We can use the function 'tf.matmul()' and pass the tensor as a parameter.
matmul_tensors = tf.matmul(a,b)
print(matmul_tensors)
Copy the code
Output:
tf.Tensor( [[ 2. 12.]
[30. 56.]], shape=(2.2), dtype=float32)
Copy the code
Note: Matmul operation is the core of deep learning algorithm. So, although you won’t use Matmul directly, it’s important to understand these operations.
Examples of other actions we listed above:
# Maximum or minimum values can be found using the 'tf.reduce_max()' and 'tf.reduce_min()' functions
print("The Max value of the tensor object b is:",
tf.reduce_max(b).numpy())
# Use the 'tf.argmax()' function to find the index of the largest element
print("The index position of the max element of the tensor object b is:",
tf.argmax(b).numpy())
# Calculate softmax using tf.nn.softmax' function
print("The softmax computation result of the tensor object b is:",
tf.nn.softmax(b).numpy())
Copy the code
Output:
The Max value of the tensor object b is: 1.0
The index position of the Max of the tensor object b is: [1 1]
The softmax computation result of the tensor object b is: [[0.11920291 0.880797 ] [0.11920291 0.880797 ]]
Copy the code
Manipulation of the shape
Just as in the NumPy array and pandas data frames, you can also reshape tensor objects.
This deformation operation is very fast because the underlying data does not need to be copied. For remodeling, we can use tF.0 function 0
# our initial tensor
a = tf.constant([[1.2.3.4.5.6]])
print('The shape of the initial Tensor object is:', a.shape)
b = tf.reshape(a, [6.1])
print('The shape of the first reshaped Tensor object is:', b.shape)
c = tf.reshape(a, [3.2])
print('The shape of the second reshaped Tensor object is:', c.shape)
# If we pass -1 with shape, the tensor flattens out.
print('The shape of the flattened Tensor object is:', tf.reshape(a, [-1]))
Copy the code
Output:
The shape of our initial Tensor object is: (1.6)
The shape of our initial Tensor object is: (6.1)
The shape of our initial Tensor object is: (3.2)
The shape of our flattened Tensor object is: tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
Copy the code
As you can see, we can easily reshape our tensor objects. It is important to note, however, that the developer must be reasonable when performing the remodeling operation. Otherwise, tensors can get confused and even produce errors. So be careful 😀.
radio
When we try to combine operations with multiple tensor objects, smaller tensors can automatically stretch to fit larger tensors, just like NumPy arrays. For example, when you try to multiply a scalar tensor by a rank-2 tensor, the scalar will be stretched to multiply each rank-2 tensor element. See the following example:
m = tf.constant([5])
n = tf.constant([[1.2], [3.4]])
print(tf.multiply(m, n))
Copy the code
Output:
tf.Tensor( [[ 5 10]
[15 20]], shape=(2.2), dtype=int32)
Copy the code
Thanks to broadcasting, you don’t have to worry about size matching when doing math on tensors.
Special types of tensors
We tend to generate rectangular tensors and store values as elements. However, TensorFlow also supports irregular or special tensor types, including:
-
Jagged tensors
-
String tensor
-
Sparse tensor
Let’s take a closer look at what each one is.
Jagged tensors
A jagged tensor is one that has a different number of elements along the dimension axis
You can build irregular tensors, as shown below
ragged_list = [[1.2.3], [4.5], [6]]
ragged_tensor = tf.ragged.constant(ragged_list)
print(ragged_tensor)
Copy the code
Output:
<tf.RaggedTensor [[1.2.3],
[4.5],
[6]] >Copy the code
String tensor
String tensors are tensors that store string objects. We can create a string tensor, just like you create a normal tensor object. However, we pass string objects as elements instead of numeric objects, as follows:
string_tensor = tf.constant(["With this"."code, I am"."creating a String Tensor"])
print(string_tensor)
Copy the code
Output:
tf.Tensor([b'With this'
b'code, I am'
b'creating a String Tensor'],
shape=(3,), dtype=string)
Copy the code
Sparse tensor
Finally, the sparse tensor is the rectangular tensor of sparse data. Sparse tensors are objects when there are empty values in the data. Creating sparse tensors is a bit time-consuming and should be more mainstream. Here’s an example:
sparse_tensor = tf.sparse.SparseTensor(indices=[[0.0], [2.2], [4.4]],
values=[25.50.100],
dense_shape=[5.5])
# We can convert a sparse tensor into a dense tensor
print(tf.sparse.to_dense(sparse_tensor))
Copy the code
Output:
tf.Tensor( [[ 25 0 0 0 0]
[ 0 0 0 0 0]
[ 0 0 50 0 0]
[ 0 0 0 0 0]
[ 0 0 0 0 100]], shape=(5.5), dtype=int32)
Copy the code
At the end
We have successfully introduced the basics of TensorFlow’s tensor objects.
This should give you a lot of confidence as you now know more about the basics of the TensorFlow framework.
See part 1 of this tutorial series: link.medium.com/yJp16uPoqab
The original link: towardsdatascience.com/mastering-t…
Welcome to panchuangai blog: panchuang.net/
Sklearn123.com/
Welcome to docs.panchuang.net/