Pytorch Error

 1. Error ::   ‘DataParallel’ object has no attribute ‘~’

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size, n_layers=1):
    super(RNN, self).__init__()
    self.input_size = input_size
    self.hidden_size = hidden_size
    self.output_size = output_size
    self.n_layers = n_layers

    self.encoder = nn.Embedding(input_size, hidden_size)
    self.gru = nn.GRU(hidden_size, hidden_size, n_layers,batch_first = True)
    self.decoder = nn.Linear(hidden_size, output_size)
    self.init_hidden(batch_size)


def forward(self, input):
    input = self.encoder(input)
    output, self.hidden = self.gru(input,self.hidden)
    output = self.decoder(output.contiguous().view(-1,self.hidden_size))
    output = output.contiguous().view(batch_size,num_steps,N_CHARACTERS)
    #print (output.size())10,50,67

    return output

def init_hidden(self,batch_size):
    self.hidden = Variable(T.zeros(self.n_layers, batch_size, self.hidden_size).cuda())

decoder = T.nn.DataParallel(RNN(N_CHARACTERS, HIDDEN_SIZE, N_CHARACTERS), dim=1).cuda()
# 在训练的时候,提示错误,AttributeError: 'DataParallel' object has no attribute '...'
for epoch in range(EPOCH_):
    hidden = decoder.init_hidden()  

Occur :‘DataParallel’ object has no attribute ‘~’

Solution:When using DataParallel your original module will be in attribute module of the parallel module:

                 简而言之,对象后面需要加个“.module”. 

for epoch in range(EPOCH_): 
    hidden = decoder.module.init_hidden()

2. Error ::   got backend CUDA and dtype Float but  got backend CUDA and dtype Long

   1)检查Tensor变量是否都添加.cuda()

   2)  Tensor变量初始化的时候,以要求的float 型为初始化

         

xs = torch.autograd.Variable(torch.arange(0, h).cuda())

改成

xs = torch.autograd.Variable(torch.arange(0, h).float().cuda())

3. Error:grad can be implicitly created only for scalar outputs

这是因为.backward() 求导的时候,默认只能从标量求导,如下代码中的 J 对 x 求导(一维);

而当从向量求导的时候,就会出现这个错误。如下代码中的 z 对 x求导。

当然,向量也可以求导,具体方法参考:

https://blog.csdn.net/wzx479/article/details/98474847

import torch

x = torch.tensor([[1.,2.,3.],[4.,5.,6.]],requires_grad=True)
y = x+1
z = 2*y*y

# Error
#z.backward() 
#print(x.grad)


# Ok
#J = torch.mean(z)
#J.backward() 
#print(x.grad)

4. SystemError: <built-in method locked of _thread.lock object at 0x0000025E48D7B9B8> returned a result with an error set

在使用C++调用TensorFlow 或者 Pytorch接口时 ,容易出现的问题。

一开始看到问题也是很头疼,类似 https://blog.csdn.net/qq_36679208/article/details/99671102 

以为是系统线程冲突,改Threading.py 文件。结果发现不行,当陷入绝境的时候,身边的大佬说,那系统屏蔽了没问题,就认为系统没问题(厉害啊,不愧是大佬)。

于是检查文件配置,果不其然,是输入文件的路径换了。因为C++调用python的时候,文件系统的报错不能以人性化的方式显示。

出现这个问题,系统和线程没有问题,检查输入文件路径、格式等。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值