optimizer = optim.Adam(model.parameters(), lr=1e-4) #criterion =…
- User
optimizer = optim.Adam(model.parameters(), lr=1e-4) #criterion = nn.TripletMarginLoss(2).to('cuda') criterion = nn.CosineEmbeddingLoss().to('cuda') batch_size = 1024 * 3 dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True)
true_nums = torch.ones(batch_size).to('cuda') false_nums = -torch.ones(batch_size).to('cuda') #loss_nums = torch.cat((true_nums, true_nums, false_nums), dim=0).to('cuda') #print(loss_nums.shape)
model.train() for epoch in range(100): for batch in dataloader: optimizer.zero_grad()
# Move data to GPU true_embeddings1 = batch['true_embedding1'].to('cuda') true_masks1 = batch['true_mask1'].to('cuda') true_embeddings2 = batch['true_embedding2'].to('cuda') true_masks2 = batch['true_mask2'].to('cuda') false_embeddings = batch['false_embedding'].to('cuda') false_masks = batch['false_mask'].to('cuda') # Forward pass anchor_style = model(true_embeddings1, true_masks1) positive_style = model(true_embeddings2, true_masks2) negative_style = model(false_embeddings, false_masks) #combined = torch.cat((anchor_style, positive_style, negative_style), dim=0) #print(combined.shape) loss1 = criterion(anchor_style, positive_style, true_nums) loss2 = criterion(anchor_style, negative_style, false_nums) loss = (loss1 + loss2) / 2 loss.backward() optimizer.step() # Print loss for debugging print(f'Epoch: {epoch+1}, Loss: {loss.item()}')more i train, worse model gets, loss barerly decreases
- Assistant
- User
мб сделать свой лосс по типу marginrankloss на cosine similarity или это тоже самое?
- Assistant