quantnn/models/pytorch/common.py
def calibration(self, data, gpu=False):
        """
        Computes the calibration of the predictions from the neural network.

        Arguments:
            data: torch dataloader object providing the data for which to compute
                the calibration.

        Returns:
            (intervals, frequencies): Tuple containing the confidence intervals and
                corresponding observed frequencies.
        """

        if gpu and torch.cuda.is_available():
            dev = torch.device("cuda")
        else:
            dev = torch.device("cpu")
        self.to(dev)

        n_intervals = self.quantiles.size // 2
        qs = self.quantiles
        intervals = np.array([q_r - q_l for (q_l, q_r) in zip(qs, reversed(qs))])[
            :n_intervals
        ]
        counts = np.zeros(n_intervals)

        total = 0.0

        for x, y in iterator:
            x = x.to(dev).detach()
            y = y.to(dev).detach()
            shape = x.size()
            shape = (shape[0], 1) + shape[2:]
            y = y.reshape(shape)

            y_pred = self(x)
            y_pred = y_pred.cpu()
            y = y.cpu()

            for i in range(n_intervals):
                l = y_pred[:, [i]]
                r = y_pred[:, [-(i + 1)]]
                counts[i] += np.logical_and(y >= l, y < r).sum()

            total += np.prod(y.size())
        return intervals[::-1], (counts / total)[::-1]

        Computes the calibration of the predictions from the neural network.

        Arguments:
            data: torch dataloader object providing the data for which to compute
                the calibration.

        Returns:
            (intervals, frequencies): Tuple containing the confidence intervals and
                corresponding observed frequencies.
        
Computes the calibration of the predictions from the neural network.
Computes the calibration of the predictions from the neural network .
def calibration(self, data, gpu=False):
        if gpu and torch.cuda.is_available():
            dev = torch.device("cuda")
        else:
            dev = torch.device("cpu")
        self.to(dev)
        n_intervals = self.quantiles.size // 2
        qs = self.quantiles
        intervals = np.array([q_r - q_l for (q_l, q_r) in zip(qs, reversed(qs))])[
            :n_intervals
        ]
        counts = np.zeros(n_intervals)
        total = 0.0
        for x, y in iterator:
            x = x.to(dev).detach()
            y = y.to(dev).detach()
            shape = x.size()
            shape = (shape[0], 1) + shape[2:]
            y = y.reshape(shape)
            y_pred = self(x)
            y_pred = y_pred.cpu()
            y = y.cpu()
            for i in range(n_intervals):
                l = y_pred[:, [i]]
                r = y_pred[:, [-(i + 1)]]
                counts[i] += np.logical_and(y >= l, y < r).sum()
            total += np.prod(y.size())
        return intervals[::-1], (counts / total)[::-1]
def calibration ( self , data , gpu = False ) : if gpu and torch . cuda . is_available ( ) : dev = torch . device ( "cuda" ) else : dev = torch . device ( "cpu" ) self . to ( dev ) n_intervals = self . quantiles . size // 2 qs = self . quantiles intervals = np . array ( [ q_r - q_l for ( q_l , q_r ) in zip ( qs , reversed ( qs ) ) ] ) [ : n_intervals ] counts = np . zeros ( n_intervals ) total = 0.0 for x , y in iterator : x = x . to ( dev ) . detach ( ) y = y . to ( dev ) . detach ( ) shape = x . size ( ) shape = ( shape [ 0 ] , 1 ) + shape [ 2 : ] y = y . reshape ( shape ) y_pred = self ( x ) y_pred = y_pred . cpu ( ) y = y . cpu ( ) for i in range ( n_intervals ) : l = y_pred [ : , [ i ] ] r = y_pred [ : , [ - ( i + 1 ) ] ] counts [ i ] += np . logical_and ( y >= l , y < r ) . sum ( ) total += np . prod ( y . size ( ) ) return intervals [ : : - 1 ] , ( counts / total ) [ : : - 1 ]
Computes the calibration of the predictions from the neural network.
Computes the calibration of the predictions from the neural network .
"""
        Computes the calibration of the predictions from the neural network.

        Arguments:
            data: torch dataloader object providing the data for which to compute
                the calibration.

        Returns:
            (intervals, frequencies): Tuple containing the confidence intervals and
                corresponding observed frequencies.
        """
(intervals, frequencies): Tuple containing the confidence intervals and
corresponding observed frequencies.
( intervals frequencies ) : Tuple containing the confidence intervals and corresponding observed frequencies .
self
data
torch dataloader object providing the data for which to compute
the calibration.
torch dataloader object providing the data for which to compute the calibration .
gpu