Getting an below error when I execute my code. I have given the snippet of the code (from my loss function) which is causing an error. I think changing the value of batch_spk_centroid[index1] in calc_cosine_sim_div() is the main cause of an error. batch_spk_centroid has require_grad=True, do we really need require_grad=True for intermediate local variables of functions? Can someone suggest solution for this to handle this?

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [8, 256]], which is output 0 of torch::autograd::CopySlices, is at version 128; expected version 127 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

def forward(self, batch_embedding, batch_label):
        batch_embedding = batch_embedding.reshape((self.hp.train.num_spk_per_batch, self.hp.train.num_utt_per_spk, batch_embedding.size(1)))
        cos_sim_matrix = self.calc_cosine_sim_div(batch_embedding, batch_label)
        torch.clamp(self.w, 1e-6)
        torch.clamp(self.b, 1e-6)
        cos_sim_matrix = cos_sim_matrix * self.w + self.b
        loss = self.embed_loss(batch_embedding, cos_sim_matrix)
        return loss.sum()

    def calc_cosine_sim_div(self, batch_embedding, batch_label):
        batch_spk_centroid = torch.mean(batch_embedding, dim = 1)
        batch_spk_centroid = batch_spk_centroid.detach()
        cos_sim_matrix = []
        for index1, spk_embedding in enumerate(batch_embedding):
            for index2, utt_embedding in enumerate(spk_embedding):
                spk_centroid_org = copy.deepcopy(batch_spk_centroid[index1].data)
                batch_spk_centroid[index1] = ((batch_spk_centroid[index1] * self.hp.train.num_utt_per_spk ) - batch_embedding[index1][index2] ) / (self.hp.train.num_utt_per_spk - 1)
                cos_sim_matrix.append(self.cos_sim(utt_embedding.repeat(batch_embedding.shape[0],1), batch_spk_centroid))
                # batch_spk_centroid[index1] = ((batch_spk_centroid[index1] * (self.hp.train.num_utt_per_spk-1) ) + batch_embedding[index1][index2] ) / self.hp.train.num_utt_per_spk
                batch_spk_centroid[index1] = spk_centroid_org
        cos_sim_matrix = torch.stack(cos_sim_matrix).reshape(batch_embedding.shape[0],batch_embedding.shape[1],batch_embedding.shape[0])
        return cos_sim_matrix