Leap of Faith in PyTorch: Introduction (Day 1)

Victor Barbarosh
Practical Coder’s Chronicles
7 min readJun 24, 2024

We will also see how China, US, and Russia fit into this world.

DALL-E, instructed and carefully guided by Victor Barbarosh

Welcome to Day 1 of our 29-day PyTorch journey! Today, we’ll get introduced to PyTorch, a powerful deep learning framework, and understand the basics of tensors. We will also explore some initial examples that combine financial, social, and political topics to make the learning process more engaging.

Introduction to PyTorch

PyTorch is an open-source machine learning library which was developed by Facebook’s AI Research Lab and it aims at providing flexibility and speed, thus making it a pretty popular choice for both research and industry.

Install PyTorch

Getting started with PyToch implies you need have it on your machine. If you haven’t installed it yet, do so with the following command:

pip install torch

Import PyTorch and Create Tensors

To beging with, we import the needed PyTorch module and create our first tensor.

import torch

tensor_example = torch.tensor([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(tensor_example)

Expected output:

tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

But wait! What’s a tensor?

Put it simple, a tensor is a bulding blocks of PyTorch. They look like arrays, but calling the tensor an array, would be oversimplifying it. It would be similar to calling the aircraft carriers simply “boats”. Nevertheless, they are similar to NumPy arrays, the tensors, although the tensors come with additional capabilities for GPU (Graphics Processing Unit) acceleration.

Side dive on tensors

The term tensor is a bit of an ambiguous term, used in many domains, and here is why. Imagine you are a mathematician and you have a coffee with two other people, one of them is a physicist and another one is computer scientist. If every one of you are in their own bubble and neither of you have taken courses from each others other fields, you may use the word “tensor” and every one of you will have a slightly different mental model about what a “tensor” means.

Mathematical perspective — for a mathematician, a tensor represents a geometric object that generalizes scalars, vectos, and matrices. More formally, a tensor is an element of a tensor product of vector spaces. In mathematician’s world, a tensor can be described in terms of their components with respect to a basis:

  • Order/Rank: The number of indices required to specify each component of a tensor. Scalras would be a 0th-order tensors, vectors are 1st-order tensors, and matrices are 2nd-order tensors.
  • Components: The elements of the tensor in a given coordinate system, which transform acording to specific rules under a change of basis.

Physics perspective — the physicist would use the term tensor to represent physical quantities that have magnitude and multiple directions, such as stressm strain, and the electromagnetic field. Physical tensors are described based on how they transform under changes in coordinate systems:

  • Tensorial Quantity: A physical quantity that obeys specific transformation rules when the coordinate system is changed. For example, the stress tensor describes the internal forces in a material.
  • Symmetry Properties: Physical tensors often have symmetries that simplify their representation. For instance, the stress tensor is symmetric in many cases.

Computer Science Perspective — finally, you the computer scientist, when hearing th word tensor, first thing you’ll think of would be data structures and algorithms, particularly in machine learning and deep learning. We will look at tensors as multi-dimensional arrays of numerical values:

  • Data Structures: Tensors are implemented as arrays with multiple dimensions, where the number of dimensions (or axes) is the tensor’s rank.
  • Operations: Various operations can be performed on tensors, such as addition, multiplication, slicing, and element-wise functions. Tensor libraries such us (surprise surprise) PyTorch, provide optimized routines for these operations.

So why I listed these three perspectives?

The scope of this post is not about tensors per se. However, we need to ancknowledge that these posts will be (hopefully) read by readers with different backgrounds and expertise. Thus, these side notes on tensors are meant to clarify a certain degree of confusion when this term called tensor, is used.

Next, we go back to our business.

Basic Tensor Operations

To get a better grasp of what tensors are and how to play with them, let’s perform a few basic operations.

Tensor Addition

Let’s start with a simple addition operation using tensors.

# Creating two tensors
tensor_a = torch.tensor([[1, 2], [3, 4]])
tensor_b = torch.tensor([[5, 6], [7, 8]])

# Adding the tensors
tensor_sum = tensor_a + tensor_b
print("Tensor Addition:\n", tensor_sum)

Expected output:

Tensor Addition:
tensor([[ 6, 8],
[10, 12]])

Tensor Multiplication

# Multiplying the tensors element-wise
tensor_product = tensor_a * tensor_b
print("Tensor Multiplication:\n", tensor_product)

Expected output:

Tensor Multiplication:
tensor([[ 5, 12],
[21, 32]])

Well, these two examples above will do the thing in terms of telling you that we can perform addition and multiplcation on tensors. But are these examples helpful? I mean, are these examples actually keeping you interested in using PyTorch? Are they sparking at least some tiny interest in getting your hands dirty and writing some code, yourself? Probably not. Thus, I decided to provide you with some more meaningful examples, meaningful for the real life, and not for the framework’s use cases only, so keep reading.

Example using Financial, Social, and Political Topics

Let’s create an example that combines financial, social, and political data to calculate some more exciting parameters. Imagine we have the following three parameters: economic indicators (GDP), social factors (population), and political factors (governance score) for five countries - USA, China, India, Canada, and Russia. The GDP data is taken from the year 2023.

Create the Tensors for our Data

# Financial data: GDP in trillion dollars (year 2023)
# USA, China, India, Canada, Russia
# define your python array for gdp data
gdp_array = [21.43, 14.34, 5.08, 1.64, 1.48]
# transform your python array into the pytorch tensor
gdp = torch.tensor(gdp_array)
print(gdp)

# Social data: Population in millions
# USA, China, India, Canada, Russia
# define your python array for population data
population_array = [330, 1400, 1380, 38, 144]
# transform your python array into the pytorch tensor
population = torch.tensor(population_array)
print(population)

# Political data: Governance score (hypothetical, adjust it to your own convenience)
# USA, China, India, Canada, Russia
# define your python array for governance score data
governance_score_array = [65, 70, 60, 60, 65]
# transform your python array into the pytorch tensor
governance_score = torch.tensor(governance_score_array)
print(governance_score)

Expected output:

tensor([21.4300, 14.3400,  5.0800,  1.6400,  1.4800])
tensor([ 330, 1400, 1380, 38, 144])
tensor([65, 70, 60, 60, 65])

Note on Governance Score

A governance score is a metric used to evaluate the quality and effectiveness of governance within a country, organization, or institution. It typically considers various dimensions of governance, such as:

  1. Regulatory Quality: The ability of the government to formulate and implement sound policies and regulations that permit and promote private sector development.
  2. Government Effectiveness: The quality of public services, the capacity of the civil service, and the degree of its independence from political pressures.
  3. Political Stability: The likelihood of political unrest or instability, including the risk of violent conflict or government collapse.
  4. Rule of Law: The extent to which agents have confidence in and abide by the rules of society, including the quality of contract enforcement, property rights, the police, and the courts.
  5. Control of Corruption: The extent to which public power is exercised for private gain, including both petty and grand forms of corruption, as well as the “capture” of the state by elites and private interests.
  6. Voice and Accountability: The degree to which a country’s citizens are able to participate in selecting their government, as well as freedom of expression, freedom of association, and a free media.

Governance scores are typically compiled by international organizations, research institutions, and think tanks, using data from surveys, expert assessments, and various indicators. Examples of such indices include the World Bank’s Worldwide Governance Indicators (WGI), Transparency International’s Corruption Perceptions Index, and the Economist Intelligence Unit’s Democracy Index.

In our example, the governance scores are hypothetical and are meant to provide a way to combine economic, social, and political factors into a single analysis.

Calculating GDP Per Capita

GDP per capita is a common economic indicator that represents the average economic output per person. We calculate it by dividing the GDP by the population.

# Adjust the GDP to billions to match the population in millions for easier interpretation
# Convert GDP from trillions to billions
gdp_in_billions = gdp * 1000

# Calculating GDP per capita in thousands of dollars
gdp_per_capita = gdp_in_billions / population
print("GDP Per Capita (in thousands of dollars):\n", gdp_per_capita)

Expected output:

GDP Per Capita (in thousands of dollars):
tensor([64.9394, 10.2429, 3.6812, 43.1579, 10.2778])

What we can learn from this output is that the GDP per capita for Canada, for example, is approximately $43,157.90 USD, indicating that on average, each Canadian produces an amazing amount of $43,157.90 USD value to the GDP of their country. Isn’t that nice? …and, is this real?

No idea, but it’s not for me to answer this question. The important thing is that it would do it for our purpose, which is learning PyTorch.

Adjusted Governance Score (GS)

Let’s say we would like to adjust the GS by the GDP per capita to get a combined variable which will reflect both, the economic output and the governace quality.

# Adjusted governance score
adjusted_governance_score = governance_score * gdp_per_capita
print("Adjusted Governance Score:\n", adjusted_governance_score)

Expected output:

Adjusted Governance Score:
tensor([4221.0605, 717.0000, 220.8696, 2589.4736, 668.0555])

Well, this is more fun to interpret. The Adjusted Governance Score values are the highest in US. Canada is not so far from that, while China and Russia are fighting for the third place, in this fictitious score ranking.

Anyways, hope you had fun playing with numbers and interpreting them as you wish. My point here is to show you a few basic operations in PyTorch, using some easy to remember examples.

Conclusion

Today, we have successfully installed PyTorch, created our first tensors, and performed basic tensor operations. We also combined financial, social, and political data to calculate interesting parameters. As we progress through the coming days, we’ll delve deeper into more advanced topics and build more complex models.

Stay tuned for Day 2, where we will explore more tensor operations and get hands-on with PyTorch’s powerful capabilities!

Follow me here and on Tweeter for more post on Software Development with hands-on examples that are very much connected to our real world!

--

--

Victor Barbarosh
Practical Coder’s Chronicles

I am a full-stack software dev, writing about code, money & code of money, in all their forms and beauty!👉🚀