How to savely clone a pytorch module? Is creating a new one faster? #Pytorch

Viewed 116

I need to repeatly create some modules, which are completely same.

In the following code, I fill two same lists with N same modules.

        MIM_N_cell = []
        MIM_S_cell = []
        for i in range(self.num_layers - 1):
            new_MIM_N_cell = MIM_NS_cell(input_dim=self.hidden_dim,
                                         hidden_dim=self.hidden_dim,
                                         kernel_size=self.kernel_size,
                                         model_cfg=model_cfg)
            new_MIM_S_cell = MIM_NS_cell(input_dim=self.hidden_dim,
                                         hidden_dim=self.hidden_dim,
                                         kernel_size=self.kernel_size,
                                         model_cfg=model_cfg)
            MIM_N_cell.append(new_MIM_N_cell)
            MIM_S_cell.append(new_MIM_S_cell)

        self.MIM_N_cell = nn.ModuleList(MIM_N_cell)
        self.MIM_S_cell = nn.ModuleList(MIM_S_cell)

Should I use .clone() instead of creating a new module each time?

I guess cloning may be faster but I am not aware of the side affects of it. If it worth to clone, how to use it safely (i.e. don't change the training/testing behaviour of the network)

0 Answers
Related