dream

Форк
0
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

15
import logging
16
from os import getenv
17
from typing import List, Union
18

19
import sentry_sdk
20
from bert_dp.preprocessing import InputFeatures
21
from overrides import overrides
22

23
from deeppavlov.core.common.registry import register
24
from deeppavlov.models.bert.bert_classifier import BertClassifierModel
25

26
sentry_sdk.init(getenv("SENTRY_DSN"))
27

28
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
29
logger = logging.getLogger(__name__)
30

31

32
@register("emotion_classification")
33
class BertFloatClassifierModel(BertClassifierModel):
34
    """
35
    Bert-based model for text classification with floating point values
36

37
    It uses output from [CLS] token and predicts labels using linear transformation.
38

39
    """
40

41
    all_columns = ["anger", "fear", "joy", "love", "sadness", "surprise", "neutral"]
42
    used_columns = all_columns  # ["neutral", "very_positive", "very_negative"]
43

44
    # map2base_sentiment = []  # {"neutral": "neutral", "very_positive": "positive", "very_negative": "negative"}
45

46
    def __init__(self, **kwargs) -> None:
47
        super().__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
59
    def __call__(self, features: List[InputFeatures]) -> Union[List[int], List[List[float]]]:
60
        """
61
        Make prediction for given features (texts).
62

63
        Args:
64
            features: batch of InputFeatures
65

66
        Returns:
67
            predicted classes or probabilities of each class
68

69
        """
70
        logging.info(features)
71
        input_ids = [f.input_ids for f in features]
72
        input_masks = [f.input_mask for f in features]
73
        input_type_ids = [f.input_type_ids for f in features]
74

75
        feed_dict = self._build_feed_dict(input_ids, input_masks, input_type_ids)
76
        if not self.return_probas:
77
            pred = self.sess.run(self.y_predictions, feed_dict=feed_dict)
78
        else:
79
            pred = self.sess.run(self.y_probas, feed_dict=feed_dict)
80
        batch_predictions = [{column: prob for column, prob in zip(self.used_columns, curr_pred)} for curr_pred in pred]
81
        return batch_predictions
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.