【问题标题】:Trouble with nn.embedding in pytorch, expected scalar type Long, but got torch.cuda.FloatTensor (how to fix)?nn.embedding 在 pytorch 中出现问题,预期标量类型为 Long,但得到了 torch.cuda.FloatTensor(如何修复)?
【发布时间】:2019-10-14 10:28:37
【问题描述】:

所以我有一个 RNN 编码器,它是更大语言模型的一部分,其过程是编码 -> rnn -> 解码。

作为我的 rnn 课程的__init__ 的一部分,我有以下内容:

self.encode_this = nn.Embedding(self.vocab_size, self.embedded_vocab_dim)

现在我正在尝试实现一个转发类,它接收批量并执行编码然后解码,

def f_calc(self, batch):
    #Here, batch.shape[0] is the size of batch while batch.shape[1] is the sequence length

    hidden_states = (torch.zeros(self.num_layers, batch.shape[0], self.hidden_vocab_dim).to(device))
    embedded_states = (torch.zeros(batch.shape[0],batch.shape[1], self.embedded_vocab_dim).to(device))

    o1, h = self.encode_this(embedded_states)

但是,我的问题总是与编码器有关,这给了我以下错误:

/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   1465         # remove once script supports set_grad_enabled
   1466         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1467     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   1468 
   1469 

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

有人知道如何解决吗?我对pytorch完全陌生,所以如果这是一个愚蠢的问题,请原谅。我知道涉及到某种形式的类型转换,但我不知道该怎么做...

非常感谢!

【问题讨论】:

    标签: python pytorch recurrent-neural-network


    【解决方案1】:

    嵌入层要求输入为整数。

    import torch as t
    
    emb = t.nn.Embedding(embedding_dim=3, num_embeddings=26)
    
    emb(t.LongTensor([0,1,2]))
    

    在您的代码中添加long()

    embedded_states = (torch.zeros(batch.shape[0],batch.shape[1], self.embedded_vocab_dim).to(device)).long()
    

    【讨论】:

    • 当我这样做并通过我的 rnn 时,self.rnn(embedded_states) 添加了一个全新的维度......所以张量变成 4d 而不是 3d,你知道为什么会这样吗?
    • 因为您进行嵌入:您将最后一维中的整数替换为其嵌入!换句话说,您将数字(标量)替换为向量并添加额外的维度。
    • 嗯,你知道如何保持 3D 吗?我一直在尝试挤压方法,但它似乎不起作用。我的 RNN 的输入是 3D 而不是 4D……而且我应该传入 3D 嵌入和隐藏状态,对吧?还是我完全做错了?
    • 不知道你的想法很难说。你可以看到我的代码here。我正在做字符嵌入。在输入时,我有 [句子、单词、字符]。我用它的嵌入来表示每个字符(现在我有 [sentences, words, characters, embeddings] 并使用 LSTM 来获取单词的嵌入。然后将 LSTM 应用于单词以解决分类问题。
    • 我的输入是 N*L 的形式,其中 N 是批量大小,L 是序列长度,我的输出是 N * L * V,其中 V 是词汇大小(嵌入每个单词)...现在我对嵌入状态的初始化是零的 3d 向量(批量大小、序列长度、嵌入的暗淡)但是现在当我运行 nn.embedding 时,我得到了一个额外的维度
    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 2023-03-21
    • 2019-09-04
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2020-05-31
    相关资源
    最近更新 更多