INNER CODE UNIT · Python

classify_sentiment

barissayil/SentimentAnalysis · analyzer.py:123

    def classify_sentiment(self, text):
        # Don't track gradient.
        with torch.no_grad():
            # Tokens are made up of CLS token, text converted to tokens, and SEP token.
            tokens = ["[CLS]"] + self.tokenizer.tokenize(text) + ["[SEP]"]
            # Convert tokens to input IDs; convert them to tensor, unsqueeze, put it to device.
            input_ids = (
                torch.tensor(self.tokenizer.convert_tokens_to_ids(tokens))
                .unsqueeze(0)
                .to(self.device)
            )
            # Create attention mask from input IDs.
            attention_mask = (input_ids != 0).long()
            # Get logit (log-odds) of sentiment being positive from the model.
            positive_logit = self.model(
                input_ids=input_ids, attention_mask=attention_mask
            )
            # Convert the logit to a probability.

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…