MLT Ese
MLT Ese
What is Tensorsflow
TensorFlow is an open-source software library for machine learning and artificial intelligence. It is used
for a wide variety of tasks, including image recognition, natural language processing, and speech
recognition. It is used by researchers and developers around the world to build and train cutting-edge
machine-learning models. It is most popularly used for Multidimensional-array based numeric
computation.
The word TensorFlow is made of two words, i.e., Tensor and Flow
• Tensor is a multidimensional array
• Flow is used to define the flow of data in operation.
• TensorFlow is used to define the flow of data in operation on a multidimensional array or Tensor.
Features of Tensorflow
• Flexible:- It is one of the essential TensorFlow Features according to its operability. It has
modularity and parts of it which we want to make standalone.
• Easily Trainable:- It is easily trainable on CPU and for GPU in distributed computing.
• Large Community:- Google has developed it, and there already is a large team of software engineers
who work on stability improvements continuously.
• Open Source:- The best thing about the machine learning library is that it is open source so anyone
can use it as much as they have internet connectivity.
• Feature Columns:- TensorFlow has feature columns that could be thought of as intermediates
between raw data and estimators; accordingly, bridging input data with our model.
• Availability of Statistical Distributions:- This library provides distribution functions including
Bernoulli, Beta, Chi2, Uniform, and Gamma, which are essential, especially when considering
probabilistic approaches such as Bayesian models.
Applications of TensorFlow:
• Image recognition: TensorFlow is used to train and run deep neural networks for image recognition
tasks, such as classifying images of objects or faces.
• Natural language processing: TensorFlow is used to train and run deep neural networks for natural
language processing tasks, such as text classification, machine translation, and question answering.
• Speech recognition: TensorFlow is used to train and run deep neural networks for speech
recognition tasks, such as transcribing audio recordings into text.
• Robotics: TensorFlow is used to train and run deep neural networks for robotics tasks, such as
controlling robots and navigating environments.
Data types in tensorflow
TensorFlow supports various data types for representing tensors, each suited for different types of
computations and memory requirements. Here are some of the commonly used data types in
TensorFlow:
• tf.float32: 32-bit floating-point numbers, commonly used for most numerical computations.
• tf.float64: 64-bit floating-point numbers, offering higher precision but requiring more memory
compared to float32.
• tf.int8, tf.int16, tf.int32, tf.int64: Signed integer types with different bit-widths (8, 16, 32, and 64
bits respectively), used for integer computations.
• tf.uint8, tf.uint16, tf.uint32, tf.uint64: Unsigned integer types with different bit-widths, used when
only non-negative integers are needed.
• tf.bool: Boolean type, representing True or False values.
• tf.complex64, tf.complex128: Complex number types with 32-bit and 64-bit floating-point
components respectively.
• tf.string: Variable-length strings, used for text data.
• tf.qint8, tf.qint16, tf.quint8: Quantized integer types, used for quantized inference in models
optimized for deployment on specific hardware, like TensorFlow Lite for mobile devices.
• tf.bfloat16: 16-bit floating-point numbers with reduced precision compared to float32, used in some
specific scenarios where lower precision is acceptable but memory and computation efficiency are
desired.
• tf.variant: A polymorphic data type used for handling heterogeneous data in TensorFlow, commonly
used in dynamic computation graphs.
What is Tensor
A tensor is a mathematical object used to represent multilinear mappings between vector spaces. In
simpler terms, you can think of tensors as multidimensional arrays of numerical values. Tensors have
various ranks, which indicate their dimensionality. For example:
• Scalar: A rank-0 tensor representing a single value. Scalars are often used to represent quantities
like temperature, mass, or energy.
• Vector: A rank-1 tensor representing an array of values arranged along a single dimension. Vectors
are commonly used to represent quantities with direction, such as velocity or force.
• Matrix: A rank-2 tensor representing a 2D array of values arranged in rows and columns. Matrices
are fundamental in linear algebra and are used to represent transformations, systems of linear
equations, and more.
• Higher-order Tensors: Tensors of rank 3 or higher are often referred to as higher-order tensors. They
represent multi-dimensional arrays of values and find applications in fields such as physics,
engineering, and computer science.
Tensor Representation
A tensor is a multi-dimensional array of data. It is represented as a data structure with the following
properties:
• Data type: The data type of a tensor specifies the type of data that it can store. The supported data
types in TensorFlow include integers, floating-point numbers, and strings.
• Shape: The shape of a tensor specifies the number of dimensions that it has. The number of
dimensions is also known as the rank of the tensor.
• Values: The values of a tensor are the elements that it stores. The values of a tensor can be accessed
using indexing.
Tensors are represented in TensorFlow using the tf.Tensor class. The tf.Tensor class has a number of
methods for creating, manipulating, and accessing tensors.
• Loading Data: Use TensorFlow's tools to get your data ready for training. You can read data from
files, databases, or even just from your computer's memory.
• Building and Training Models: You create your machine learning model using TensorFlow's
easy-to-use tools. You can either build your own model using Kera’s, which is like building with
blocks, or use ready-made models for common tasks. Then you teach your model what to do by
showing it lots of examples.
• Debugging and Optimization: You can check how your model is doing and fix any problems
using TensorFlow's debugging tools. When everything looks good, you can make your model
run faster by turning it into a special kind of program that TensorFlow understands well.
• Distributed Training: If you have a big task, you can get help from other computers to train your
model faster. TensorFlow makes it easy to spread out the work across many computers.
• Exporting Models: Once your model is trained and working well, you can save it in a format
that can be used in lots of different places. This makes it easy to share your model with others
or use it in different programs.
Keras Architecture
Kera’s provides a complete framework to create any type of neural network. Kera’s is innovative as
well as very easy to learn. It supports simple neural networks to very large and complex neural network
models. Let us understand the architecture of Kera’s framework and how Kera’s helps in deep learning
in this chapter.
The architecture of Kera’s: Kera’s API can be divided into three main categories.
• Model
• Layer
• Core Modules
In Kera’s, every ANN is represented by Kera’s Models. In turn, every Kera’s Model is composition of
Kera’s Layers and represents ANN layers like input, hidden layer, output layers, convolution layer,
pooling layer, etc., Kera’s model and layer access Kera’s modules for activation function, loss function,
regularization function, etc., Using Kera’s model, Kera’s Layer, and Kera’s modules, any ANN
algorithm (CNN, RNN, etc.,) can be represented simply and efficiently.
The following diagram depicts the relationship between the model, layer, and core modules −
Let us see the overview of Kera’s models, Kera’s layers, and Kera’s modules.
➢ Model
Kera’s Models are of two types as mentioned below –
• Sequential Model − The sequential model is a linear composition of Kera’s Layers. The sequential
model is easy, minimal as well as can represent nearly all available neural networks.
A simple sequential model is as follows –
from keras.models import Sequential
from keras.layers import Dense, Activation
➢ Layer
Each Keras layer in the Keras model represent the corresponding layer (input layer, hidden layer and
output layer) in the actual proposed neural network model. Keras provides a lot of pre-build layers so
that any complex neural network can be easily created. Some of the important Keras layers are specified
below,
• Core Layers
• Convolution Layers
• Pooling Layers
• Recurrent Layers
A simple python code to represent a neural network model using sequential model is as follows
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout model = Sequential()
• Activations module− The activation function is an important concept in ANN and activation
modules provide many activations functions like softmax, relu, etc.,
• Loss module− The loss module provides loss functions like mean_squared_error,
mean_absolute_error, poisson, etc.,
• Optimizer module− The Optimizer module provides optimizer functions like adam, sgd, etc.,
• Regularizers− Regularizer module provides functions like L1 regularizer, L2 regularizer, etc.,
Tensorflow Playground
TensorFlow Playground is an interactive web-based tool developed by Google that allows users to
explore and experiment with neural networks directly in their web browser. It provides a simplified
interface for building, training, and visualizing neural networks without requiring any installation or
coding knowledge.
Key features of TensorFlow Playground include:
• Visualization: Users can visualize the behavior of neural networks in real-time as they train. This
includes the ability to observe how changing network architecture, activation functions, and dataset
properties affect the training process.
• Dataset Selection: TensorFlow Playground offers various synthetic datasets, such as spiral, circle,
and Gaussian clouds, as well as the ability to upload custom datasets. These datasets help users
understand how neural networks learn patterns and make predictions.
• Network Configuration: Users can configure the architecture of the neural network by adjusting
parameters such as the number of hidden layers, the number of neurons in each layer, and the
activation functions used.
• Training Controls: TensorFlow Playground provides controls for adjusting the learning rate, batch
size, and regularization strength, allowing users to observe how these hyperparameters influence
the training process and model performance.
• Interactive Features: Users can interactively manipulate the input data points, observe the
corresponding changes in the model's predictions, and visualize decision boundaries in the feature
space.
UNIT 2
Perceptron and its equations basics
In machine learning and neural networks, a perceptron is one of the simplest artificial neurons,
originally proposed by Frank Rosenblatt in 1957. It serves as the building block for more complex
neural network architectures. The perceptron takes multiple inputs, applies weights to them, sums them
up, and then applies an activation function to produce an output.
Here are the basic components and equations associated with a perceptron:
• Inputs (𝑥𝑖): The perceptron takes multiple input values (𝑥1,𝑥2,...,𝑥𝑛), each associated with a
weight (𝑤𝑖).
• Weights (𝑤𝑖 ): Each input is associated with a weight (𝑤𝑖 ), which determines the importance
of that input to the output. Weights can be positive or negative, indicating the direction and
strength of the influence.
• Summation of weighted inputs (𝑧): The weighted inputs are summed up along with a bias term
(𝑏), resulting in the net input to the perceptron, denoted as 𝑧:𝑧 =∑𝑛𝑖=1(xi ⋅ wi) + b
• Activation function (𝑓): The net input (𝑧) is then passed through an activation function (𝑓) to
produce the output of the perceptron. The activation function introduces non-linearity into the
model and determines whether the perceptron "fires" (outputs a signal) based on the input it
receives. Common activation functions include:
Step function: Outputs 1 if the net input is greater than or equal to a threshold, and 0 otherwise.
Sigmoid function: S-shaped curve squashes the output between 0 and 1, useful for binary classification.
ReLU (Rectified Linear Unit): Outputs the input if it is positive, and 0 otherwise. It's widely used in
deep learning due to its simplicity and effectiveness.Mathematically, the output (y) of the perceptron is
calculated as:
𝑦 = 𝑓(𝑧)
Where 𝑧 is the net input calculated earlier, and 𝑓 is the activation function.
The perceptron learning algorithm adjusts the weights (𝑤𝑖 ) and bias (𝑏)) during training to minimize
the difference between the predicted output and the actual output. This process involves updating the
weights based on the error in prediction, typically using techniques such as gradient descent or
stochastic gradient descent.
The perceptron is the fundamental unit of computation in neural networks, and by connecting multiple
perceptrons in layers, more complex models such as multi-layer perceptrons (MLPs) can be constructed
to solve more sophisticated tasks.
The key differences between single-layer and multi-layer perceptron’s are:
• Representation: Single-layer perceptron’s can only represent linearly separable functions, while
multi-layer perceptron’s can represent complex non-linear functions.
• Learning: Multi-layer perceptron’s use techniques like backpropagation and gradient descent
to learn the optimal weights and biases, allowing them to learn more complex relationships
between inputs and outputs.
• Performance: Multi-layer perceptron’s generally outperform single-layer perceptron’s on tasks
involving complex patterns or non-linear relationships, such as image classification, because of
their ability to learn hierarchical representations of the data.
• Input layer: The input layer is the first layer of the perceptron. It receives the input data.
• Weights: The weights are the connections between the input layer and the output layer.
They determine how much influence each input has on the output.
• Bias: The bias is a value that is added to the weighted sum of the inputs. It can help to shift
the output of the perceptron.
• Activation function: The activation function is a mathematical function that is applied to
the weighted sum of the inputs. It determines whether the output of the perceptron is 0 or
1.
• Output layer: The output layer is the last layer of the perceptron. It produces the output of
the perceptron.
Features:
• Linearity: A single layer perceptron can only learn linearly separable patterns. This means
that the data points must be able to be separated by a straight line.
• Single layer: A single layer perceptron only has one hidden layer. This makes it a simple
model, but it can also be less powerful than more complex models.
• Supervised learning: A single layer perceptron is a supervised learning model. This means
that it requires labelled data to train.
Advantages:
• Simple: Single layer perceptrons are simple models that are easy to understand and
implement.
• Fast: Single layer perceptrons are fast to train and can be used to solve small problems
quickly.
• Efficient: Single layer perceptrons are efficient in terms of memory and computational
resources.
• Input layer: The input layer is the first layer of the MLP. It receives the input data.
• Hidden layers: The hidden layers are the intermediate layers of the MLP. They are responsible
for transforming the input data into a more abstract representation.
• Output layer: The output layer is the last layer of the MLP. It produces the output of the MLP.
• Weights: The weights are the connections between the neurons in the different layers. They
determine how much influence each neuron has on the output.
• Bias: The bias is a value that is added to the weighted sum of the inputs. It can help to shift the
output of the MLP.
• Activation function: The activation function is a mathematical function that is applied to the
weighted sum of the inputs. It determines the output of each neuron.
Features:
• Non-linearity: A multilayer perceptron can learn non-linear patterns. This means that the data
points do not need to be able to be separated by a straight line.
• Multiple layers: A multilayer perceptron has multiple hidden layers. This makes it a more
powerful model than a single layer perceptron.
• Supervised learning: A multilayer perceptron is a supervised learning model. This means that it
requires labelled data to train.
Advantages:
• Powerful: Multilayer perceptrons are powerful models that can learn to solve complex
problems.
• Robust to noise: Multilayer perceptrons are robust to noise in the data. This makes them less
likely to misclassify data points.
• Can handle multiple features: Multilayer perceptrons can handle multiple features at the same
time. This makes them suitable for problems with a large number of features.
Diagram:
Tensorflow API
The TensorFlow API is like a toolbox for building and using machine learning models. It provides
easy-to-use tools for creating, training, and using these models. TensorFlow is known for its
flexibility, meaning it can work on different types of devices, from regular computers to big servers.
It's also got a lot of extra features, like tools for serving models to users and for deploying models
on smartphones or the web. Plus, there's a big community of people using TensorFlow, so there's
lots of help available if you get stuck. Overall, it's a powerful tool that makes machine learning
more accessible to everyone.
UNIT – 3
Numerical on precision recall accuracy (as per syllabus and notes)
True Positive (TP), True Negative (TN), False Positive (FP), and False Negative (FN) are terms
used in confusion matrices to evaluate the performance of classification models, particularly in
binary classification tasks. Here's what each term represents:
• True Positive (TP): True Positive refers to the number of instances that were correctly predicted
as positive by the model.
• True Negative (TN): True Negative refers to the number of instances that were correctly
predicted as negative by the model.
• False Positive (FP): False Positive refers to the number of instances that were incorrectly
predicted as positive by the model.
• False Negative (FN): False Negative refers to the number of instances that were incorrectly
predicted as negative by the model.
• Accuracy: Accuracy measures the proportion of correctly classified instances out of the total
number of instances.
Formula: Accuracy=Number of Predictions/Total Number of Predictions
• Precision: Precision measures the proportion of true positive predictions (correctly predicted
positive instances) out of all instances predicted as positive by the model.
Formula: Precision=True Positives/True Positives+False Positives
• Recall (Sensitivity): Recall measures the proportion of true positive predictions (correctly
predicted positive instances) out of all actual positive instances in the dataset.
Formula: Recall= True Positives//True Positives+False Negatives
Numerical on loss func (mean square error and mean absolute error)
Confusion matrix or performace tuning
Confusion matrix and performance tuning are two different aspects of evaluating and improving the
performance of a machine learning model.
1. **Confusion Matrix**: It's a table that is often used to describe the performance of a classification
model. It displays the counts of true positive, false positive, true negative, and false negative predictions
made by the model. From the confusion matrix, various performance metrics like accuracy, precision,
recall, and F1 score can be calculated, providing insights into the model's behavior and potential areas
for improvement.
2. **Performance Tuning**: This involves adjusting various parameters or hyperparameters of a
machine learning model to improve its performance. It includes techniques such as hyperparameter
optimization, feature engineering, data augmentation, regularization, and model selection. Performance
tuning aims to optimize the model's predictive accuracy, reduce overfitting, and enhance generalization
to unseen data.
In summary, confusion matrix analysis helps understand the current performance of the model, while
performance tuning involves iteratively refining the model to achieve better results. Both are critical
steps in the machine learning workflow for building robust and accurate models.
NVIDIA cuda
NVIDIA CUDA (Compute Unified Device Architecture) is a parallel computing platform and
application programming interface (API) model created by NVIDIA. It allows developers to leverage
the power of NVIDIA GPUs (Graphics Processing Units) for general-purpose processing tasks beyond
just rendering graphics. CUDA enables efficient parallel computing on NVIDIA GPUs, making it
possible to accelerate a wide range of computationally intensive applications.
Here are some key aspects and features of NVIDIA CUDA:
1. Parallel Computing: CUDA provides a framework for harnessing the parallel processing
capabilities of GPUs. It allows developers to write programs that can perform thousands of
parallel tasks simultaneously, making it suitable for tasks that can be divided into many smaller,
independent operations.
2. GPU Programming Model: CUDA introduces a programming model that allows developers
to write code for both the CPU (Central Processing Unit) and GPU in a single program. This
hybrid approach lets developers offload parallelizable tasks to the GPU while keeping other
parts of the program on the CPU.
3. C/C++ Language Extensions: CUDA extends the C and C++ programming languages with
special keywords and constructs to define and control parallel execution on the GPU. This
allows developers to write GPU-accelerated code using familiar programming languages.
4. GPU Libraries: NVIDIA provides a range of GPU-accelerated libraries for various domains,
such as linear algebra, image processing, machine learning, and more. These libraries enable
developers to leverage GPU acceleration without writing low-level CUDA code.
5. Tools and SDK: NVIDIA offers a comprehensive suite of development tools and software
development kits (SDKs) to assist in CUDA development. These tools include profilers,
debuggers, and performance analysis tools.
6. Compatibility: CUDA is compatible with a wide range of NVIDIA GPUs, from entrylevel to
high-end models. It also supports multiple operating systems, including Windows, Linux, and
macOS.
7. Applications: CUDA has been widely adopted in various fields, including scientific
computing, machine learning, deep learning, computer vision, data analytics, and more. It has
played a crucial role in accelerating the performance of applications in these domains. CUDA
has had a significant impact on the field of high-performance computing and has made GPU
acceleration accessible to a broad range of developers and researchers. It continues to be a vital
technology for accelerating computationally intensive workloads across different industries.
NVIDIA cudnn
The NVIDIA cuDNN (CUDA Deep Neural Network) toolkit is a GPU-accelerated library of primitives
for deep neural networks (DNNs). It is developed by NVIDIA and is designed to improve the
performance of deep learning frameworks that utilize GPUs (Graphics Processing Units) for training
and inference tasks.
Here are some key aspects and features of cuDNN:
1. GPU Optimization: cuDNN is optimized to take full advantage of the parallel processing
capabilities of NVIDIA GPUs. It provides highly efficient implementations of key operations
used in deep learning, such as convolutions, pooling, normalization, and activation functions.
• Gather a dataset of music samples in MIDI format or a similar representation. MIDI files contain
musical notes, their timings, and other musical information in a structured format, making them
suitable for training machine learning models.
• Preprocess the MIDI data to convert it into a suitable format for training the neural network. This
may involve extracting musical features such as note pitches, durations, and velocities, and
representing them as numerical sequences.
Model Architecture:
• Choose an appropriate architecture for the RNN-based music generation model. Common choices
include vanilla RNNs, Long Short-Term Memory (LSTM) networks, or Gated Recurrent Units
(GRUs).
• Design the input and output layers of the model to match the format of the preprocessed music data.
For example, if representing musical notes as one-hot encoded vectors, the input layer should have
the same dimensionality as the number of unique notes in the dataset.
Training:
• Split the preprocessed music dataset into training and validation sets.
• Train the RNN model on the training data, using techniques such as teacher forcing (feeding the
true target outputs as inputs during training) to stabilize training.
• Monitor the model's performance on the validation set and adjust hyperparameters (e.g., learning
rate, batch size) as necessary to prevent overfitting and improve performance.
Generation:
• Evaluate the generated music samples using metrics such as musicality, coherence, and novelty.
Refine the model architecture and training process based on feedback from the evaluation process,
and continue to iterate until satisfactory results are achieved.
Post-processing and Rendering:
• Convert the generated music sequences back into a human-readable format (e.g., MIDI) for
playback and further processing.
• Optionally, use digital audio workstation (DAW) software or other tools to render the MIDI data
into audio files for listening and sharing