Okoge-keys commited on
Commit
3b1c7d9
·
verified ·
1 Parent(s): 0ad3eb5

Delete rnn.ipynb

Browse files
Files changed (1) hide show
  1. rnn.ipynb +0 -446
rnn.ipynb DELETED
@@ -1,446 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 5,
6
- "metadata": {},
7
- "outputs": [
8
- {
9
- "name": "stdout",
10
- "output_type": "stream",
11
- "text": [
12
- "None\n"
13
- ]
14
- }
15
- ],
16
- "source": [
17
- "print(torch.version.cuda)"
18
- ]
19
- },
20
- {
21
- "cell_type": "code",
22
- "execution_count": 6,
23
- "metadata": {},
24
- "outputs": [],
25
- "source": [
26
- "torch.cuda.empty_cache()"
27
- ]
28
- },
29
- {
30
- "cell_type": "code",
31
- "execution_count": 1,
32
- "metadata": {},
33
- "outputs": [
34
- {
35
- "data": {
36
- "text/plain": [
37
- "8"
38
- ]
39
- },
40
- "execution_count": 1,
41
- "metadata": {},
42
- "output_type": "execute_result"
43
- }
44
- ],
45
- "source": [
46
- "import multiprocessing\n",
47
- "import os\n",
48
- "\n",
49
- "# CPU コア数を取得(物理コア数)\n",
50
- "num_cpu = multiprocessing.cpu_count()\n",
51
- "num_workers = min(num_cpu - 1, 8) # 1コアは他の処理用に残す\n",
52
- "num_workers"
53
- ]
54
- },
55
- {
56
- "cell_type": "code",
57
- "execution_count": 4,
58
- "metadata": {},
59
- "outputs": [
60
- {
61
- "name": "stdout",
62
- "output_type": "stream",
63
- "text": [
64
- "NVIDIA GeForce RTX 4060 Laptop GPU\n",
65
- "0\n"
66
- ]
67
- }
68
- ],
69
- "source": [
70
- "print(torch.cuda.get_device_name(torch.cuda.current_device()))\n",
71
- "print(torch.cuda.memory_allocated())"
72
- ]
73
- },
74
- {
75
- "cell_type": "markdown",
76
- "metadata": {},
77
- "source": [
78
- "テキストデータのTensor化\n",
79
- "1. テキストの読み込み\n",
80
- "2. テキストのトークン化\n",
81
- "3. トークンのインデックス化\n",
82
- "4. 複数テキストのバッチ化\n",
83
- "5. テキストの単語ベクトル化"
84
- ]
85
- },
86
- {
87
- "cell_type": "code",
88
- "execution_count": 2,
89
- "metadata": {},
90
- "outputs": [
91
- {
92
- "name": "stderr",
93
- "output_type": "stream",
94
- "text": [
95
- "c:\\Users\\kenta\\AppData\\Local\\Programs\\Python\\workspace_env\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
96
- " from .autonotebook import tqdm as notebook_tqdm\n"
97
- ]
98
- }
99
- ],
100
- "source": [
101
- "import torch\n",
102
- "import torch.nn as nn\n",
103
- "from torch.utils.data import DataLoader, Dataset, random_split\n",
104
- "from transformers import AutoTokenizer\n",
105
- "from datasets import load_dataset\n",
106
- "import torch.optim as optim\n",
107
- "from tqdm import tqdm"
108
- ]
109
- },
110
- {
111
- "cell_type": "code",
112
- "execution_count": 4,
113
- "metadata": {},
114
- "outputs": [
115
- {
116
- "name": "stdout",
117
- "output_type": "stream",
118
- "text": [
119
- "True\n",
120
- "1\n",
121
- "0\n",
122
- "NVIDIA GeForce RTX 4060 Laptop GPU\n",
123
- "(8, 9)\n"
124
- ]
125
- }
126
- ],
127
- "source": [
128
- "print(torch.cuda.is_available())\n",
129
- "print(torch.cuda.device_count())\n",
130
- "print(torch.cuda.current_device())\n",
131
- "print(torch.cuda.get_device_name())\n",
132
- "print(torch.cuda.get_device_capability())"
133
- ]
134
- },
135
- {
136
- "cell_type": "code",
137
- "execution_count": 5,
138
- "metadata": {},
139
- "outputs": [],
140
- "source": [
141
- "# データセットのロード\n",
142
- "dataset = load_dataset(\"stanfordnlp/imdb\")\n",
143
- "train_texts = dataset['train']['text'][:100]\n",
144
- "train_labels = dataset['train']['label'][100:]\n",
145
- "test_texts = dataset['test']['text'][100:]\n",
146
- "test_labels = dataset['test']['label'][100:]\n",
147
- "\n",
148
- "# トークナイザーの準備\n",
149
- "tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')\n",
150
- "\n",
151
- "# テキストのトークン化とインデックス化\n",
152
- "def tokenize_function(text_dataset):\n",
153
- " return tokenizer(text_dataset['text'], \n",
154
- " padding=True,\n",
155
- " truncation=True,\n",
156
- " max_length=256)\n",
157
- "\n",
158
- "train_encodings = dataset['train'].map(tokenize_function, batched=True)\n",
159
- "test_encodings = dataset['test'].map(tokenize_function, batched=True)\n"
160
- ]
161
- },
162
- {
163
- "cell_type": "code",
164
- "execution_count": 6,
165
- "metadata": {},
166
- "outputs": [],
167
- "source": [
168
- "\n",
169
- "# カスタムデータセットクラス\n",
170
- "class CustomDataset(Dataset):\n",
171
- " def __init__(self, encodings, labels):\n",
172
- " self.encodings = encodings\n",
173
- " self.labels = labels\n",
174
- " \n",
175
- " def __getitem__(self, idx):\n",
176
- " item = {\n",
177
- " 'input_ids': torch.tensor(self.encodings['input_ids'][idx]),\n",
178
- " 'attention_mask': torch.tensor(self.encodings['attention_mask'][idx])\n",
179
- " }\n",
180
- " item['labels'] = torch.tensor(self.labels[idx])\n",
181
- " return item\n",
182
- " \n",
183
- " def __len__(self):\n",
184
- " return len(self.labels)\n",
185
- "\n",
186
- "train_dataset = CustomDataset(train_encodings, train_labels)\n",
187
- "test_dataset = CustomDataset(test_encodings, test_labels)\n"
188
- ]
189
- },
190
- {
191
- "cell_type": "code",
192
- "execution_count": 7,
193
- "metadata": {},
194
- "outputs": [],
195
- "source": [
196
- "\n",
197
- "# 訓練データをtrainとvalに分割\n",
198
- "train_size = int(0.7 * len(train_dataset))\n",
199
- "val_size = len(train_dataset) - train_size\n",
200
- "train_dataset, val_dataset = random_split(train_dataset, [train_size, val_size])\n",
201
- "\n",
202
- "# データローダー\n",
203
- "train_loader = DataLoader(train_dataset, \n",
204
- " batch_size=64, \n",
205
- " shuffle=True,\n",
206
- " num_workers=8, # 並列データロード\n",
207
- " pin_memory=True, # GPUへの転送を高速化\n",
208
- " prefetch_factor=2 # 先読み\n",
209
- " )\n",
210
- "val_loader = DataLoader(val_dataset, \n",
211
- " batch_size=64, \n",
212
- " shuffle=False,\n",
213
- " num_workers=8, # 並列データロード\n",
214
- " pin_memory=True, # GPUへの転送を高速化\n",
215
- " prefetch_factor=2 # 先読み\n",
216
- " )\n",
217
- "test_loader = DataLoader(test_dataset,\n",
218
- " batch_size=64, \n",
219
- " shuffle=False,\n",
220
- " num_workers=8, # 並列データロード\n",
221
- " pin_memory=True, # GPUへの転送を高速化\n",
222
- " prefetch_factor=2 # 先読み\n",
223
- " )\n"
224
- ]
225
- },
226
- {
227
- "cell_type": "code",
228
- "execution_count": 8,
229
- "metadata": {},
230
- "outputs": [],
231
- "source": [
232
- "\n",
233
- "# LSTMモデルの定義\n",
234
- "class LstmClassifier(nn.Module):\n",
235
- " def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, num_layers=2, dropout=0.5):\n",
236
- " super(LstmClassifier, self).__init__()\n",
237
- " \n",
238
- " # 埋め込み層を追加\n",
239
- " self.embedding = nn.Embedding(vocab_size, embedding_dim)\n",
240
- " \n",
241
- " # LSTM層\n",
242
- " self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers, batch_first=True)\n",
243
- " self.dropout = nn.Dropout(dropout)\n",
244
- " self.fc = nn.Linear(hidden_dim, output_dim)\n",
245
- " self.softmax = nn.Softmax(dim=1)\n",
246
- " \n",
247
- " def forward(self, x):\n",
248
- " # 埋め込み層を通す\n",
249
- " embedded = self.embedding(x) # (batch_size, seq_length, embedding_dim)\n",
250
- " \n",
251
- " # LSTM層\n",
252
- " lstm_out, (hn, cn) = self.lstm(embedded)\n",
253
- " final_hidden_state = hn[-1]\n",
254
- " \n",
255
- " # ドロップアウトと全結合層\n",
256
- " x = self.dropout(final_hidden_state)\n",
257
- " x = self.fc(x)\n",
258
- " return self.softmax(x)\n"
259
- ]
260
- },
261
- {
262
- "cell_type": "code",
263
- "execution_count": 9,
264
- "metadata": {},
265
- "outputs": [],
266
- "source": [
267
- "\n",
268
- "# モデルのインスタンスを作成\n",
269
- "# input_dim = tokenizer.model_max_length # bertの埋め込みサイズ\n",
270
- "vocab_size = tokenizer.vocab_size # トークナイザーの語彙サイズ\n",
271
- "embedding_dim = 300 # 埋め込みベクトルの次元数\n",
272
- "hidden_dim = 128\n",
273
- "output_dim = 2\n",
274
- "model = LstmClassifier(vocab_size, embedding_dim, hidden_dim, output_dim)\n",
275
- "\n",
276
- "# 最適化手法と損失関数\n",
277
- "optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
278
- "criterion = nn.CrossEntropyLoss()\n"
279
- ]
280
- },
281
- {
282
- "cell_type": "code",
283
- "execution_count": 10,
284
- "metadata": {},
285
- "outputs": [
286
- {
287
- "name": "stdout",
288
- "output_type": "stream",
289
- "text": [
290
- "cuda\n"
291
- ]
292
- },
293
- {
294
- "data": {
295
- "text/plain": [
296
- "LstmClassifier(\n",
297
- " (embedding): Embedding(30522, 300)\n",
298
- " (lstm): LSTM(300, 128, num_layers=2, batch_first=True)\n",
299
- " (dropout): Dropout(p=0.5, inplace=False)\n",
300
- " (fc): Linear(in_features=128, out_features=2, bias=True)\n",
301
- " (softmax): Softmax(dim=1)\n",
302
- ")"
303
- ]
304
- },
305
- "execution_count": 10,
306
- "metadata": {},
307
- "output_type": "execute_result"
308
- }
309
- ],
310
- "source": [
311
- "# 学習ループ\n",
312
- "num_epochs = 1\n",
313
- "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
314
- "print(device)\n",
315
- "\n",
316
- "model.to(device)\n"
317
- ]
318
- },
319
- {
320
- "cell_type": "code",
321
- "execution_count": null,
322
- "metadata": {},
323
- "outputs": [],
324
- "source": [
325
- "\n",
326
- "for epoch in range(num_epochs):\n",
327
- " model.train()\n",
328
- " running_loss = 0.0\n",
329
- " for batch in train_loader:\n",
330
- " optimizer.zero_grad()\n",
331
- " # データをGPUに転送\n",
332
- " input_ids = batch['input_ids'].to(device)\n",
333
- " attention_mask = batch['attention_mask'].to(device)\n",
334
- " labels = batch['labels'].to(device)\n",
335
- " # LSTMモデルの予測\n",
336
- " outputs = model(input_ids) # shape:(batch_size, output_dim)\n",
337
- " \n",
338
- " loss = criterion(outputs, labels)\n",
339
- " loss.backward()\n",
340
- " optimizer.step()\n",
341
- "\n",
342
- " running_loss += loss.item()\n",
343
- "\n",
344
- " print(f\"Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss/len(train_loader)}\")\n"
345
- ]
346
- },
347
- {
348
- "cell_type": "code",
349
- "execution_count": null,
350
- "metadata": {},
351
- "outputs": [],
352
- "source": [
353
- "\n",
354
- "# バリデーションでの評価\n",
355
- "model.eval()\n",
356
- "correct = 0\n",
357
- "total = 0\n",
358
- "with torch.no_grad():\n",
359
- " for batch in tqdm(val_loader):\n",
360
- " input_ids = batch['input_ids'].to(device)\n",
361
- " attention_mask = batch['attention_mask'].to(device)\n",
362
- " labels = batch['labels'].to(device)\n",
363
- " # LSTMモデルの予測\n",
364
- " outputs = model(input_ids)\n",
365
- " _, predicted = torch.max(outputs, dim=1)\n",
366
- "\n",
367
- " total += labels.size(0)\n",
368
- " correct += (predicted == labels).sum().item()\n",
369
- "val_accuracy = correct / total\n",
370
- "print(f\"Validation Accuracy: {val_accuracy * 100:.2f}%\")\n",
371
- "\n",
372
- "\n",
373
- "# モデルの保存\n",
374
- "torch.save(model.state_dict(), 'LstmClassifier.pth')\n",
375
- "print(\"Model saved!\")\n",
376
- "\n",
377
- "# # モデルの状態をPickle形式で保存\n",
378
- "# import pickle\n",
379
- "# with open(\"model.pkl\", \"wb\") as f:\n",
380
- "# pickle.dump(model.state_dict(), f)\n"
381
- ]
382
- },
383
- {
384
- "cell_type": "code",
385
- "execution_count": null,
386
- "metadata": {},
387
- "outputs": [],
388
- "source": [
389
- "\n",
390
- "# 保存したモデルの読み込み\n",
391
- "model = LstmClassifier(input_dim, hidden_dim, output_dim)\n",
392
- "model.load_state_dict(torch.load(\"LstmClassifier.pth\"))\n",
393
- "model.to(device)\n",
394
- "print(\"Model loaded!\")\n",
395
- "\n",
396
- "# # Pickleファイルからモデルの状態を読み込む\n",
397
- "# import pickle\n",
398
- "# with open(\"model.pkl\", \"rb\") as f:\n",
399
- "# model_state_dict = pickle.load(f)\n",
400
- "\n",
401
- "# # モデルに状態を読み込む\n",
402
- "# model.load_state_dict(model_state_dict)\n",
403
- "# model.to(device)\n",
404
- "\n",
405
- "model.eval()\n",
406
- "correct = 0\n",
407
- "total = 0\n",
408
- "with torch.no_grad():\n",
409
- " for batch in test_loader:\n",
410
- " input_ids = batch['input_ids'].to(device)\n",
411
- " attention_mask = batch['attention_mask'].to(device)\n",
412
- " labels = batch['labels'].to(device)\n",
413
- " \n",
414
- " outputs = model(input_ids)\n",
415
- " _, predicted = torch.max(outputs, 1)\n",
416
- " \n",
417
- " total += labels.size(0)\n",
418
- " correct += (predicted == labels).sum().item()\n",
419
- "\n",
420
- "test_accuracy = correct / total\n",
421
- "print(f'Test Accuracy: {test_accuracy * 100:.2f}%')\n"
422
- ]
423
- }
424
- ],
425
- "metadata": {
426
- "kernelspec": {
427
- "display_name": "workspace_env",
428
- "language": "python",
429
- "name": "python3"
430
- },
431
- "language_info": {
432
- "codemirror_mode": {
433
- "name": "ipython",
434
- "version": 3
435
- },
436
- "file_extension": ".py",
437
- "mimetype": "text/x-python",
438
- "name": "python",
439
- "nbconvert_exporter": "python",
440
- "pygments_lexer": "ipython3",
441
- "version": "3.12.9"
442
- }
443
- },
444
- "nbformat": 4,
445
- "nbformat_minor": 2
446
- }