https://github.com/salesforce/pytorch-qrnn
上記urlにて実装されたPytorchによるQRNNを学習させたく思い,
Pytorch tutorialのRNN word language modelである
https://github.com/pytorch/examples/tree/master/word_language_model
を参考に実装してみたのですが,
python
1# At any point you can hit Ctrl + C to break out of training early. 2epochs = 50 3try: 4 for epoch in range(1, epochs+1): 5 epoch_start_time = time.time() 6 train() 7 val_loss = evaluate(val_data) 8 print('-' * 89) 9 print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | ' 10 'valid ppl {:8.2f}'.format(epoch, (time.time() - epoch_start_time), 11 val_loss, math.exp(val_loss))) 12 print('-' * 89) 13 # Save the model if the validation loss is the best we've seen so far. 14 if not best_val_loss or val_loss < best_val_loss: 15 with open('model.pt', 'wb') as f: 16 torch.save(model, f) 17 best_val_loss = val_loss 18 else: 19 # Anneal the learning rate if no improvement has been seen in the validation dataset. 20 lr /= 4.0 21except KeyboardInterrupt: 22 print('-' * 89) 23 print('Exiting from training early')
にて,以下のエラーが排出されました.
python
1--------------------------------------------------------------------------- 2AttributeError Traceback (most recent call last) 3<ipython-input-11-48e657d4a3b2> in <module>() 4 4 for epoch in range(1, epochs+1): 5 5 epoch_start_time = time.time() 6----> 6 train() 7 7 val_loss = evaluate(val_data) 8 8 print('-' * 89) 9 10<ipython-input-10-04629ba4da48> in train() 11 47 start_time = time.time() 12 48 ntokens = len(corpus.dictionary) 13---> 49 hidden = model.init_hidden(batch_size) 14 50 for batch, i in enumerate(range(0, train_data.size(0) - 1, bptt)): 15 51 data, targets = get_batch(train_data, i) 16 17<ipython-input-5-29d356b3a836> in init_hidden(self, bsz) 18 21 def init_hidden(self, bsz): 19 22 weight = next(self.parameters()) 20---> 23 return weight.new_zeros(self.layers, bsz, self.hidden_size) 21 24 22 25 def init_weights(self): 23 24~/anaconda3/envs/pytorch-py3.6/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name) 25 533 return modules[name] 26 534 raise AttributeError("'{}' object has no attribute '{}'".format( 27--> 535 type(self).__name__, name)) 28 536 29 537 def __setattr__(self, name, value): 30 31AttributeError: 'QRNNModel' object has no attribute 'layers'
エラーを起こしているのは,
python
1def train(): 2 # Turn on training mode which enables dropout. 3 model.train() 4 total_loss = 0. 5 start_time = time.time() 6 ntokens = len(corpus.dictionary) 7 hidden = model.init_hidden(batch_size) 8 for batch, i in enumerate(range(0, train_data.size(0) - 1, bptt)): 9 data, targets = get_batch(train_data, i) 10 # Starting each batch, we detach the hidden state from how it was previously produced. 11 # If we didn't, the model would try backpropagating all the way to start of the dataset. 12 hidden = repackage_hidden(hidden) 13 model.zero_grad() 14 output, hidden = model(data, hidden) 15 loss = criterion(output.view(-1, ntokens), targets) 16 loss.backward() 17 18 # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs. 19 torch.nn.utils.clip_grad_norm_(model.parameters(), clip) 20 for p in model.parameters(): 21 p.data.add_(-lr, p.grad.data) 22 23 total_loss += loss.item() 24 25 if batch % log_interval == 0 and batch > 0: 26 cur_loss = total_loss / log_interval 27 elapsed = time.time() - start_time 28 print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | ' 29 'loss {:5.2f} | ppl {:8.2f}'.format( 30 epoch, batch, len(train_data) // bptt, lr, 31 elapsed * 1000 / log_interval, cur_loss, math.exp(cur_loss))) 32 total_loss = 0 33 start_time = time.time()
の
hidden = model.init_hidden(batch_size)
の部分であり,modelは
python
1nhid=256 2nlayers=2 3dropout=0.5 4nvocab = 35 # 最大35単語までのシーケンスを扱う 5 6ntokens = len(corpus.dictionary) 7model = QRNNModel(hidden_size=nhid, layers=nlayers, 8 vocab=nvocab, dropout=0.5, parallel=True) 9model = model.to(device)
で定義しているので,QRNNModelを見ると
python
1class QRNNModel(nn.Module): 2 3 def __init__(self, hidden_size=1024, parallel=True, layers=3, dropout=0.5, vocab=100): 4 super().__init__() 5 6 self.embedding = nn.Embedding(vocab, hidden_size) 7 8 from torchqrnn import QRNN 9 self.rnn = QRNN(input_size=hidden_size, hidden_size=hidden_size, 10 num_layers=layers, dropout=0.5) 11 #self.rnn = nn.LSTM(hidden_size, hidden_size) 12 # Note: we tell DataParallel to split on the second dimension as RNNs are batch second by default in PyTorch 13 if parallel: self.rnn = nn.DataParallel(self.rnn, dim=1) 14 15 def forward(self, x): 16 x = self.embedding(x) 17 out, hidden = self.rnn(x) 18 return out[:-1] 19 20 def init_hidden(self, bsz): 21 weight = next(self.parameters()) 22 return weight.new_zeros(self.layers, bsz, self.hidden_size) 23 24 def init_weights(self): 25 initrange = 0.1 26 self.encoder.weight.data.uniform_(-initrange, initrange) 27 self.decoder.bias.data.zero_() 28 self.decoder.weight.data.uniform_(-initrange, initrange)
このクラス内の,
python
1def __init__(self, hidden_size=1024, parallel=True, layers=3, dropout=0.5, vocab=100): 2 super().__init__()
の箇所にてエラーで指摘された
python
1AttributeError: 'QRNNModel' object has no attribute 'layers'
layersを入れているのでクリアしていると思ったのですが,できていないようです.
なぜなのかお教えいただけないでしょうか.
よろしくお願いいたします.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/20 12:43