<jupyter_start><jupyter_text>TensorFlow 2.0 Introduction<jupyter_code>!which jupyter<jupyter_output>/home/james/anaconda3/envs/learn_tensor/bin/jupyter<jupyter_text>Importing TensorFlowThe first step here is going to be to select the correct version of TensorFlow from within collabratory!<jupyter_code>%tensorflow_version 2.x  # this line is not required unless you are in a notebook
# Imports
import tensorflow as tf  # now import the tensorflow module
import numpy as np
print(tf.version)  # make sure the version is 2.x<jupyter_output><module 'tensorflow._api.v2.version' from '/home/james/anaconda3/envs/learn_tensor/lib/python3.8/site-packages/tensorflow/_api/v2/version/__init__.py'><jupyter_text>Creating TensorsBelow is an example of how to create some different tensors.You simply define the value of the tensor and the datatype and you are good to go! It's worth mentioning that usually we deal with tensors of numeric data, it is quite rare to see string tensors.For a full list of datatypes please refer to the following guide.https://www.tensorflow.org/api_docs/python/tf/dtypes/DType?version=stable<jupyter_code># using tf.Variable to create different tensors. 
string = tf.Variable("this is a string", tf.string) 
number = tf.Variable(324, tf.int16)
floating = tf.Variable(3.567, tf.float64)
print(floating)<jupyter_output><tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.567><jupyter_text>Rank/Degree of TensorsAnother word for rank is degree, these terms simply mean the number of dimensions involved in the tensor. What we created above is a *tensor of rank 0*, also known as a scalar. Now we'll<jupyter_code># creating tensors of higher degrees/ranks. 
rank1_tensor = tf.Variable(["Test"], tf.string) 
tf.rank(rank1_tensor)
rank2_tensor = tf.Variable([["test", "ok"], ["test", "yes"]], tf.string)
tf.rank(rank2_tensor)
# get the shape** of a tensor using the shape attribute.
rank2_tensor.shape<jupyter_output><empty_output><jupyter_text>The number of elements of a tensor is the product of the sizes of all its shapes. There are often many shapes that have the same number of elements, making it convient to be able to change the shape of a tensor.The example below shows how to change the shape of a tensor.<jupyter_code># Changing Shape of tensor
# tf.ones generates just 1s as elements in the object
tensor1 = tf.ones([1,2,3])  # tf.ones() creates a shape [1,2,3] tensor full of ones
print(tensor1)                                                                       
tensor2 = tf.reshape(tensor1, [2,3,1])  # reshape existing data to shape [2,3,1]
print(tensor2)
tensor3 = tf.reshape(tensor2, [3, -1])  # -1 tells the tensor to calculate the size of the dimension in that place
print(tensor3)
# The numer of elements in the reshaped tensor is always the same<jupyter_output>tf.Tensor(
[[1. 1.]
 [1. 1.]
 [1. 1.]], shape=(3, 2), dtype=float32)<jupyter_text>Slicing TensorsYou may be familiar with the term "slice" in python and its use on lists, tuples etc. Well the slice operator can be used on tensors to select specific axes or elements.When we slice or select elements from a tensor, we can use comma seperated values inside the set of square brackets. Each subsequent value refrences a different dimension of the tensor.Ex: ```tensor[dim1, dim2, dim3]```I've included a few examples that will hopefully help illustrate how we can manipulate tensors with the slice operator.<jupyter_code># Creating a 2D tensor
matrix = [[1,2,3,4,5],
          [6,7,8,9,10],
          [11,12,13,14,15],
          [16,17,18,19,20]]

tensor = tf.Variable(matrix, dtype=tf.int32) 
print(tf.rank(tensor))
print(tensor.shape)
tensor
# Now lets select some different rows and columns from our tensor

three = tensor[0,2]  # selects the 3rd element from the 1st row
print(three)  # -> 3

row1 = tensor[0]  # selects the first row
print(row1)



column1 = tensor[:, 0]  # selects the first column
print(column1)


row_2_and_4 = tensor[1::2]  # selects second and fourth row
print(row_2_and_4)
column_1_in_row_2_and_3 = tensor[1:3, 0]
print(column_1_in_row_2_and_3)<jupyter_output>tf.Tensor([ 6 11], shape=(2,), dtype=int32)<jupyter_text>Types of TensorsBefore we go to far, I will mention that there are diffent types of tensors. These are the most used and we will talk more in depth about each as they are used.- Variable- Constant- Placeholder- SparseTensor<jupyter_code># Constant 1-D Tensor from a python list.
const_tensor = tf.constant([1, 2, 3, 4, 5, 6])
tf.rank(const_tensor)
# Constant 2-D rom a numpy array
arr = np.array([[1, 2, 3], [4, 5, 6]])
const_tensor_2 = tf.constant(arr)
tf.rank(const_tensor_2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09680188881734278, 'fraction_numerical': 0.032839665164198326, 'mean_word_length': 4.676004872107186, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '95521', 'license': 'MIT', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1472, 'n_words': 550, 'path': 'TensorFlow_Introduction.ipynb', 'repo_name': 'james-westwood/tensor_flow', 'size': '4659'}