Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么将输入和模型转换为float16不';不行?_Python_Pytorch - Fatal编程技术网

Python 为什么将输入和模型转换为float16不';不行?

Python 为什么将输入和模型转换为float16不';不行?,python,pytorch,Python,Pytorch,我正在尝试将输入和深度学习模型更改为flaot16,因为我使用的是T4 GPU,它们在fp16上的工作速度要快得多。 下面是代码的一部分:我首先建立了我的模型,然后制作了一些虚拟数据点,以便首先计算出数据转换(我用整个批运行它,得到了相同的错误) 下面是我得到的错误: > --------------------------------------------------------------------------- RuntimeError

我正在尝试将输入和深度学习模型更改为flaot16,因为我使用的是T4 GPU,它们在fp16上的工作速度要快得多。 下面是代码的一部分:我首先建立了我的模型,然后制作了一些虚拟数据点,以便首先计算出数据转换(我用整个批运行它,得到了相同的错误)

下面是我得到的错误:

> --------------------------------------------------------------------------- 
RuntimeError                              Traceback (most recent call
> last) <ipython-input-27-1fe8ecc524aa> in <module>
> ----> 1 model(dummy)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-12-06f39f9304a1> in forward(self, inputs, test)
>      57 
>      58             net['t%d_x0'%(i-1)] = net['t%d_x0'%(i-1)].view(times, batch, self.filter_size, width,
> height)
> ---> 59             net['t%d_x0'%i] = self.bcrnn(inputs, net['t%d_x0'%(i-1)], test)
>      60             net['t%d_x0'%i] = net['t%d_x0'%i].view(-1, self.filter_size, width, height)
>      61 
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-11-b687949e9ce5> in forward(self, inputs,
> input_iteration, test)
>      31         hidden = initial_hidden
>      32         for i in range(times):
> ---> 33             hidden = self.CRNN(inputs[i], input_iteration[i], hidden)
>      34             output_forward.append(hidden)
>      35         output_forward = torch.cat(output_forward)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-10-15c0b221226b> in forward(self, inputs,
> hidden_iteration, hidden)
>      23     def forward(self, inputs, hidden_iteration, hidden):
>      24         in_to_hid = self.i2h(inputs)
> ---> 25         hid_to_hid = self.h2h(hidden)
>      26         ih_to_ih = self.ih2ih(hidden_iteration)
>      27 
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in
> forward(self, input)
>     336                             _pair(0), self.dilation, self.groups)
>     337         return F.conv2d(input, self.weight, self.bias, self.stride,
> --> 338                         self.padding, self.dilation, self.groups)
>     339 
>     340 
> 
> RuntimeError: Input type (torch.cuda.FloatTensor) and weight type
> (torch.cuda.HalfTensor) should be the same
-------------------------------------------------------------
运行时错误回溯(最近的调用)
>最后)在
>--->1模型(虚拟)
> 
>/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
>在调用中(self,*input,**kwargs)
>491结果=self.\u slow\u forward(*输入,**kwargs)
>492其他:
>-->493结果=self.forward(*输入,**kwargs)
>494用于钩住自身。\u向前\u钩住.values():
>495钩子结果=钩子(自身、输入、结果)
> 
>正向输入(自身、输入、测试)
>      57 
>58净['t%d\u x0'(i-1)]=净['t%d\u x0'(i-1)]。视图(次数、批次、自过滤大小、宽度、,
>高度)
>--->59净['t%d\u x0'%i]=self.bcrnn(输入,净['t%d\u x0'(i-1)],测试)
>60净['t%d\u x0'%i]=净['t%d\u x0'%i]。视图(-1,self.filter\u大小、宽度、高度)
>      61 
> 
>/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
>在调用中(self,*input,**kwargs)
>491结果=self.\u slow\u forward(*输入,**kwargs)
>492其他:
>-->493结果=self.forward(*输入,**kwargs)
>494用于钩住自身。\u向前\u钩住.values():
>495钩子结果=钩子(自身、输入、结果)
> 
>在正向(自我、输入、,
>输入(迭代、测试)
>31隐藏=初始值隐藏
>范围内的i为32(次):
>-->33 hidden=self.CRNN(输入[i],输入迭代[i],隐藏)
>34输出前向追加(隐藏)
>35输出向前=火炬。cat(输出向前)
> 
>/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
>在调用中(self,*input,**kwargs)
>491结果=self.\u slow\u forward(*输入,**kwargs)
>492其他:
>-->493结果=self.forward(*输入,**kwargs)
>494用于钩住自身。\u向前\u钩住.values():
>495钩子结果=钩子(自身、输入、结果)
> 
>在正向(自我、输入、,
>隐藏(迭代,隐藏)
>23 def前进(自身、输入、隐藏迭代、隐藏):
>24 in_至_hid=self.i2h(输入)
>-->25 hid\u至\u hid=self.h2h(隐藏)
>26 ih_to_ih=self.ih2ih(隐藏迭代)
>      27 
> 
>/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
>在调用中(self,*input,**kwargs)
>491结果=self.\u slow\u forward(*输入,**kwargs)
>492其他:
>-->493结果=self.forward(*输入,**kwargs)
>494用于钩住自身。\u向前\u钩住.values():
>495钩子结果=钩子(自身、输入、结果)
> 
>/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in
>转发(自我,输入)
>336对(0),自膨胀,自组)
>337返回F.conv2d(输入、自身重量、自身偏差、自身步幅、,
>-->338自填充、自膨胀、自组)
>     339 
>     340 
> 
>运行时错误:输入类型(torch.cuda.FloatTensor)和权重类型
>(torch.cuda.HalfTensor)应该是相同的

查看您的
CRNN
实现。我的猜测是,模型中存储了“隐藏”状态张量,但不是作为“缓冲区”,而是作为常规张量。因此,当将模型强制转换为float16时,隐藏状态保持float32,并导致此错误

尝试将隐藏状态存储为模块中的寄存器(有关更多信息,请参阅)。

或者,通过重载模型的
.to()
方法,可以显式地将模块中的任何成员张量转换为float16

查看您的
CRNN
实现。我的猜测是,模型中存储了“隐藏”状态张量,但不是作为“缓冲区”,而是作为常规张量。因此,当将模型强制转换为float16时,隐藏状态保持float32,并导致此错误

尝试将隐藏状态存储为模块中的寄存器(有关更多信息,请参阅)。

或者,通过重载模型的
.to()
方法,可以显式地将模块中的任何成员张量转换为float16

您能提供一些关于您的型号、torch和cuda版本的信息吗。我可以成功运行带有torch 1.1.0和cuda 10.0的float16 Resnet18型号。您能提供一些关于您的型号、torch和cuda版本的信息吗。我可以使用torch 1.1.0和cuda 10.0成功运行float16 Resnet18模型。
> --------------------------------------------------------------------------- 
RuntimeError                              Traceback (most recent call
> last) <ipython-input-27-1fe8ecc524aa> in <module>
> ----> 1 model(dummy)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-12-06f39f9304a1> in forward(self, inputs, test)
>      57 
>      58             net['t%d_x0'%(i-1)] = net['t%d_x0'%(i-1)].view(times, batch, self.filter_size, width,
> height)
> ---> 59             net['t%d_x0'%i] = self.bcrnn(inputs, net['t%d_x0'%(i-1)], test)
>      60             net['t%d_x0'%i] = net['t%d_x0'%i].view(-1, self.filter_size, width, height)
>      61 
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-11-b687949e9ce5> in forward(self, inputs,
> input_iteration, test)
>      31         hidden = initial_hidden
>      32         for i in range(times):
> ---> 33             hidden = self.CRNN(inputs[i], input_iteration[i], hidden)
>      34             output_forward.append(hidden)
>      35         output_forward = torch.cat(output_forward)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> <ipython-input-10-15c0b221226b> in forward(self, inputs,
> hidden_iteration, hidden)
>      23     def forward(self, inputs, hidden_iteration, hidden):
>      24         in_to_hid = self.i2h(inputs)
> ---> 25         hid_to_hid = self.h2h(hidden)
>      26         ih_to_ih = self.ih2ih(hidden_iteration)
>      27 
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py
> in __call__(self, *input, **kwargs)
>     491             result = self._slow_forward(*input, **kwargs)
>     492         else:
> --> 493             result = self.forward(*input, **kwargs)
>     494         for hook in self._forward_hooks.values():
>     495             hook_result = hook(self, input, result)
> 
> /opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/conv.py in
> forward(self, input)
>     336                             _pair(0), self.dilation, self.groups)
>     337         return F.conv2d(input, self.weight, self.bias, self.stride,
> --> 338                         self.padding, self.dilation, self.groups)
>     339 
>     340 
> 
> RuntimeError: Input type (torch.cuda.FloatTensor) and weight type
> (torch.cuda.HalfTensor) should be the same