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

ValueError: Network output phi is too small for idx_durations #59

Closed
zzs1852 opened this issue Jan 7, 2021 · 3 comments
Closed

ValueError: Network output phi is too small for idx_durations #59

zzs1852 opened this issue Jan 7, 2021 · 3 comments

Comments

@zzs1852
Copy link

zzs1852 commented Jan 7, 2021

Ubuntu 16.04.6 LTS
Python 3.7.7
torch 1.2.0

Hi, as I followed the example_4 in ”Get Started“, something wrong with this:

callbacks = [tt.cb.EarlyStopping(patience=5)]
epochs = 50
verbose = True
log = model.fit_dataloader(dl_train, epochs, callbacks, verbose, val_dataloader=dl_test) 

here is my network with input-shape(32,1,246,246) :

class Net(nn.Module):
    def __init__(self,n_intervals):
        super().__init__()
        self.backbone = models.resnet50(pretrained=False)
        self.backbone.conv1 = nn.Conv2d(1,64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
        self.fc1 = nn.Linear(1000, 128)
        self.fc2 = nn.Linear(128,n_intervals)
        self.bn1 = nn.BatchNorm1d(1000)
        self.bn2 = nn.BatchNorm1d(128)
        
    def forward(self, x):
        x = self.backbone(x)
        x = self.bn1(x)
        x = F.relu(x)
        x = self.fc1(x)
        x = self.bn2(x)
        x = F.relu(x)
        x = self.fc2(x)
        return x

The error information as follow:

ValueError                                Traceback (most recent call last)
<ipython-input-18-7ae9e531dd45> in <module>
      2 epochs = 50
      3 verbose = True
----> 4 log = model.fit_dataloader(dl_train, epochs, callbacks, verbose, val_dataloader=dl_test)

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/torchtuples/base.py in fit_dataloader(self, dataloader, epochs, callbacks, verbose, metrics, val_dataloader)
    227                 if stop: break
    228                 self.optimizer.zero_grad()
--> 229                 self.batch_metrics = self.compute_metrics(data, self.metrics)
    230                 self.batch_loss = self.batch_metrics['loss']
    231                 self.batch_loss.backward()

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/torchtuples/base.py in compute_metrics(self, data, metrics)
    178         out = self.net(*input)
    179         out = tuplefy(out)
--> 180         return {name: metric(*out, *target) for name, metric in metrics.items()}
    181 
    182     def _setup_metrics(self, metrics=None):

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/torchtuples/base.py in <dictcomp>(.0)
    178         out = self.net(*input)
    179         out = tuplefy(out)
--> 180         return {name: metric(*out, *target) for name, metric in metrics.items()}
    181 
    182     def _setup_metrics(self, metrics=None):

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    545             result = self._slow_forward(*input, **kwargs)
    546         else:
--> 547             result = self.forward(*input, **kwargs)
    548         for hook in self._forward_hooks.values():
    549             hook_result = hook(self, input, result)

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/pycox/models/loss.py in forward(self, phi, idx_durations, events)
    466     """
    467     def forward(self, phi: Tensor, idx_durations: Tensor, events: Tensor) -> Tensor:
--> 468         return nll_logistic_hazard(phi, idx_durations, events, self.reduction)
    469 
    470 

~/anaconda3/envs/pytorch37/lib/python3.7/site-packages/pycox/models/loss.py in nll_logistic_hazard(phi, idx_durations, events, reduction)
     41         raise ValueError(f"Network output `phi` is too small for `idx_durations`."+
     42                          f" Need at least `phi.shape[1] = {idx_durations.max().item()+1}`,"+
---> 43                          f" but got `phi.shape[1] = {phi.shape[1]}`")
     44     if events.dtype is torch.bool:
     45         events = events.float()

ValueError: Network output `phi` is too small for `idx_durations`. Need at least `phi.shape[1] = 2717.0`, but got `phi.shape[1] = 4
@zzs1852 zzs1852 closed this as completed Jan 8, 2021
@havakv
Copy link
Owner

havakv commented Jan 8, 2021

@zzs1852 did you find out what was wrong here, since you closed the issue?

@zzs1852
Copy link
Author

zzs1852 commented Jan 9, 2021

@zzs1852 did you find out what was wrong here, since you closed the issue?
Yeah! I made a stupid mistake for inputting the incorrect label and it works well now.

@chengzhishi
Copy link

I had the same error when I had the wrong last layer.

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

3 participants