NLP
Transformers for NLP: My Public Sentiment Analysis Project
Krishna Mishra explains transformer text classification through his public movie-sentiment code and model, with lessons for evaluating language systems in Nepal.
A concrete starting point for understanding transformers
My public Movie Sentiment Analysis using Transformer repository and published sentiment model are part of my NLP work. I am Krishna Mishra, founder of AI Automation Nepal in Kathmandu, Nepal. This article uses that project as a starting point for understanding text classification: turning a piece of text into a defined label, rather than generating an open-ended answer.
A transformer uses attention to build representations that account for relationships within its input. The architecture can support different tasks depending on its training and surrounding components. A sentiment classifier and a conversational assistant can therefore involve transformers while exposing very different outputs. Inspect the linked project for its implementation rather than assuming it behaves like a general chatbot.
Keep the text preparation and model together
Tokenisation converts the input into units the model can process. The tokenizer, vocabulary, special tokens and input-length handling belong to the model interface. Replacing those components casually can change the prediction even when the model weights remain the same. Save the preprocessing configuration alongside the artifact used for inference.
A useful test includes an empty input, a short review, a long review and punctuation or spelling variations. Decide what happens when text is truncated. If a review begins positively and ends with a complaint, losing the ending can affect the result. The interface should explain relevant input limits rather than silently presenting every prediction as equally dependable.
Evaluate what the label actually means
Sentiment is not the same as purchase intent, satisfaction with every product feature or a confirmed business outcome. Test mixed opinions, negation and sarcasm. Inspect errors by class and compare with a simple baseline. A score from one movie-review dataset does not establish reliability on customer messages from a different industry.
Nepali, English and Romanised Nepali also need separate attention. My public movie-sentiment artifact is not proof of a validated multilingual customer-support system. For a Nepal-focused application, collect appropriate evaluation examples, document labels and check language-specific failures before making performance claims.
Understand attention and the choice of architecture
My learning notes also explore positional information, self-attention, multi-head attention, residual connections and normalisation. Together these explain more than the model’s brand name: tokens need information about position, and attention mixes information from relevant positions. The precise arrangement depends on the architecture; a decoder-only model does not require the encoder cross-attention of an encoder-decoder system.
Encoder models are commonly used for representation and classification, decoder models for causal generation, and encoder-decoder models for sequence-to-sequence tasks. These are useful distinctions, not a rule that only one architecture is allowed or that every task has one universally best model. Start with the task and compare suitable baselines.
Separate feature extraction from fine-tuning
With a frozen pretrained backbone, a downstream classifier can learn from extracted representations. Fine-tuning updates some or all of the model parameters for the task. Layer freezing, learning rate, batch size and early stopping are experiment choices to evaluate; a fixed row-count threshold does not guarantee which approach will work better.
Keep the tokenizer and label mapping aligned with the checkpoint. Preserve held-out examples and review class-level errors before and after adaptation. My notes cover these training concepts, while the linked public repository is the evidence for this particular sentiment project. They do not establish that I have pretrained a foundation model from scratch.
Follow the tensors through a classification model
For a batch of text, tokenisation typically produces token IDs and an attention mask. The model maps token IDs to embeddings and combines them with position information. In self-attention, learned projections produce queries, keys and values. Scaled dot-product attention can be written as softmax(QKᵀ / √dₖ)V: query-key similarity determines how value vectors are mixed, with the mask excluding positions that should not participate.
Multiple heads allow several learned projections to operate in parallel; their outputs are combined before the following transformations. Residual connections carry information across sublayers, and normalisation supports stable computation. The exact normalisation order and position mechanism vary by architecture. This explains the general pattern rather than claiming that every model has the original paper’s configuration.
For sequence classification, a task head converts a suitable sequence representation into class logits. Training compares those logits with the labels using an appropriate loss. Inference maps the output back to the documented label order. Token IDs, padding mask, checkpoint configuration and label mapping all belong to the same reproducible prediction path.
Design a fine-tuning experiment before running it
Begin with the task, label policy and split. Remove or group related examples so duplicates do not cross evaluation boundaries. Establish a baseline and test the pretrained checkpoint under its intended preprocessing. Then choose the trainable parameters, learning rate and training budget. Compare experiments using the same development criteria instead of selecting a model from its training accuracy.
Monitor training and validation behaviour, save the selected checkpoint and evaluate the final choice once on the reserved test. Keep class-level errors, input-length effects and inference latency in the report. If a run is interrupted, resuming training may require optimiser and scheduler state in addition to model weights. Record what was actually saved rather than assuming every checkpoint is a complete training recovery.
Frozen features, partial adaptation and full fine-tuning have different resource and data trade-offs. No fixed dataset-size rule makes one universally superior. My public sentiment artifact supports a concrete project reference; the experiment design here is guidance for assessing a new task, not a claim that every variant has been benchmarked in my repository.
A worked review of a mixed-sentiment input
Consider the invented review: The picture quality is excellent, but the battery stopped working after a week. A single overall sentiment label compresses two different opinions. If the business needs separate product-aspect feedback, a binary overall classifier is not automatically the right output. Define whether the task concerns the whole review or a particular aspect before assigning labels.
For a test like this, record the intended annotation, the tokenizer’s input length, the predicted label and the model version. Compare a short negative sentence, its negated version and a longer mixed review. If the results change unexpectedly, inspect truncation, preprocessing and the label mapping before assuming that more training epochs will solve the problem.
Keep model confidence separate from calibrated probability. A high classifier score does not prove the opinion is unambiguous or the text belongs to the training domain. A reviewed threshold for sending uncertain inputs to a person can be more useful than forcing every input into an apparently definitive answer.
Connect a prediction to a reviewable workflow
As an illustrative business design, a classifier could suggest which feedback needs a person’s attention. Keep the original message accessible to the authorised reviewer and record the model version. A label should assist a decision rather than silently erase a complaint or approve an important action.
Through AI Automation Nepal, I connect discussions about models with the software and workflows around them. Start by reviewing the public repository and model below, then define the actual data, decision and acceptance test for a new requirement. Public project evidence shows the work available to inspect; it does not establish independent certification or a guaranteed result.