python - 为什么将输入和模型转换为 float16 不起作用?
问题描述
我正在尝试将输入和深度学习模型更改为 flaot16,因为我使用的是 T4 GPU,并且它们在 fp16 上的工作速度要快得多。这是代码的一部分:我首先有我的模型,然后制作了一些虚拟数据点,以便首先弄清楚数据转换(我用整个批次运行它并得到相同的错误)。
model = CRNN().to(device)
model = model.type(torch.cuda.HalfTensor)
data_recon = torch.from_numpy(data_recon)
data_truth = torch.from_numpy(data_truth)
dummy = data_recon[0:1,:,:,:,:] # Gets just one batch
dummy = dummy.to(device)
dummy = dummy.type(torch.cuda.HalfTensor)
model(dummy)
这是我得到的错误:
> ---------------------------------------------------------------------------
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
解决方案
检查您的CRNN
. 我的猜测是您在模型中存储了“隐藏”状态张量,但不是作为“缓冲区”,而是作为常规张量。因此,将模型转换为 float16 时,隐藏状态保持为 float32 并导致此错误。
尝试将隐藏状态存储为模块中的寄存器(register_buffer
有关详细信息,请参阅)。或者,您可以通过重载模型的方法
将模块中的任何成员张量显式转换为 float16 。.to()
推荐阅读
- r - 如何将滑块输入作为 pie() 的标签包含在内
- spring - spring maven简单代码出错
- c - 查找数组中与给定值最接近的数字的函数(在 C 中)
- blazor-server-side - Blazor 服务器组件 - 如何以两种方式绑定输入 html 标记和调用函数 onchange
- angular - 如何在 Angular 9 中覆盖 addEventListener 的单元测试用例
- c# - MediaTypeHeaderValue 类是线程安全的吗?
- graphql - 从 GraphQL 中的动态键中收集内容
- c# - odata:如何使用 .net core 3.1 在 Odata 中使用 $expand 扩展嵌套的 ICollection 项目
- python - 分阶段烧瓶函数缺少 1 个必需的位置参数
- apache-spark - 子字符串类型不匹配中的 Spark Length 函数