Behavior of "=" operator for Block/HybridBlock

Dear all,

I would please like some advice/comments on what is the behaviour of the assignment operator “=” when it comes to assigning a whole model. E.g. say I have a model

net = gluon.nn.HybridSequential()
with net.name_scope():
    for i in range(5):
        net.add(gluon.nn.Conv2D( ...) )

# copy to other model
net_2 = net

Without having performed weights initialization, net_2 is a copy of the initial net (i.e. an independent object) or a reference to it? Do they have the same names in param_dict? That is, modification of net_2 will pass in net? And also, does similar hold for individual layers? E.g.

class Custom(HybridBlock):
    def __init__(self,**kwards):
        HybridBlock.__init__(self,**kwards):

        with self.name_scope():
            self.feature1 = gluon.nn.Conv2D(...)
            self.feature2 = gluon.nn.Conv2D(...)

    def hybrid_forward(self,F,_x):
        out = self.feature1(_x)
        return self.feature2(out)


net = Custom()
net2 = net # is this copy/reference?

Thank you very much.

edit:

net2 == net # prints True
net2.collect_params().keys() == net.collect_params().keys # prints True
net2.collect_params() == net.collect_params() # prints False

net_2 is a reference to net.

As for:
net2.collect_params() == net.collect_params() # prints False
if you look at the source you have a new ParameterDict initialized on every collect_params() call, that is why you don’t get the same object from the two references block.py:285:

...
ret = ParameterDict(self._params.prefix)
...
return ret

edit: to answer the second part of your question,
modification to net_2 will pass to net.
net_2.this_is_a_test = 'test'
print(net.this_is_a_test)
> test

1 Like

Thank you very much @ThomasDelteil !!