NN: Invalid index in gather

I am new to PyTorch and am getting the following error when building a neural network. Please let me know if you need any additional details. Thanks in advance for your help!

(I have also tested generating a random result data set of the same shape and the model works
y = torch.randint(0, 4, (X_train.shape[0], 1), dtype=torch.long)
)

Error:

Re-initializing module because the following parameters were re-set: num_classes, predictor.
Re-initializing optimizer.

RuntimeError Traceback (most recent call last)
in

----> 1 skorch_model.fit(X_train_torch,y_train_torch)

~/.local/lib/python3.6/site-packages/skorch/net.py in fit(self, X, y, **fit_params)
846 self.initialize()
847
→ 848 self.partial_fit(X, y, **fit_params)
849 return self
850

~/.local/lib/python3.6/site-packages/skorch/net.py in partial_fit(self, X, y, classes, **fit_params)
805 self.notify(‘on_train_begin’, X=X, y=y)
806 try:
→ 807 self.fit_loop(X, y, **fit_params)
808 except KeyboardInterrupt:
809 pass

~/.local/lib/python3.6/site-packages/skorch/net.py in fit_loop(self, X, y, epochs, **fit_params)
737 yi_res = yi if not y_train_is_ph else None
738 self.notify(‘on_batch_begin’, X=Xi, y=yi_res, training=True)
→ 739 step = self.train_step(Xi, yi, **fit_params)
740 train_batch_count += 1
741 self.history.record_batch(‘train_loss’, step[‘loss’].item())

~/.local/lib/python3.6/site-packages/skorch/net.py in train_step(self, Xi, yi, **fit_params)
662 step_accumulator.store_step(step)
663 return step[‘loss’]
→ 664 self.optimizer_.step(step_fn)
665 self.optimizer_.zero_grad()
666 return step_accumulator.get_step()

~/.local/lib/python3.6/site-packages/torch/optim/sgd.py in step(self, closure)
78 loss = None
79 if closure is not None:
—> 80 loss = closure()
81
82 for group in self.param_groups:

~/.local/lib/python3.6/site-packages/skorch/net.py in step_fn()
659 step_accumulator = self.get_train_step_accumulator()
660 def step_fn():
→ 661 step = self.train_step_single(Xi, yi, **fit_params)
662 step_accumulator.store_step(step)
663 return step[‘loss’]

~/.local/lib/python3.6/site-packages/skorch/net.py in train_step_single(self, Xi, yi, **fit_params)
602 self.module_.train()
603 y_pred = self.infer(Xi, **fit_params)
→ 604 loss = self.get_loss(y_pred, yi, X=Xi, training=True)
605 loss.backward()
606

~/.local/lib/python3.6/site-packages/skorch/net.py in get_loss(self, y_pred, y_true, X, training)
1097 “”"
1098 y_true = to_tensor(y_true, device=self.device)
→ 1099 return self.criterion_(y_pred, y_true)
1100
1101 def get_dataset(self, X, y=None):

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
539 result = self._slow_forward(*input, **kwargs)
540 else:
→ 541 result = self.forward(*input, **kwargs)
542 for hook in self._forward_hooks.values():
543 hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/spacecutter/losses.py in forward(self, y_pred, y_true)
106 return cumulative_link_loss(y_pred, y_true,
107 reduction=self.reduction,
→ 108 class_weights=self.class_weights)

~/.local/lib/python3.6/site-packages/spacecutter/losses.py in cumulative_link_loss(y_pred, y_true, reduction, class_weights)
66 “”"
67 eps = 1e-15
—> 68 likelihoods = torch.clamp(torch.gather(y_pred, 1, y_true), eps, 1 - eps)
69 neg_log_likelihood = -torch.log(likelihoods)
70

RuntimeError: Invalid index in gather at /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:657

Could you post the code for cumulative_link_loss and if possible the tensors you are passing to it (you can also create dummy random values for them)?
Apparently the torch.gather call throws an invalid indexing error.

1 Like

That is from the spacecutter library

from skorch import NeuralNet
import numpy as np
import torch
from torch import nn
from sklearn.model_selection import train_test_split
from spacecutter.callbacks import AscensionCallback
from spacecutter.losses import CumulativeLinkLoss
from spacecutter.models import OrdinalLogisticModel

predictor = nn.Sequential(
nn.Linear(num_features, num_features),
nn.ReLU(),
nn.Linear(num_features, 1)
)

from skorch.net import NeuralNet
skorch_model = NeuralNet(
module=OrdinalLogisticModel,
module__predictor=predictor,
module__num_classes=num_classes,
criterion=CumulativeLinkLoss,
train_split=None,
callbacks=[
(‘ascension’, AscensionCallback()),
],
)