Conteúdo do Curso
Introduction to TensorFlow
Introduction to TensorFlow
Tensor Properties
Tensor Properties
Tensors come with distinct properties that dictate their structure and how they process and store data.
- Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the
.ndim
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
Note
We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.
- Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of
(2, 3)
. The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the.shape
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
Note
Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.
- Types: Tensors come in various data types. While there are many, some common ones include
float32
,int32
, andstring
. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the.dtype
attribute:
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
Note
Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.
- Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Swipe to show code editor
In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:
- Rank:
3
. - Shape:
(2, 4, 3)
. - Data type:
float
.
So, your steps are:
- Retrieve the properties of the first tensor.
- Construct a tensor that meets the specified criteria.
Obrigado pelo seu feedback!
Tensor Properties
Tensor Properties
Tensors come with distinct properties that dictate their structure and how they process and store data.
- Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the
.ndim
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
Note
We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.
- Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of
(2, 3)
. The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the.shape
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
Note
Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.
- Types: Tensors come in various data types. While there are many, some common ones include
float32
,int32
, andstring
. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the.dtype
attribute:
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
Note
Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.
- Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Swipe to show code editor
In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:
- Rank:
3
. - Shape:
(2, 4, 3)
. - Data type:
float
.
So, your steps are:
- Retrieve the properties of the first tensor.
- Construct a tensor that meets the specified criteria.
Obrigado pelo seu feedback!
Tensor Properties
Tensor Properties
Tensors come with distinct properties that dictate their structure and how they process and store data.
- Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the
.ndim
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
Note
We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.
- Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of
(2, 3)
. The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the.shape
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
Note
Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.
- Types: Tensors come in various data types. While there are many, some common ones include
float32
,int32
, andstring
. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the.dtype
attribute:
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
Note
Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.
- Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Swipe to show code editor
In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:
- Rank:
3
. - Shape:
(2, 4, 3)
. - Data type:
float
.
So, your steps are:
- Retrieve the properties of the first tensor.
- Construct a tensor that meets the specified criteria.
Obrigado pelo seu feedback!
Tensor Properties
Tensors come with distinct properties that dictate their structure and how they process and store data.
- Rank: It tells you the number of dimensions present in the tensor. For instance, a matrix has a rank of 2. You can get the rank of the tensor using the
.ndim
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3]) tensor_2D = tf.constant([ [1, 2], [3, 4] ]) tensor_3D = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) # Get ranks print(f'Rank of 1D tensor: {tensor_1D.ndim}') print(f'Rank of 2D tensor: {tensor_2D.ndim}') print(f'Rank of 3D tensor: {tensor_3D.ndim}')
Note
We've structured the definition of Python lists over multiple lines for clearer readability. You can condense it into a single line to see that it functions the same way.
- Shape: This describes how many values exist in each dimension. A 2x3 matrix has a shape of
(2, 3)
. The length of the shape parameter matches the tensor's rank (its number of dimensions). You can get the the shape of the tensor by the.shape
attribute:
import tensorflow as tf # Create tensors tensor_1D = tf.constant([1, 2, 3, 4]) tensor_2D = tf.constant([ [1, 2, 3], [4, 5, 6] ]) tensor_3D = tf.constant([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]] ]) # Get shapes print(f'Shape of 1D tensor: {tensor_1D.shape}') print(f'Shape of 2D tensor: {tensor_2D.shape}') print(f'Shape of 3D tensor: {tensor_3D.shape}')
Note
Getting tensor shapes and ranks correct is crucial in deep learning. Dimension mismatches are common pitfalls, especially when building complex models in TensorFlow.
- Types: Tensors come in various data types. While there are many, some common ones include
float32
,int32
, andstring
. We'll delve deeper into tensor data types in upcoming chapters. You can get the the data type of the tensor by the.dtype
attribute:
import tensorflow as tf # Create tensors tensor_int = tf.constant([1, 2, 3, 4]) tensor_float = tf.constant([1., 2., 3., 4.]) tensor_string = tf.constant(['a', 'b', 'c', 'd']) # Get data type print(f'Data type of 1D tensor: {tensor_int.dtype}') print(f'Data type of 2D tensor: {tensor_float.dtype}') print(f'Data type of 3D tensor: {tensor_string.dtype}')
Note
Data type of a tensor is determined by the content it contains. It's essential that all elements within the tensor are of the same type.
- Axes: Axes help us navigate through dimensions of tensors. By specifying an axis, you can pinpoint a specific layer or direction in the tensor, making it easier to process and understand the data. Axes correspond directly to shape dimensions. Each axis corresponds to a specific shape value, with the 0th axis aligning with the first shape value, the 1st axis with the second, and so on.
Swipe to show code editor
In this task, you are provided with two tensors. The first tensor is already created for you; your task is to display its properties using the relevant tensor attributes. For the second tensor, you'll need to construct it yourself with the following specifications:
- Rank:
3
. - Shape:
(2, 4, 3)
. - Data type:
float
.
So, your steps are:
- Retrieve the properties of the first tensor.
- Construct a tensor that meets the specified criteria.