You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

classification_dataset.py 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from typing import Any, Callable, List, Tuple, Optional
  2. import torch
  3. from torch.utils.data import Dataset
  4. class ClassificationDataset(Dataset):
  5. """
  6. Dataset used for classification task.
  7. Parameters
  8. ----------
  9. X : List[Any]
  10. The input data.
  11. Y : List[int]
  12. The target data.
  13. transform : Callable[..., Any], optional
  14. A function/transform that takes an object and returns a transformed version.
  15. Defaults to None.
  16. """
  17. def __init__(self, X: List[Any], Y: List[int], transform: Optional[Callable[..., Any]] = None):
  18. if (not isinstance(X, list)) or (not isinstance(Y, list)):
  19. raise ValueError("X and Y should be of type list.")
  20. if len(X) != len(Y):
  21. raise ValueError("Length of X and Y must be equal.")
  22. self.X = X
  23. self.Y = torch.LongTensor(Y)
  24. self.transform = transform
  25. def __len__(self) -> int:
  26. """
  27. Return the length of the dataset.
  28. Returns
  29. -------
  30. int
  31. The length of the dataset.
  32. """
  33. return len(self.X)
  34. def __getitem__(self, index: int) -> Tuple[Any, torch.Tensor]:
  35. """
  36. Get the item at the given index.
  37. Parameters
  38. ----------
  39. index : int
  40. The index of the item to get.
  41. Returns
  42. -------
  43. Tuple[Any, torch.Tensor]
  44. A tuple containing the object and its label.
  45. """
  46. if index >= len(self):
  47. raise ValueError("index range error")
  48. x = self.X[index]
  49. if self.transform is not None:
  50. x = self.transform(x)
  51. y = self.Y[index]
  52. return x, y

An efficient Python toolkit for Abductive Learning (ABL), a novel paradigm that integrates machine learning and logical reasoning in a unified framework.