昇思25天学习打卡营第7天|函数式自动微分

It is a bit complex to understand what the math works in gradient and backward process. However, it is easier to capture the conception.

After we import mindspore, we can show how gradient works to change the parameters.

This graph shows what we have (weight w, bias b) and how they are changed according to the prediction z and the label y. x is the input.

w = Parameter(Tensor(np.random.randn(5,3), mindspore.float32), name='w')
b = Parameter(Tensor(np.random.randn(3,),mindspore.float32),name='b')

we construct calculation function, in code below binary_cross_entropy_with_logits is a loss fn that calculate binary cross entropy loss.

def function(x,y,w,b):
    z = ops.matmul(x,w) + b
    loss = ops.binary_cross_entropy_with_logits(z,y,ops.ones_like(z),ops.ones_like(z))
    return loss

loss = function(x,y,w,b)

grad_fn = mindspore.grad(function,(2,3))

fucntion is the function we choose to get its gradient, and grad_position ((2,3)) is the position of weight w and bias b.

We sometimes hope to stop getting gradient.

Here we calculate gradient with z.

def function_with_logits(x, y, w, b):
    z = ops.matmul(x, w) + b
    loss = ops.binary_cross_entropy_with_logits(z, y, ops.ones_like(z), ops.ones_like(z))
    return loss, z
grad_fn = mindspore.grad(function_with_logits,(2,3))
grads = grad_fn(x,y,w,b)
print(grads)

we get something seemed like a tensor.

we hope to erase the effect of z, (then our parameters wouldnot change since z is the only process-related tensor we used to backward and change w,b. See the graph above  second most)

return loss,ops.stop_gradient(z)

using the code to revise return loss,z

then our w, b unchanged after we 

grad_fn = mindspore.grad(function_stop_gradient,(2,3)) #the revised function
grads = grad_fn(x,y,w,b)
print(grads)

the last line is w,b

if we use auxiliary data, we get the same result just like we stop_gradient manually.

grad_fn = mindspore.grad(function_with_logits, (2,3), has_aux = True)

# Define model
class Network(nn.Cell):
    def __init__(self):
        super().__init__()
        self.w = w
        self.b = b

    def construct(self, x):
        z = ops.matmul(x, self.w) + self.b
        return z
# Instantiate model
model = Network()
# Instantiate loss function
loss_fn = nn.BCEWithLogitsLoss()
# Define forward function
def forward_fn(x, y):
    z = model(x)
    loss = loss_fn(z, y)
    return loss
grad_fn = mindspore.value_and_grad(forward_fn, None, weights=model.trainable_params())#weights:the inner belongings of Cell, w and b None:the grad_poisition is not needed manually since it is the inner belongings in Cell too
loss, grads = grad_fn(x, y) #e.g. x = ops.ones(5, mindspore.float32)
print(grads)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值