1
Python Deep Learning in Practice: Building an LSTM Sentiment Analysis System from Scratch

2024-10-24

Hello, today I want to share a fascinating topic with you—how to implement a sentiment analysis system using Python. As a deep learning practitioner, I've noticed that many beginners face the same confusion: concepts are too abstract, and they don't know where to start. So today, we'll use the simplest language to step-by-step build a practical deep learning application.

Before we start, we need to understand a few key concepts. Don't be intimidated by these technical terms; they're actually quite easy to grasp.

Deep learning is like teaching computers to understand human thinking. Take the sentiment analysis we want to implement today: humans can easily judge whether a sentence is positive or negative because our brains have learned to understand the emotional tendencies in language. Deep learning aims to teach computers to do the same.

At this point, you might ask: how do computers understand text? This brings us to text vectorization. We need to convert text into numbers that computers can understand. It's like translating Chinese into English, except this time, we're translating text into numbers.

Before we officially begin, we need to prepare the development environment. The most common frameworks for Python deep learning are TensorFlow and PyTorch. In this project, we choose to use Keras (the high-level API of TensorFlow). Why Keras? Because its API design is very elegant and particularly suitable for beginners.

import numpy as np
from tensorflow import keras
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense


print(f"TensorFlow version: {tf.__version__}")

Data processing is one of the most important steps in machine learning. I often tell my students: Garbage In, Garbage Out. If the quality of the input data is poor, no matter how powerful the model is, it won't produce good results.

Let's illustrate the data processing process with a simple example:

texts = [
    "This movie is amazing",
    "The plot is very boring",
    "The actors' performances are great",
    "Completely a waste of time"
]
labels = [1, 0, 1, 0]  # 1 indicates positive, 0 indicates negative


tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(texts)


sequences = tokenizer.texts_to_sequences(texts)


padded_sequences = pad_sequences(sequences, maxlen=50)

Now comes the most exciting part—building the deep learning model. We will use LSTM (Long Short-Term Memory Network) to process text sequences. Why choose LSTM? Because it excels at handling sequential data and can "remember" long-term dependencies in text.

model = Sequential([
    Embedding(5000, 32, input_length=50),
    LSTM(64, return_sequences=True),
    LSTM(32),
    Dense(16, activation='relu'),
    Dense(1, activation='sigmoid')
])


model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


model.summary()

This model structure is the best solution I found after numerous experiments. Did you know? In actual work, finding the most suitable model structure often requires repeated trials. It's like cooking; you need to keep adjusting the proportion of ingredients to find the tastiest recipe.

Model training is a process of patience. I remember the first time I trained a model, watching the numbers on the terminal jump continuously, I was very anxious. This is normal; every deep learning engineer has gone through this stage.

history = model.fit(
    padded_sequences,
    np.array(labels),
    epochs=10,
    validation_split=0.2,
    batch_size=32
)

During training, I suggest paying special attention to the following indicators: 1. Training accuracy: how the model performs on the training data 2. Validation accuracy: how the model performs on new data 3. Loss value: reflects the stability of model training

With the theory covered, let's see how to apply this model to real-world scenarios. Suppose you want to analyze a new review:

def predict_sentiment(text):
    # Preprocess
    sequence = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(sequence, maxlen=50)

    # Predict
    prediction = model.predict(padded)
    return "Positive" if prediction[0] > 0.5 else "Negative"


test_text = "This product exceeded my expectations, very satisfied"
result = predict_sentiment(test_text)
print(f"Sentiment: {result}")

In actual projects, I've found a few tips particularly useful:

  1. Clean data carefully: remove unnecessary symbols, emoticons, and other interfering information
  2. Word vectors are important: you can use pre-trained Chinese word vectors, such as those open-sourced by Tencent
  3. Be patient with model tuning: tuning is a gradual process; don't expect to get the best results at once

Sentiment analysis technology is constantly advancing, especially with the development of large language models. We can expect more innovative applications, such as: - Emotion recognition in intelligent customer service systems - Public opinion analysis on social media - Automatic classification of product reviews

What other interesting application scenarios do you think exist? Feel free to discuss with me in the comments section.

Today we built a sentiment analysis system from scratch. Did you notice that deep learning isn't as difficult as imagined? The key is to master the correct learning methods, take it step by step, start with simple examples, and gradually transition to complex applications.

Remember, programming and deep learning are both arts of practice. Just watching without practicing won't work; I suggest you try this project yourself. I'm sure you'll gain new insights. If you encounter problems, feel free to reach out for discussion.

The journey of learning never ends; let's continue exploring this field full of challenges and opportunities together. Are you ready to start your deep learning journey?