# 遇到的错误
RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient
# 解决办法
把模型构建过程中的list([])换成pytorch原生的nn.ModuleList()。
本文介绍了解决在使用PyTorch构建模型时遇到的一个常见错误:RuntimeError: Cannot insert a Tensor that requires grad as a constant。该错误通常发生在尝试将需要梯度计算的张量作为常量插入模型中时。解决方法是使用PyTorch提供的nn.ModuleList替代Python原生的list([]),以确保所有需要计算梯度的张量都能正确地被跟踪。
# 遇到的错误
RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient
# 解决办法
把模型构建过程中的list([])换成pytorch原生的nn.ModuleList()。
3844
7730
7523
3524

被折叠的 条评论
为什么被折叠?