Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: The size of tensor a (64) must match the size of tensor b (128) at non-singleton dimension 0 #201

Open
Raj10Gupta opened this issue Jan 16, 2021 · 1 comment

Comments

@Raj10Gupta
Copy link

@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)

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

@Raj10Gupta
Copy link
Author

If you read it ,
pls send it solution
or share it to respective individual or community

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant