質問編集履歴
9
文字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
機械学習で言語を学習をさせたいのですが以下のエラーで困っています。
|
1
|
+
機械学習で言語を学習をさせたいのですが以下のエラーで困っています。
|
2
2
|
|
3
3
|
jupyter notebookで実行しました。
|
4
4
|
|
8
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -172,8 +172,6 @@
|
|
172
172
|
|
173
173
|
|
174
174
|
|
175
|
-
```python
|
176
|
-
|
177
175
|
|
178
176
|
|
179
177
|
```ValueError Traceback (most recent call last)
|
7
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -172,13 +172,11 @@
|
|
172
172
|
|
173
173
|
|
174
174
|
|
175
|
-
```
|
176
|
-
|
177
|
-
エラー
|
178
|
-
|
179
175
|
```python
|
180
176
|
|
177
|
+
|
178
|
+
|
181
|
-
ValueError Traceback (most recent call last)
|
179
|
+
```ValueError Traceback (most recent call last)
|
182
180
|
|
183
181
|
<ipython-input-29-d4ab6d8de40f> in <module>
|
184
182
|
|
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
```ここに言語を入力
|
2
|
-
|
3
|
-
コード
|
4
|
-
|
5
|
-
|
1
|
+
機械学習で言語を学習をさせたいのですが以下のエラーで困っています。調べてみましたがよくわからなかったです。
|
6
2
|
|
7
3
|
jupyter notebookで実行しました。
|
8
4
|
|
@@ -180,11 +176,9 @@
|
|
180
176
|
|
181
177
|
エラー
|
182
178
|
|
183
|
-
```
|
179
|
+
```python
|
184
|
-
|
185
|
-
|
180
|
+
|
186
|
-
|
187
|
-
|
181
|
+
ValueError Traceback (most recent call last)
|
188
182
|
|
189
183
|
<ipython-input-29-d4ab6d8de40f> in <module>
|
190
184
|
|
5
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -180,9 +180,13 @@
|
|
180
180
|
|
181
181
|
エラー
|
182
182
|
|
183
|
+
```ここに言語を入力
|
184
|
+
|
185
|
+
コード
|
186
|
+
|
183
|
-
ValueError Traceback (most recent call last)
|
187
|
+
```ValueError Traceback (most recent call last)
|
184
|
-
|
188
|
+
|
185
|
-
<ipython-input-2
|
189
|
+
<ipython-input-29-d4ab6d8de40f> in <module>
|
186
190
|
|
187
191
|
72 model.init_hidden()
|
188
192
|
|
@@ -196,7 +200,7 @@
|
|
196
200
|
|
197
201
|
|
198
202
|
|
199
|
-
ValueError: expected sequence of length 1
|
203
|
+
ValueError: expected sequence of length 12 at dim 1 (got 10)
|
200
204
|
|
201
205
|
|
202
206
|
|
4
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,119 @@
|
|
10
10
|
|
11
11
|
```python
|
12
12
|
|
13
|
-
|
13
|
+
import logging
|
14
|
+
|
15
|
+
import torch
|
16
|
+
|
17
|
+
import torch.nn as nn
|
18
|
+
|
19
|
+
import torch.nn.functional as F
|
20
|
+
|
21
|
+
import torch.optim as optim
|
22
|
+
|
23
|
+
from text_encoder import JapaneseTextEncoder
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
|
30
|
+
|
31
|
+
logging.info("Building dictionary and dataset.")
|
32
|
+
|
33
|
+
encoder = JapaneseTextEncoder(corpus, append_eos=True, maxlen=10, padding=True)
|
34
|
+
|
35
|
+
encoder.build()
|
36
|
+
|
37
|
+
logging.info("Done...")
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
n_vocab = len(encoder.word2id)
|
42
|
+
|
43
|
+
EMBEDDING_DIM = HIDDEN_DIM =128
|
44
|
+
|
45
|
+
batch_size = 10
|
46
|
+
|
47
|
+
logging.info("Vocab has %i elements.", n_vocab)
|
48
|
+
|
49
|
+
logging.info("The longest sentence has %i elements.", len(max(encoder.dataset, key=len)))
|
50
|
+
|
51
|
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
52
|
+
|
53
|
+
class RNNLM(nn.Module):
|
54
|
+
|
55
|
+
def __init__(self, embedding_dim, hidden_dim, vocab_size, batch_size=10, num_layers=1):
|
56
|
+
|
57
|
+
super(RNNLM, self).__init__()
|
58
|
+
|
59
|
+
self.batch_size = batch_size
|
60
|
+
|
61
|
+
self.num_layers = num_layers
|
62
|
+
|
63
|
+
self.hidden_dim = hidden_dim
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
self.word_embeddings = nn.Embedding(vocab_size, embedding_dim, padding_idx=1)
|
68
|
+
|
69
|
+
self.dropout = nn.Dropout(p=0.5)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
self.gru = nn.GRU(embedding_dim, hidden_dim, batch_first=True, num_layers=self.num_layers)
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
self.output = nn.Linear(hidden_dim, vocab_size)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def init_hidden(self):
|
82
|
+
|
83
|
+
self.hidden_state = torch.zeros(self.num_layers, self.batch_size, self.hidden_dim, device=device)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def forward(self, indices):
|
88
|
+
|
89
|
+
embed = self.word_embeddings(indices) # batch_len x sequence_length x embedding_dim
|
90
|
+
|
91
|
+
drop_out = self.dropout(embed)
|
92
|
+
|
93
|
+
if drop_out.dim() == 1:
|
94
|
+
|
95
|
+
drop_out = torch.unsqueeze(drop_out, 1)
|
96
|
+
|
97
|
+
gru_out, self.hidden_state = self.gru(drop_out, self.hidden_state)# batch_len x sequence_length x hidden_dim
|
98
|
+
|
99
|
+
gru_out = gru_out.contiguous()
|
100
|
+
|
101
|
+
return self.output(gru_out)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def train2batch(dataset):
|
108
|
+
|
109
|
+
batch_dataset = []
|
110
|
+
|
111
|
+
for i in range(0, 1350, batch_size):
|
112
|
+
|
113
|
+
batch_dataset.append(dataset[i:i+batch_size])
|
114
|
+
|
115
|
+
return batch_dataset
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
n_epoch = 75000
|
120
|
+
|
121
|
+
model = RNNLM(EMBEDDING_DIM, HIDDEN_DIM, n_vocab).to(device)
|
122
|
+
|
123
|
+
optimizer = optim.SGD(model.parameters(), lr=0.01)
|
124
|
+
|
125
|
+
criterion = nn.CrossEntropyLoss(ignore_index=0)# Training
|
14
126
|
|
15
127
|
logging.info("Training mode")
|
16
128
|
|
3
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
|
3
|
+
コード
|
4
|
+
|
1
|
-
機械学習で言語を学習をさせたいのですが以下のエラーで困っています。調べてみましたがよくわからなかったです。
|
5
|
+
```機械学習で言語を学習をさせたいのですが以下のエラーで困っています。調べてみましたがよくわからなかったです。
|
2
6
|
|
3
7
|
jupyter notebookで実行しました。
|
4
8
|
|
5
9
|
|
10
|
+
|
11
|
+
```python
|
6
12
|
|
7
13
|
# Training
|
8
14
|
|
@@ -58,6 +64,8 @@
|
|
58
64
|
|
59
65
|
|
60
66
|
|
67
|
+
```
|
68
|
+
|
61
69
|
エラー
|
62
70
|
|
63
71
|
ValueError Traceback (most recent call last)
|
2
実行環境の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
機械学習で言語を学習をさせたいのですが以下のエラーで困っています。調べてみましたがよくわからなかったです。
|
2
|
+
|
3
|
+
jupyter notebookで実行しました。
|
4
|
+
|
5
|
+
|
2
6
|
|
3
7
|
# Training
|
4
8
|
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|