mxnet学习(3):autograd

reference:https://mxnet.incubator.apache.org/api/python/autograd/autograd.html

1.基本操作

使用x.attach_grad()为梯度分配空间,调用with autograd.record()计算梯度,再使用backward()进行反传

2. BN and dropout

BN, dropout这些层在训练和测试的时候是不同的,BN在训练的时候是根据每个mini-batch的均值和方差进行计算并更新参数,在测试的时候是使用训练集上得到的一个参数进行计算。dropout在测试的时候是没有的。因此需要在不同的时候加以区别(这一点在pytorch中也是存在)。

使用with autograd.record():,默认为train_mode = True

也可以使用with autograd.train_mode(),with autograd.predict_mode()或者with autograd.record(train_mode = False)等方式手动控制

3. pause

可以在with autograd.record():中使用with autograd.pause()暂停保存梯度.

eg:

with autograd.record():
    y = model(x)
    backward([y])
    with autograd.pause():
        # testing, IO, gradient updates..
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章