TensorFlow
neural networks
sentiment analysis
machine learning
phrase classification

Tensorflow Using neural network to classify positive or negative phrases

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Classifying text as positive or negative is one of the simplest useful natural-language tasks. In TensorFlow, the practical recipe is usually: convert text to tokens, map tokens to vectors, learn a compact sequence model, and output a single probability. You do not need a huge architecture to get a good baseline. A small TextVectorization plus embedding model is often enough to start.

Build a Minimal Sentiment Pipeline

For short phrases, the hardest part is usually not the neural network itself. It is turning variable-length text into a numeric form the network can consume.

TensorFlow's TextVectorization layer handles tokenization and vocabulary building inside the model pipeline. That keeps preprocessing consistent between training and inference.

Here is a small runnable example using a tiny in-memory dataset:

python
1import tensorflow as tf
2
3texts = tf.constant([
4    "I love this product",
5    "This is fantastic",
6    "Absolutely terrible experience",
7    "I hate it",
8    "Pretty good overall",
9    "Worst purchase ever",
10])
11labels = tf.constant([1, 1, 0, 0, 1, 0], dtype=tf.float32)
12
13vectorize = tf.keras.layers.TextVectorization(
14    max_tokens=1000,
15    output_mode="int",
16    output_sequence_length=8,
17)
18vectorize.adapt(texts)
19
20model = tf.keras.Sequential([
21    tf.keras.Input(shape=(1,), dtype=tf.string),
22    vectorize,
23    tf.keras.layers.Embedding(input_dim=1000, output_dim=16),
24    tf.keras.layers.GlobalAveragePooling1D(),
25    tf.keras.layers.Dense(16, activation="relu"),
26    tf.keras.layers.Dense(1, activation="sigmoid"),
27])
28
29model.compile(
30    optimizer="adam",
31    loss="binary_crossentropy",
32    metrics=["accuracy"],
33)
34
35model.fit(texts, labels, epochs=20, verbose=0)
36
37predictions = model.predict(tf.constant(["really good", "absolutely awful"]), verbose=0)
38print(predictions)

The final layer uses sigmoid, so outputs are probabilities between 0 and 1. Values closer to 1 mean positive sentiment.

Why This Architecture Works

For phrase-level sentiment, a strong baseline does not need a large recurrent model. The architecture above works because each stage has a specific job:

  • 'TextVectorization converts raw text into integer token IDs'
  • 'Embedding learns a dense vector for each token'
  • 'GlobalAveragePooling1D compresses the sequence into one fixed-size representation'
  • dense layers learn the final positive-versus-negative boundary

This setup is much easier to train and debug than jumping immediately to LSTMs or transformers. If the dataset is moderate and the phrases are short, it is often competitive enough to serve as a baseline.

Preparing Better Training Data

The model quality depends far more on data quality than on swapping one layer for another. For sentiment classification, your labels should reflect the exact task.

For example, these are not all equivalent:

  • product-review sentiment
  • movie-review sentiment
  • sarcasm detection
  • toxicity detection

A label set built for one domain rarely transfers cleanly to another. "Sick" may be positive in one domain and negative in another. That is why many disappointing sentiment models are actually data-definition problems, not TensorFlow problems.

When building a real dataset:

  • normalize the labeling rules first
  • split into train, validation, and test sets
  • watch for class imbalance
  • keep preprocessing identical between training and serving

Turning Probabilities Into Labels

After training, you usually choose a threshold to convert probabilities into positive or negative labels. A common default is 0.5, but that is only a default.

python
scores = model.predict(tf.constant(["not bad", "complete waste"]), verbose=0).flatten()
labels = ["positive" if score >= 0.5 else "negative" for score in scores]
print(list(zip(scores, labels)))

If false positives are expensive, raise the threshold. If false negatives are worse, lower it. This is a product decision as much as a modeling decision.

When to Use a More Complex Model

A simple embedding model is enough for many phrase tasks, but it has limits. It may struggle with long negation patterns, subtle sarcasm, or domain-specific phrasing.

Move to more advanced models when:

  • phrase order matters strongly
  • the dataset is large enough to justify extra capacity
  • baseline accuracy plateaus after label cleanup and tuning

At that point, an LSTM, 1D CNN, or transformer-based encoder may help. The key is to earn the extra complexity by first proving the baseline is the bottleneck.

Common Pitfalls

The most common mistake is overcomplicating the model before validating the dataset. A noisy label set will not be rescued by adding more layers.

Another mistake is fitting the tokenizer separately at training time and serving time. If the vocabulary changes, the model sees different token IDs and predictions become unreliable.

Developers also often ignore threshold selection. A model with decent probabilities can still perform poorly if the decision cutoff is mismatched to the use case.

Finally, do not judge the model by training accuracy alone. Sentiment datasets are often small, and overfitting can make the training metric look much better than real-world performance.

Summary

  • TensorFlow can handle sentiment classification with a compact text model.
  • 'TextVectorization plus embedding and pooling is a strong baseline for positive-versus-negative phrases.'
  • Good labels and consistent preprocessing matter more than adding architectural complexity early.
  • Use probabilities from the sigmoid output and choose a threshold deliberately.
  • Start simple, then move to larger sequence models only if the baseline is clearly the limiting factor.

Course illustration
Course illustration

All Rights Reserved.