49514


以下のコードを動かそうと思っているのですが、  
エラーが出力されてしまいます。  
もしよろしければ、ご教授よろしくお願いいたします  
何卒、よろしくお願いいたします。

> python3 '/home/hoge/train.py'  
>  Traceback (most recent call last):  
>  File "/home/hoge/train.py", line 56, in  
>  model = RNN(n_vocab, embedding_dim, hidden_dim)  
>  File "/home/hoge/train.py", line 24, in **init**  
>  self.decoder = nn.Linear(hidden_dim, n_vocab)  
>  File "/home/hoge/.local/lib/python3.6/site-
> packages/torch/nn/modules/linear.py", line 44, in **init**  
>  self.reset_parameters()  
>  File "/home/hoge/.local/lib/python3.6/site-
> packages/torch/nn/modules/linear.py", line 47, in reset_parameters  
>  stdv = 1. / math.sqrt(self.weight.size(1))  
>  RuntimeError: invalid argument 2: dimension 1 out of range of 0D tensor at
> /pytorch/torch/lib/TH/generic/THTensor.c:24
[code]

    import time
    from urllib import request
    from bs4 import BeautifulSoup
    import MeCab
    
    
    def get_data(url):
        req = request.urlopen(url)
        soup = BeautifulSoup(req.read(), 'html.parser')
    
        # 俳句
        poem_elem = soup.select('td[height=40] b')[0]
        poem = poem_elem.text.replace('*', '').strip()  # サニタイズ
        if not poem:
            return False  # 存在しないID
        return {'poem': poem}
    
    
    def write2file(i, morphemes):
        with open('poem.txt', 'a', encoding="utf-8") as f:
            f.write(morphemes + '\n')
    
    def get_morphemes(sentences):
        temp = tagger.parse(sentences).split()
        return " ".join(temp)
    
    tagger = MeCab.Tagger("-Owakati")
    MAX_ID = 10  # 登録されているIDの最大値
    for i in range(1, MAX_ID):
        url = 'http://www.haiku-data.jp/work_detail.php?cd= 
    {id}'.format(id=i)
        print('fetching data... ' + url, end=' ')
        d = get_data(url)
        if d:
           print('result: SUCCESS')
           morphemes = get_morphemes(d['poem'])
           write2file(i, morphemes)
        else:
            print('result: MISSING')
    
        time.sleep(1)
    
[/code]

data_util.py

[code]

    # -*- coding: utf-8 -*-
    import codecs
    
    
    def read_file(fname):
        """ Read file
        :param fname: file name
        :return: word list in the file
        """
        with codecs.open('poem.txt', 'a', encoding='utf-8') as f:
            return f.read().splitlines()
    
    def select_sentences(sentences):
        dataset = []
        for sent in sentences:
            morphemes = sent.split()
            if len(morphemes) > 30:
               continue
            for i in range(len(morphemes)-2):
                if morphemes[i] == morphemes[i+1]:
                    break
                if morphemes[i] == morphemes[i+2]:
                    break
            else:
                dataset.append(sent)
        return dataset
    
    def make_vocab(sentences):
        """ make dictionary
        :param sentences: word list ex. ["I", "am", "stupid"]
        """
        global word2id
    
        for sent in sentences:
            for morpheme in sent.split():
                if morpheme in word2id:
                    continue
                word2id[morpheme] = len(word2id)
    
    def sent2id(sentences):
        id_list = []
        for sent in sentences:
            temp = []
            for morpheme in sent.split():
                temp.append(word2id[morpheme])
            id_list.append(temp)
        return id_list
    
    def get_dataset():
        fname_list = []
        dataset = []
        # make dictionary
        for fname in fname_list:
            sentences = read_file(fname)
            sentences = select_sentences(sentences)
            make_vocab(sentences)
            dataset = dataset + sentences
        id2sent = sent2id(dataset)
        return word2id, id2sent, dataset
    
    
    word2id = {}
    
[/code]

train.py

[code]

    import random
    import data_util as U
    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    import torch.optim as O
    from torch.autograd import Variable
    
    ### Prepare data ###
    word2id, id2sent, dataset = U.get_dataset()
    
    USE_CUDA = False
    FloatTensor = torch.cuda.FloatTensor if USE_CUDA else 
    torch.FloatTensor
    LongTensor = torch.cuda.LongTensor if USE_CUDA else torch.LongTensor
    class RNN(nn.Module):
        def __init__(self, n_vocab, embedding_dim, hidden_dim):
            super(RNN, self).__init__()
            self.encoder = nn.Embedding(n_vocab, embedding_dim)
            self.gru = nn.GRU(embedding_dim, hidden_dim)
            self.decoder = nn.Linear(hidden_dim, n_vocab)
            self.embedding_dim = embedding_dim
            self.num_layers = 1
            self.dropout = nn.Dropout(0.1)
            self.init_hidden()
    
        def init_hidden(self):
            self.hidden = Variable(\
                            FloatTensor(self.num_layers, 1, 
    self.embedding_dim).fill_(0))
            if USE_CUDA:
                self.hidden.cuda()
    
        def forward(self, x):
            x = self.encoder(x.view(1, -1))
            x = self.dropout(x)
            y, self.hidden = self.gru(x.view(1, 1, -1), self.hidden)
            y = self.decoder(y.view(1, -1))
            return y
    
    def variable(index):
        tensor = LongTensor(index)
        if USE_CUDA:
            return Variable(tensor).cuda()
        return Variable(tensor)
    
    ### Training ###
    n_epochs = 10
    n_vocab = len(word2id)
    embedding_dim = 128
    hidden_dim = 128
    learning_rate = 0.01
    
    model = RNN(n_vocab, embedding_dim, hidden_dim)
    if USE_CUDA:
        model.cuda()
    criterion = nn.CrossEntropyLoss()
    optimizer = O.Adam(model.parameters(), lr=learning_rate)
    
    print("USE_CUDA: {}\nn_epochs: {}\nn_vocab: {}\n".format(USE_CUDA, 
    n_epochs, n_vocab))
    
    for epoch in range(n_epochs):
        if (epoch+1) % 1 == 0:
            print("Epoch {}".format(epoch+1))
        random.shuffle(id2sent)
        for indices in id2sent:
            model.init_hidden()
            model.zero_grad()
            source = variable(indices[:-1])
            target = variable(indices[1:])
            loss = 0
            for x, t in zip(source, target):
                y = model(x)
                loss += criterion(y, t)
            loss.backward()
            optimizer.step()
    #model_name = "/output/example_gpu.model" if USE_CUDA else 
    #"/output/example.model"
    model_name = "example_gpu.model" if USE_CUDA else "example.model"
    torch.save(model.state_dict(), model_name)
    
[/code]

環境  
Ubuntu 18.04

Python 3.6.6  
torch (0.2.0.post2)  
mecab-python3 (0.7)  
beautifulsoup4 (4.6.0)


CC BY-SA 4.0
2018-10-19T08:54:32.020
49452
2018-10-22T02:31:07.350
2018-10-19T09:46:59.383
19110
30516
question
python python3 機械学習 pytorch
pythonでRuntimeErrorが出ます


見た感じだとtrain.pyの  
decoder = nn.Linear(hidden_dim, n_vocab)  
の第2引数、つまりn_vocabのところでエラーが起こっていますね。n_vocabのdim=1が見つからないよ〜って言ってます。torchに関してあまり詳しくありませんが、n_vocab=len(word2id)のshapeについて今一度確認してみてください。


CC BY-SA 4.0
2018-10-22T02:03:10.813
49514
2018-10-22T02:31:07.350
2018-10-22T02:31:07.350
29506
29506
49452
answer