Sign In
Sign In

How to Convert String to Float in Python

How to Convert String to Float in Python
Amr Essam
Technical writer
Python
02.10.2024
Reading time: 7 min

Python variables provide an easy way to store and access data in a program. They represent the memory addresses that contain the required data values. Each variable has a specific data type which reflects the kind of data it can store like an int, float, or a string.

In some scenarios, we might need to convert one data type to another in order to be used in a later operation in our program. For example, if we receive an integer number from a user like this x = input(“Please enter a number:”) this input variable will be automatically stored as a string. So, if we’re to do a numeric operation on it we’ll need to convert it to an int first.

This process of converting between data types is called type casting or type conversion. It is a fundamental concept in programming that offers compatibility and flexibility in our programs.

In this article, we will cover a common example of type casting in Python which is converting a string to a float. We will also look at handling conversion errors that might appear in some scenarios.

Type Casting Categories

There are mainly two kinds of type casting, explicit casting and implicit casting.

Explicit Casting

In the explicit casting the developer manually declares the conversion and writes it inside the code. This is usually done by using a conversion function for a specific data type.

For example, we can convert a float to an int with the following code:

x=1.5  # float variable
y=int(x) # convert to integer with the int() function

To determine the data type of y we can use the type() function as follows:

print(type(y))

Now the output should print the type of y as int

Image8

The explicit casting gives the programmer control over when and how to execute the conversion.

Implicit Casting

In the implicit casting the interpreter automatically converts between data types without the need for the developer to declare it in the code. This is usually done to allow for compatibility in an operation and prevent data loss.

For example, when performing addition operation between a float and an int as follows:

x=1.5
y=1
z= x+y

In the above example, Python will automatically convert the integer value 1 to a float:

print(type(z))
print(z)

Now the output should print the type of z and its value:

Image3

As we can see from the image, the variable z which is the result of the addition has a data type of float.

Converting Strings to Floats Using float() function

To convert a string to a float in Python we use a function called float(). It is a built-in function that takes an argument (whether a string or an int) and constructs a floating-point number from it.

For example, the following code will convert the value of the my_string variable to a float:

my_string="10.267"
my_float=float(my_string)

We can then check the type and value of the my_float variable with the following code:

print(type(my_float))
print(my_float)

Now if we run the above example we’ll get the type of my_float variable as a float with the same value constructed from the converted string:

Image6

By converting the string to a float we can now execute the numeric operations we need on the converted variable:

Image2

In the above image we performed an addition operation on our variable (my_float+10) and it was executed successfully.

When we use the float() function, what happens under the hood is that it calls an object method named __float__(). This __float__() method implements the float() function and executes the logic for the conversion. In other words, when we write float(x) it is translated into x.__float__().

Handling Conversion Errors with try/except

We might encounter a scenario where a string value isn’t applicable for conversion to a float. For example, if the user inputs a string that doesn’t match a valid float number format (contains letters, special characters, etc).

To handle such cases, we need to implement a validation logic that checks if the input is applicable for conversion. A common implementation for this logic can be done using the Python try/except block.

First let’s test the scenario without error handling using the following code:

invalid_string="abc10.5"
my_float=float(invalid_string)

Now let’s try to run our code:

Image7

As we can see in the above image, the code produced a ValueError because the invalid_string variable contained an improper float value format.

To handle such error, we can use a try/except block as follows:

invalid_string="abc10.5"
try:
    my_float=float(invalid_string)
except ValueError:
    print("Please enter a valid string value")

In the above code we are executing our conversion inside the try block, then we’re using the except block to check if the conversion throws a ValueError exception.

Let’s run our code again:

Image9

As we can see in the above image, because this conversion throws a ValueError the code inside the except block gets executed and prints our output message.

Converting Lists of Strings to Floats

We can also apply the type casting process to a list of objects instead of a single variable. In that case we’ll be converting each item in the list to a different data type. So, we can extend upon our previous example and convert a list of strings to floats.

Let’s explore a couple of ways in which we can achieve this:

Using List Comprehension

List comprehension is a very handy way to create a new list out of an existing list in Python. It provides a simpler and shorter syntax in which you can apply specific logic or operations on the existing list items to produce the new list.

We can convert a list of strings to floats using list comprehension with the following code:

string_list=["10.1", "10.2", "10.3", "10.4"]
float_list=[float(item) for item in string_list]

In the above code, we create the float_list from the string_list by iterating over each item in the string_list and executing the float() function.

We can then print the new float_list and the type of each item inside it with the following code:

print(float_list)
for x in float_list:
    print(type(x))

Now let’s run our code and check the output:

Image5

As we can see in the above image, the float_list was populated by the items from the string_list, but the type of each item was converted to a float.

Using the map() function

Another way for converting a list of strings to floats is by using the map() function. The map() function returns a map object after taking two arguments, the first is a function that we want to execute, and the second is an iterable (list, tuple, etc) where we want to execute the first function on each item.

Let’s explain this on our scenario using the following code:

string_list=["10.1", "10.2", "10.3", "10.4"]
float_list=list(map(float, string_list))

Again we’ve our existing string_list and we want to create a new float_list from it after conversion. The map() function here is taking two arguments which are float and string_list. This means we want to apply the float() function on each item in the string_list.

Since the map() function returns a map object, we’re passing it to the list() function to convert the return object into a list which will be stored in the float_list object.

Let’s run our code and check the output:

Image4

We can see the float_list is again created from the string_list by converting the string items to floats.

Using Traditional for loop

We can also convert our list of strings to floats using our good friend, the Python for loop as follows: 

string_list=["10.1", "10.2", "10.3", "10.4"]
float_list=[]
for item in string_list:
    float_list.append(float(item))

In the above code, we iterate over the string_list and append each item into the float_list after converting it to a float.

Now let’s run our code:

Image1

Again we’ve our float_list here is populated from the string_list and the items are converted from strings to floats.

Conclusion

Python type casting is a fundamental concept that involves converting one data type to another. It provides compatibility and flexibility for programmers in their code. In this article we’ve covered a common example of type casting which is converting a string to a float using the float() function. We also used the try/except block to handle conversion errors when the input string format is not valid.

On our app platform you can find Python applications, such as Celery, Django, FastAPI and Flask. 

Python
02.10.2024
Reading time: 7 min

Similar

Python

Functions in Python

Functions in Python are blocks of reusable code that you can access by calling the function name and passing arguments. Using functions in Python significantly simplifies a programmer's work because, instead of writing code repeatedly, one can simply call a function. How to Create a Function in Pyhton Let's start with an example and then move on to the explanation: def multiply(first, second):    return first * second We have just written a function that performs a simple task: it multiplies the values (arguments) passed to it. These values can then be entered after the function name in the program to get the product of the factors. Now, enter the following in IDLE: >>> multiply(7, 8) Arguments can include not only whole numbers but also decimal numbers, for example: >>> multiply(7.4, 8.2)60.68 Now, let's break down the code. Here, we define a Python function using the def keyword and the function name. In parentheses, we specify parameters that will accept various arguments from user input. A colon must follow the closing parenthesis, after which a new line with indentation starts the function body, describing what the function does. If you're writing code in an editor, the indentation will be added automatically. We used the return operator, which explicitly returns arguments. Note that after return, there is an instruction on what the program should do with the arguments. In this case, it multiplies them. Practical Example of Using Python Functions Here, we will demonstrate how Python functions help optimize routine tasks. The following example is simplified but illustrative. By understanding how functions work, you can learn to solve your own tasks, which will become more complex and interesting as you progress in the language. Let's say we opened a bookstore and purchased a cash register, and the cashier had already issued receipts for the first customers. Initially, a receipt might look like this: print("Learn Now, LLC") print("Programming Book", end=" ") print(1, end=" pcs. ") print(50, end=" euro") print("\nAdvanced Programming Book", end=" ") print(1, end=" pcs. ") print(100, end=" euro") print("\nTotal:", 150, end=" euro") print("\nThank you for your purchase!") Output: Learn Now, LLC Programming Book 1 pcs. 50 euro Advanced Programming Book 1 pcs. 100 euro Total: 150 euro Thank you for your purchase! Now, imagine that a whole stack of books has been purchased, and the number of customers is increasing daily. While you manually calculate the total for one customer, others start getting impatient. This is where automation comes in.  Let's say someone buys seven different books, with some books purchased in multiple copies: def check(book_attr): total = 0 print("Learn Now, LLC") for book in book_attr: a = book[0] b = book[1] c = book[2] print(f"{a} ({b} pcs.) - {c} euro") total += b * c print(f"\nTotal: {total} euro") print("Thank you for your purchase!") book_attr = [ ("Programming Book", 2, 50), ("Advanced Programming Book", 2, 100), ("Programming Book 80 lvl", 2, 195), ("Beginner's Guide to Python", 1, 120), ("You Can Become a Programmer", 1, 98), ("Functional Programming in Python", 1, 95), ("Secrets of Clean Code", 1, 80), ] As we can see, new variables appeared, and the purchase list was placed in a separate block. Now, when generating a new receipt, all we need to do for automatic total calculation is enter the book names, quantities, and prices per unit. Once all items are entered, we call our function with the parameter formatted as a tuple above: check(book_attr) This produces the following output: Learn Now, LLC Programming Book (2 pcs.) - 50 euro Advanced Programming Book (2 pcs.) - 100 euro Programming Book 80 lvl (2 pcs.) - 195 euro Beginner's Guide to Python (1 pcs.) - 120 euro You Can Become a Programmer (1 pcs.) - 98 euro Functional Programming in Python (1 pcs.) - 95 euro Secrets of Clean Code (1 pcs.) - 80 euro Total: 1083 euro Thank you for your purchase! That's it! The total amount was calculated automatically. Let’s break down the code: The variable total stores the purchase total and changes as new values are added to the tuple. A for loop is used to define a set of variables that store the following values: a: product name b: quantity c: price per unit Next, we give the print command. The letter f in print statements (which is itself a built-in function, by the way) means that f-strings are used. For now, it's enough to know that they are a convenient formatting method, and the code is self-explanatory. The next line should not be surprising: it calculates the total by multiplying the quantity of each item by its price and adding the result to the running total. Finally, we use another f-string for text formatting, and we have already discussed the tuple block that stores the necessary data for purchase calculations. Features of Functions in Python Key Advantages: No need to repeat specific blocks of code, which can sometimes be quite large. Functions can be called as many times as needed, even consecutively. When divided into multiple functional blocks, large programs become much easier to track. There are almost no downsides to functions in Python, except that they may not always be convenient. In some cases, it is easier to use generators, as certain functions (e.g., filter) may return iterators, requiring additional code to process them. For example, if we enter the following in IDLE: >>> numbers = [2, 4, 6, 8, 10, 12, 14] >>> filter(lambda num: num >= 10, numbers) We get this result: <filter object at 0x00000000030C3220> To correctly display elements that meet the condition, we need to wrap this expression as follows: >>> list(filter(lambda num: num >= 10, numbers))[10, 12, 14] Built-in Functions in Python You have almost certainly used them in your first Python lesson. Here’s an example: print("Hello, World!") The print function is a built-in function, and "Hello, World!" is its argument. Python has hundreds, even thousands, of built-in functions, especially when additional libraries are included. You don't need to know all of them; you can always check the documentation if you encounter an unfamiliar function. However, you will need to learn some common built-in functions, as these core elements are essential for writing any useful program. Here are some commonly used built-in functions: len returns the length (number of elements) of a sequence such as a string, list, tuple, range, or array: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]len(flowers) Output: 5 str converts numbers into strings (since Python does not allow direct concatenation of strings and numbers): year = 2008"Euro " + str(year) Output: 'Euro 2008' int converts strings into integers. It also rounds floating-point numbers to the nearest integer, always towards zero: int(554.995) Output: 554 float converts integer values into floating-point numbers, which can be useful for certain calculations: float(55) Output: 55.0 tuple converts lists into tuples: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]tuple(flowers) Output: ('bellflower', 'cornflower', 'buttercup', 'forget-me-not', 'daisy') dict allows you to create dictionaries. Here’s an example of creating a dictionary from a list of tuples using dict: clubs = [('Barcelona', 1), ('Juventus', 3), ('Liverpool', 2), ('Real Madrid', 5), ('Bayern München', 4)]dict(clubs) Output: {'Barcelona': 1, 'Juventus': 3, 'Liverpool': 2, 'Real Madrid': 5, 'Bayern München': 4} range creates number sequences, which can be useful for iterating through numeric values: for number in range(0, 30, 3): print(number) Output: 0 3 6 9 12 15 18 21 24 27 The range function takes three parameters: The first two define the range limits. The third (optional) parameter specifies the step. In this case, numbers from 0 to 30 are printed in steps of 3. The upper bound is not included in the output. To include it, the range should be extended slightly: for number in range(0, 31, 3): print(number) Output: 0 3 … 27 30 Using the Result of One Function in Another Python Function Finally, let’s look at another interesting technique. Since functions in Python are objects, they can be passed as arguments to other functions and referenced. def check(company="Learn Now"): """Allows inserting different company names in the receipt""" print(f"{company}, LLC") Let’s enter the name of another company: check("Enlightenment") Output: Enlightenment, LLC Now, let’s pass the created function to the built-in help function to learn what it does: help(check) Output: Help on function check in module __main__: check(company='Learn Now') Allows inserting different company names in the receipt As we can see, it is quite simple. What We Learned In this tutorial, we explored how functions work in Python 3 and learned how to create and use them. We discussed built-in tools and examined an example of passing functions as objects to other functions. By studying functions more deeply, you will appreciate their usefulness even when writing relatively small applications.
02 April 2025 · 8 min to read
Python

Comments in Python 3

Comments in a program are parts of the code that are ignored by the interpreter or compiler. They are used to: Make the code more readable; Explain what the code does and why; Prevent parts of the code from executing during testing/execution; Leave notes about things that need to be done/modified/removed. Overall, comments are meant to make a programmer's life easier—they play no role for the computer. However, in some programming methodologies, such as extreme programming, it is believed that if code needs comments, then the code is poorly written. In this article, you will learn how to write comments in Python 3 and what Docstrings and PEP are. Comments in Python Different programming languages use different syntax for comments. Often, it's a double slash (//). In Python 3, comments in the code start with the # symbol. For example: # The code prints "Hello, World!" to the consoleprint("Hello, World!") You can also place a comment on the same line as the code: print("Hello, World!")  # The code prints "Hello, World!" to the console Comments should be useful to the reader. For example, this comment is not helpful: # This code clearly does somethingprint("Hello, World!") A good comment should explain or describe the code and its purpose. Some developers believe that comments should describe the programmer’s intent. In general, it is best to think of comments as a form of code documentation. If they are not useful, they should be removed. You can also use comments to disable parts of the code to prevent them from executing. This can be useful for testing and debugging. Suppose we need to comment the following code: db_lp = sqlite3.connect('login_password.db') cursor_db = db_lp.cursor() sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) db_lp.commit() cursor_db.close() db_lp.close() The goal of commenting is to make this block of code understandable. For example, we can comment it like this: db_lp = sqlite3.connect('login_password.db') # Creating the login_password database cursor_db = db_lp.cursor() # Cursor object for executing SQL queries # SQL query to create the "passwords" table in the database sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) # Executing the sql_create query db_lp.commit() # Committing changes # Closing the Cursor and database cursor_db.close() db_lp.close() Manually commenting Python code can be inconvenient. To format a block of code as single-line comments, you can use keyboard shortcuts: PyCharm: Ctrl + / Visual Studio Code: To comment/uncomment a line Ctrl + /, for a block of code Shift + Alt + A Eclipse: To comment/uncomment a line Ctrl + /, for a block of code Ctrl + Shift + / Visual Studio: Ctrl + K then Ctrl + C to comment a block of code, and Ctrl + K then Ctrl + U to uncomment it Docstring in Python A Docstring is a string literal placed immediately after the declaration of a module, function, class, or other structure. It is a convenient way to document code, making it accessible for reference. Docstrings were introduced in Python in 2001 and are described in PEP 257. What is PEP? Python's development follows a structured process involving creating, discussing, selecting, and implementing PEP (Python Enhancement Proposal) documents. PEPs contain proposals for language development, including new features, modifications to existing ones, etc. One of the most well-known and useful PEP documents is PEP 8, which outlines guidelines and conventions for writing Python code. If you plan to write in Python, familiarize yourself with these conventions. Since there are many rules, special tools exist to help enforce them. Some useful tools are listed below. Now, back to Docstring. A Docstring is the first statement in an object's declaration. Here’s an example: def function(x, y, z): """ Docstring of this function """ def inner_function(): """ Docstring of the nested function """ The syntax for a Docstring is three double quotes at the beginning and end. You can also use single quotes or fewer than three quotes, but PEP 257 recommends using three double quotes. You can access an object’s Docstring using the __doc__ method: def subtraction(a, b): """Function subtracts b from a""" return a - b print(subtraction.__doc__) Output: Function subtracts b from a You can also use the __doc__ property to get information about built-in Python methods, such as print: print(print.__doc__) Output: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. String literals placed anywhere in the Python code can also serve as documentation. The Python bytecode compiler will not recognize them, and they will not be accessible at runtime via __doc__. However, there are two additional types of Docstrings that can be extracted using documentation tools. Additional Docstrings Additional Docstrings are string literals ignored by the Python compiler but recognized by Docutils tools. They are placed immediately after a Docstring. Example: def function(arg): """This is the Docstring of this function. It will be available via __doc__.""" """ This is an additional Docstring. It will be ignored by the compiler but recognized by Docutils. """ pass Attribute Docstrings Attribute Docstrings are string literals placed immediately after a simple assignment at the module, class, or __init__ method level. Example: def f(x): """This is the Docstring of this function. It will be available via __doc__""" return x**2 f.a = 1 """ This is an Attribute Docstring for the attribute f.a, it will be ignored by the compiler but recognized by Docutils. """ Here are the main PEP 257 guidelines for using docstrings: Leave a blank line after all Docstrings. The script's Docstring should serve as a "usage message," potentially displayed to the user if incorrect arguments are provided. It should describe functionality, parameter syntax, environment variables, and files used. The module's Docstring should list important objects with a one-line explanation for each. The function/method Docstring should describe behavior, arguments, return values, possible exceptions, and constraints. The class Docstring should include methods, instance variables, and describe the class behavior. The constructor (__init__) should have its own separate Docstring. If a class is a subclass and mostly inherits behavior from a parent class, its documentation should mention this and describe any differences. Useful Tools Here are some tools to help with PEP 8 and comments in Python 3: pycodestyle — Checks if your code follows PEP 8. Black — Formats code according to PEP 8 (mostly). Doxygen, PyDoc, pdoc — Automatically generate documentation from Docstrings.
02 April 2025 · 6 min to read
Python

How to Install and Set Up PyTorch

PyTorch is a free, open-source deep learning library. With its help, a computer can detect objects, classify images, generate text, and perform other complex tasks. PyTorch is also a rich tool ecosystem that supports and accelerates AI development and research. In this article, we will cover only the basics: we will learn how to install PyTorch and verify that it works. To work with PyTorch, you will need: At least 1 GB of RAM. Installed Python 3 and pip.  A configured local development environment. Deep knowledge of machine learning is not required for this tutorial. It is assumed that you are familiar with basic Python terms and concepts. Installing PyTorch We will be working in a Windows environment but using the command line. This makes the tutorial almost universal—you can use the same commands on Linux and macOS. First, create a workspace where you will work with Torch Python. Navigate to the directory where you want to place the new folder and create it: mkdir pytorch Inside the pytorch directory, create a new virtual environment. This is necessary to isolate projects and, if needed, use different library versions. python3 -m venv virtualpytorch To activate the virtual environment, first go to the newly created directory: cd virtualpytorch Inside, there is a scripts folder (on Windows) or bin (on other OS). Navigate to it: cd scripts Activate the virtual environment using a bat file by running the following command in the terminal: activate.bat The workspace is now ready. The next step is to install the PyTorch library. The easiest way to find the installation command is to check the official website. There is a convenient form where you select the required parameters. As an example, install the stable version for Windows using CPU via pip. Select these parameters in the form, and you will get the necessary command: pip3 install torch torchvision torchaudio Copy and execute the pip install torch command in the Windows command line. You are also installing two sub-libraries: torchvision – contains popular datasets, model architectures, and image transformations for computer vision. torchaudio – a library for processing audio and signals using PyTorch, providing input/output functions, signal processing, datasets, model implementations, and application components. This is the standard setup often used when first exploring the library. The method described above is not the only way to install PyTorch. If Anaconda is installed on Windows, you can use its graphical interface. If your computer has NVIDIA GPUs, you can select the CUDA version instead of CPU. In that case, the installation command will be different. All possible local installation methods are listed in the official documentation. You can also find commands for installing older versions of the library there. To install them, just select the required version and install it the same way as the current package builds. You don't need to write a script to check if the library is working. The Python interpreter has enough capabilities to perform basic operations. If you have successfully installed PyTorch in the previous steps, then launching the Python interpreter won’t be an issue. Run the following command in the command line: python Then enter the following code: import torch x = torch.rand(5, 3) print(x) You should see an output similar to this: tensor([[0.0925, 0.3696, 0.4949], [0.0240, 0.2642, 0.1545], [0.7274, 0.4975, 0.0753], [0.4438, 0.9685, 0.5022], [0.4757, 0.6715, 0.4298]]) Now, you can move on to solving more complex tasks. PyTorch Usage Example To make learning basic concepts more engaging, let’s do it in practice. For example, let’s create a neural network using PyTorch that can recognize the digit shown in an image. Prerequisites To create a neural network, we need to import eight modules: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets All of these are standard PyTorch libraries plus Matplotlib. They handle image processing, optimization, neural network construction, and graph visualization. Loading and Transforming Data We will train the neural network on the MNIST dataset, which contains 70,000 images of handwritten digits. 60,000 images will be used for training. 10,000 images will be used for testing. Each image is 28 × 28 pixels. Each image has a label representing the digit (e.g., 1, 2, 5, etc.). train = datasets.MNIST("", train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform=transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) First, we divide the data into training and testing sets by setting train=True/False. The test set must contain data that the machine has not seen before. Otherwise, the neural network’s performance would be biased. Setting shuffle=True helps reduce bias and overfitting. Imagine that the dataset contains many consecutive "1"s. If the machine gets too good at recognizing only the digit 1, it might struggle to recognize other numbers. Shuffling the data prevents the model from overfitting specific patterns and ensures a more generalized learning process. Definition and Initialization of the Neural Network The next step is defining the neural network: class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() The neural network consists of four layers: one input layer, two hidden layers, and one output layer. The Linear type represents a simple neural network. For each layer, it is necessary to specify the number of inputs and outputs. The output number of one layer becomes the input for the next layer. The input layer has 784 nodes. This is the result of multiplying 28 × 28 (the image size in pixels). The first hidden layer has 86 output nodes, so the input to the next layer must be 86 as well.The same logic applies further. 86 is an arbitrary number—you can use a different value. The output layer contains 10 nodes because the images represent digits from 0 to 9. Each time data passes through a layer, it is processed by an activation function. There are several activation functions. In this example, we use ReLU (Rectified Linear Unit). This function returns 0 if the value is negative or the value itself if it is positive. The softmax function is used at the output layer to normalize values. For example, it might return an 80% probability that the digit in the image is 1, or a 30% probability that the digit is 5, and so on. The highest probability is selected as the final prediction. Training The next step is training. optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) The optimizer calculates the difference (loss) between the actual data and the prediction, adjusts the weights, recalculates the loss, and continues the cycle until the loss is minimized. Training Verification Here, we compare the actual values with the predictions made by the model. For this tutorial, the accuracy is high because the neural network effectively recognizes each digit. correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) To verify that the neural network works, pass it an image of a digit from the test set: plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) The output should display the digit shown in the provided image. Final Script Here’s the full script you can run to see how the neural network works: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets train = datasets.MNIST("", train=True, download=True, transform = transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform = transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) Each time we run the network, it will take a random image from the test set and analyze the digit depicted on it. After the process is completed, it will display the recognition accuracy in percentage, the image itself, and the digit recognized by the neural network. This is how it looks: Conclusion PyTorch is a powerful open-source machine learning platform that accelerates the transition from research prototypes to production deployments. With it, you can solve various tasks in the fields of artificial intelligence and neural networks. You don’t need deep knowledge of machine learning to begin working with PyTorch. It is enough to know the basic concepts to repeat and even modify popular procedures like image recognition to suit your needs. A big advantage of PyTorch is the large user community that writes tutorials and shares examples of using the library. Object recognition in images is one of the simplest and most popular tasks in PyTorch for beginners. However, the capabilities of the library are not limited to this. To create powerful neural networks, you need a lot of training data. These can be stored, for example, in an object-based S3 storage such as Hostman, with instant data access via API or web interface. This is an excellent solution for storing large volumes of information.
01 April 2025 · 10 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support