eXNN.NetBayesianization.api

 1from typing import Dict, Optional
 2import torch
 3import torch.optim
 4from eXNN.NetBayesianization.wrap import create_bayesian_wrapper
 5
 6
 7def BasicBayesianPrediction(data: torch.Tensor,
 8                            model: torch.nn.Module,
 9                            n_iter: int,
10                            mode: str,
11                            p: Optional[float] = None,
12                            a: Optional[float] = None,
13                            b: Optional[float] = None) -> Dict[str, torch.Tensor]:
14    """Function computes mean and standard deviation of bayesian equivalent
15        of a neural network.
16
17    Args:
18        data (torch.Tensor): input data of shape NxC1x...xCk,
19            where N is the number of data points,
20            C1,...,Ck are dimensions of each data point
21        model (torch.nn.Module): neural network
22        n_iter (int): number of samplings form the bayesian equivalent of a neural network
23        mode (str): bayesianization method (`basic` or `beta`)
24        p (Optional[float], optional): parameter of dropout (for `basic`
25            bayesianization). Defaults to None.
26        a (Optional[float], optional): parameter of beta distribution (for `beta`
27            bayesianization). Defaults to None.
28        b (Optional[float], optional): parameter of beta distribution (for `beta`
29            bayesianization). Defaults to None.
30
31    Returns:
32        Dict[str, torch.Tensor]: dictionary with `mean` and `std` of prediction
33    """
34    return BasicBayesianWrapper(model, mode, p=p, a=a, b=b).predict(data, n_iter)
35
36
37class BasicBayesianWrapper:
38    def __init__(self,
39                 model: torch.nn.Module,
40                 mode: str,
41                 p: Optional[float] = None,
42                 a: Optional[float] = None,
43                 b: Optional[float] = None):
44        """Class representing bayesian equivalent of a neural network.
45
46        Args:
47            model (torch.nn.Module): neural network
48            mode (str): bayesianization method (`basic` or `beta`)
49            p (Optional[float], optional): parameter of dropout (for `basic`
50                bayesianization). Defaults to None.
51            a (Optional[float], optional): parameter of beta distribution (for `beta`
52                bayesianization). Defaults to None.
53            b (Optional[float], optional): parameter of beta distribution (for `beta`
54                bayesianization). Defaults to None.
55        """
56        self.model = create_bayesian_wrapper(model, mode, p=p, a=a, b=b)
57
58    def predict(self, data, n_iter) -> Dict[str, torch.Tensor]:
59        """Function computes mean and standard deviation of bayesian equivalent
60            of a neural network.
61
62        Args:
63            data (_type_): input data of shape NxC1x...xCk,
64                where N is the number of data points,
65                C1,...,Ck are dimensions of each data point
66            n_iter (_type_): number of samplings form the bayesian equivalent
67                of a neural network
68
69        Returns:
70            Dict[str, torch.Tensor]: dictionary with `mean` and `std` of prediction
71        """
72        res = self.model.mean_forward(data, n_iter)
73        return {'mean': res[0], 'std': res[1]}
def BasicBayesianPrediction( data: torch.Tensor, model: torch.nn.modules.module.Module, n_iter: int, mode: str, p: Union[float, NoneType] = None, a: Union[float, NoneType] = None, b: Union[float, NoneType] = None) -> Dict[str, torch.Tensor]:
 8def BasicBayesianPrediction(data: torch.Tensor,
 9                            model: torch.nn.Module,
10                            n_iter: int,
11                            mode: str,
12                            p: Optional[float] = None,
13                            a: Optional[float] = None,
14                            b: Optional[float] = None) -> Dict[str, torch.Tensor]:
15    """Function computes mean and standard deviation of bayesian equivalent
16        of a neural network.
17
18    Args:
19        data (torch.Tensor): input data of shape NxC1x...xCk,
20            where N is the number of data points,
21            C1,...,Ck are dimensions of each data point
22        model (torch.nn.Module): neural network
23        n_iter (int): number of samplings form the bayesian equivalent of a neural network
24        mode (str): bayesianization method (`basic` or `beta`)
25        p (Optional[float], optional): parameter of dropout (for `basic`
26            bayesianization). Defaults to None.
27        a (Optional[float], optional): parameter of beta distribution (for `beta`
28            bayesianization). Defaults to None.
29        b (Optional[float], optional): parameter of beta distribution (for `beta`
30            bayesianization). Defaults to None.
31
32    Returns:
33        Dict[str, torch.Tensor]: dictionary with `mean` and `std` of prediction
34    """
35    return BasicBayesianWrapper(model, mode, p=p, a=a, b=b).predict(data, n_iter)

Function computes mean and standard deviation of bayesian equivalent of a neural network.

Args: data (torch.Tensor): input data of shape NxC1x...xCk, where N is the number of data points, C1,...,Ck are dimensions of each data point model (torch.nn.Module): neural network n_iter (int): number of samplings form the bayesian equivalent of a neural network mode (str): bayesianization method (basic or beta) p (Optional[float], optional): parameter of dropout (for basic bayesianization). Defaults to None. a (Optional[float], optional): parameter of beta distribution (for beta bayesianization). Defaults to None. b (Optional[float], optional): parameter of beta distribution (for beta bayesianization). Defaults to None.

Returns: Dict[str, torch.Tensor]: dictionary with mean and std of prediction

class BasicBayesianWrapper:
38class BasicBayesianWrapper:
39    def __init__(self,
40                 model: torch.nn.Module,
41                 mode: str,
42                 p: Optional[float] = None,
43                 a: Optional[float] = None,
44                 b: Optional[float] = None):
45        """Class representing bayesian equivalent of a neural network.
46
47        Args:
48            model (torch.nn.Module): neural network
49            mode (str): bayesianization method (`basic` or `beta`)
50            p (Optional[float], optional): parameter of dropout (for `basic`
51                bayesianization). Defaults to None.
52            a (Optional[float], optional): parameter of beta distribution (for `beta`
53                bayesianization). Defaults to None.
54            b (Optional[float], optional): parameter of beta distribution (for `beta`
55                bayesianization). Defaults to None.
56        """
57        self.model = create_bayesian_wrapper(model, mode, p=p, a=a, b=b)
58
59    def predict(self, data, n_iter) -> Dict[str, torch.Tensor]:
60        """Function computes mean and standard deviation of bayesian equivalent
61            of a neural network.
62
63        Args:
64            data (_type_): input data of shape NxC1x...xCk,
65                where N is the number of data points,
66                C1,...,Ck are dimensions of each data point
67            n_iter (_type_): number of samplings form the bayesian equivalent
68                of a neural network
69
70        Returns:
71            Dict[str, torch.Tensor]: dictionary with `mean` and `std` of prediction
72        """
73        res = self.model.mean_forward(data, n_iter)
74        return {'mean': res[0], 'std': res[1]}
BasicBayesianWrapper( model: torch.nn.modules.module.Module, mode: str, p: Union[float, NoneType] = None, a: Union[float, NoneType] = None, b: Union[float, NoneType] = None)
39    def __init__(self,
40                 model: torch.nn.Module,
41                 mode: str,
42                 p: Optional[float] = None,
43                 a: Optional[float] = None,
44                 b: Optional[float] = None):
45        """Class representing bayesian equivalent of a neural network.
46
47        Args:
48            model (torch.nn.Module): neural network
49            mode (str): bayesianization method (`basic` or `beta`)
50            p (Optional[float], optional): parameter of dropout (for `basic`
51                bayesianization). Defaults to None.
52            a (Optional[float], optional): parameter of beta distribution (for `beta`
53                bayesianization). Defaults to None.
54            b (Optional[float], optional): parameter of beta distribution (for `beta`
55                bayesianization). Defaults to None.
56        """
57        self.model = create_bayesian_wrapper(model, mode, p=p, a=a, b=b)

Class representing bayesian equivalent of a neural network.

Args: model (torch.nn.Module): neural network mode (str): bayesianization method (basic or beta) p (Optional[float], optional): parameter of dropout (for basic bayesianization). Defaults to None. a (Optional[float], optional): parameter of beta distribution (for beta bayesianization). Defaults to None. b (Optional[float], optional): parameter of beta distribution (for beta bayesianization). Defaults to None.

def predict(self, data, n_iter) -> Dict[str, torch.Tensor]:
59    def predict(self, data, n_iter) -> Dict[str, torch.Tensor]:
60        """Function computes mean and standard deviation of bayesian equivalent
61            of a neural network.
62
63        Args:
64            data (_type_): input data of shape NxC1x...xCk,
65                where N is the number of data points,
66                C1,...,Ck are dimensions of each data point
67            n_iter (_type_): number of samplings form the bayesian equivalent
68                of a neural network
69
70        Returns:
71            Dict[str, torch.Tensor]: dictionary with `mean` and `std` of prediction
72        """
73        res = self.model.mean_forward(data, n_iter)
74        return {'mean': res[0], 'std': res[1]}

Function computes mean and standard deviation of bayesian equivalent of a neural network.

Args: data (_type_): input data of shape NxC1x...xCk, where N is the number of data points, C1,...,Ck are dimensions of each data point n_iter (_type_): number of samplings form the bayesian equivalent of a neural network

Returns: Dict[str, torch.Tensor]: dictionary with mean and std of prediction