Working with Text Data
Duration: 7 min
This module covers the essential techniques for working with text data using TensorFlow and Keras. We'll explore text preprocessing, tokenization, and how to build neural network models for text classification and sequence generation. Understanding these techniques is crucial for developing applications like sentiment analysis, chatbots, and text summarization.
Text Preprocessing and Tokenization
Text preprocessing is a critical step in preparing text data for machine learning models. It involves cleaning the text, removing stop words, and converting text into a numerical format that models can understand. Tokenization is the process of splitting text into individual words or tokens. TensorFlow and Keras provide tools to perform these tasks efficiently.
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
# Sample text data
texts = ['TensorFlow is great for machine learning.', 'Keras makes deep learning easy!']
# Create a tokenizer
tokenizer = Tokenizer(num_words=100)
# Fit the tokenizer on the texts
tokenizer.fit_on_texts(texts)
# Convert texts to sequences
sequences = tokenizer.texts_to_sequences(texts)
print(sequences)[[1, 2, 3, 4], [5, 6, 7, 8]]Building a Text Classification Model
Once the text data is preprocessed and tokenized, we can build a neural network model for text classification. Common architectures include using embedding layers, convolutional neural networks (CNNs), and recurrent neural networks (RNNs). In this example, we'll build a simple model using an embedding layer and an LSTM layer for binary text classification.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
# Sample preprocessed data
sequences = [[1, 2, 3, 4], [5, 6, 7, 8]]
labels = [1, 0]
# Padding sequences to ensure uniform length
from tensorflow.keras.preprocessing.sequence import pad_sequences
padded_sequences = pad_sequences(sequences, maxlen=5)
# Building the model
model = Sequential()
model.add(Embedding(input_dim=100, output_dim=8, input_length=5))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))
# Compiling the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Training the model
model.fit(padded_sequences, labels, epochs=5)💡 Tip: When working with text data, ensure that your sequences are padded to a uniform length to avoid issues during model training.
❓ What is the purpose of tokenization in text preprocessing?
❓ Which layer is commonly used in text classification models to capture sequential information?