<jupyter_start><jupyter_text>1 - Tensors and variables Motivation- `tf.Tensor` and `tf.Variable` are the most basic elements of TensorFlow.- `tf.Tensor`s are used to feed neural networks. Neural networks outputs are also `tf.Tensor`. - `tf.Variable`s are used to store model parameters.- It is thus essential to well understand how to create and manipulate them. Objectives- Learn what tensors are.- Create and manipulate tensors.- Discover special tensors like ragged tensors and sparse tensors.- Understand the difference between `tf.Tensor` and `tf.Variable`.- Understand the difference between eager execution and graph execution.  Table of contents:*  [Part 1: Tensors](part-1)    *  [1.1 Overview](1.1)    *  [1.2 Create tensors](1.2)    *  [1.3 Manipulate tensors](1.3)         * [1.3.1 Reshaping](1.3.1)        * [1.3.2 Indexing](1.3.2)        * [1.3.3 Tensor operations](1.3.3)    *  [1.4 Exercise](1.4)*  [Part 2: Variables](part-2)    *  [2.1 Overview](2.1)    *  [2.2 Create variables](2.2)*  [Part 3: A brief explanation of eager execution vs graph execution](part-3)*  [Summary](summary)*  [Where to go next](next)  Import<jupyter_code>import tensorflow as tf
import numpy as np
tf.__version__<jupyter_output><empty_output><jupyter_text>Part 1: Tensors  1.1 Overview - Tensors are multi-dimensional arrays with a uniform type called dtype.- Tensors are very similar to Numpy arrays.- Tensors are **immutable**. You cannot update the content of an already existing tensor.- `tf.constant` is used to create a `tf.Tensor`. 1.2 Create tensors<jupyter_code># scalar tensor
tf.constant(2.5)
# vector tensor
tf.constant([1, 2, 3, 4])
# matrix tensor
tf.constant([[1, 2], [3, 4], [5, 6]])
# tensors can have an arbitrary number of dimensions
tf.constant(
    [
        [[1, 2, 3, 4], [5, 6, 7, 8]],
        [[9, 10, 11, 12], [13, 14, 15, 16]],
        [[17, 18, 19, 20], [21, 22, 23, 24]]
    ]
)
# Convert a Numpy array to tf.Tensor
tf.constant(np.array([1, 2, 3]))
# convert a tf.Tensor to Numpy array
tf.constant([1, 2, 3]).numpy()<jupyter_output><empty_output><jupyter_text>1.3 Manipulate tensors **Some vocabulary:**- Axis or Dimension: for a tensor it is often batch x feature_1 x ... x feature_n.- Shape is the number of elements of each of the axes of a tensor.- Rank is the number of tensor dimensions (eg. 0 for a scalar, 1 for a vector, or 2 for a matrix).- Size is the total number of items in the tensor.**⚠️ Caveat:** When you train a model in batches, TensorFlow supposes the batch dimension to be the first dimension.  1.3.1 Reshaping<jupyter_code># tf.ones creates a tensors of specified shape full of 1.
tensor = tf.ones((2, 2, 3))
# You can reshape a tensor with tf.reshape
tf.reshape(tensor, (4, 3))
# A -1 in the shape argument is interpreted as "Whatever fits":
tf.reshape(tensor, (-1, 6))<jupyter_output><empty_output><jupyter_text>1.3.2 Indexing TensorFlow follows standard Python indexing rules, similar to indexing a list or Numpy arrays.<jupyter_code>tensor = tf.constant(
    [
        [0, 1, 2, 3, 4, 5],
        [6, 7, 8, 9, 10, 11]
    ]
)
# indexes start at 0:
tensor[0]
# negative indices count backwards from the end
tensor[-1]
# colons are used for slices: start:stop:step
tensor[0, 1:6:2]
# colons are also used to access deeper dimensions.
tensor[:, 0]<jupyter_output><empty_output><jupyter_text>1.3.3 Tensor operations - You can do basic math on tensors, including addition, element-wise multiplication, and matrix multiplication, using methods or operators.- Remember that tensors are immutable. Doing operations on them returns a new tensor.<jupyter_code>a = tf.constant([[1, 1], [2, 2]])
b = tf.constant([[1, 2], [3, 4]])
# addition
tf.add(a, b)
# or
a + b
# multiplication
tf.multiply(a, b)
# or
a * b
# matrix multiplication
tf.matmul(a, b)
# or
a @ b
# Tensors can be "stretched" to fit other tensor shape when doing operations on them:
tf.ones((2, 2)) + tf.constant(4.)
# This one produces an error
# tf.ones((2, 2)) + tf.constant(4)
# both tensors should have the same type<jupyter_output><empty_output><jupyter_text>1.4 Exercise  **Question 1:** Create a tensor of shape (1, 2 , 3) containing the numbers 1 to 6.<jupyter_code>a = tf.constant([[1, 2, 3], [4, 5, 6]])<jupyter_output><empty_output><jupyter_text>**Question 2:** Remove the first dimension of the tensor using tf.reshape.<jupyter_code>a = tf.reshape(a, (2, -1))<jupyter_output><empty_output><jupyter_text>**Question 3:** Select and print the first two elements of the last dimension.<jupyter_code>print(a[:, 0])<jupyter_output>tf.Tensor([1 4], shape=(2,), dtype=int32)<jupyter_text>**Question 4:** Multiply it with a tensor full of ones and of shape (3, 1)    Click for a hint            Don't forget to use `dtype="int32"` if you use `tf.ones`.<jupyter_code>a @ tf.ones((3, 1), dtype="int32")<jupyter_output><empty_output><jupyter_text>Part 2: Variables  2.1 Overview - A `tf.Variable` represents a tensor whose values can be changed by running operations on it (like gradient descent).- `tf.Variable`s are used to store **model parameters**. 2.2 Create variables - Like tensors, `tf.Variable` have a dtype and a shape, and can be exported to NumPy.- Most tensor operations work on variables as expected.- **Variables cannot be reshaped**.<jupyter_code># tf.Variable are created just like tf.constant
x = tf.Variable([1, 2, 3])
# You cannot reshape a tf.Variable
# tf.Variable([1, 2, 3]).reshape((1, 2))
# You can reassign its values using tf.Variable.assign
x.assign([1, 1, 1])
# You can turn off gradient descent for a variable by setting trainable to false at variable creation:
tf.Variable(10, trainable=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12773851590106006, 'fraction_numerical': 0.0392226148409894, 'mean_word_length': 4.310506566604127, 'pattern_counts': {'":': 1, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '224629', 'license': 'Apache-2.0', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1824, 'n_words': 716, 'path': '1 - Tensors and variables.ipynb', 'repo_name': 'oyiakoumis/tensorflow2-course', 'size': '5660'}