<jupyter_start><jupyter_text>Introduction to Tensors<jupyter_code>import tensorflow as tf
print(tf.__version__)
scaler = tf.constant(7)
scaler
scaler.ndim
vector = tf.constant([10,7])
vector
vector.ndim
matrix = tf.constant(
    [[10,7],
    [2,9]])
matrix
matrix.ndim
another_matrix = tf.constant([[10,7],
                             [3,2],
                             [8,9]], dtype = tf.float16)
another_matrix
another_matrix.ndim
# Tensors
tensor = tf.constant([[[1, 2, 3],
                       [4, 5, 6]],
                      [[7, 8, 9],
                       [10, 11, 12]],
                      [[13, 14, 15],
                       [16, 17, 18]]])
tensor
tensor.ndim<jupyter_output><empty_output><jupyter_text>![alt text](https://raw.githubusercontent.com/mrdbourke/tensorflow-deep-learning/main/images/00-scalar-vector-matrix-tensor.png)  Changeable Tensors `tf.Variable()`<jupyter_code>changeable_tensor = tf.Variable([10,7])
unchangeable_tensor = tf.constant([10,7])
changeable_tensor,unchangeable_tensor
# Will Show Error
#changeable_tensor[0] = 7
#changeable_tensor
changeable_tensor[0].assign(7)
changeable_tensor<jupyter_output><empty_output><jupyter_text>Creating Random Tensors<jupyter_code>random_1 = tf.random.Generator.from_seed(42)
random_1 = random_1.normal(shape=(3,2)) # Creating Tensors from Normal Distribution

random_2 = tf.random.Generator.from_seed(42)
random_2 = random_2.normal(shape=(3,2))

random_1,random_2<jupyter_output><empty_output><jupyter_text>Other ways to make Tensors<jupyter_code>tf.ones(shape=(3,2))
tf.zeros(shape=(3,2),dtype = tf.int64)<jupyter_output><empty_output><jupyter_text>Converting Numpy Array to Tensors<jupyter_code>import numpy as np
numpy_array = np.arange(1,25,dtype = np.int32) # Numpy Array Between 1 and 25
tensor_A = tf.constant(numpy_array,shape = [2,4,3])
numpy_array, tensor_A<jupyter_output><empty_output><jupyter_text>Getting Information from Tensors<jupyter_code>rank4_tensor = tf.zeros(shape = (2,3,4,5))
rank4_tensor
rank4_tensor.shape
rank4_tensor.ndim
tf.size(rank4_tensor)<jupyter_output><empty_output><jupyter_text>You can also index tensors just like Python Lists<jupyter_code>rank4_tensor[:2,:2,:2,:2]
rank4_tensor[:1,:1,:1,:1]
rank_2_tensor = tf.constant([[10, 7],
                             [3, 4]])
rank_2_tensor[:, -1]<jupyter_output><empty_output><jupyter_text>Manipulating Tensors  Basic Operations<jupyter_code>tensor = tf.constant([[10,7],[3,4]])
tensor + 10
tensor
tensor * 11
# Another Method
tf.multiply(tensor, 10)
tensor
# Matix Multiplication
tf.matmul(tensor, tensor)
# Also can use this
tensor @ tensor<jupyter_output><empty_output><jupyter_text>Changing datatype of Tensors<jupyter_code>B = tf.constant([1.7,7.4])
C = tf.constant([1,7])
B,C
B = tf.cast(B, dtype = tf.float16)
B
C = tf.cast(C, dtype = tf.float16)
C<jupyter_output><empty_output><jupyter_text>Finding the min, max, mean, sum<jupyter_code>E = tf.constant(np.random.randint(low = 0, high = 100, size = 50))
E
tf.reduce_min(E) 
tf.reduce_max(E)
tf.reduce_mean(E)
tf.reduce_sum(E)
# Find the maximum element Position
print(tf.argmax(E))
# Find the Position of Minimum Element
print(tf.argmin(E))
print(f"The maximum value of E is at position: {tf.argmax(E).numpy()}") 
print(f"The maximum value of E is: {tf.reduce_max(E).numpy()}") 
print(f"Using tf.argmax() to index E, the maximum value of E is: {E[tf.argmax(E)].numpy()}")
print(f"Are the two max values the same (they should be)? {E[tf.argmax(E)].numpy() == tf.reduce_max(E).numpy()}")<jupyter_output>The maximum value of E is at position: 15
The maximum value of E is: 97
Using tf.argmax() to index E, the maximum value of E is: 97
Are the two max values the same (they should be)? True<jupyter_text>Squeezing a tensor<jupyter_code># Creating a rank 5 (5 dimension) tensor of 50 numbers between 0 and 100
G = tf.constant(np.random.randint(0,100,50),shape = (1,1,1,1,50))
G.shape, G.ndim
G_squeezed = tf.squeeze(G)
G_squeezed.shape, G_squeezed.ndim<jupyter_output><empty_output><jupyter_text>One-Hot Encoding<jupyter_code>some_list = [0,1,2,3]

# One hot encode them
tf.one_hot(some_list,depth = 4)
tf.one_hot(some_list, depth=4, on_value="We'r live !", off_value = "offline")<jupyter_output><empty_output><jupyter_text>Squaring, log, square root<jupyter_code>H = tf.constant(np.arange(1,10))
H
tf.square(H)
H = tf.cast(H, dtype = tf.float32)
H
tf.sqrt(H)
tf.math.log(H)<jupyter_output><empty_output><jupyter_text>Manupulating `tf.Variable` tensors<jupyter_code>I = tf.Variable(np.arange(0,5))
I
I.assign([0,1,2,3,50])
I<jupyter_output><empty_output><jupyter_text>Tensors and Numpy<jupyter_code>J = tf.constant(np.array([3.0,7.0,10.0]))
J
# Convert Tensors to Numpy
np.array(J), type(np.array(J))
# Convert tensors J to numpy with .numpynumpy_array
J.numpy(), type(J.numpy())<jupyter_output><empty_output><jupyter_text>Using `@tf.function`<jupyter_code># Creating a Simple Function

def function(x,y):
    return x**2 + y

x = tf.constant(np.arange(0,10))
y = tf.constant(np.arange(10,20))
function(x,y)
# Creating same function and decorate it with tf.function

@tf.function
def tf_function(x,y):
    return x**2 + y 
tf_function(x,y)<jupyter_output><empty_output><jupyter_text>If you noticed no difference between the above two functions (the decorated one and the non-decorated one) you'd be right.Much of the difference happens behind the scenes. One of the main ones being potential code speed-ups where possible.  Finding access to GPUs<jupyter_code>print(tf.config.list_physical_devices("GPU"))
!nvidia-smi<jupyter_output>Tue Mar  1 08:25:52 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla T4            Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   45C    P0    28W /  70W |  14060MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-------[...]
{'fraction_non_alphanumeric': 0.17757437070938215, 'fraction_numerical': 0.04057971014492753, 'mean_word_length': 3.359042553191489, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '449751', 'license': 'MIT', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2330, 'n_words': 478, 'path': 'Practical Tensorflow/Getting Started/00_Getting_Started.ipynb', 'repo_name': 'GurpreetMeelu/Tensorflow-Practice', 'size': '6555'}