dream
81 строка · 3.2 Кб
1# Copyright 2017 Neural Networks and Deep Learning lab, MIPT
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import logging
16from os import getenv
17from typing import List, Union
18
19import sentry_sdk
20from bert_dp.preprocessing import InputFeatures
21from overrides import overrides
22
23from deeppavlov.core.common.registry import register
24from deeppavlov.models.bert.bert_classifier import BertClassifierModel
25
26sentry_sdk.init(getenv("SENTRY_DSN"))
27
28logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
29logger = logging.getLogger(__name__)
30
31
32@register("emotion_classification")
33class BertFloatClassifierModel(BertClassifierModel):
34"""
35Bert-based model for text classification with floating point values
36
37It uses output from [CLS] token and predicts labels using linear transformation.
38
39"""
40
41all_columns = ["anger", "fear", "joy", "love", "sadness", "surprise", "neutral"]
42used_columns = all_columns # ["neutral", "very_positive", "very_negative"]
43
44# map2base_sentiment = [] # {"neutral": "neutral", "very_positive": "positive", "very_negative": "negative"}
45
46def __init__(self, **kwargs) -> None:
47super().__init__(**kwargs)
48# FOR INIT GRAPH when training was used the following loss function
49# we have multi-label case
50# some classes for some samples are true-labeled as `-1`
51# we should not take into account (loss) this values
52# self.y_probas = tf.nn.sigmoid(logits)
53# chosen_inds = tf.not_equal(one_hot_labels, -1)
54#
55# self.loss = tf.reduce_mean(
56# tf.nn.sigmoid_cross_entropy_with_logits(labels=one_hot_labels, logits=logits)[chosen_inds])
57
58@overrides
59def __call__(self, features: List[InputFeatures]) -> Union[List[int], List[List[float]]]:
60"""
61Make prediction for given features (texts).
62
63Args:
64features: batch of InputFeatures
65
66Returns:
67predicted classes or probabilities of each class
68
69"""
70logging.info(features)
71input_ids = [f.input_ids for f in features]
72input_masks = [f.input_mask for f in features]
73input_type_ids = [f.input_type_ids for f in features]
74
75feed_dict = self._build_feed_dict(input_ids, input_masks, input_type_ids)
76if not self.return_probas:
77pred = self.sess.run(self.y_predictions, feed_dict=feed_dict)
78else:
79pred = self.sess.run(self.y_probas, feed_dict=feed_dict)
80batch_predictions = [{column: prob for column, prob in zip(self.used_columns, curr_pred)} for curr_pred in pred]
81return batch_predictions
82