Установите torch.cuda.IntTensor в Long для встраивания слоя - PullRequest
0 голосов
/ 06 февраля 2020

В данный момент у меня возникают проблемы с тем, чтобы моя сеть понимала мои данные, потому что я создал ошибку в слое внедрения. Он ожидает Long, но я передаю его torch.cuda.IntTensor Моя модель

class LSTMClassification(torch.nn.Module):

    def __init__(self, n_vocab, n_embed, n_hidden, n_output, n_layers, drop_p = 0.5):
        super().__init__()
        # params: "n_" means dimension
        self.n_vocab = n_vocab     # number of unique words in vocabulary
        self.n_layers = n_layers   # number of LSTM layers 
        self.n_hidden = n_hidden   # number of hidden nodes in LSTM

        self.embedding = torch.nn.Embedding(n_vocab, n_embed)
        self.lstm = torch.nn.LSTM(n_embed, n_hidden, n_layers, batch_first = True, dropout = drop_p)
        self.dropout = torch.nn.Dropout(drop_p)
        self.fc = torch.nn.Linear(n_hidden, n_output)
        self.sigmoid = torch.nn.Sigmoid()


    def forward (self, input_words):
                                             # INPUT   :  (batch_size, seq_length)
        embedded_words = self.embedding(input_words)    # (batch_size, seq_length, n_embed)
        lstm_out, h = self.lstm(embedded_words)         # (batch_size, seq_length, n_hidden)
        lstm_out = self.dropout(lstm_out)
        lstm_out = lstm_out.contiguous().view(-1, self.n_hidden) # (batch_size*seq_length, n_hidden)
        fc_out = self.fc(lstm_out)                      # (batch_size*seq_length, n_output)
        sigmoid_out = self.sigmoid(fc_out)              # (batch_size*seq_length, n_output)
        sigmoid_out = sigmoid_out.view(batch_size, -1)  # (batch_size, seq_length*n_output)

        # extract the output of ONLY the LAST output of the LAST element of the sequence
        sigmoid_last = sigmoid_out[:, -1]               # (batch_size, 1)

        return sigmoid_last, h


    def init_hidden (self, batch_size):  # initialize hidden weights (h,c) to 0

        device = "cuda" if torch.cuda.is_available() else "cpu"
        weights = next(self.parameters()).data
        h = (weights.new(self.n_layers, batch_size, self.n_hidden).zero_().to(device),
             weights.new(self.n_layers, batch_size, self.n_hidden).zero_().to(device))

        return h

** Инициализация и обучение модели **

num_epochs_first = 1
for epoch in range(n_epochs):
    h = model.init_hidden(batch_size)

    for i, data in enumerate(train_loader, 0):
        step += 1
        inputs, labels = data[0].to(device), data[1].to(device)
        # making requires_grad = False for the latest set of h
        h = tuple([each.data for each in h])   

        model.zero_grad()
        output, h = model(inputs)
        #loss = criterion(output, labels)
        #loss.backward()
        #optimizer.step()

Моя ошибка

RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda.IntTensor instead (while checking arguments for embedding)

** Ошибка происходит в режиме онлайн: выход, h = модель (входы) **

...