Lab3 - Introduction To Machine Learning Algorithms With A Focus On Robotics Applications
Lab3 - Introduction To Machine Learning Algorithms With A Focus On Robotics Applications
The intersec on of machine learning (ML) and robo cs is one of the most exci ng fron ers of modern
technology. At its core, machine learning is about teaching computers to learn from data, without being
explicitly programmed. When combined with robo cs, ML algorithms empower robots to learn from
their environment, adapt to uncertain es, and perform tasks that were once deemed too complex or
nuanced for automa on.In the world of robo cs, the applica on of machine learning is vast: from robots
learning to navigate unfamiliar terrains, to recognizing and manipula ng objects, to collabora ng with
humans safely. These capabili es are facilitated by a range of ML paradigms:
Supervised Learning, where robots can be trained using labeled data to perform tasks such as object
recogni on or trajectory predic on.
Unsupervised Learning, enabling robots to discern pa erns or structures in their environment without
any pre-labeled data, which can be invaluable for tasks like anomaly detec on or clustering sensory
inputs.
Reinforcement Learning, a paradigm where robots learn to make decisions by interac ng with their
environment, receiving feedback, and op mizing their ac ons to achieve specific goals.
1. So ware:
Python Environment:
Python is a versa le programming language widely used in both the machine learning and robo cs
communi es due to its simplicity and vast library support.
Jupyter Notebooks:
A web-based interac ve compu ng environment that allows for dynamic code execu on, visualiza on,
and documenta on all in one place. It's especially useful for labs and tutorials because of its interac ve
nature and ability to mix code with explanatory text.
Installa on:
To set up JupyterLab (an evolved version of Jupyter Notebooks with a more flexible user interface), use
the command pip install jupyterlab.
Scikit-learn:
A popular Python library for data mining and data analysis. It offers simple and efficient tools for various
machine learning algorithms, including both supervised and unsupervised learning tasks.
Installa on:
You can integrate it into your Python environment using pip install scikit-learn.
TensorFlow and PyTorch:
These are two leading deep learning frameworks. They provide a set of flexible tools for building and
training complex neural network models.
TensorFlow:
Developed by Google Brain, TensorFlow is widely used for various machine learning applica ons,
especially deep neural networks.
PyTorch:
Developed by Facebook's AI Research lab, PyTorch is known for its dynamic computa onal graph, making
it par cularly useful for research and prototyping.
Installa on:
For TensorFlow, use the command pip install tensorflow. For PyTorch, installa on might vary based on
the system and whether GPU support is needed, so it's best to refer to the official guide.
Gym:
An open-source library developed by OpenAI for developing and comparing reinforcement learning
algorithms. It provides a variety of predefined environments, including some related to robo cs, which
makes it easier to train and test agents.
Installa on:
Integrate Gym into your Python setup using pip install gym.
2.Robo cs Simulators:
These are so ware pla orms that allow for the simula on of robo c systems. Simulators enable tes ng
and development of algorithms in a controlled environment before deploying them on real hardware.
Gazebo:
Widely used in the robo cs community, Gazebo offers a robust physics engine and 3D simula ons,
making it suitable for a wide range of robo c applica ons.
3. Datasets:
In order to prac cally understand the working of various machine learning algorithms, the right datasets
are crucial.
Iris Dataset: Consists of 3 classes of 50 instances each, where each class refers to a type of iris plant.
Useful for classifica on tasks.
Boston Housing Dataset: Pertains to housing values in suburbs of Boston. It contains 506 cases and is a
mix of categorical and con nuous variables, making it ideal for regression tasks.
Image Dataset: A collec on of unlabeled images. Robots might use such datasets to learn features or
detect pa erns.
Robot Sensor Readings: Data collected from sensors on robots, like Lidar or infrared sensors, without
predefined labels.
While it doesn't rely on tradi onal datasets, the learning environment, ini al condi ons, and the reward
structure play a role similar to that of a dataset.
Gym Environments: Pre-defined simulated environments that allow robots or agents to interact and
learn. For instance, CartPole-v1 or MountainCar-v0 can be used for various reinforcement learning tasks.
LAB 3. Prepara on
A primer on the concepts of machine learning and its transforma ve role in the domain of robo cs.
Discuss the evolu on of robo cs and how machine learning has opened avenues for advanced autonomy
and adaptability in robots.
Se ng up the Environment:
Python Setup:
Make sure to check the box that says "Add Python to PATH" during installa on. This makes it easier to
run Python from the command line or terminal.
Type python --version and press Enter. It should display the version of Python you installed.
Step 1: Se ng Up Pip
If you installed Python from the official site, pip (Python's package manager) should be included.
Install scikit-learn:
Install TensorFlow:
Install Gym
Robo cs Interface
Gazebo is recommended. It provides a virtual environment where robots and their sensors can be
simulated.
Step 2: Installa on
Gazebo:
Installa on varies depending on the OS. Detailed instruc ons can be found on the official website.
Gazebo offers Python APIs. A er installing the simulator, you can control and interface with them using
Python scripts. And we use the gazebo-python package.
Tutorials & Exercises:
Supervised Learning
1. Linear Regression:
Objec ve: To understand the rela onship between robot inputs (e.g., motor voltages) and outputs (e.g.,
speed).
Theory:
Linear regression aims to establish a linear rela onship between the input and output variables. This is
represented by the equa on:
Y=mX+c Where:
Dataset Descrip on: The Boston Housing dataset comprises 13 features related to housing in Boston, like
crime rate, average number of rooms per dwelling, etc. The target variable is the median value of owner-
occupied homes in $1000's.
Implementa on Steps:
2. Logis c Regression:
Objec ve:
Theory:
Logis c Regression is used for binary classifica on tasks. It predicts the probability that a given instance
belongs to a par cular category. The output is a value between 0 and 1, which can be thresholded to
assign a class label.
Exercise:
Implementa on Steps:
Collect and preprocess sensor data: normalize values, handle missing data, etc.
Split the data into training and tes ng sets.
Train a logis c regression model using the training data.
Evaluate the model using accuracy, precision, and recall metrics on the test set.
Objec ve:
Theory:
SVM tries to find the op mal hyperplane that correctly classifies data into categories, maximizing the
margin between different classes.
Exercise:
Scenario: A robot with a camera scans its environment. Using features extracted from images (like shape,
texture, and size), it should classify objects into predefined categories.
Implementa on Steps:
Objec ve:
Theory:
ANNs are inspired by the neural networks present in the human brain. They consist of interconnected
nodes (neurons) that can learn pa erns from data.
Exercise:
Implementa on Steps:
Unsupervised Learning
Unsupervised Learning
1. K-means Clustering:
Objec ve: To understand data clustering based on inherent structures in the data.
Theory:
K-means is an itera ve clustering algorithm that aims to par on n observa ons into k clusters in which
each observa on belongs to the cluster with the nearest mean. The algorithm converges when the
assignments no longer change.
Exercise:
Scenario: A robot constantly provides various sensor readings. By clustering these readings, one can
iden fy pa erns or anomalies which can be indica ve of robot behavior or health.
Implementa on Steps:
Collect sensor readings: this can include temperature, pressure, lt angle, etc.
Normalize the data.
Apply the K-means algorithm and determine an op mal number of clusters.
Visualize clusters to interpret pa erns or anomalies.
Objec ve: To grasp dimensionality reduc on techniques to focus on the most significant data aspects.
Theory:
PCA transforms the original variables to a new set of variables (principal components) which are
orthogonal (uncorrelated), and which reflect the maximum variance in the data.
Exercise:
Implementa on Steps:
Reinforcement Learning
1. Q-Learning:
Objec ve:
Exercise:
Program a robot in a simulated environment to navigate through a simple maze using Q-tables.
Implementa on Steps:
1. Setup Environment:
Represent the maze as a grid. Each cell is a state.
Ac ons are movements like up, down, le , right.
Rewards are given for reaching the goal and penal es for hi ng walls.
import numpy as np
n_states = len(maze) * len(maze[0])
n_ac ons = 4 # up, down, le , right
Q = np.zeros((n_states, n_ac ons))
3.Q-learning Algorithm:
ALPHA = 0.1
GAMMA = 0.9
EPSILON = 0.1
Exercise:
Implementa on Steps:
import tensorflow as
.keras.layers.Dense(24, ac va on='relu'),
.keras.layers.Dense(24, ac va on='relu'),
.keras.layers.Dense(n_ac ons)
])
2. DQN Algorithm:
Exercise:
Implementa on Steps:
1. Policy Network:
.keras.layers.Dense(24, ac va on='relu'),
.keras.layers.Dense(24, ac va on='relu'),
])
PPO Algorithm:
CLIP_EPSILON = 0.2
EPOCHS = 10
for episode in range(num_episodes):
# Gather trajectories using current policy
trajectories = gather_trajectories(policy_model)
for _ in range(EPOCHS):
for trajectory in trajectories:
state, ac on_prob, reward, value = trajectory
old_prob = ac on_prob[ac on]
new_prob = policy_model.predict(state)[0][ac on]
ra o = new_prob / old_prob
# Clip the objec ve
surrogate_obj = .minimum(
ra o * advantage,
.clip_by_value(ra o, 1.0 - CLIP_EPSILON, 1.0 + CLIP_EPSILON) * advantage
)
loss = -surrogate_obj + vf_loss + entropy_bonus
# Backpropaga on to update policy_model
5. Evalua on and Feedback Tools: