How to create a snapshot of a Block?


#1

By creating a snapshot I mean create a clone of the parameters. For instance, how to clone the parameters of A into B so A and B have the same parameters which can be updated independently?

UPD: It seems that one way to achieve this is to manually call set_data for every parameter in the ParameterDict of the Block. Another possible choice is to use the update method of the ParameterDict.


#2

As you already mentioned you can use set_data. Here an example, how to update the weights in a specific layer of A with the weights from B.

class CustomeBlock(Block):
    def __init__(self, **kwargs):
        super(CustomeBlock, self).__init__(**kwargs)
        with self.name_scope():
            self.dense0 = gluon.nn.Dense(5)
    def forward(self, x):
        return self.dense0(x)

A = CustomeBlock()
A.initialize()

B = CustomeBlock()
B.initialize()

A(nd.array([1,1,1,1,1]))
print(A.dense0._params.values()[0].data())

B(nd.array([1,2,3,4,5]))
print(B.dense0._params.values()[0].data())

B.dense0._params.values()[0].set_data(A.dense0._params.values()[0].data())