Coverage for src/sensai/torch/torch_models/mlp/mlp_models.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-13 22:17 +0000

1import logging 

2from typing import Callable, Optional, Sequence, Union 

3 

4import torch.nn.functional 

5 

6from .mlp_modules import MultiLayerPerceptron 

7from ...torch_base import VectorTorchModel, TorchVectorRegressionModel, TorchVectorClassificationModel, ClassificationOutputMode 

8from ...torch_enums import ActivationFunction 

9from ...torch_opt import NNOptimiserParams 

10from .... import NormalisationMode 

11 

12log: logging.Logger = logging.getLogger(__name__) 

13 

14 

15class MultiLayerPerceptronTorchModel(VectorTorchModel): 

16 def __init__(self, 

17 cuda: bool, 

18 hidden_dims: Sequence[int], 

19 hid_activation_function: Callable[[torch.Tensor], torch.Tensor], 

20 output_activation_function: Optional[Callable[[torch.Tensor], torch.Tensor]], 

21 p_dropout: Optional[float] = None, 

22 input_dim: Optional[int] = None) -> None: 

23 """ 

24 :param cuda: whether to enable CUDA 

25 :param hidden_dims: the sequence of hidden layer dimensions 

26 :param hid_activation_function: the output activation function for hidden layers 

27 :param output_activation_function: the output activation function 

28 :param p_dropout: the dropout probability for training 

29 :param input_dim: the input dimension; if None, use dimensions determined by the input data (number of columns in data frame) 

30 """ 

31 super().__init__(cuda=cuda) 

32 self.hidActivationFunction = ActivationFunction.torch_function_from_any(hid_activation_function) 

33 self.outputActivationFunction = ActivationFunction.torch_function_from_any(output_activation_function) 

34 self.hiddenDims = hidden_dims 

35 self.pDropout = p_dropout 

36 self.overrideInputDim = input_dim 

37 

38 def create_torch_module_for_dims(self, input_dim: int, output_dim: int) -> torch.nn.Module: 

39 return MultiLayerPerceptron(input_dim if self.overrideInputDim is None else self.overrideInputDim, output_dim, self.hiddenDims, 

40 hid_activation_fn=self.hidActivationFunction, output_activation_fn=self.outputActivationFunction, 

41 p_dropout=self.pDropout) 

42 

43 

44class MultiLayerPerceptronVectorRegressionModel(TorchVectorRegressionModel): 

45 def __init__(self, 

46 hidden_dims: Sequence[int] = (5, 5), 

47 hid_activation_function: Callable[[torch.Tensor], torch.Tensor] = torch.sigmoid, 

48 output_activation_function: Optional[Callable[[torch.Tensor], torch.Tensor]] = None, 

49 input_dim: Optional[int] = None, 

50 normalisation_mode: NormalisationMode = NormalisationMode.NONE, 

51 cuda: bool = True, 

52 p_dropout: Optional[float] = None, 

53 nn_optimiser_params: Optional[NNOptimiserParams] = None) -> None: 

54 """ 

55 :param hidden_dims: sequence containing the number of neurons to use in hidden layers 

56 :param hid_activation_function: the activation function (torch.nn.functional.* or torch.*) to use for all hidden layers 

57 :param output_activation_function: the output activation function (torch.nn.functional.* or torch.* or None) 

58 :param input_dim: the input dimension; if None, use dimensions determined by the input data (number of columns in data frame) 

59 :param normalisation_mode: the normalisation mode to apply to input and output data 

60 :param cuda: whether to use CUDA (GPU acceleration) 

61 :param p_dropout: the probability with which to apply dropouts after each hidden layer 

62 :param nn_optimiser_params: parameters for NNOptimiser; if None, use default (or what is specified in nnOptimiserDictParams) 

63 """ 

64 self.hidden_dims = hidden_dims 

65 self.hid_activation_function = hid_activation_function 

66 self.output_activation_function = output_activation_function 

67 self.input_dim = input_dim 

68 self.cuda = cuda 

69 self.p_dropout = p_dropout 

70 super().__init__(self._create_torch_model, normalisation_mode, nn_optimiser_params) 

71 

72 def _create_torch_model(self) -> MultiLayerPerceptronTorchModel: 

73 return MultiLayerPerceptronTorchModel(self.cuda, self.hidden_dims, self.hid_activation_function, self.output_activation_function, 

74 p_dropout=self.p_dropout, input_dim=self.input_dim) 

75 

76 

77class MultiLayerPerceptronVectorClassificationModel(TorchVectorClassificationModel): 

78 def __init__(self, 

79 hidden_dims: Sequence[int] = (5, 5), 

80 hid_activation_function: Callable[[torch.Tensor], torch.Tensor] = torch.sigmoid, 

81 output_activation_function: Optional[Union[Callable[[torch.Tensor], torch.Tensor], str, ActivationFunction]] = 

82 ActivationFunction.LOG_SOFTMAX, 

83 input_dim: Optional[int] = None, 

84 normalisation_mode: NormalisationMode = NormalisationMode.NONE, cuda: bool = True, 

85 p_dropout: Optional[float] = None, 

86 nn_optimiser_params: Optional[NNOptimiserParams] = None) -> None: 

87 """ 

88 :param hidden_dims: sequence containing the number of neurons to use in hidden layers 

89 :param hid_activation_function: the activation function (torch.nn.functional.* or torch.*) to use for all hidden layers 

90 :param output_activation_function: the output activation function (function from torch.nn.functional.*, function name, enum instance or None) 

91 :param input_dim: the input dimension; if None, use dimensions determined by the input data (number of columns in data frame) 

92 :param normalisation_mode: the normalisation mode to apply to input and output data 

93 :param cuda: whether to use CUDA (GPU acceleration) 

94 :param p_dropout: the probability with which to apply dropouts after each hidden layer 

95 :param nn_optimiser_params: parameters for NNOptimiser; if None, use default 

96 """ 

97 self.hidden_dims = hidden_dims 

98 self.hid_activation_function = hid_activation_function 

99 self.output_activation_function = output_activation_function 

100 self.input_dim = input_dim 

101 self.cuda = cuda 

102 self.p_dropout = p_dropout 

103 output_mode = ClassificationOutputMode.for_activation_fn(ActivationFunction.torch_function_from_any(output_activation_function)) 

104 super().__init__(output_mode, 

105 self._create_torch_model, 

106 normalisation_mode, 

107 nn_optimiser_params) 

108 

109 def _create_torch_model(self) -> MultiLayerPerceptronTorchModel: 

110 return MultiLayerPerceptronTorchModel(self.cuda, self.hidden_dims, self.hid_activation_function, self.output_activation_function, 

111 p_dropout=self.p_dropout, input_dim=self.input_dim)