text stringlengths 1 93.6k |
|---|
batch_time = ExpoAverageMeter() # forward prop. + back prop. time
|
losses = ExpoAverageMeter() # loss (per word decoded)
|
accs = ExpoAverageMeter() # accuracy
|
start = time.time()
|
# Batches
|
for i_batch, (imgs, label_ids, attributes) in enumerate(train_loader):
|
# Zero gradients
|
optimizer.zero_grad()
|
# Set device options
|
imgs = imgs.to(device)
|
# print(img.size())
|
label_ids = label_ids.view(-1).to(device)
|
# print('label_ids: ' + str(label_ids))
|
# print('label_ids.size(): ' + str(label_ids.size()))
|
attributes = attributes.to(device)
|
# print('targets: ' + str(targets))
|
# print('targets.size(): ' + str(targets.size()))
|
X = model(imgs) # (batch_size, 2048)
|
preds = X.mm(W) # (batch_size, 123)
|
_, scores = batched_KNN(preds, 1, attributes_per_class)
|
# print('scores: ' + str(scores))
|
# print('scores.size(): ' + str(scores.size()))
|
# loss = criterion(preds, attributes)
|
# loss = torch.norm(torch.matmul(X, W) - attributes) ** 2 + 1.0 / lambda1 * torch.norm(
|
# X - torch.matmul(attributes, W.t())) ** 2
|
loss = (X.mm(W) - attributes).pow(2).mean() + 1.0 / lambda1 * (X - attributes.mm(W.t())).pow(2).mean()
|
loss.backward()
|
optimizer.step()
|
acc = accuracy(scores, label_ids)
|
# print('acc: ' + str(acc))
|
# Keep track of metrics
|
losses.update(loss.item())
|
batch_time.update(time.time() - start)
|
accs.update(acc)
|
start = time.time()
|
# Print status
|
if i_batch % print_freq == 0:
|
print('Epoch: [{0}][{1}/{2}]\t'
|
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
|
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
|
'Accuracy {accs.val:.3f} ({accs.avg:.3f})'.format(epoch, i_batch, len(train_loader),
|
batch_time=batch_time,
|
loss=losses,
|
accs=accs))
|
def valid(val_loader, model, W, attributes_per_class):
|
model.eval() # eval mode (no dropout or batchnorm)
|
# Loss function
|
# criterion = nn.MSELoss().to(device)
|
batch_time = ExpoAverageMeter() # forward prop. + back prop. time
|
losses = ExpoAverageMeter() # loss (per word decoded)
|
accs = ExpoAverageMeter() # accuracy
|
start = time.time()
|
with torch.no_grad():
|
# Batches
|
for i_batch, (imgs, label_ids, attributes) in enumerate(val_loader):
|
# Set device options
|
imgs = imgs.to(device)
|
label_ids = label_ids.view(-1).to(device)
|
attributes = attributes.to(device) # (batch_size, 123)
|
X = model(imgs) # (batch_size, 2048)
|
preds = X.mm(W) # (batch_size, 123)
|
# loss = criterion(preds, attributes)
|
# loss = torch.norm(torch.matmul(X, W) - attributes) ** 2 + 1.0 / lambda1 * torch.norm(
|
# X - torch.matmul(attributes, W.t())) ** 2
|
loss = (X.mm(W) - attributes).pow(2).mean() + 1.0 / lambda1 * (X - attributes.mm(W.t())).pow(2).mean()
|
_, scores = batched_KNN(preds, 1, attributes_per_class)
|
acc = accuracy(scores, label_ids)
|
# Keep track of metrics
|
losses.update(loss.item())
|
batch_time.update(time.time() - start)
|
accs.update(acc)
|
start = time.time()
|
# Print status
|
if i_batch % print_freq == 0:
|
print('Validation: [{0}/{1}]\t'
|
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
|
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.