{"id":410949,"date":"2024-06-29T21:41:10","date_gmt":"2024-06-29T21:41:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=410949"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=410949","title":{"rendered":"<span>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 (NLP). \u041b\u0438\u0447\u043d\u044b\u0439 \u043e\u043f\u044b\u0442 \u2014 \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a BERT<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/17a\/b9c\/5ce\/17ab9c5ceb33fbea072e23dfba027d0d.jpg\" width=\"1000\" height=\"667\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/17a\/b9c\/5ce\/17ab9c5ceb33fbea072e23dfba027d0d.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>BERT\u00a0\u2014 Bidirectional Encoder Representations from Transformers<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u0442\u0430\u043a\u043e\u0435 BERT, \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u0434\u043b\u044f\u00a0\u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f\u00a0\u2014 \u0432\u00a0\u0441\u0435\u0442\u0438 \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e\u00a0\u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u043f\u044b\u0442\u00a0\u2014 \u043a\u0430\u043a\u00a0\u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0443\u00a0\u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c BERT \u0441\u00a0\u0447\u0438\u0441\u0442\u043e\u0433\u043e Colab \u043f\u043e\u00a0\u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f\u043c.<\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/h2>\n<p>\u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445:<\/p>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/486158\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044f\u00a0\u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0439\u00a0\u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0439\u00a0\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u00a0(seq2seq\u00a0\u043c\u043e\u0434\u0435\u043b\u0438\u00a0\u0441\u00a0\u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c\u00a0\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f)<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/486358\/\" rel=\"noopener noreferrer nofollow\">Transformer\u00a0\u0432\u00a0\u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/487358\/\" rel=\"noopener noreferrer nofollow\">BERT,\u00a0ELMO\u00a0\u0438\u00a0\u041a\u043e\u00a0\u0432\u00a0\u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445\u00a0(\u043a\u0430\u043a\u00a0\u0432\u00a0NLP\u00a0\u043f\u0440\u0438\u0448\u043b\u043e\u00a0\u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u043e\u0435\u00a0\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435)<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/498144\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u0448\u00a0\u043f\u0435\u0440\u0432\u044b\u0439\u00a0BERT:\u00a0\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435\u00a0\u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e<\/a><br \/><a href=\"https:\/\/colab.research.google.com\/github\/jalammar\/jalammar.github.io\/blob\/master\/notebooks\/bert\/A_Visual_Notebook_to_Using_BERT_for_the_First_Time.ipynb\" rel=\"noopener noreferrer nofollow\">A Visual Notebook to Using BERT for the First TIme.ipynb<\/a><\/p>\n<h2>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430<\/h2>\n<p>\u041a\u043e\u0434 \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438\u0437\u043b\u043e\u0436\u0435\u043d\u043e \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445.<\/p>\n<pre><code># \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b !pip install transformers <\/code><\/pre>\n<pre><code># \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438  import numpy as np import pandas as pd import torch import transformers as ppb  from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.model_selection import GridSearchCV from sklearn.model_selection import cross_val_score  import warnings warnings.filterwarnings('ignore')<\/code><\/pre>\n<pre><code># \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c dataset df = pd.read_csv('https:\/\/github.com\/clairett\/pytorch-sentiment-classification\/raw\/master\/data\/SST2\/train.tsv', delimiter='\\t', header=None)<\/code><\/pre>\n<pre><code># \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0438 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 batch_1 = df[:2000]<\/code><\/pre>\n<pre><code># \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432\u0441\u0435 \u043e\u043a print(batch_1[:5])<\/code><\/pre>\n<pre><code># \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u044b  model_class, tokenizer_class, pretrained_weights = (ppb.DistilBertModel, ppb.DistilBertTokenizer, 'distilbert-base-uncased') tokenizer = tokenizer_class.from_pretrained(pretrained_weights) model = model_class.from_pretrained(pretrained_weights)<\/code><\/pre>\n<pre><code># \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0438\u0440\u0443\u0435\u043c  tokenized = batch_1[0].apply((lambda x: tokenizer.encode(x, add_special_tokens=True)))<\/code><\/pre>\n<pre><code># \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430\u044f \u0434\u043b\u0438\u043d\u0430  max_len = 0 for i in tokenized.values:     if len(i) > max_len:         max_len = len(i)  padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])<\/code><\/pre>\n<pre><code># \u043c\u0430\u0441\u043a\u0438\u0440\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f attention_mask = np.where(padded != 0, 1, 0)<\/code><\/pre>\n<pre><code># \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0442\u043e\u043a\u0435\u043d\u043e\u0432  input_ids = torch.tensor(padded)   attention_mask = torch.tensor(attention_mask)  with torch.no_grad():     last_hidden_states = model(input_ids, attention_mask=attention_mask)<\/code><\/pre>\n<pre><code># \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0443\u044e \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0438 labels = batch_1[1] train_features, test_features, train_labels, test_labels = train_test_split(features, labels)<\/code><\/pre>\n<pre><code># \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 \u043d\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 lr_clf = LogisticRegression() lr_clf.fit(train_features, train_labels)<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u044b.<\/p>\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/18WwT4uTn3IlURWkjv5iuaYJaFzCNZpje\" rel=\"noopener noreferrer nofollow\"><u>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0431\u043b\u043e\u043a\u043d\u043e\u0442 Colab<\/u><\/a><\/p>\n<h2>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code># \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0430, \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u0442\u0440\u0438\u043a\u0438  print('# train:',lr_clf.score(train_features, train_labels)) print('# test:',lr_clf.score(test_features, test_labels))<\/code><\/pre>\n<p># train: 0.906 <br \/># test: 0.844  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u00ab\u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e\u00bb, \u0447\u0442\u043e\u00a0\u0443\u00a0\u043d\u0430\u0441 \u0432\u00a0\u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0438\u0437\u00a0\u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430, \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u0441\u0430\u043c\u0438 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u0432 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<pre><code># \u0440\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 print(batch_1[:5])<\/code><\/pre>\n<p>0  a stirring , funny and finally transporting re&#8230;  1 <br \/>1  apparently reassembled from the cutting room f&#8230;  0 <br \/>2  they presume their audience wo n&#8217;t sit still f&#8230;  0 <br \/>3  this is a visually stunning rumination on love&#8230;  1 <br \/>4  jonathan parker &#8216;s bartleby should have been t&#8230;  1  <\/p>\n<pre><code># \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0430\u043c\u0438 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c  def LR(x): return (1 if x > 0 else 0) def correct(LR, label): return True if LR - label == 0 else False  sum_false = 0 N = 5  for i in range (N):   if i and i % 100 == 0: print(i)   array = np.array(tokenized[i])   array = np.pad(array, (0, max_len - len(array)), 'constant')   attention_mask_this = np.where(array != 0, 1, 0)   input_ids_this = torch.tensor([array])     attention_mask_this = torch.tensor(attention_mask_this)   with torch.no_grad():       last_hidden_states_this = model(input_ids_this, attention_mask=attention_mask_this)   features_this = last_hidden_states_this[0][:,0,:].numpy()    sum = np.dot(features_this,lr_clf.coef_[0])     if N &lt; 20: print(i, labels[i], LR(sum), correct(LR(sum), labels[i]), sum)     if correct(LR(sum), labels[i]) == True: sum_false += 1  print() print(sum_false\/(i+1))<\/code><\/pre>\n<p>0 1 1 True [3.85857511] <br \/>1 0 0 True [-6.51904703] <br \/>2 0 0 True [-2.21694384] <br \/>3 1 1 True [3.65850942] <br \/>4 1 0 False [-0.32528463] <\/p>\n<p>0.8<\/p>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e 1\u00a0\u043e\u0448\u0438\u0431\u043a\u0430 \u0438\u0437 5, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441\u00a0\u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<br \/>\u041c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0442\u0440\u043e\u043a, \u043c\u0435\u043d\u044f\u044f N, \u0438 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u044b\u0432\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441\u00a0\u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u043e\u0439 \u0432\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0447\u0442\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043d\u044f\u0442\u0430 \u0438 \u00ab\u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043d\u0430\u00bb \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e. <\/p>\n<h2>\u0410\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0439 \u0442\u0435\u043a\u0441\u0442<\/h2>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/14d\/a11\/914\/14da11914834596fe05bd47d59f8af89.jpg\" width=\"480\" height=\"300\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/14d\/a11\/914\/14da11914834596fe05bd47d59f8af89.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0442\u0435\u043a\u0441\u0442\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0438 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u043e\u0446\u0435\u043d\u043a\u0443 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u0432.<\/p>\n<pre><code># \u0437\u0430\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b  texts = [     'All is good',      'it is so bad',      'nice to meet you',     'it is so rainy',     'he is a stupid',     'I like my car',     'have a nice day'         ]<\/code><\/pre>\n<pre><code># \u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b  for text in texts:   array = tokenizer.encode(text, add_special_tokens=True)   array = np.pad(array, (0, max_len - len(array)), 'constant')   attention_mask_this = np.where(array != 0, 1, 0)   input_ids_this = torch.tensor([array])     attention_mask_this = torch.tensor(attention_mask_this)   with torch.no_grad():       last_hidden_states_this = model(input_ids_this, attention_mask=attention_mask_this)   features_this = last_hidden_states_this[0][:,0,:].numpy()    sum = np.dot(features_this,lr_clf.coef_[0])     print(text, ':', sum, ':', LR(sum)) <\/code><\/pre>\n<p>All is good : [1.90097556] : 1 <br \/>it is so bad : [-1.86999172] : 0 <br \/>nice to meet you : [4.96587936] : 1 <br \/>it is so rainy : [-2.53429642] : 0 <br \/>he is a stupid : [-2.4318853] : 0 <br \/>I like my car : [0.2118134] : 1 <br \/>have a nice day : [2.30864912] : 1  <\/p>\n<p>1 &#8212; \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u043e<br \/>0 &#8212; \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e<\/p>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/2cc\/2ee\/a66\/2cc2eea666c665c1086f7cc06a594a7f.jpg\" width=\"320\" height=\"320\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2cc\/2ee\/a66\/2cc2eea666c665c1086f7cc06a594a7f.jpg\" data-blurred=\"true\"\/><\/figure>\n<h2>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h2>\n<p>\u0412\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430\u00a0\u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439\/\u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0439.<\/p>\n<p>\u041d\u0430\u00a0\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f\u00a0\u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430.<\/p>\n<hr\/>\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/18WwT4uTn3IlURWkjv5iuaYJaFzCNZpje\" rel=\"noopener noreferrer nofollow\"><u>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0431\u043b\u043e\u043a\u043d\u043e\u0442 Colab<\/u><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/718352\/\"> https:\/\/habr.com\/ru\/articles\/718352\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>BERT\u00a0\u2014 Bidirectional Encoder Representations from Transformers<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u00a0\u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u00a0\u0442\u043e\u043c, \u0447\u0442\u043e\u00a0\u0442\u0430\u043a\u043e\u0435 BERT, \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u0434\u043b\u044f\u00a0\u0447\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f\u00a0\u2014 \u0432\u00a0\u0441\u0435\u0442\u0438 \u043e\u0431\u00a0\u044d\u0442\u043e\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438.<\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e\u00a0\u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u043f\u044b\u0442\u00a0\u2014 \u043a\u0430\u043a\u00a0\u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0443\u00a0\u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c BERT \u0441\u00a0\u0447\u0438\u0441\u0442\u043e\u0433\u043e Colab \u043f\u043e\u00a0\u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f\u043c.<\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/h2>\n<p>\u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u043b\u043e\u0441\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445:<\/p>\n<p><a href=\"https:\/\/habr.com\/ru\/post\/486158\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044f\u00a0\u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0439\u00a0\u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0439\u00a0\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u00a0(seq2seq\u00a0\u043c\u043e\u0434\u0435\u043b\u0438\u00a0\u0441\u00a0\u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c\u00a0\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f)<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/486358\/\" rel=\"noopener noreferrer nofollow\">Transformer\u00a0\u0432\u00a0\u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/487358\/\" rel=\"noopener noreferrer nofollow\">BERT,\u00a0ELMO\u00a0\u0438\u00a0\u041a\u043e\u00a0\u0432\u00a0\u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445\u00a0(\u043a\u0430\u043a\u00a0\u0432\u00a0NLP\u00a0\u043f\u0440\u0438\u0448\u043b\u043e\u00a0\u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u043e\u0435\u00a0\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435)<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/post\/498144\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u0448\u00a0\u043f\u0435\u0440\u0432\u044b\u0439\u00a0BERT:\u00a0\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435\u00a0\u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e<\/a><br \/><a href=\"https:\/\/colab.research.google.com\/github\/jalammar\/jalammar.github.io\/blob\/master\/notebooks\/bert\/A_Visual_Notebook_to_Using_BERT_for_the_First_Time.ipynb\" rel=\"noopener noreferrer nofollow\">A Visual Notebook to Using BERT for the First TIme.ipynb<\/a><\/p>\n<h2>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430<\/h2>\n<p>\u041a\u043e\u0434 \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0438\u0437\u043b\u043e\u0436\u0435\u043d\u043e \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445.<\/p>\n<pre><code># \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u044b !pip install transformers <\/code><\/pre>\n<pre><code># \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438  import numpy as np import pandas as pd import torch import transformers as ppb  from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.model_selection import GridSearchCV from sklearn.model_selection import cross_val_score  import warnings warnings.filterwarnings('ignore')<\/code><\/pre>\n<pre><code># \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0435\u043c dataset df = pd.read_csv('https:\/\/github.com\/clairett\/pytorch-sentiment-classification\/raw\/master\/data\/SST2\/train.tsv', delimiter='\\t', header=None)<\/code><\/pre>\n<pre><code># \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0438 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 batch_1 = df[:2000]<\/code><\/pre>\n<pre><code># \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432\u0441\u0435 \u043e\u043a print(batch_1[:5])<\/code><\/pre>\n<pre><code># \u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u043e\u0431\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0442\u043e\u0440\u044b  model_class, tokenizer_class, pretrained_weights = (ppb.DistilBertModel, ppb.DistilBertTokenizer, 'distilbert-base-uncased') tokenizer = tokenizer_class.from_pretrained(pretrained_weights) model = model_class.from_pretrained(pretrained_weights)<\/code><\/pre>\n<pre><code># \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0438\u0440\u0443\u0435\u043c  tokenized = batch_1[0].apply((lambda x: tokenizer.encode(x, add_special_tokens=True)))<\/code><\/pre>\n<pre><code># \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430\u044f \u0434\u043b\u0438\u043d\u0430  max_len = 0 for i in tokenized.values:     if len(i) > max_len:         max_len = len(i)  padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])<\/code><\/pre>\n<pre><code># \u043c\u0430\u0441\u043a\u0438\u0440\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f attention_mask = np.where(padded != 0, 1, 0)<\/code><\/pre>\n<pre><code># \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0442\u043e\u043a\u0435\u043d\u043e\u0432  input_ids = torch.tensor(padded)   attention_mask = torch.tensor(attention_mask)  with torch.no_grad():     last_hidden_states = model(input_ids, attention_mask=attention_mask)<\/code><\/pre>\n<pre><code># \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0443\u044e \u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0438 labels = batch_1[1] train_features, test_features, train_labels, test_labels = train_test_split(features, labels)<\/code><\/pre>\n<pre><code># \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043b\u043e\u0433\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0440\u0435\u0433\u0440\u0435\u0441\u0441\u0438\u0438 \u043d\u0430 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 lr_clf = LogisticRegression() lr_clf.fit(train_features, train_labels)<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u0441\u0442\u0430\u0442\u044c\u044f\u0445, \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u044b.<\/p>\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/18WwT4uTn3IlURWkjv5iuaYJaFzCNZpje\" rel=\"noopener noreferrer nofollow\"><u>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0431\u043b\u043e\u043a\u043d\u043e\u0442 Colab<\/u><\/a><\/p>\n<h2>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code># \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0430, \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u0442\u0440\u0438\u043a\u0438  print('# train:',lr_clf.score(train_features, train_labels)) print('# test:',lr_clf.score(test_features, test_labels))<\/code><\/pre>\n<p># train: 0.906 <br \/># test: 0.844  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u00ab\u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e\u00bb, \u0447\u0442\u043e\u00a0\u0443\u00a0\u043d\u0430\u0441 \u0432\u00a0\u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043e\u0431\u0443\u0447\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0438\u0437\u00a0\u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430, \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u0441\u0430\u043c\u0438 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u0432 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<pre><code># \u0440\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0438\u0437 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430 print(batch_1[:5])<\/code><\/pre>\n<p>0  a stirring , funny and finally transporting re&#8230;  1 <br \/>1  apparently reassembled from the cutting room f&#8230;  0 <br \/>2  they presume their audience wo n&#8217;t sit still f&#8230;  0 <br \/>3  this is a visually stunning rumination on love&#8230;  1 <br \/>4  jonathan parker &#8216;s bartleby should have been t&#8230;  1  <\/p>\n<pre><code># \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0430\u043c\u0438 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c  def LR(x): return (1 if x > 0 else 0) def correct(LR, label): return True if LR - label == 0 else False  sum_false = 0 N = 5  for i in range (N):   if i and i % 100 == 0: print(i)   array = np.array(tokenized[i])   array = np.pad(array, (0, max_len - len(array)), 'constant')   attention_mask_this = np.where(array != 0, 1, 0)   input_ids_this = torch.tensor([array])     attention_mask_this = torch.tensor(attention_mask_this)   with torch.no_grad():       last_hidden_states_this = model(input_ids_this, attention_mask=attention_mask_this)   features_this = last_hidden_states_this[0][:,0,:].numpy()    sum = np.dot(features_this,lr_clf.coef_[0])     if N &lt; 20: print(i, labels[i], LR(sum), correct(LR(sum), labels[i]), sum)     if correct(LR(sum), labels[i]) == True: sum_false += 1  print() print(sum_false\/(i+1))<\/code><\/pre>\n<p>0 1 1 True [3.85857511] <br \/>1 0 0 True [-6.51904703] <br \/>2 0 0 True [-2.21694384] <br \/>3 1 1 True [3.65850942] <br \/>4 1 0 False [-0.32528463] <\/p>\n<p>0.8<\/p>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e 1\u00a0\u043e\u0448\u0438\u0431\u043a\u0430 \u0438\u0437 5, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441\u00a0\u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<br \/>\u041c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0442\u0440\u043e\u043a, \u043c\u0435\u043d\u044f\u044f N, \u0438 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0432\u044b\u0432\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441\u00a0\u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u043e\u0439 \u0432\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445 \u0437\u0430\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0447\u0442\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043d\u044f\u0442\u0430 \u0438 \u00ab\u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043d\u0430\u00bb \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e. <\/p>\n<h2>\u0410\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0439 \u0442\u0435\u043a\u0441\u0442<\/h2>\n<figure class=\"\"><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0442\u0435\u043a\u0441\u0442\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0438 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u043e\u0446\u0435\u043d\u043a\u0443 \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u0432.<\/p>\n<pre><code># \u0437\u0430\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b  texts = [     'All is good',      'it is so bad',      'nice to meet you',     'it is so rainy',     'he is a stupid',     'I like my car',     'have a nice day'         ]<\/code><\/pre>\n<pre><code># \u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b  for text in texts:   array = tokenizer.encode(text, add_special_tokens=True)   array = np.pad(array, (0, max_len - len(array)), 'constant')   attention_mask_this = np.where(array != 0, 1, 0)   input_ids_this = torch.tensor([array])     attention_mask_this = torch.tensor(attention_mask_this)   with torch.no_grad():       last_hidden_states_this = model(input_ids_this, attention_mask=attention_mask_this)   features_this = last_hidden_states_this[0][:,0,:].numpy()    sum = np.dot(features_this,lr_clf.coef_[0])     print(text, ':', sum, ':', LR(sum)) <\/code><\/pre>\n<p>All is good : [1.90097556] : 1 <br \/>it is so bad : [-1.86999172] : 0 <br \/>nice to meet you : [4.96587936] : 1 <br \/>it is so rainy : [-2.53429642] : 0 <br \/>he is a stupid : [-2.4318853] : 0 <br \/>I like my car : [0.2118134] : 1 <br \/>have a nice day : [2.30864912] : 1  <\/p>\n<p>1 &#8212; \u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u043e<br \/>0 &#8212; \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e<\/p>\n<p>\u0412\u0438\u0434\u0438\u043c, \u0447\u0442\u043e\u00a0\u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b.<\/p>\n<figure class=\"\"><\/figure>\n<h2>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h2>\n<p>\u0412\u00a0\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0442\u0435\u043a\u0441\u0442\u0430 \u043d\u0430\u00a0\u043f\u043e\u0437\u0438\u0442\u0438\u0432\u043d\u044b\u0439\/\u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0439.<\/p>\n<p>\u041d\u0430\u00a0\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f\u00a0\u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430.<\/p>\n<hr\/>\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/18WwT4uTn3IlURWkjv5iuaYJaFzCNZpje\" rel=\"noopener noreferrer nofollow\"><u>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0431\u043b\u043e\u043a\u043d\u043e\u0442 Colab<\/u><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/718352\/\"> https:\/\/habr.com\/ru\/articles\/718352\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-410949","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/410949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410949"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/410949\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}