File size: 9,850 Bytes
5bdc726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "S52EVP7k-rl7"
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import torch\n",
        "import re\n",
        "import string\n",
        "import numpy as np\n",
        "import streamlit as st\n",
        "import faiss # хранение индексов\n",
        "from tqdm import tqdm\n",
        "from transformers import AutoTokenizer, AutoModel\n",
        "from joblib import dump, load # Для сохранения/загрузки эмбэддингов"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "12BEEwcF-rl9"
      },
      "outputs": [],
      "source": [
        "path = '/content/movies_filtered.csv' # ИЗМЕНИ ТУТ ПУТЬ!\n",
        "a\n",
        "df = pd.read_csv(path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "df5lg8-m-rl-"
      },
      "outputs": [],
      "source": [
        "def clean(text):\n",
        "    text = text.lower()  # Нижний регистр\n",
        "    text = re.sub(r'\\d+', ' ', text)  # Удаляем числа\n",
        "    # text = text.translate(str.maketrans('', '', string.punctuation))  # Удаляем пунктуацию\n",
        "    text = re.sub(r'\\s+', ' ', text)  # Удаляем лишние пробелы\n",
        "    text = text.strip()  # Удаляем начальные и конечные пробелы\n",
        "    text = re.sub(r'\\s+|\\n', ' ', text) # Удаляет \\n и \\xa0\n",
        "    # text = re.sub(r'\\b\\w{1,2}\\b', '', text)  # Удаляем слова длиной менее 3 символов\n",
        "    # Дополнительные шаги, которые могут быть полезны в данном контексте:\n",
        "    # text = re.sub(r'\\b\\w+\\b', '', text)  # Удаляем отдельные слова (без чисел и знаков препинания)\n",
        "    # text = ' '.join([word for word in text.split() if word not in stop_words])  # Удаляем стоп-слова\n",
        "    return text\n",
        "\n",
        "for i, row in df.iterrows():\n",
        "    df.at[i, 'description'] = clean(row['description'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0huKeMs4-rl_",
        "outputId": "8659997c-9b8a-45bb-e2d7-fcc05422b92a"
      },
      "outputs": [],
      "source": [
        "# pip install transformers sentencepiece\n",
        "\n",
        "tokenizer = AutoTokenizer.from_pretrained(\"cointegrated/rubert-tiny2\")\n",
        "model = AutoModel.from_pretrained(\"cointegrated/rubert-tiny2\")\n",
        "# model.cuda()  # uncomment it if you have a GPU"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "Xsxq-Ohx-rmA"
      },
      "outputs": [],
      "source": [
        "# применяем токенизатор:\n",
        "# -≥ add_special_tokens = добавляем служебные токены (CLS=101, EOS=102)\n",
        "# -≥ truncation = обрезаем по максимальной длине\n",
        "# -≥ max_length = максимальная длина последовательности\n",
        "tokenized = df['description'].apply((lambda x: tokenizer.encode(x,\n",
        "                                                                      add_special_tokens=True,\n",
        "                                                                      truncation=True,\n",
        "                                                                      max_length=1024)))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "id": "OuaXqHNj-rmB"
      },
      "outputs": [],
      "source": [
        "max_len = 1024\n",
        "# Делаю пэддинг чтобы добить до max_len последовательности\n",
        "padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])\n",
        "# И маску чтобы не применять self-attention на pad\n",
        "attention_mask = np.where(padded != 0, 1, 0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "id": "h3bfQh2o-rmC"
      },
      "outputs": [],
      "source": [
        "# Датасет для массивов\n",
        "class BertInputs(torch.utils.data.Dataset):\n",
        "    def __init__(self, tokenized_inputs, attention_masks):\n",
        "        super().__init__()\n",
        "        self.tokenized_inputs = tokenized_inputs\n",
        "        self.attention_masks = attention_masks\n",
        "\n",
        "    def __len__(self):\n",
        "        return self.tokenized_inputs.shape[0]\n",
        "\n",
        "    def __getitem__(self, idx):\n",
        "        ids = self.tokenized_inputs[idx]\n",
        "        ams = self.attention_masks[idx]\n",
        "\n",
        "        return ids, ams\n",
        "\n",
        "dataset = BertInputs(padded, attention_mask)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Q7yYgEP3-rmC",
        "outputId": "76047d40-f793-4cef-fc02-b98b232661f8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "torch.Size([100, 1024]) torch.Size([100, 1024])\n"
          ]
        }
      ],
      "source": [
        "#DataLoader чтобы отправлять бачи в цикл обучения\n",
        "loader = torch.utils.data.DataLoader(dataset, batch_size=100, shuffle=True)\n",
        "sample_ids, sample_ams = next(iter(loader))\n",
        "print(sample_ids.shape, sample_ams.shape)\n",
        "\n",
        "# shape BATCH_SIZE x MAX_LEN - что заходит в BERT"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "r1h0BNy1-rmD",
        "outputId": "adea19c9-a0f2-418c-9a21-ebe8daa00077"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "100%|██████████| 94/94 [01:13<00:00,  1.28it/s]"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "CPU times: user 1min 10s, sys: 145 ms, total: 1min 10s\n",
            "Wall time: 1min 13s\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n"
          ]
        }
      ],
      "source": [
        "%%time\n",
        "\n",
        "vectors_in_batch = []\n",
        "\n",
        "# Iterate over all batches\n",
        "for inputs, attention_masks in tqdm(loader):\n",
        "    vectors_in_mini_batch = []  # Store vectors in mini-batch\n",
        "    with torch.no_grad():\n",
        "        last_hidden_states = model(inputs.cuda(), attention_mask=attention_masks.cuda())\n",
        "        vector = last_hidden_states[0][:,0,:].detach().cpu().numpy()\n",
        "        vectors_in_mini_batch.append(vector)\n",
        "\n",
        "    vectors_in_batch.extend(vectors_in_mini_batch)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {},
      "outputs": [],
      "source": [
        "import itertools\n",
        "\n",
        "# Open the file and load the nested list\n",
        "vectors_in_batch = load('vectors_in_batch.joblib')\n",
        "\n",
        "# Convert the nested list to an unnested list\n",
        "text_embeddings = list(itertools.chain.from_iterable(vectors_in_batch))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Сохранение эмбеддингов\n",
        "dump(vectors_in_batch, 'vectors_in_batch.joblib')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "94"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "len(vectors_in_batch)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "9366"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "len(text_embeddings)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 0
}