Pytorch入门

在PyTorch中,对requires_grad=True的张量不能进行就地操作或直接转换为numpy。要安全地转换,需先detach并可能clone。文章强调了在RL中切换使用numpy和PyTorch,以及定义网络、计算损失、零化梯度、反向传播和更新权重的训练流程。
摘要由CSDN通过智能技术生成

Pytorch tips:(From Berkeley)

A few things to watch out for variables:

  • You can’t do any in-place operations on a tensor that has requires_grad=True. (This prevents you from inadvertently mutating it in a way that isn’t tracked for backprop purposes.)

  • You also can’t convert a tensor with requires_grad=True to numpy (for the same reason as above). Instead, you need to detach it first, e.g. y.detach().numpy().

  • Even though y.detach() returns a new tensor, that tensor occupies the same memory as y. Unfortunately, PyTorch lets you make changes to y.detach() or y.detach.numpy() which will affect y as well! If you want to safely mutate the detached version, you should use y.detach().clone() instead, which will create a tensor in new memory.

RL Connection: You would want to be doing simulator-related tasks with numpy, convert to torch when doing model-related tasks, and convert back to feed output into simulator.

Tips for define networks and compute gradients

  • Defined a class for our neural network (subclass of nn.Module)
  • Specified a loss function (MSE loss) and optimizer (Adam) --> make sure to pass all model parameters (especially for multimodal)
  • Performed training by doing the following in a loop:
    • Make prediction
    • Compute loss
    • Zero the stored gradients
    • Backprop the loss with .backward()
    • Update the weights by taking a step of gradient descent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值