pytorch_generative/models/nade.py
def _forward(self, x):
        """Computes the forward pass and samples a new output.

        Returns:
            (p_hat, x_hat) where p_hat is the probability distribution over dimensions
            and x_hat is sampled from p_hat.
        """
        # If the input is an image, flatten it during the forward pass.
        original_shape = x.shape
        if len(x.shape) > 2:
            x = x.view(original_shape[0], -1)

        in_W, in_b = self.params["in_W"], self.params["in_b"]
        h_W, h_b = self.params["h_W"], self.params["h_b"]
        batch_size = 1 if x is None else x.shape[0]

        p_hat = []
        x_hat = []
        # Only the bias is used to compute the first hidden unit so we must
        # replicate it to account for the batch size.
        a = in_b.expand(batch_size, -1)
        for i in range(self._input_dim):
            h = torch.relu(a)
            p_i = torch.sigmoid(h_b[i : i + 1] + h @ h_W[i : i + 1, :].t())
            p_hat.append(p_i)

            # Sample 'x' at dimension 'i' if it is not given.
            x_i = x[:, i : i + 1]
            x_i = torch.where(x_i < 0, distributions.Bernoulli(probs=p_i).sample(), x_i)
            x_hat.append(x_i)

            # We do not need to add in_b[i:i+1] when computing the other hidden units
            # since it was already added when computing the first hidden unit.
            a = a + x_i @ in_W[:, i : i + 1].t()
        if x_hat:
            return (
                torch.cat(p_hat, dim=1).view(original_shape),
                torch.cat(x_hat, dim=1).view(original_shape),
            )
        return []
Computes the forward pass and samples a new output.

        Returns:
            (p_hat, x_hat) where p_hat is the probability distribution over dimensions
            and x_hat is sampled from p_hat.
        
Computes the forward pass and samples a new output.
Computes the forward pass and samples a new output .
def _forward(self, x):
        original_shape = x.shape
        if len(x.shape) > 2:
            x = x.view(original_shape[0], -1)
        in_W, in_b = self.params["in_W"], self.params["in_b"]
        h_W, h_b = self.params["h_W"], self.params["h_b"]
        batch_size = 1 if x is None else x.shape[0]
        p_hat = []
        x_hat = []
        a = in_b.expand(batch_size, -1)
        for i in range(self._input_dim):
            h = torch.relu(a)
            p_i = torch.sigmoid(h_b[i : i + 1] + h @ h_W[i : i + 1, :].t())
            p_hat.append(p_i)
            x_i = x[:, i : i + 1]
            x_i = torch.where(x_i < 0, distributions.Bernoulli(probs=p_i).sample(), x_i)
            x_hat.append(x_i)
            a = a + x_i @ in_W[:, i : i + 1].t()
        if x_hat:
            return (
                torch.cat(p_hat, dim=1).view(original_shape),
                torch.cat(x_hat, dim=1).view(original_shape),
            )
        return []
def _forward ( self , x ) : original_shape = x . shape if len ( x . shape ) > 2 : x = x . view ( original_shape [ 0 ] , - 1 ) in_W , in_b = self . params [ "in_W" ] , self . params [ "in_b" ] h_W , h_b = self . params [ "h_W" ] , self . params [ "h_b" ] batch_size = 1 if x is None else x . shape [ 0 ] p_hat = [ ] x_hat = [ ] a = in_b . expand ( batch_size , - 1 ) for i in range ( self . _input_dim ) : h = torch . relu ( a ) p_i = torch . sigmoid ( h_b [ i : i + 1 ] + h @ h_W [ i : i + 1 , : ] . t ( ) ) p_hat . append ( p_i ) x_i = x [ : , i : i + 1 ] x_i = torch . where ( x_i < 0 , distributions . Bernoulli ( probs = p_i ) . sample ( ) , x_i ) x_hat . append ( x_i ) a = a + x_i @ in_W [ : , i : i + 1 ] . t ( ) if x_hat : return ( torch . cat ( p_hat , dim = 1 ) . view ( original_shape ) , torch . cat ( x_hat , dim = 1 ) . view ( original_shape ) , ) return [ ]
Computes the forward pass and samples a new output.
Computes the forward pass and samples a new output .
"""Computes the forward pass and samples a new output.

        Returns:
            (p_hat, x_hat) where p_hat is the probability distribution over dimensions
            and x_hat is sampled from p_hat.
        """ # If the input is an image, flatten it during the forward pass. # Only the bias is used to compute the first hidden unit so we must # replicate it to account for the batch size. # Sample 'x' at dimension 'i' if it is not given. # We do not need to add in_b[i:i+1] when computing the other hidden units # since it was already added when computing the first hidden unit.
(p_hat, x_hat) where p_hat is the probability distribution over dimensions
and x_hat is sampled from p_hat.
( p_hat x_hat ) where p_hat is the probability distribution over dimensions and x_hat is sampled from p_hat .
self
x