aws / studio-lab-examples

Example notebooks for working with SageMaker Studio Lab. Sign up for an account at the link below!
https://studiolab.sagemaker.aws
Apache License 2.0
655 stars 193 forks source link

Weather Data Image Classification notebook shows "IndexError: Target 4 is out of bounds" #236

Open typex1 opened 1 year ago

typex1 commented 1 year ago

Link to the notebook Weather Data Image Classification notebook

What aspects of the notebook do not work?

For the following code cell in section "5. Fine-tune the pre-trained model on the labelled weather data" print('Training of Model:') model_ft = train_model(model_ft, criterion, optimizer_ft,model_checkpoint=0,num_epochs=epochs) I get the following error:

`--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[26], line 2 1 print('Training of Model:') ----> 2 model_ft = train_model(model_ft, criterion, optimizer_ft,model_checkpoint=0,num_epochs=epochs)

Cell In[12], line 35, in train_model(model, criterion, optimizer, model_checkpoint, early_stop, numepochs) 33 , preds = torch.max(output, 1) 34 # calculate the batch loss ---> 35 loss = criterion(output, target) 36 # backward pass: compute gradient of the loss with respect to model parameters 37 loss.backward()

File ~/.conda/envs/cv/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, *kwargs) 1190 # If we don't have any hooks, we want to skip the rest of the logic in 1191 # this function, and just call forward. 1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1193 or _global_forward_hooks or _global_forward_pre_hooks): -> 1194 return forward_call(input, **kwargs) 1195 # Do not call functions when jit is used 1196 full_backward_hooks, non_full_backward_hooks = [], []

File ~/.conda/envs/cv/lib/python3.9/site-packages/torch/nn/modules/loss.py:1174, in CrossEntropyLoss.forward(self, input, target) 1173 def forward(self, input: Tensor, target: Tensor) -> Tensor: -> 1174 return F.cross_entropy(input, target, weight=self.weight, 1175 ignore_index=self.ignore_index, reduction=self.reduction, 1176 label_smoothing=self.label_smoothing)

File ~/.conda/envs/cv/lib/python3.9/site-packages/torch/nn/functional.py:3026, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing) 3024 if size_average is not None or reduce is not None: 3025 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 3026 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)

IndexError: Target 4 is out of bounds.`

MicheleMonclova commented 6 months ago

This looks like a Pytorch issue that may be in the code. did you see this: https://discuss.pytorch.org/t/indexerror-target-4-is-out-of-bounds/99172