Sentiment analysis helps us understand if a text is happy, sad, or neutral. It is useful to know how people feel from their words.
Sentiment analysis in 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.
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')
])model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
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.
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}")
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.
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
Solution
Step 1: Understand sentiment analysis purpose
Sentiment analysis is used to find the feeling or opinion in text, usually positive or negative.Step 2: Match goal with options
Among the options, only classifying text as positive or negative matches sentiment analysis.Final Answer:
To classify text as positive or negative -> Option BQuick Check:
Sentiment analysis = classify positive/negative [OK]
- Confusing sentiment analysis with translation
- Thinking it generates new text
- Believing it only counts words
Solution
Step 1: Identify layer for word representation
Embedding layers convert words into numeric vectors that models can understand.Step 2: Compare with other layers
Dense layers are for fully connected neurons, Dropout prevents overfitting, Flatten reshapes data, none convert words to numbers.Final Answer:
Embedding layer -> Option DQuick Check:
Embedding layer = word to number conversion [OK]
- Choosing Dense layer for word conversion
- Confusing Dropout with embedding
- Using Flatten layer incorrectly
Solution
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.Step 2: Calculate output shape
Output shape is (batch_size, sequence_length, embedding_dim) = (None, 10, 16).Final Answer:
(None, 10, 16) -> Option AQuick Check:
Embedding output shape = (batch, seq_len, embed_dim) [OK]
- Swapping sequence length and embedding dimension
- Ignoring batch size dimension
- Confusing input and output shapes
model = tf.keras.Sequential([ tf.keras.layers.Embedding(input_dim=1000, output_dim=16, input_length=10), tf.keras.layers.Dense(1, activation='sigmoid') ])
Solution
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.Step 2: Identify missing layer
Without Flatten or GlobalPooling, Dense layer will raise shape error.Final Answer:
Missing Flatten or GlobalPooling layer before Dense -> Option CQuick Check:
Embedding output must be flattened before Dense [OK]
- Confusing input_dim and output_dim in Embedding
- Using wrong activation for binary classification
- Thinking input_length is invalid parameter
Solution
Step 1: Consider model improvements for short text
Increasing embedding dimension helps capture more word features; Bidirectional LSTM captures context from both directions.Step 2: Evaluate other options
Reducing embedding or removing recurrent layers loses information; training on fewer examples reduces learning quality.Final Answer:
Increase embedding dimension and add a Bidirectional LSTM layer -> Option AQuick Check:
More embedding + Bidirectional LSTM = better short text accuracy [OK]
- Thinking smaller embeddings improve accuracy
- Removing recurrent layers harms context understanding
- Training on fewer examples reduces model learning
Ready to prove it?
Pass the test and this pattern moves unknown
