<jupyter_start><jupyter_text>TensorsTensor are multi-dimensitonal arrays that are used in Tensorflow.We use the following definition:* **Rank:** The number of dimensions that a vector has.Below, we will define different kinds of tensors and show their rank using [tf.rank](https://www.tensorflow.org/api_docs/python/tf/rank) function.<jupyter_code>tensor = tf.constant(0)
print("Print constant tensor {} of rank {}".format(tensor, tf.rank(tensor)))
print("Show full tensor:", tensor)
# NOTE: We use .numpy() to transform tf.tensor to numpy
tensor = tf.constant([1,2,3])
print("Tensor:", tensor)
print("Rank:", tf.rank(tensor).numpy())<jupyter_output>Tensor: tf.Tensor([1 2 3], shape=(3,), dtype=int32)
Rank: 1<jupyter_text>Tensor Operations<jupyter_code>x = tf.constant([[1, 1],
                 [1, 1]])
y = tf.constant([[2, 4],
                 [6, 8])

# Add two tensors
print(tf.add(x, y), "\n")

# Add two tensors
print(tf.matmul(x, y), "\n")<jupyter_output><empty_output><jupyter_text>Muti-dimentional TensorsThis part is not much different compared to what we learned so far. However, it would be nice to try extracting as much information as we can from a multi-dimentional tensor.Let's use [tf.ones](https://www.tensorflow.org/api_docs/python/tf/ones) for our purpose here. It creates an all-one tensor.<jupyter_code># We set the shape of the tensor and the desired data type.
tensor = tf.ones(shape = [2, 3, 6], dtype = tf.float32)
print('Tensor:', tensor)
print("Tensor Rank: ", tf.rank(tensor).numpy())
print("Shape: ", tensor.shape)
print("Elements' type", tensor.dtype)
print("The size of the second axis:", tensor.shape[1])
print("The size of the last axis:", tensor.shape[-1])
print("Total number of elements: ", tf.size(tensor).numpy())
print("How many dimensions? ", tensor.ndim)<jupyter_output>Tensor Rank:  3
Shape:  (2, 3, 6)
Elements' type <dtype: 'float32'>
The size of the second axis: 3
The size of the last axis: 6
Total number of elements:  36
How many dimensions?  3<jupyter_text>IndexingTensorFlow indexing is aligned with Python indexing. See the following examples.<jupyter_code>x = tf.constant([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
# All elements
print(x[:].numpy())
# All elements of the first row
print(x[0,:].numpy())
# First row and last column
print(x[0,-1].numpy())
# From second row to last and from third column to last
print(x[1:,2:].numpy)<jupyter_output><bound method _EagerTensorBase.numpy of <tf.Tensor: shape=(1, 1), dtype=int32, numpy=array([[6]], dtype=int32)>><jupyter_text>Data typesYou can change the data type of the tesnorflow tensors for your purpose. This will be done easily by [tf.cast](https://www.tensorflow.org/api_docs/python/tf/cast).<jupyter_code>original_tensor = tf.constant([1, 2, 3, 4], dtype=tf.int32)
print('Original tensor: ', original_tensor)
print("Tensor type before casting: ", original_tensor.dtype)

# Casting to change dtype
casted_tensor = tf.cast(original_tensor, dtype=tf.float32)
print('New tensor: ', casted_tensor)
print("Tensor type after casting: ", casted_tensor.dtype)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14377406931964057, 'fraction_numerical': 0.021181001283697046, 'mean_word_length': 5.1237721021611, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '316489', 'license': 'MIT', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1011, 'n_words': 323, 'path': 'codes/ipython/1-basics/tensors.ipynb', 'repo_name': 'ns-m/TensorFlow-Course-master', 'size': '3116'}