thx for relpying!
i have one more question.
when execute without .item(), gpu memory usage keep increase continously so 1 epoch can’t be done
class TripletLoss(nn.Module):
    def __init__(self, a1, a2, a3):
        super(TripletLoss, self).__init__()
        self.a1 = a1
        self.a2 = a2
        self.a3 = a3
        
    def forward(self, anchor, positive, negative):
        distance_positive = (anchor - positive).pow(2).sum(1)
        distance_negative = (anchor - negative).pow(2).sum(1)
        loss1 = distance_positive - distance_negative + self.a1
        loss2 = self.a3 * (distance_positive)
        losses = loss1 + loss2
        
        return losses.mean()

class TripletNet(nn.Module):
    def __init__(self, device):
        super(TripletNet, self).__init__()
        
        self.embedding_net = Encoder_id(in_channel = 3, n_ker = 32, device=device).to(device)
        
    def forward(self, s1, s2, t):
        s1 = self.embedding_net(s1)
        s2 = self.embedding_net(s2)
        t = self.embedding_net(t)
        
        return s1, s2, t

class Encoder_id(nn.Module):
    def __init__(self, in_channel, n_ker, device):
        super(Encoder_id, self).__init__()
        
        self.encoder = nn.Sequential(
            nn.Conv2d(in_channel, n_ker, 4, 2),
            nn.LeakyReLU(),
            nn.Conv2d(n_ker, n_ker * 2, 4, 2),
            nn.LeakyReLU(),
            nn.Conv2d(n_ker * 2, n_ker * 4, 4, 2),
            nn.LeakyReLU(),
            nn.Conv2d(n_ker * 4, n_ker * 8, 4, 2),
            nn.LeakyReLU(),
            nn.Conv2d(n_ker * 8, n_ker * 16, 4, 2),
            nn.LeakyReLU()
        )
        
        self.device = device
        
    def forward(self, x):
        batch_size = x.size(0)
        
        enc = self.encoder(x)

        enc = enc.view(batch_size, -1)

        fc = nn.Linear(enc.size(1), 128).to(self.device)
        enc = fc(enc)
        enc = torch.sigmoid(enc)
        
        return enc

this is my IdentLoss function, encoder and embedding function for IdentLoss input
is any wrong my code??