TensorFlow — Aamir P

Aamir P
9 min readApr 16, 2024

--

Hi all!
This is just some overview which I’m going to write about. Some beginners were asking me for a basic understanding of these concepts.

TENSORFLOW

Firstly, Artificial Intelligence(AI) is a set of predefined rules since the start of computer creation. For example, you play tic tac toe or Chess. This AI will have predefined rules to play with you. This was in no way built with deep learning or Machine learning. This is something like based on our move the game should play in the areas defined. Complexity AI is built based on necessity. In simple words, AI is something that interacts with humans.

Machine Learning(ML) is a part of AI. It sets the rules. You give the input data and say how the output data should be the ML will set the rules and work on it. ML is never 100% accurate. This has reduced the hard coding for us.

Neural Networks are a form of ML that uses a layered data representation. Neural Networks have multiple layers. Data gets transformed in different layers. The input in one layer, in the next layer, undergoes the required transformation and that keeps going until the output. ML learning has one or two layers. AI need not necessarily have layers.

Tensor means vectors and matrices generalised to higher dimensions. You must have gone through vector algebra if not have this in mind, a vector is nothing but a data point. It never has coordinates. Vector can have ’N’ no. of dimensions. Tensor has datatype and shape.

TensorFlow represents tensors as n-dimensional arrays of base datatypes. It works by building a graph of Tensor objects to show the relation of these Tensors. We run different parts of the graph in our code. Scalar means one value.

Rank and Degree of Sensors
The number of dimensions involved in the tensor.

Types of Tensors
Variable
Constant
Placeholder
SparseTensor

Except for variables, all are immutable means they cannot be changed.

Let me type some code.
%tensorflow_version 2.x
import tensorflow as tf
print(tf.version)
t = tf.ones(5,5,5,5)
print(t)

I have set the tensorflow to the latest version. Then importing the library and printing its version. I want the values to be one. Creates a 5x5 tensor filled with ones.

Similarly,

%tensorflow_version 2.x
import tensorflow as tf
print(tf.version)
t = tf.zeros(5,5,5,5)
print(t)

So, 5 to the power of 4 which is 625.

%tensorflow_version 2.x
import tensorflow as tf
print(tf.version)
t = tf.zeros(5,5,5,5)
t =tf.reshape(t, [625])
print(t)

This is the way you reshape the output.

Core Learning Algorithms
These are the building blocks of neural networks. These are very powerful in ML.
The algorithms are namely

1. Linear Regression
Basic forms of ML. We use this for predictions. For example, we only have an x value, and with that, we need to predict the Y value. The data points are coloured and a line is drawn to unite the points. To make it simple, linear regression is used when you have data points that correlate in a kind of linear fashion. We use Matplotlib for plotting the points, axis, etc. That line is called the line of best fit. Use must have seen this formula y = mx+b (a slope-intercept form of a straight line).

Slope

So, b is the point on the y-axis. x, y are the coordinates of the data point. m is the slope or steepness. The slope is calculated by the line-over-run method. How can I say this is right? 2 points on the left to the line and 2 points on the right to the line and 2 points on the line. So, this is perfect to say the line of best fit.

Use Pip install Sklearn
Then import the modules

Let me overview some important modules.
. Numpy is used for array dimensional calculations. Cross-product, vector operations, etc. can be done with this Numpy.
. Pandas is used to manipulate data.
. Matplotlib is used for graphs.

Pandas extension is pd. pd.read_csv simply means read the csv file. Always train the model and then test the model. Check its accuracy. Head generally should first five entries; Tail shows us the last 5 entries. This is the default for both. If you need more than 5 or less than 5 specify it in the parameters. loc is used to know about one specific data. Numpy and Tensors have shape. The shape says rows and columns. A histogram is a graph that shows the frequency of numerical data using rectangles.

dfeval.shape this is to evaluate the model’s shape. Any data that is not numeric comes under categorical data.

The training process is like feeding the model. For some models, we load them as batches. Epochs are how many times the model is going to see the same data. Never overfeed the model because it will lose the ability to predict. Batch is like blocks of code that need to run. When you pass the feature columns in the estimator of Linear Classifier it will build the model accordingly. TensorFlow is best for large volumes of data. When you give predict you will get the probabilities.

2. Classification
Differentiating data points and separating them into classes. A best in-built example for this is three different classes of species namely Setosa, Versicolor, and Virginica. Information about these three flowers are sepal length, sepal width, petal length and petal width. They say this in a term called iris. Keras is a Python interface for artificial neural networks that acts as an interface for the TensorFlow library. Feature columns are used to get all attributes. For example shape, datatype, normaliser, etc. Train.keys will give you all columns.

There are 2 classifiers in default namely:-
DNN(Deep Neural Network Classifier)
Linear Classifier

Mostly, in TensorFlow we use DNN but in Machine Learning cases we opt for Linear Classifier. The estimator module generally stores a bunch of pre-made models. Hidden units are used to build the neural architecture. Lambda is used when you want to say what that function does.

For example,
x = lambda: print(“Hello”)
x()
This is a valid statement where lambda works.

3. Clustering
It is an unsupervised algorithm. Finds clusters like data points and tells the location of these clusters. Centroid says where our cluster is defined. K-means works by randomly picking the K-centroids. A data point to centroid the distance is calculated by Euclidean distance. Within the data points, we have a centroid and that we call it a cluster. A new data point will reach out to the closest cluster to get the output.

To summarise,
. Randomly pick K points to place K centroids.
. Assign each K point to the closest K centroid.
. Move each K centroid into the middle of all their data points.
. Reassign each K point to the closest K centroid.

4. Hidden Markov Models
These are finite sets of states, each of which is associated with a probability distribution. Transitions among the states are governed by a set of probabilities called transition probabilities. A simple example is let us say we have a weather prediction model. The states will be warm, cold, hot, etc. The observations will tell the chances of that day. Standard deviation is vital here. With the model, we predict the weather for each day in future.

TensorFlow Probabilities
The probabilities are encoded by 0s and 1s. 1 says it will happen and 0 says no chance. Always learn standard deviation to some extent. Steps are something that we use to say how many days we want to predict. Mostly, TensorFlow probabilities are used with Hidden Markov Models.

Neural Networks
These are complex and have many components. Neural networks provide classifications or predictions for us. You give too many inputs and you get one meaningful output that satisfies all inputs. Let me tell you the different layers in Neural networks. The first is the input layer that has raw data. Neurons are like nodes in a layer is what we define them. The next layer after the input layer is the hidden layer. Every single layer is connected to another layer by something called weights. We can have different architectures. A densely connected neural network means a node that is connected to every layer.

Activation Function
Each neuron has an activation function and is applied to the output. Activation functions can move the neurons out.

  1. Rectified Linear Unit
    It takes any values less than zero and just makes them zero. Positive is always positive.
  2. Tanh
    How much ever negative they are taken as -1 and how much ever positive they are taken as +1.
  3. Sigmoid
    Values will be between 0 and 1.

Some common loss errors are:-

. Mean Squared Error
. Mean Absolute Error
. Hinge Loss

With neural networks, we can minimise the loss.

Optimiser
A function that implements the backpropagation algorithm. The common ones are:-
. Gradient Descent
. Momentum
. Stochastic Gradient Descent
. Mini-Batch Gradient Descent
. Nesterov Accelerated Gradient

Data Preprocessing
This is the last step before creating our model. It means applying prior transformations to our data before feeding our model.

Building the model
Input, hidden, and output are the three layers of Keras that constitute building a model.

keras.sequential means information going from left to right side. I mean the layers, that way is called Sequential.

Deep Computer Vision
This is also a part of CNN. It performs image classification and object detection. The image data is our features. There are 3 dimensions in an image image height, image width and color channels. Color Channels are nothing but RGB pixels. So, three layers we need. Dense neural networks will learn the pattern. For example a dog, dense neural network will measure the height of ears, eyes, etc. It trains the model based on such information. CNN will flip an image and learn the pattern. To make it simple words, Dense neural networks analyze input on a global scale and recognize patterns in specific areas. Convolutional neural networks scan through the entire input a little at a time and learn local patterns. CNN should return as some feature map. Generally, in each convolutional layer, they look for different layers. If you apply two filters, you have to get two maps. How do we get this? By dot product method. The three main properties of each convolutional layer are input size, the number of filters, and the sample size of the filters. CNN is done with the help of Keras.

from keras.preprocessing.image import ImageDataGenerator

The above code is used for Data Augmentation. In TensorFlow, pretrained models refer to deep learning models that have been trained on large datasets for specific tasks such as image classification, object detection, natural language processing, and more. These models are trained using architectures like Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Transformer models, etc., and are made available for use by the broader community. Fine-tuning in TensorFlow refers to the process of taking a pre-trained neural network model and adapting it to a new dataset or task.

Good ways to increase the accuracy of a convolutional neural network are augmenting the data or using a pre-trained model. Google’s pre-trained MobileNet V2 model serves as a base for a convolutional neural network.

NLP with RNNs
Natural Language Processing(NLP) is a discipline that deals with Human and computer languages. Recurrent Neural Networks(RNN) are used to process sequential data such as text/characters. We use RNN for Sentimental Analysis and Character Generation purposes. Recurrent Neural Networks maintain an internal memory/state of the input that was already processed. RNNs contain a loop and process one piece of input at a time.

Word embeddings are a vectorized representation of words in a given document that places words with similar meanings near each other. Feed-forward neural networks mean that all our data are fed forward from left to right.

Before you predict with your review, you should use the encodings from the training dataset to encode your review.

RNN Play Generator is used to predict the next character.

Reinforcement Learning

This will explore the environment with its own examples. It always keeps learning from its past mistakes and provides the best in the present.

Refer to the below link to learn reinforcement learning in detail.

https://medium.com/@aamir1610-67719/reinforcement-learning-aamir-p-bef28ea56bf1

All codes are hosted in GitHub. Please check if you need them for practice. If I write the codes in this article you can’t check with the output I am getting.

https://github.com/Best-forever-003

Check out this link to know more about me

Let’s get to know each other!
https://lnkd.in/gdBxZC5j

Get my books, podcasts, placement preparation, etc.
https://linktr.ee/aamirp

Get my Podcasts on Spotify
https://lnkd.in/gG7km8G5

Catch me on Medium
https://lnkd.in/gi-mAPxH

Follow me on Instagram
https://lnkd.in/gkf3KPDQ

Subscribe to my YouTube Channel and Share it with your friends
https://www.youtube.com/@knowledge_engine_from_AamirP

Udemy
Udemy (Python Course)
https://lnkd.in/grkbfz_N

Browse, my name Aamir P on Google

--

--

Aamir P

Hi! This is Aamir P. I am working as a Data Engineer. Google search AAMIR P to get my books from Amazon! Follow my medium account to get motivational content.