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:
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:
- '
TextVectorizationconverts raw text into integer token IDs' - '
Embeddinglearns a dense vector for each token' - '
GlobalAveragePooling1Dcompresses 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.
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.
- '
TextVectorizationplus 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.

