Difference of : Direct assign vs. .copy_()

Hello, in order to get EMA model, i tried the first line in the below block.

        for param_net, param_eval_net in zip(self.train_model.parameters(), self.eval_model.parameters()):
            # param_eval_net.data = param_eval_net.data * self.ema_m + param_net.data.detach() * (1. - self.ema_m)
            param_eval_net.copy_(param_eval_net * self.ema_m + param_net.detach() * (1. - self.ema_m))

However, i found that it didn’t actually update the parameter value.

When i tried to use .copy_ method, i found that the model is normally updated.
What is the difference between two methods??


In general, .data should never be used anymore :slight_smile: So you should not use the first line.
Doing a copy_ is the right thing to do to update the value.