What is Machine Learning?
You might be familiar with the term ‘Machine Learning’ nowadays. Have you ever thought about what Machine Learning actually is? Let's take a look.
Machine Learning is a subfield of Artificial Intelligence (AI) and computer science that enables systems to perform complex tasks in a way that mimics human problem-solving.
Machine Learning is useful in cases where the applicability of traditional programming is limited. Traditional programming requires detailed instructions and data to make the computer solve a problem. However, in situations involving complex problems—where writing programs is impractical and time-consuming, such as training a computer to recognize pictures of different animals—Machine Learning can be employed. Some real-world examples of Machine Learning applications include spam email detection, driverless cars, video recommendations on streaming platforms, chatbots, traffic prediction, product recommendations, and speech recognition.
How Machine learning works?
The initial step of machine learning is data collection. Data can be numbers, texts, pictures or voice. The gathered data will be processed and will be used to train the machine learning model. The result will be better as the data is more. Once the data is prepared the next step is to choose the right model. There are different types of machine learning models available.
linear regression, decision tree, random forest and neural networks. Choose the models based on the nature of the data and the problem. Then, the machine learning model will convert these data into patterns. Final step is integrating the trained model into real word problems to solve the problem.
What is PyTorch?
I hope you now have a basic understanding of what Machine Learning is and how it works. Next, let’s move on to PyTorch. PyTorch is a machine learning library for the Python programming language, based on Torch. It is commonly used in applications running on GPUs and CPUs. PyTorch was made open source in 2017 and was developed by Facebook (Currently Meta) AI Research.
pip install torch
You can install the Pytorch using the command -
pip install torch
Basics of PyTorch
Tensors
Tensors are the building blocks of PyTorch and it’s the same as the numpy array. They are used for converting various data, such as images, audio, video, time series etc into patterns and numbers, so the computers can understand. The main feature of tensors is they can keep track of all the operations performed on them, which results in providing optimized outputs.
Let’s have a look at some basic operations on tensors.
Import PyTorch library
import torch
Creating tensor from a list,
Tensors are defined using the torch.tensor() function; it can have the values ranging from float to integers or decimals.
tensorA = torch.tensor([1, 2, 3])
print("Tensor from list:", tensorA)
Will give a tensor like this
Tensor from list: tensor([1, 2, 3])
Creating tensor of zeros having shape (2, 3)
Tensors with values of 0 can be created using torch.zeros function.
tensorB = torch.zeros(2, 3)
print("Tensor of zeros:", tensorB)
Tensor of zeros: tensor([[0., 0., 0.],
[0., 0., 0.]])
This will return a tensor having two rows and three columns.
Creating random tensor having shape (3, 2)
tensorC = torch.rand(3, 2)
print("Random tensor:", tensorC)
Random tensor: tensor([[0.2076, 0.2355],
[0.7989, 0.4775],
[0.4822, 0.8217]])
This will return random tensors with 3 rows and 2 columns each time you run this code.
Moreover, you can perform some operations on Tensors.
Addition of two tensors,
result_sum = tensorA + tensorB
print("Addition result:", result_sum)
Addition result: tensor([[1., 2., 3.],
[1., 2., 3.]])
Multiplication of two tensors,
result_mul = tensorA * 5
print("Multiplication result:", result_mul)
Multiplication result: tensor([ 5, 10, 15])
Similarly, you can perform subtraction, division, and other operations on tensors.
In this blog, we've covered the fundamental concepts of Machine Learning, PyTorch, and tensors. Machine Learning is a powerful tool for solving complex problems where traditional programming falls short. PyTorch is a machine learning library for Python, and tensors are the building blocks of PyTorch.
By understanding these basics, you’re now equipped with the foundational knowledge needed to explore deeper into the world of machine learning and PyTorch. As you continue learning, you'll discover more advanced techniques and applications that build on these concepts, enabling you to tackle a wide range of real-world challenges.