Hi,
I wrote bellowing code to use original layer.
class binary_activate(Function):
    def forward(self,x):
    positives = torch.ge(x, 0)
    negatives = torch.le(x, 0)
    le0xmin1 = torch.mul(negatives.float(), -1)
    binary_output = positives.float() + le0xmin1.float()
    self.save_for_backward(x)
    return binary_output

    def backward(self, grad_output):
        result = self.saved_tensors[0]
        grad_input = F.hardtanh(Variable(result)).data * grad_output
        return grad_input

class Model1(nn.Module):
    def __init__(self):
        super(Model1, self).__init__()
        self.fc1 = nn.Linear(INPUT_SIZE,HIDDEN1)
        self.fc2 = nn.Linear(HIDDEN1, HIDDEN2)
        self.fc3 = nn.Linear(HIDDEN2, HIDDEN3)
        self.fc4 = nn.Linear(HIDDEN3, OUTPUT_SIZE)
        self.binary = binary_activate()

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)
        x = self.binary(x)
        return x

model = Model1()
model.cuda()
optimizer = torch.optim.Adam(model.parameters(),lr=0.0002)
model.train()
criterion = nn.HingeEmbeddingLoss().cuda()
for i in range(EPOCH):
        x = Variable(torch.FloatTensor(torch.randn(batch_size,INPUT_SIZE)).cuda())
        logits = model(x)
        loss = criterion(logits, x)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

Then, I got error which said
result = self.saved_tensors[0]
RuntimeError: Trying to backward through the graph second time, but the buffers have already been freed. Please specify retain_variables=True when calling backward for the first time.

Though Error message specifies the problem somehow, I couldn’t figure out the way to solve it.
How can I backward correctly?
Thanks