object has no attribute 'backward'
时间: 2023-10-07 07:04:30 浏览: 229
"object has no attribute 'backward'"是一个常见的错误信息,在PyTorch中出现这个错误通常是因为没有对正确的对象调用backward()方法。在你的引用中,错误的代码示例中,出现了这个错误。在这些错误的代码中,my_loss对象和CrossEntropyLoss对象被错误地调用了backward()方法,而应该是对loss对象调用backward()方法。正确的写法是首先将计算得到的损失值赋值给loss变量,然后对loss对象调用backward()方法。例如:
loss = my_loss(my_y, y)
loss.backward()
另外,当你调用CrossEntropyLoss_func函数时,需要将输出和目标作为参数传递给函数,并将函数返回的损失值赋值给loss变量,然后对loss对象调用backward()方法。正确的写法是:
loss = CrossEntropyLoss_func(output, target)
loss.backward()
总结起来,'object has no attribute 'backward''错误通常是因为对错误的对象调用了backward()方法,正确的做法是对损失值对象调用backward()方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [AttributeError: ‘MSELoss‘ object has no attribute ‘backward‘解决方案](https://blog.csdn.net/qq_43631827/article/details/121567607)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [‘CrossEntropyLoss‘ object has no attribute ‘backward‘报错解决](https://blog.csdn.net/L888666Q/article/details/124321572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文