Bird
Raised Fist0
TensorFlowml~5 mins

Sentiment analysis in TensorFlow

Choose your learning style10 modes available
Introduction

Sentiment analysis helps us understand if a text is happy, sad, or neutral. It is useful to know how people feel from their words.

Checking if customer reviews are positive or negative
Understanding if social media posts are happy or upset
Analyzing feedback to improve products or services
Sorting emails or messages by mood
Helping chatbots respond with the right emotion
Syntax
TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

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

The model uses an embedding layer to understand words as numbers.

The last layer uses sigmoid to predict positive (1) or negative (0) sentiment.

Examples
This example shows a simple sentiment model with 1000 words vocabulary and 100 words per input.
TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(1000, 16, input_length=100),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(24, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
Compile the model with loss for two classes and accuracy to check performance.
TensorFlow
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Sample Model

This program trains a small sentiment analysis model on simple sentences. It then predicts the sentiment of new sentences, showing the chance they are positive.

TensorFlow
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

# Sample data
sentences = [
    'I love this movie',
    'This film was terrible',
    'What a great day',
    'I hate this song',
    'This is awesome',
    'I do not like this',
    'Such a bad experience',
    'I am very happy',
    'This is disappointing',
    'I feel fantastic'
]
labels = [1, 0, 1, 0, 1, 0, 0, 1, 0, 1]  # 1=positive, 0=negative

# Tokenize and pad
vocab_size = 50
max_length = 5
tokenizer = Tokenizer(num_words=vocab_size, oov_token='<OOV>')
tokenizer.fit_on_texts(sentences)
sequences = tokenizer.texts_to_sequences(sentences)
padded = pad_sequences(sequences, maxlen=max_length, padding='post')

# Build model
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, 8, input_length=max_length),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

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

# Train model
history = model.fit(padded, labels, epochs=10, verbose=0)

# Predict
test_sentences = ['I love this', 'This is bad', 'I am happy', 'I do not like it']
test_seq = tokenizer.texts_to_sequences(test_sentences)
test_pad = pad_sequences(test_seq, maxlen=max_length, padding='post')
predictions = model.predict(test_pad)

for i, sentence in enumerate(test_sentences):
    print(f"'{sentence}' -> Positive sentiment probability: {predictions[i][0]:.4f}")
OutputSuccess
Important Notes

More data helps the model learn better feelings from text.

Padding makes all sentences the same length for the model.

Sigmoid output close to 1 means positive, close to 0 means negative.

Summary

Sentiment analysis finds if text is positive or negative.

Use embedding layers to turn words into numbers the model can understand.

Train with labeled examples and check accuracy to see how well it learns.

Practice

(1/5)
1. What is the main goal of sentiment analysis in TensorFlow?
easy
A. To translate text into another language
B. To classify text as positive or negative
C. To generate new text automatically
D. To count the number of words in a sentence

Solution

  1. Step 1: Understand sentiment analysis purpose

    Sentiment analysis is used to find the feeling or opinion in text, usually positive or negative.
  2. Step 2: Match goal with options

    Among the options, only classifying text as positive or negative matches sentiment analysis.
  3. Final Answer:

    To classify text as positive or negative -> Option B
  4. Quick Check:

    Sentiment analysis = classify positive/negative [OK]
Hint: Sentiment analysis = positive or negative text classification [OK]
Common Mistakes:
  • Confusing sentiment analysis with translation
  • Thinking it generates new text
  • Believing it only counts words
2. Which TensorFlow layer is commonly used to convert words into numbers for sentiment analysis?
easy
A. Dense layer
B. Flatten layer
C. Dropout layer
D. Embedding layer

Solution

  1. Step 1: Identify layer for word representation

    Embedding layers convert words into numeric vectors that models can understand.
  2. Step 2: Compare with other layers

    Dense layers are for fully connected neurons, Dropout prevents overfitting, Flatten reshapes data, none convert words to numbers.
  3. Final Answer:

    Embedding layer -> Option D
  4. Quick Check:

    Embedding layer = word to number conversion [OK]
Hint: Embedding layer turns words into numbers for models [OK]
Common Mistakes:
  • Choosing Dense layer for word conversion
  • Confusing Dropout with embedding
  • Using Flatten layer incorrectly
3. What will be the output shape of the embedding layer if input shape is (None, 10) and embedding dimension is 16?
medium
A. (None, 10, 16)
B. (None, 16, 10)
C. (10, 16)
D. (None, 10)

Solution

  1. Step 1: Understand embedding input and output shapes

    Input shape (None, 10) means batch size unknown, sequence length 10. Embedding outputs a vector per word with size 16.
  2. Step 2: Calculate output shape

    Output shape is (batch_size, sequence_length, embedding_dim) = (None, 10, 16).
  3. Final Answer:

    (None, 10, 16) -> Option A
  4. Quick Check:

    Embedding output shape = (batch, seq_len, embed_dim) [OK]
Hint: Embedding output shape = input length x embedding size [OK]
Common Mistakes:
  • Swapping sequence length and embedding dimension
  • Ignoring batch size dimension
  • Confusing input and output shapes
4. Identify the error in this TensorFlow model snippet for sentiment analysis:
model = tf.keras.Sequential([
  tf.keras.layers.Embedding(input_dim=1000, output_dim=16, input_length=10),
  tf.keras.layers.Dense(1, activation='sigmoid')
])
medium
A. Dense layer activation should be 'relu' for binary output
B. Embedding layer input_dim should be output_dim
C. Missing Flatten or GlobalPooling layer before Dense
D. input_length parameter is not allowed in Embedding layer

Solution

  1. Step 1: Analyze model layer sequence

    Embedding outputs 3D tensor (batch, seq_len, embed_dim). Dense expects 2D input, so flattening or pooling is needed.
  2. Step 2: Identify missing layer

    Without Flatten or GlobalPooling, Dense layer will raise shape error.
  3. Final Answer:

    Missing Flatten or GlobalPooling layer before Dense -> Option C
  4. Quick Check:

    Embedding output must be flattened before Dense [OK]
Hint: Add Flatten or pooling before Dense after Embedding [OK]
Common Mistakes:
  • Confusing input_dim and output_dim in Embedding
  • Using wrong activation for binary classification
  • Thinking input_length is invalid parameter
5. You want to improve a sentiment analysis model's accuracy on short reviews. Which approach is best?
hard
A. Increase embedding dimension and add a Bidirectional LSTM layer
B. Reduce embedding dimension and remove all recurrent layers
C. Use only a Dense layer without embedding
D. Train on fewer examples to avoid overfitting

Solution

  1. Step 1: Consider model improvements for short text

    Increasing embedding dimension helps capture more word features; Bidirectional LSTM captures context from both directions.
  2. Step 2: Evaluate other options

    Reducing embedding or removing recurrent layers loses information; training on fewer examples reduces learning quality.
  3. Final Answer:

    Increase embedding dimension and add a Bidirectional LSTM layer -> Option A
  4. Quick Check:

    More embedding + Bidirectional LSTM = better short text accuracy [OK]
Hint: Use bigger embeddings and bidirectional LSTM for short texts [OK]
Common Mistakes:
  • Thinking smaller embeddings improve accuracy
  • Removing recurrent layers harms context understanding
  • Training on fewer examples reduces model learning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard