Facing inplace operation in NTM similar works

I create a model and it’s similar to NTM(neural turing mathine) .
But when it comes to writing module , it got a inplace operation error. The error happens in the following code.

                w_pattern = self.fc2(pattern[i])
                prev_mem = self.mem

                erase = self.get_erase(w_pattern)  # [1, M]
                era_mat = torch.zeros(self.N, self.M)
                era_mat[self.num_patterns, :] = erase

                add = weight[i] * w_pattern # [1, M]
                add_mat = torch.zeros(self.N, self.M)
                add_mat[self.num_patterns, :] = add
                
                # this line below got an error
                self.mem = (prev_mem * (torch.ones(self.N, self.M) - era_mat) + add_mat)

                self.access_order.append(self.num_patterns)
                self.num_patterns = self.num_patterns + 1

I found this line because I debugged line by line and found that in this line the property self.mem. _version has changed from 1 to 0.
But i don’t know how to solve it ,I’m still trying

I mimicked it from this code.

    def write(self, w, e, a):
        """write to memory (according to section 3.2)."""
        self.prev_mem = self.memory
        self.memory = torch.Tensor(self.batch_size, self.N, self.M)
        erase = torch.matmul(w.unsqueeze(-1), e.unsqueeze(1))
        add = torch.matmul(w.unsqueeze(-1), a.unsqueeze(1))
        self.memory = self.prev_mem * (1 - erase) + add