You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@torch.no_grad()
def evaluate(model,val_loader):
model.eval()
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
def fit(epochs,lr,model,train_loader,val_loader,opt_func = torch.optim.SGD):
history = []
optimizer = opt_func(model.parameters(),lr)
for epoch in range(epochs):
model.train()
train_losses = []
for batch in train_loader:
loss = model.training_step(batch)
train_losses.append(loss)
loss.backward()
optimizer.step()
optimizer.zero_grad()
result = evaluate(model,val_loader)
result['train_loss'] = torch.stack(train_losses).mean().item()
model.epoch_end(epoch,result)
history.append(result)
return history
RuntimeError Traceback (most recent call last)
in
----> 1 history = fit(epochs,learning_rate, model,train_loader,test_loader,opt_func = optimizer)
@torch.no_grad()
def evaluate(model,val_loader):
model.eval()
outputs = [model.validation_step(batch) for batch in val_loader]
return model.validation_epoch_end(outputs)
def fit(epochs,lr,model,train_loader,val_loader,opt_func = torch.optim.SGD):
history = []
optimizer = opt_func(model.parameters(),lr)
for epoch in range(epochs):
model.train()
train_losses = []
for batch in train_loader:
loss = model.training_step(batch)
train_losses.append(loss)
loss.backward()
optimizer.step()
optimizer.zero_grad()
RuntimeError Traceback (most recent call last)
in
----> 1 history = fit(epochs,learning_rate, model,train_loader,test_loader,opt_func = optimizer)
in fit(epochs, lr, model, train_loader, val_loader, opt_func)
18 optimizer.zero_grad()
19
---> 20 result = evaluate(model,val_loader)
21 result['train_loss'] = torch.stack(train_losses).mean().item()
22 model.epoch_end(epoch,result)
/opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
24 def decorate_context(*args, **kwargs):
25 with self.class():
---> 26 return func(*args, **kwargs)
27 return cast(F, decorate_context)
28
in evaluate(model, val_loader)
2 def evaluate(model,val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)
6
in (.0)
2 def evaluate(model,val_loader):
3 model.eval()
----> 4 outputs = [model.validation_step(batch) for batch in val_loader]
5 return model.validation_epoch_end(outputs)
6
in validation_step(self, batch)
14 out = self(images)
15 loss = F.cross_entropy(out,labels)
---> 16 acc = accuracy(out,labels)
17 return {'val_loss': loss.detach(), 'val_acc': acc}
18
in accuracy(outputs, val_loader)
1 def accuracy(outputs, val_loader):
2 _,preds = torch.max(outputs,dim=1)
----> 3 return torch.tensor(torch.sum(preds == labels).item() / len(preds))
4
5 class MalariaPrediction(nn.Module):
/opt/conda/lib/python3.7/site-packages/torch/tensor.py in wrapped(*args, **kwargs)
25 return handle_torch_function(wrapped, args, *args, **kwargs)
26 try:
---> 27 return f(*args, **kwargs)
28 except TypeError:
29 return NotImplemented
RuntimeError: The size of tensor a (64) must match the size of tensor b (128) at non-singleton dimension 0
The text was updated successfully, but these errors were encountered: