Fastbook chapter 4 :minst classification

Hello

I am trying to solve chapter 4 Further Research MINST classification. I have converted the input training and valid set to tensor 60000 * 784 and 10000 * 784. The dataloader object that I created with input set and One Hot encoded version of labels.
The basic model is as follows

simple_net = nn.Sequential(
nn.Linear(28*28,30),
nn.ReLU(),
nn.Linear(30,10),
nn.LogSoftmax(10),

)
learn = Learner(dls, simple_net, opt_func=SGD,

                loss_func=F.mse_loss, metrics=error_rate)

learn.fit(20, lr = 0.1)
is throwing error “Dimension out of range (expected to be in range of [-2, 1], but got 10)”
Please help to resolve the issue :face_with_hand_over_mouth:

Hi. So that someone can help you, please include the stack trace and the shapes of dls.train.one_batch().

:slightly_smiling_face:

shape of train:
dls.train.one_batch()[0].shape,dls.train.one_batch()[1].shape (torch.Size([256, 784]), torch.Size([256, 10]))
stack trace is as follows
IndexError Traceback (most recent call last)

in ()
1
----> 2 learn.fit(20, lr = 0.1)

15 frames

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
219 self.opt.set_hypers(lr=self.lr if lr is None else lr)
220 self.n_epoch = n_epoch
→ 221 self._with_events(self._do_fit, ‘fit’, CancelFitException, self._end_cleanup)
222
223 def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_fit(self)
210 for epoch in range(self.n_epoch):
211 self.epoch=epoch
→ 212 self._with_events(self._do_epoch, ‘epoch’, CancelEpochException)
213
214 def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch(self)
204
205 def _do_epoch(self):
→ 206 self._do_epoch_train()
207 self._do_epoch_validate()
208

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch_train(self)
196 def _do_epoch_train(self):
197 self.dl = self.dls.train
→ 198 self._with_events(self.all_batches, ‘train’, CancelTrainException)
199
200 def _do_epoch_validate(self, ds_idx=1, dl=None):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in all_batches(self)
167 def all_batches(self):
168 self.n_iter = len(self.dl)
→ 169 for o in enumerate(self.dl): self.one_batch(*o)
170
171 def _do_one_batch(self):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in one_batch(self, i, b)
192 b = self._set_device(b)
193 self._split(b)
→ 194 self._with_events(self._do_one_batch, ‘batch’, CancelBatchException)
195
196 def _do_epoch_train(self):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_one_batch(self)
170
171 def _do_one_batch(self):
→ 172 self.pred = self.model(*self.xb)
173 self(‘after_pred’)
174 if len(self.yb):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py in forward(self, input)
137 def forward(self, input):
138 for module in self:
→ 139 input = module(input)
140 return input
141

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/activation.py in forward(self, input)
1325
1326 def forward(self, input: Tensor) → Tensor:
→ 1327 return F.log_softmax(input, self.dim, _stacklevel=5)
1328
1329 def extra_repr(self):

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel, dtype)
1766 dim = _get_softmax_dim(“log_softmax”, input.dim(), _stacklevel)
1767 if dtype is None:
→ 1768 ret = input.log_softmax(dim)
1769 else:
1770 ret = input.log_softmax(dim, dtype=dtype)

IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 10)

Ah, you can see from the stack trace that that the error occurs in the log_softmax function. Checking the PyTorch docs, the parameter to log_softmax is the dimension along which it is computed, not the length of that dimension.

The correct dimension is probably 1. But you can debug by running the model on a sample batch, and altering the model until it gives a reasonable result. The expected result would have shape [bs,10].

simple_net(dls.train.one_batch()[0])

HTH, Malcolm
:slightly_smiling_face:

Thank you @ Malcolm McLean :ok_hand:

1 Like