Elena' s AI Blog

TensorFlow: Romancing with TensorFlow and NLP

11 Jul 2022 (updated: 24 Aug 2026) / 40 minutes to read

Elena Daehnhardt

Jasper AI-generated art, January 2023


TL;DR:
  • Use Tokenizer for text preprocessing, one-hot encoding for categorical text, Embedding layers for word vectors. Build LSTM/RNN models for sequence generation—essential for NLP tasks.

Previous: Part 18 — Decision Tree versus Random Forest, and Hyperparameter Optimisation

Next: Part 20 — Cross-Validation Techniques

What Is Natural Language Processing (NLP)?

Natural Language Processing (NLP) is a subfield of artificial intelligence that enables computers to preprocess, analyse, and generate human language in textual or voice form. NLP powers many automated tools: text translation, spell checking, search autocompletion, abstract generation, voice text messaging, messenger bots, chatbots, question-answering systems, and virtual assistants such as Amazon Alexa. You can use NLP tools to preprocess and analyse human language in textual or voice media and, to a certain extent, “understand” its meaning, intent, sentiment, or find named entities such as personal names or cities. I like this short definition of NLP from Wikipedia:

Natural language processing (NLP) is a subfield of linguistics, computer science, and artificial intelligence concerned with the interactions between computers and human language, mainly how to program computers to process and analyze large amounts of natural language data. The goal is a computer capable of “understanding” the contents of documents, including the contextual nuances of the language within them. The technology can then accurately extract information and insights in the papers and categorize and organize the documents themselves.

You can also use NLP for natural language generation. For instance, a poem generator that created this sonnet for me:

Ode to the Ocean

My square Ocean, you inspire me to write.
How I love the way you sings, talk and walk,
Invading my mind day and through the night,
Always dreaming about the good crosstalk.

Let me compare you to a direct sky?
You are more honest, dishonest and nice.
True drought dries the fond picnics of July,
And summertime has the attractive gneiss.

How do I love you? Let me count the ways.
I love your loving eyes, smile and kindness.
Thinking of your longing smile fills my days.
My love for you is the river blindness.

Now I must away with a perfect heart,
Remember my straight words whilst we're apart.

Not bad, is it? I don’t know exactly what algorithm is behind it. But we can do so much more with NLP in just a few lines of code. In this article I focus on the fundamentals that let us build our own poem generator — we might even out-write a few famous poets in a couple of minutes! Will these poems be any good? Let’s find out.

What are we going to do? How will we build our poem/sonnet generator? We can implement the poem generator in different ways, but for the beginning, we can follow this strategy:

  1. Firstly, we will need to have some existing text created by human writers. This text will be used to “teach” our program how to write poems. I suggest extracting text from The Project Gutenberg EBook of The Love Poems, by Émile Verhaeren. You can have your own choice; just change the web source to your liking. We will preprocess the extracted text by removing unnecessary parts, such as the beginning of the file and some concluding parts.
  2. Secondly, we tokenize text, thus converting it to numerical format, which is needed for neural networks.
  3. Next, we build the poem-generating model with the help of the TensorFlow library and Keras.
  4. Finally, we generate our love poem!

How did I come up with this solution? As you may have read in my previous posts, I follow the Udemy course “TensorFlow Developer Certificate in 2022: Zero to Mastery.” On the way, I go deeper into each topic and do a write-up in my blog posts, each of them having a complete Deep-learning application using the knowledge I get from the course and other resources I find online (used in the Reference section). In this post, I will focus on the chapter 08. “Natural Language Processing with TensorFlow.” I want to create a simple poem-generating model and also describe things I have learned on the way. Hopefully, it will be helpful for some of you and me to keep track of my learning process and do a bit more than merely digesting the ready material.

Text Vectorisation Concepts for NLP: One-Hot, Integer, and Word Embeddings

🔒 Subscribe to keep reading.

Python Code: Building a Poem Generator with TensorFlow and Keras

🔒 Subscribe to keep reading.

Final Thoughts: NLP Poem Generation with Keras

🔒 Subscribe to keep reading.

References

🔒 Subscribe to keep reading.

You've hit a Deep Dive tutorial.

I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):

Already a subscriber? Use the magic link from your last newsletter, or reset your password.

New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in — use the same email afterwards.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2022) 'TensorFlow: Romancing with TensorFlow and NLP', daehnhardt.com, 11 July 2022. Available at: https://daehnhardt.com/blog/2022/07/11/python-natural-language-processing-tensorflow-one-hot-encodings-tokenizer-sequence-modeling-word-embeddings/
All Posts