python - nn.embedding 在 pytorch 中出现问题,预期标量类型为 Long,但得到了 torch.cuda.FloatTensor(如何修复)?
问题描述
所以我有一个 RNN 编码器,它是更大语言模型的一部分,其中的过程是编码 -> rnn -> 解码。
作为__init__
我的 rnn 课程的一部分,我有以下内容:
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完全陌生,所以如果这是一个愚蠢的问题,请原谅。我知道涉及某种形式的类型转换,但我不知道该怎么做......
非常感激!
解决方案
推荐阅读
- php - 带有recaptcha的PHP邮件程序不发送回复电子邮件
- python - 尝试在我的 Mac 中查找 Python 版本时得到不同的结果
- time - 是否可以在 Swi-Prolog 中以微秒为单位获得经过的时间?
- php - PHP脚本崩溃,内存不足
- firewall - PXEBOOT、TFTPD-HPA 和防火墙
- mysql - 在 MySQL 中打印原始字符串
- linux - 如何在 Linux shell 脚本中从用户输入中添加未知数量的数字
- angular - 如何更新表中的多行?角 7
- c++ - C++ 中的有符号溢出和未定义行为 (UB)
- apache-nifi - Nifi 1.10 PutSplunk 是否支持端口的表达式语言?