---
title: Transfer Learning with Transformer Neural Networks
description: Learn how to leverage Hugging Face’s Transformer library for efficient NLP tasks like sentiment analysis and text summarization with minimal code.
image: https://blog.rheinwerk-computing.com/hubfs/Transfer%20Learning%20with%20Transformer%20Neural%20Networks.jpg
---

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://blog.rheinwerk-computing.com/) [Blog](https://blog.rheinwerk-computing.com)

- Books 
    - Get an overview of our wide selection of books on every relevant computing topic. 
          - [General Computing](https://www.sap-press.com/rheinwerk-computing/general-computing/)
          - [DevOps](https://www.sap-press.com/rheinwerk-computing/devops/)
          - [Programming Languages](https://www.sap-press.com/rheinwerk-computing/programming-languages/)
          - [Security](https://www.sap-press.com/rheinwerk-computing/security/)
          - [Software Development](https://www.sap-press.com/rheinwerk-computing/software-development/)
- Book Subscription 
    - Get unlimited access to all Rheinwerk Computing books! 
          - [Programming Subscription](https://sap-press.com/subscriptions/)

[Artificial Intelligence](https://blog.rheinwerk-computing.com/tag/artificial-intelligence)

# Transfer Learning with Transformer Neural Networks

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)  by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/rheinwerk-computing)

Transformer neural networks have revolutionized the field of natural language processing (NLP).

 

Instead of training large models from scratch, developers can now leverage transfer learning with pretrained transformer models to perform complex NLP tasks quickly and efficiently. In this post, we’ll explore how Hugging Face’s Transformer library makes it easy to implement text classification, sentiment analysis, summarization, and more—all with just a few lines of code.

 

## Key NLP Tasks Solved by Transformers

Some typical tasks in the field of *natural language processing* (NLP) are described in the following list:

- **Text classification:** Assignment of a text to one or more predefined categories, such as spam detection or sentiment analysis.
- **Machine translation:** Automatic translation of texts between different languages.
- **Text summary:** Automatic creation of a shorter version of a text that contains the essential content.
- **Question answering:** Answering questions based on a trained knowledge base.
- **Text generation:** Automatic creation of new texts based on *prompts*.

Using the *transformer* library from *Hugging Face*, we now demonstrate how we can use pretrained transformer networks for different tasks.

 

## Hugging Face: The Hub for Pretrained Models

*Hugging Face* is a company and an open source platform that originally specialized in NLP technologies. Today, Hugging Face is best known for its extensive library of pretrained models for all kinds of AI tasks, as well as tools for developing and implementing AI solutions. The main goal is to make the use of modern machine learning models more accessible to researchers and developers.

 

On this platform, you can find the following (and much more):

- A collection of datasets for training neural networks
- A central repository with thousands of pretrained models for various tasks
- A platform for trying out applications (here, referred to as *spaces*) with the help of user-friendly interfaces

## Getting Started with the Transformer Library

Before you start, you must install the necessary libraries. You should already be familiar with the installation in the Anaconda environment and in a Google Colab. In this listing, we specify the installation command in a Jupyter Notebook or Google Colab cell.

 

`!pip install transformers datasets evaluate accelerate`

## Example: Sentiment Analysis in Action

To access one of the models for a specific NLP task, we create a pipeline() instance and define the type of task. In the listing below, we show an example that performs a text classification—more precisely, a sentiment analysis. The aim is to classify a text in terms of its positivity or negativity.

 

`from transformers import pipeline`

`sentclassifier = pipeline("text-classification")`

`result= sentclassifier("I am very happy with this book on Neural Nets!")`

`print(result)`

`# Output:`

`([{'label': 'POSITIVE', 'score': 0.9998326301574707}]`

We’ve only specified the task here; the pipeline() function then uses a default transformer network, which is a *BERT transformer* in this example: distilbert/distilbertbase-uncased-finetuned-sst-2-english. However, this model only works for the English language.

 

You’ll also find models for other languages on Hugging Face, which you’ll then have to specify in more detail, as shown in this listing.

 

`from transformers import pipeline`

`sentclassifier = pipeline("text-classification", model=" nlptown/bert-basemultilingual-`

`uncased-sentiment",device="cuda")`

`result= sentclassifier("I am very happy with this book on neural networks!")`

`print(result)`

`# Output:`

`[{'label': '5 stars', 'score': 0.8118821382522583}]`

You can see that the output is slightly different, but the text is still rated positively. In addition, we’ve specified the device="cuda" parameter in the definition of sentclassifier so that the function also uses the GPU for sentiment analysis.

 

Of course, it’s also possible to transfer multiple sentences at the same time, as shown here.

 

`from transformers import pipeline`

`sentclassifier = pipeline("text-classification", model="nlptown/bert-basemultilingual-`

`uncased-sentiment",device="cuda")`

`results = sentclassifier(["I am very happy with this book on Neural Nets!", "I`

`don't like the topic at all!"])`

`print(results)`

`# Output:`

`[{'label': '5 stars', 'score': 0.7789900898933411}, {'label': '1 star', 'score':`

`0.673673152923584}]`

## Tokenization and Model Internals

Let’s now take a closer look at the internal processes. The first step is to transform text into tokens or token IDs. In the previous examples, this happens automatically, but now we want to see exactly what happens here in this listing.

 

`from transformers import pipeline`

`from transformers import AutoTokenizer, AutoModelForSequenceClassification`

`model_name = "nlptown/bert-base-multilingual-uncased-sentiment"`

`model = AutoModelForSequenceClassification.from_pretrained(model_name)`

`tokenizer = AutoTokenizer.from_pretrained(model_name)`

`sentclassifier = pipeline("sentiment-analysis", model=model, tokenizer=`

`tokenizer)`

We use the same model as shown earlier and take a look at what a token list looks like. The AutoTokenizer() function provides us with the tokens as it runs through the model. It’s important to note that all models have their own tokenizers and that these are usually different, as shown below.

 

`sequence = "I still like this book about neural networks!"`

`# Transformation to tokens`

`tokens = tokenizer.tokenize(sequence)`

`print("Tokens: ", tokens)`

`# Transformation in IDs`

`ids = tokenizer.convert_tokens_to_ids(tokens)`

`print("IDs: ", ids)`

`# Back-transformation of IDs into a sentence`

`decode_ids = tokenizer.decode(ids)`

`print("IDs for sentence: ", decode_ids)`

`# Output:`

`Tokens: ['I', 'still', 'like', 'this', 'book', 'about', 'neural', 'networks',`

`'!']`

`IDs: [151, 12440, 11531, 10372, 11768, 10935, 86165, 28310, 106]`

`IDs for sentence: i still like this book about neural networks!`

Here, you can see that not every word is in fact a token, but that some words are subdivided. Each token has its own ID, which is ultimately used. It’s also interesting that both the tokens and the back-transformed sentence are in lowercase. Why is that? Well, if we look at the name of the model used, bert-base-multilingual-uncased-sentiment, we read uncased, which means *case-insensitive*.

 

## Exploring the Hugging Face Model Hub

So how do we find the right model for our NLP tasks? To get started, we want to find a model that summarizes a text, so go to [https://huggingface.co/models](https://huggingface.co/models)to get an overview of all the models Hugging Face makes available to us.

 

![Model Selection on Hugging Face](https://blog.rheinwerk-computing.com/hs-fs/hubfs/image-png-Aug-27-2025-01-18-44-3039-PM.png?width=876&height=798&name=image-png-Aug-27-2025-01-18-44-3039-PM.png)

 

Then, we select the **Summarization** task under **Tasks** (shown in the bottom left of the above figure) and the **English** language under **Languages**. Let’s now summarize some text from the book *Programming Neural Networks with Python*.

 

We can select any model from the model selection. Models that have already been downloaded several times are ideal. This often indicates acceptable quality. We therefore select the facebook/bart-large-cnn model, which is based on the facebook/bartbase model and has been customized with a special dataset (shown below).

 

`from transformers import pipeline`

`summarizer = pipeline("summarization",model="facebook/bart-large-cnn", device=`

`"cuda")`

`text = "Having acquired the theoretical knowledge of Convolutional Neural Networks and Transformer Neural Networks in the previous chapter, we will now implement the tools using TensorFlow and the integrated Keras library. We first created our own network model to generate a classifier for the MNIST dataset. We then used (very complex and powerful) pre-trained Deep Neural Nets to apply them to tasks. On the one hand, we used a Convolutional Neural Net called Inception-v3, which is already very good at extracting essential features from an image. Secondly, we used the Hugging Face library to solve typical NLP tasks. This approach, also known as transfer learning, saves us the timeconsuming training of our own complex network."`

 

`summary = summarizer(text, min_length=10, max_length=100)`

`print(summary)`

`# Output:`

`Device set to use cuda`

`[{'summary_text': 'We first created our own network model to generate a`

`classifier for the MNIST dataset. We then used (very complex and powerful) pretrained`

`Deep Neural Nets to apply them to tasks. This approach, also known as`

`transfer learning, saves us the time-consuming training of our own complex`

`network.'}]`

Remember that the output of transformer neural networks doesn’t always have to be the same. So, you should expect to receive a different summary of the text.

 

## Conclusion

Transfer learning with Hugging Face Transformers allows developers to solve NLP tasks with minimal effort while still achieving state-of-the-art results. By using pretrained models, you save time, resources, and computing power while unlocking the ability to perform sentiment analysis, translation, summarization, and more. Whether you’re just starting out or refining advanced applications, Hugging Face’s ecosystem makes cutting-edge NLP accessible to everyone.

 

 Editor’s note: This post has been adapted from a section of the book *[Programming Neural Networks with Python](https://www.amazon.com/Programming-Neural-Networks-Python-Practical/dp/1493226967?maas=maas_adg_8E3A17FE2271C01E187F250FA2BC2FA6_afap_abs&ref_=aa_maas&tag=maas)* by [Joachim Steinwendner](https://www.linkedin.com/in/joachim-steinwendner-52979418/) and [Roland Schwaiger](https://www.linkedin.com/in/rolandschwaiger/). Dr. Steinwendner is a scientific project leader specializing in data science, machine learning, recommendation systems, and deep learning. Dr. Schwaiger is a software developer, freelance trainer, and consultant. He has a PhD in mathematics and he has spent many years working as a researcher in the development of artificial neural networks, applying them in the field of image recognition. 

This post was originally published 9/2025.

## Recommendation

[![Programming Neural Networks with Python](https://blog.rheinwerk-computing.com/hs-fs/hubfs/social-suggested-images/2696_2d-3.png?width=170&name=2696_2d-3.png)](https://www.amazon.com/Programming-Neural-Networks-Python-Practical/dp/1493226967?maas=maas_adg_8E3A17FE2271C01E187F250FA2BC2FA6_afap_abs&ref_=aa_maas&tag=maas)

**Your Neural Networks Starter Kit Is Here!**

Whether you're brand new to AI or ready to dive straight into code, this guide has you covered — from a beginner-friendly primer on Python and the math behind neural networks, to building, training, and refining simple, multi-layer, and deep networks. With practical examples spanning image classification, LLMs, and more, it's the hands-on foundation you need to work confidently with modern AI.

[Learn More](https://www.amazon.com/Programming-Neural-Networks-Python-Practical/dp/1493226967?maas=maas_adg_8E3A17FE2271C01E187F250FA2BC2FA6_afap_abs&ref_=aa_maas&tag=maas)

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)

**by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/rheinwerk-computing)**

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

[Artificial Intelligence](https://blog.rheinwerk-computing.com/tag/artificial-intelligence)

[![Share on facebook](https://7528309.fs1.hubspotusercontent-na1.net/hub/7528309/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/facebook-color.png?width=24&name=facebook-color.png)](https://www.facebook.com/share.php?u=https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Dfacebook) [![Share on linkedin](https://7528302.fs1.hubspotusercontent-na1.net/hub/7528302/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/linkedin-color.png?width=24&name=linkedin-color.png)](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Dlinkedin) [![Share on twitter](https://7528304.fs1.hubspotusercontent-na1.net/hub/7528304/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/twitter-color.png?width=24&name=twitter-color.png)](https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&source=tweetbutton&text=) [![Share on email](https://7528311.fs1.hubspotusercontent-na1.net/hub/7528311/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/email-color.png?width=24&name=email-color.png)](mailto:?subject=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Demail&body=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Ftransfer-learning-with-transformer-neural-networks%3Futm_medium%3Dsocial%26utm_source%3Demail)

### Comments

### Latest Blog Posts

[![A Beginner’s Guide to Transformer Neural Networks and Self-Attention in AI](https://blog.rheinwerk-computing.com/hs-fs/hubfs/A%20Beginner%E2%80%99s%20Guide%20to%20Transformer%20Neural%20Networks%20and%20Self-Attention%20in%20AI.jpg?height=600&name=A%20Beginner%E2%80%99s%20Guide%20to%20Transformer%20Neural%20Networks%20and%20Self-Attention%20in%20AI.jpg)](https://blog.rheinwerk-computing.com/a-beginners-guide-to-transformer-neural-networks-and-self-attention-in-ai)

[Featured](https://blog.rheinwerk-computing.com/tag/featured)

## [A Beginner’s Guide to Transformer Neural Networks and Self-Attention in AI](https://blog.rheinwerk-computing.com/a-beginners-guide-to-transformer-neural-networks-and-self-attention-in-ai)

[Read More](https://blog.rheinwerk-computing.com/a-beginners-guide-to-transformer-neural-networks-and-self-attention-in-ai)

[![How Do You Measure an Error in a Multilayer Network?](https://blog.rheinwerk-computing.com/hs-fs/hubfs/How%20Do%20You%20Measure%20an%20Error%20in%20a%20Multilayer%20Network_.jpg?height=600&name=How%20Do%20You%20Measure%20an%20Error%20in%20a%20Multilayer%20Network_.jpg)](https://blog.rheinwerk-computing.com/how-do-you-measure-an-error-in-a-multilayer-network)

[Artificial Intelligence](https://blog.rheinwerk-computing.com/tag/artificial-intelligence)

## [How Do You Measure an Error in a Multilayer Network?](https://blog.rheinwerk-computing.com/how-do-you-measure-an-error-in-a-multilayer-network)

[Read More](https://blog.rheinwerk-computing.com/how-do-you-measure-an-error-in-a-multilayer-network)

**Subscribe to our blog!**Get notified about future blog updates.

- <https://www.linkedin.com/showcase/rheinwerk-computing/>
- <https://www.youtube.com/channel/UChj-U-uWGM7qRlHXSFU93ew>
- <https://blog.rheinwerk-computing.com/rss.xml>

### The official Rheinwerk Computing Blog

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes resources that will help you accelerate your computing journey. The Rheinwerk Computing Blog is designed to provide helpful, actionable information on a variety of topics, including programming, administration, security, and analytics!

### Blog Topics

- [All Topics](https://blog.rheinwerk-computing.com)
- [Programming Languages](https://blog.rheinwerk-computing.com/tag/programming-languages)
- [How To](https://blog.rheinwerk-computing.com/tag/how-to)
- [JavaScript](https://blog.rheinwerk-computing.com/tag/javascript)
- [DevOps](https://blog.rheinwerk-computing.com/tag/devops)
- [Web Development](https://blog.rheinwerk-computing.com/tag/web-development)
- [Python](https://blog.rheinwerk-computing.com/tag/python)
- [What Is?](https://blog.rheinwerk-computing.com/tag/what-is)
- [Cybersecurity](https://blog.rheinwerk-computing.com/tag/cybersecurity)
- [Software Development](https://blog.rheinwerk-computing.com/tag/software-development)
- [Java](https://blog.rheinwerk-computing.com/tag/java)

### Blog curated by

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://www.sap-press.com/rheinwerk-computing/) [Visit Rheinwerk Computing Store](https://www.sap-press.com/rheinwerk-computing/)

### About

- [Home](https://sap-press.com/)
- [About Us](https://sap-press.com/the-publisher/)
- [Contact](mailto:info@rheinwerk-publishing.com)
- [Legal Notes](https://sap-press.com/legal-notes/)
- [Privacy Policy](https://sap-press.com/privacy/)
- [Terms of Use](https://sap-press.com/terms/)

© 2026 Rheinwerk Publishing, Inc. | Change Privacy Options

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Rheinwerk Computing",
    "url" : "https://blog.rheinwerk-computing.com/author/rheinwerk-computing"
  },
  "dateModified" : "2026-03-13T15:46:46.605Z",
  "datePublished" : "2025-09-10T13:00:00.000Z",
  "headline" : "Transfer Learning with Transformer Neural Networks",
  "image" : [ "https://blog.rheinwerk-computing.com/hubfs/Transfer%20Learning%20with%20Transformer%20Neural%20Networks.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.rheinwerk-computing.com/transfer-learning-with-transformer-neural-networks",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.rheinwerk-computing.com/hubfs/Logo-1.jpg"
    },
    "name" : "Rheinwerk Publishing, Inc."
  }
}
```