Machine Learning EN

How to build and train your own GPT model?

We have grown accustomed to the fact that large language models—such as those powering ChatGPT, Claude, or Gemini—can generate long, coherent, and generally accurate-sounding texts. Their high efficiency, combined with their ability to tap into vast repositories of data, has led to their widespread adoption; all signs point to their position only strengthening further in the future.

Have you ever wondered how such a model works? How does it know how to begin a statement, or which word should follow next in a sentence?

The goal of this project is to explore the fundamental mechanisms behind large language models and to train one ourselves, using a dataset consisting of literary Polish text.

What exactly is “GPT”? The acronym stands for “Generative Pre-trained Transformer.”

Generative – designed to produce new content (in this case, text).
Pre-trained – trained beforehand on a large dataset.
Transformer – based on a neural network architecture that utilizes an “attention” mechanism.

How will we approach this?

First, we will cover some theory—without getting bogged down in unnecessary complexity. Keeping in mind the rule of thumb that every mathematical formula included in a text halves the number of its readers, we will only skim over certain topics, dedicating the saved time to hands-on practice instead.

Another important factor is the hardware required, as well as the time invested in the entire process (which, when you think about it, is also a resource in itself). And this brings us to the central question: is it possible to train a model within a reasonable timeframe using an ordinary laptop?

The secret of the project lies in its scale—specifically, the volume of data and the number of configured parameters. Training an LLM (Large Language Model) comparable to those currently used commercially requires immense hardware resources; doing so on a single computer equipped with an NVIDIA graphics card could take hundreds of years. However, if we limit both the size of the model and the dataset—for instance, to a single novel—the training time can be reduced to a matter of hours or days.

Can this be achieved on an everyday laptop? We shall put it to the test.

For this project, we will use the novel In Desert and Wilderness (W pustyni i w puszczy) by Henryk Sienkiewicz as our dataset.

Why this particular book?

There are several reasons. First, I have access to a text version, which allows me to use it as training data.

Second, the choice of a Polish-language book is intentional. Many educational resources on GPT-based models rely on English-language examples. I wanted to see whether a small-scale model could learn at least some of the linguistic patterns and relationships inherent in the Polish language.

The novel features dialogues, descriptions of events, a variety of grammatical structures, and recurring character names. It also contains archaic phrasing. All of this makes for an intriguing experiment: will the model “absorb” this vocabulary, or will it fail to do so?

Where does the project’s source code come from?

It all began in the laboratories of OpenAI, where the GPT architecture was developed, leading to the unveiling of the GPT-2 model in 2019.

Sebastian Raschka, an artificial intelligence researcher, created his own implementation of the GPT-2 architecture. In his book, “Build a Large Language Model (From Scratch),” he describes how to train a custom model using Python and the PyTorch library. For his dataset, he used the English-language short story “The Verdict” by the American author Edith Wharton.

Taking a similar path, I—Karol 😉 decided to simplify things a bit. Instead of replicating every single implementation detail, I leveraged existing components from the PyTorch library to build my own, much smaller model—while still retaining the core mechanisms of the GPT architecture.

As we will explain later, these models rely on parameters. Ours will have over six million of them.

Wow! Six million parameters—that sounds truly impressive!

Does it?

Well… actually, no… 🙂

Compared to GPT-2 Small—which, despite the “Small” in its name, is built on approximately 124 million parameters—our model is quite tiny. But it’s more than enough for our purposes.

As mentioned, we will use Henryk Sienkiewicz’s book “In Desert and Wilderness” (W pustyni i w puszczy) as our training data. Since the original text is in the public domain, we can use it legally.

The primary goal of this project is to explore how a model—constructed from text fragments, numbers, and parameters—learns to predict the next parts of sentences.

Building Our Own GPT Model