MindSpore: 如何在静态图模式下,在construct函数里更新网络权重?

import numpy as np

import mindspore

import mindspore as ms

from mindspore import ops

from mindspore import nn, Tensor, Parameter, context

# context.set_context(mode=context.PYNATIVE_MODE)

class MyConv2d(nn.Cell):

    def __init__(self):

        super().__init__()

        self.conv = nn.Conv2d(120, 240, 4, has_bias=False, weight_init='normal')

        self.tmp = ms.ParameterTuple(self.get_parameters()) 

    def construct(self, x, w):

        # w是权重

        for weight in self.tmp:

            # 更新权重

            ops.Assign()(weight, w)

        return self.conv(x)

x = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)

w = Tensor(np.ones([240, 120, 4, 4]), mindspore.float32)

output = MyConv2d()(x, w)

print(output)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值