当前位置: 首页>>代码示例>>Python>>正文


Python distributions.Bernoulli方法代码示例

本文整理汇总了Python中torch.distributions.Bernoulli方法的典型用法代码示例。如果您正苦于以下问题:Python distributions.Bernoulli方法的具体用法?Python distributions.Bernoulli怎么用?Python distributions.Bernoulli使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.distributions的用法示例。


在下文中一共展示了distributions.Bernoulli方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: forward

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def forward(self, g, action=None):
        graph_embed = self.graph_op['embed'](g)

        logit = self.add_node(graph_embed)
        prob = torch.sigmoid(logit)

        if not self.training:
            action = Bernoulli(prob).sample().item()
        stop = bool(action == self.stop)

        if not stop:
            g.add_nodes(1)
            self._initialize_node_repr(g, action, graph_embed)

        if self.training:
            sample_log_prob = bernoulli_action_log_prob(logit, action)
            self.log_prob.append(sample_log_prob)

        return stop 
开发者ID:dmlc,项目名称:dgl,代码行数:21,代码来源:model.py

示例2: forward

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def forward(self, x, gamma):
        # shape: (bsize, channels, height, width)

        if self.training:
            batch_size, channels, height, width = x.shape
            
            bernoulli = Bernoulli(gamma)
            mask = bernoulli.sample((batch_size, channels, height - (self.block_size - 1), width - (self.block_size - 1))).cuda()
            #print((x.sample[-2], x.sample[-1]))
            block_mask = self._compute_block_mask(mask)
            #print (block_mask.size())
            #print (x.size())
            countM = block_mask.size()[0] * block_mask.size()[1] * block_mask.size()[2] * block_mask.size()[3]
            count_ones = block_mask.sum()

            return block_mask * x * (countM / count_ones)
        else:
            return x 
开发者ID:kjunelee,项目名称:MetaOptNet,代码行数:20,代码来源:dropblock.py

示例3: forward

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def forward(self, x, gamma):
        # shape: (bsize, channels, height, width)

        if self.training:
            batch_size, channels, height, width = x.shape
            bernoulli = Bernoulli(gamma)
            mask = bernoulli.sample((batch_size, channels, height - (self.block_size - 1), width - (self.block_size - 1)))
            if torch.cuda.is_available():
                mask = mask.cuda()
            block_mask = self._compute_block_mask(mask)
            countM = block_mask.size()[0] * block_mask.size()[1] * block_mask.size()[2] * block_mask.size()[3]
            count_ones = block_mask.sum()

            return block_mask * x * (countM / count_ones)
        else:
            return x 
开发者ID:Sha-Lab,项目名称:FEAT,代码行数:18,代码来源:dropblock.py

示例4: bernoulli_action_log_prob

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def bernoulli_action_log_prob(logit, action):
    """Calculate the log p of an action with respect to a Bernoulli
    distribution. Use logit rather than prob for numerical stability."""
    if action == 0:
        return F.logsigmoid(-logit)
    else:
        return F.logsigmoid(logit) 
开发者ID:dmlc,项目名称:dgl,代码行数:9,代码来源:model.py

示例5: bernoulli_action_log_prob

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def bernoulli_action_log_prob(logit, action):
    """
    Calculate the log p of an action with respect to a Bernoulli
    distribution across a batch of actions. Use logit rather than
    prob for numerical stability.
    """
    log_probs = torch.cat([F.logsigmoid(-logit), F.logsigmoid(logit)], dim=1)
    return log_probs.gather(1, torch.tensor(action).unsqueeze(1)) 
开发者ID:dmlc,项目名称:dgl,代码行数:10,代码来源:model_batch.py

示例6: forward

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def forward(self, g):
        if g.number_of_edges() > 0:
            for t in range(self.num_prop_rounds):
                g.update_all(message_func=self.dgmg_msg,
                             reduce_func=self.reduce_funcs[t])
                g.ndata['hv'] = self.node_update_funcs[t](
                     g.ndata['a'], g.ndata['hv'])

#######################################################################################
# Actions
# ``````````````````````````
# All actions are sampled from distributions parameterized using neural networks
# and here they are in turn.
#
# Action 1: Add nodes
# ''''''''''''''''''''''''''
#
# Given the graph embedding vector :math:`\textbf{h}_{G}`, evaluate
#
# .. math::
#
#    \text{Sigmoid}(\textbf{W}_{\text{add node}}\textbf{h}_{G}+b_{\text{add node}}),\\
#
# which is then used to parametrize a Bernoulli distribution for deciding whether
# to add a new node.
#
# If a new node is to be added, initialize its feature with
#
# .. math::
#
#    \textbf{W}_{\text{init}}\text{concat}([\textbf{h}_{\text{init}} , \textbf{h}_{G}])+\textbf{b}_{\text{init}},\\
#
# where :math:`\textbf{h}_{\text{init}}` is a learnable embedding module for
# untyped nodes.
# 
开发者ID:dmlc,项目名称:dgl,代码行数:37,代码来源:5_dgmg.py

示例7: _hard_bernoulli

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def _hard_bernoulli(self, dist):
  return dist.Bernoulli(logits=dist.logits) 
开发者ID:mjendrusch,项目名称:torchsupport,代码行数:4,代码来源:__init__.py

示例8: __init__

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def __init__(self, block_size):
        super(DropBlock, self).__init__()

        self.block_size = block_size
        #self.gamma = gamma
        #self.bernouli = Bernoulli(gamma) 
开发者ID:kjunelee,项目名称:MetaOptNet,代码行数:8,代码来源:dropblock.py

示例9: act

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def act(batch_states, theta, values):
    batch_states = torch.from_numpy(batch_states).long()
    probs = torch.sigmoid(theta)[batch_states]
    m = Bernoulli(1-probs)
    actions = m.sample()
    log_probs_actions = m.log_prob(actions)
    return actions.numpy().astype(int), log_probs_actions, values[batch_states] 
开发者ID:alexis-jacq,项目名称:LOLA_DiCE,代码行数:9,代码来源:ipd_DiCE.py

示例10: act

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def act(batch_states, theta):
    batch_states = torch.from_numpy(batch_states).long()
    probs = torch.sigmoid(theta)[batch_states]
    m = Bernoulli(1-probs)
    actions = m.sample()
    log_probs_actions = m.log_prob(actions)
    return actions.numpy().astype(int), log_probs_actions 
开发者ID:alexis-jacq,项目名称:LOLA_DiCE,代码行数:9,代码来源:ipd_exact_om.py

示例11: sample_mask

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def sample_mask(self, p, shape):
        """Samples a dropout mask from a Bernoulli distribution.

        Args:
            p(float): the dropout probability [0, 1].
            shape(torch.Size): shape of the mask to be sampled.
        """
        if self.training:
            self._mask = Bernoulli(1. - p).sample(shape)
        else:
            self._mask = (1. - p) 
开发者ID:tom-pelsmaeker,项目名称:deep-generative-lm,代码行数:13,代码来源:dropout.py

示例12: forward

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def forward(self, x):
        bests = x.max(dim=1, keepdim=True)[1]
        sampled = Categorical(probs=th.ones_like(x)).sample()
        probs = th.ones(x.size(0), 1) - self.epsilon
        b = Bernoulli(probs=probs).sample().long()
        ret = bests * b + (1 - b) * sampled
        return ret 
开发者ID:learnables,项目名称:cherry,代码行数:9,代码来源:epsilon_greedy.py

示例13: decode

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def decode(self, p, z=None, c=None, **kwargs):
        ''' Returns occupancy values for the points p at time step 0.

        Args:
            p (tensor): points
            z (tensor): latent code z
            c (tensor): latent conditioned code c (For OFlow, this is
                c_spatial)
        '''
        logits = self.decoder(p, z, c, **kwargs)
        p_r = dist.Bernoulli(logits=logits)
        return p_r 
开发者ID:autonomousvision,项目名称:occupancy_flow,代码行数:14,代码来源:__init__.py

示例14: decode

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def decode(self, p, z=None, c=None, **kwargs):
        ''' Returns occupancy values for the points p at time step t.

        Args:
            p (tensor): points of dimension 4
            z (tensor): latent code z
            c (tensor): latent conditioned code c (For OFlow, this is
                c_spatial, whereas for ONet 4D, this is c_temporal)
        '''
        logits = self.decoder(p, z, c, **kwargs)
        p_r = dist.Bernoulli(logits=logits)
        return p_r 
开发者ID:autonomousvision,项目名称:occupancy_flow,代码行数:14,代码来源:__init__.py

示例15: generate

# 需要导入模块: from torch import distributions [as 别名]
# 或者: from torch.distributions import Bernoulli [as 别名]
def generate(self, T, B):
        if not self.T_condition:
            raise NotImplementedError("Only the version conditioned on T has been implemented.")

        hidden = self.init_hidden(B)
        lengths = torch.tensor([T]*B)
        device = hidden[0].device

        cond_inp = make_pos_cond(T, B, lengths, self.max_T).to(device)

        if self.indep_bernoulli:
            generation = torch.zeros(T, B, self.vocab_size, dtype=torch.long, device=device)
        else:
            generation = torch.zeros(T, B, dtype=torch.long, device=device)

        last_rnn_outp = hidden[0][-1]
        for t in range(T):
            scores = self.output_embedding(last_rnn_outp) # [B, V]
            if self.indep_bernoulli:
                word_dist = Bernoulli(logits=scores)
            else:
                word_dist = Categorical(logits=scores)

            selected_index = word_dist.sample()
            generation[t] = selected_index

            if t < T-1:
                if self.indep_bernoulli:
                    inp_embeddings = torch.matmul(generation[t].float(), self.input_embedding.weight)
                else:
                    inp_embeddings = self.input_embedding(generation[t]) # [B, E]
                inp_embeddings = torch.cat((inp_embeddings, cond_inp[t+1]), -1)

                last_rnn_outp, hidden = self.rnn(inp_embeddings[None, :, :], hidden)
                last_rnn_outp = last_rnn_outp[0]

        return generation 
开发者ID:harvardnlp,项目名称:TextFlow,代码行数:39,代码来源:baseline_model.py


注:本文中的torch.distributions.Bernoulli方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。