當前位置: 首頁>>代碼示例>>Python>>正文


Python torch.multinomial方法代碼示例

本文整理匯總了Python中torch.multinomial方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.multinomial方法的具體用法?Python torch.multinomial怎麽用?Python torch.multinomial使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在torch的用法示例。


在下文中一共展示了torch.multinomial方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: create_random_binary_mask

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def create_random_binary_mask(features):
    """
    Creates a random binary mask of a given dimension with half of its entries
    randomly set to 1s.

    :param features: Dimension of mask.
    :return: Binary mask with half of its entries set to 1s, of type torch.Tensor.
    """
    mask = torch.zeros(features).byte()
    weights = torch.ones(features).float()
    num_samples = features // 2 if features % 2 == 0 else features // 2 + 1
    indices = torch.multinomial(
        input=weights,
        num_samples=num_samples,
        replacement=False
    )
    mask[indices] += 1
    return mask 
開發者ID:bayesiains,項目名稱:nsf,代碼行數:20,代碼來源:torchutils.py

示例2: sample

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def sample(self, labels):
        """
            labels: [b1, b2]
        Return
            true_log_probs: [b1, b2]
            samp_log_probs: [n_sample]
            neg_samples: [n_sample]
        """

        # neg_samples = torch.empty(0).long()
        n_sample = self.n_sample
        n_tries = 2 * n_sample

        with torch.no_grad():
            neg_samples = torch.multinomial(self.dist, n_tries, replacement=True).unique()
            device = labels.device
            neg_samples = neg_samples.to(device)
            true_log_probs = self.log_q[labels].to(device)
            samp_log_probs = self.log_q[neg_samples].to(device)
            return true_log_probs, samp_log_probs, neg_samples 
開發者ID:649453932,項目名稱:Bert-Chinese-Text-Classification-Pytorch,代碼行數:22,代碼來源:modeling_transfo_xl_utilities.py

示例3: random_tensor

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def random_tensor(inputs, output=reals()):
    """
    Creates a random :class:`funsor.tensor.Tensor` with given inputs and output.
    """
    backend = get_backend()
    assert isinstance(inputs, OrderedDict)
    assert isinstance(output, Domain)
    shape = tuple(d.dtype for d in inputs.values()) + output.shape
    if output.dtype == 'real':
        data = randn(shape)
    else:
        num_elements = reduce(operator.mul, shape, 1)
        if backend == "torch":
            import torch

            data = torch.multinomial(torch.ones(output.dtype), num_elements, replacement=True)
        else:
            data = np.random.choice(output.dtype, num_elements, replace=True)
        data = data.reshape(shape)
    return Tensor(data, inputs, output.dtype) 
開發者ID:pyro-ppl,項目名稱:funsor,代碼行數:22,代碼來源:testing.py

示例4: _sqrt_hessian_sampled

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def _sqrt_hessian_sampled(self, module, g_inp, g_out, mc_samples=1):
        self._check_2nd_order_parameters(module)

        M = mc_samples
        C = module.input0.shape[1]

        probs = self._get_probs(module)
        V_dim = 0
        probs_unsqueezed = probs.unsqueeze(V_dim).repeat(M, 1, 1)

        multi = multinomial(probs, M, replacement=True)
        classes = one_hot(multi, num_classes=C)
        classes = einsum("nvc->vnc", classes).float()

        sqrt_mc_h = (probs_unsqueezed - classes) / sqrt(M)

        if module.reduction == "mean":
            N = module.input0.shape[0]
            sqrt_mc_h /= sqrt(N)

        return sqrt_mc_h 
開發者ID:f-dangel,項目名稱:backpack,代碼行數:23,代碼來源:crossentropyloss.py

示例5: gen_step

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def gen_step(self, src, rel, dst, n_sample=1, temperature=1.0, train=True):
        if not hasattr(self, 'opt'):
            self.opt = Adam(self.mdl.parameters(), weight_decay=self.weight_decay)
        n, m = dst.size()
        rel_var = Variable(rel.cuda())
        src_var = Variable(src.cuda())
        dst_var = Variable(dst.cuda())

        logits = self.mdl.prob_logit(src_var, rel_var, dst_var) / temperature
        probs = nnf.softmax(logits)
        row_idx = torch.arange(0, n).type(torch.LongTensor).unsqueeze(1).expand(n, n_sample)
        sample_idx = torch.multinomial(probs, n_sample, replacement=True)
        sample_srcs = src[row_idx, sample_idx.data.cpu()]
        sample_dsts = dst[row_idx, sample_idx.data.cpu()]
        rewards = yield sample_srcs, sample_dsts
        if train:
            self.mdl.zero_grad()
            log_probs = nnf.log_softmax(logits)
            reinforce_loss = -torch.sum(Variable(rewards) * log_probs[row_idx.cuda(), sample_idx.data])
            reinforce_loss.backward()
            self.opt.step()
            self.mdl.constraint()
        yield None 
開發者ID:cai-lw,項目名稱:KBGAN,代碼行數:25,代碼來源:base_model.py

示例6: select_paths

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def select_paths(self, logprobs, prior_scores, current_length):
        # Unlike the other treesearch methods, we have to switch to linspace
        # for the probabilities in order to compute the CDF.
        probs = torch.softmax(logprobs, dim=-1)
        sprobs, sinds = probs.sort(dim=-1, descending=True)
        # The subtraction here is to get the exclusive prefix sum,
        # to guarantee the first element is not masked
        mask = (sprobs.cumsum(dim=-1) - sprobs) >= self.p
        sprobs[mask] = 0
        sprobs.div_(sprobs.sum(dim=-1).unsqueeze(1))
        choices = torch.multinomial(sprobs, 1)[:, 0]
        hyp_ids = torch.arange(logprobs.size(0)).to(logprobs.device)
        tok_ids = sinds[hyp_ids, choices]
        # Convert back to logspace.
        scores = sprobs[hyp_ids, choices].log()
        best_scores = prior_scores.expand_as(scores) + scores
        return (hyp_ids, tok_ids, best_scores) 
開發者ID:facebookresearch,項目名稱:ParlAI,代碼行數:19,代碼來源:torch_generator_agent.py

示例7: sample_sequence

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def sample_sequence(model, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0, device='cuda', sample=True):
    if start_token is None:
        assert context is not None, 'Specify exactly one of start_token and context!'
        context = torch.tensor(context, device=device, dtype=torch.long).unsqueeze(0).repeat(batch_size, 1)
    else:
        assert context is None, 'Specify exactly one of start_token and context!'
        context = torch.full((batch_size, 1), start_token, device=device, dtype=torch.long)
    prev = context
    output = context
    past = None
    with torch.no_grad():
        for i in trange(length):
            logits, past = model(prev, past=past)
            logits = logits[:, -1, :] / temperature
            logits = top_k_logits(logits, k=top_k)
            log_probs = F.softmax(logits, dim=-1)
            if sample:
                prev = torch.multinomial(log_probs, num_samples=1)
            else:
                _, prev = torch.topk(log_probs, k=1, dim=-1)
            output = torch.cat((output, prev), dim=1)
    return output 
開發者ID:martiansideofthemoon,項目名稱:squash-generation,代碼行數:24,代碼來源:run_gpt2.py

示例8: decode

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def decode(self, out):
        """
        Args:
            out: unnormalized word distribution [batch_size, vocab_size]
        Return:
            x: word_index [batch_size]
        """

        # Sample next word from multinomial word distribution
        if self.sample:
            # x: [batch_size] - word index (next input)
            x = torch.multinomial(self.softmax(out / self.temperature), 1).view(-1)

        # Greedy sampling
        else:
            # x: [batch_size] - word index (next input)
            _, x = out.max(dim=1)
        return x 
開發者ID:ctr4si,項目名稱:A-Hierarchical-Latent-Structure-for-Variational-Conversation-Modeling,代碼行數:20,代碼來源:decoder.py

示例9: generate

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def generate(model, idx2word, word_len=200, temperature=1.0):
    """生成一定數量的文本,temperature結合多項式分布可增添抽樣的多樣性。"""
    model.eval()
    hidden = model.init_hidden(1)  # batch_size為1
    inputs = Variable(torch.rand(1, 1).mul(len(idx2word)).long(), volatile=True)  # 隨機選取一個字作為開始
    if use_cuda:
        inputs = inputs.cuda()

    word_list = []
    for i in range(word_len):  # 逐字生成
        output, hidden = model(inputs, hidden)
        word_weights = output.squeeze().data.div(temperature).exp().cpu()

        # 基於詞的權重,對其再進行一次抽樣,增添其多樣性,如果不使用此法,會導致常用字的無限循環
        word_idx = torch.multinomial(word_weights, 1)[0]
        inputs.data.fill_(word_idx)  # 將新生成的字賦給inputs
        word = idx2word[word_idx]
        word_list.append(word)
    return word_list 
開發者ID:gaussic,項目名稱:char_rnn_lm_zh,代碼行數:21,代碼來源:main.py

示例10: _draw_choices

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def _draw_choices(self, probs, n_choices):
        """
        Draw `n_choices` sample from `probs`.

        References:
            Code from https://github.com/BlackHC/BatchBALD/blob/master/src/torch_utils.py#L187

        Returns:
            choices: B... x `n_choices`

        """
        probs = probs.permute(0, 2, 1)
        probs_B_C = probs.reshape((-1, probs.shape[-1]))

        # samples: Ni... x draw_per_xx
        choices = torch.multinomial(probs_B_C,
                                    num_samples=n_choices, replacement=True)

        choices_b_M = choices.reshape(list(probs.shape[:-1]) + [n_choices])
        return choices_b_M.long() 
開發者ID:ElementAI,項目名稱:baal,代碼行數:22,代碼來源:heuristics.py

示例11: logits2words

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def logits2words(self, output, decoded_words, dataset, sample_size):
		'''
		* Decode words from logits output at a time step AND put decoded words in final results *
		* take argmax if sample size == 1
		'''
		batch_size = output.size(0)
		if sample_size == 1: # take argmax directly w/o sampling
			topv, topi = F.softmax(output, dim=1).data.topk(1) # both (batch_size, 1)

		else: # sample over word distribution
			topv, topi = [], []
			word_dis = F.softmax(output, dim=1) # (batch_size, output_size)

			# sample from part of the output distribution for word variations
			n_candidate = 3
			word_dis_sort, idx_of_idx = torch.sort(word_dis, dim=1, descending=True)
			word_dis_sort = word_dis_sort[:, :n_candidate]
			idx_of_idx = idx_of_idx[:, :n_candidate]
			sample_idx = torch.multinomial(word_dis_sort, 1) # (batch_size,)
			for b in range(batch_size):
				i = int(sample_idx[b])
				idx = int(idx_of_idx[b][i])
				prob = float(word_dis[b][idx])
				topi.append(idx)
				topv.append(prob)
				
			topv = torch.FloatTensor(topv).view(batch_size, 1)
			topi = torch.LongTensor(topi).view(batch_size, 1)
			
		decoded_words_t = np.zeros((batch_size, self.output_size))
		for b in range(batch_size):
			idx = topi[b][0]
			word = dataset.index2word[idx.item()]
			decoded_words[b] += (word + ' ')
			decoded_words_t[b][idx] = 1
		decoded_words_t = Variable(torch.from_numpy(decoded_words_t.astype(np.float32)))

		if self.USE_CUDA:
			decoded_words_t = decoded_words_t.cuda()

		return decoded_words_t 
開發者ID:ConvLab,項目名稱:ConvLab,代碼行數:43,代碼來源:decoder_deep.py

示例12: multinomial

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def multinomial(w: torch.Tensor, normalized=False):
    """
    Performs multinomial sampling.
    :param w: The weights to use for resampling
    :param normalized: Whether the data is normalized
    :return: Resampled indices
    """

    return torch.multinomial(normalize(w) if not normalized else w, w.shape[-1], replacement=True) 
開發者ID:tingiskhan,項目名稱:pyfilter,代碼行數:11,代碼來源:resampling.py

示例13: residual

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def residual(w: torch.Tensor, normalized=False):
    """
    Performs residual resampling. Inspired by solution provided by the package "particles" on GitHub
    authored by the user "nchopin".
    :param w: The weights to use for resampling
    :param normalized: Whether the data is normalized
    :return: Resampled indices
    """

    if w.dim() > 1:
        raise NotImplementedError('Not implemented for multidimensional arrays!')

    w = normalize(w) if not normalized else w

    # ===== Calculate the number of deterministic to get ===== #
    mw = (w.shape[-1] * w)
    floored = mw.floor()
    res = mw - floored

    # ===== Make flat ===== #
    out = torch.ones_like(w, dtype=torch.long)

    # ===== Get the indexes of those to sample ===== #
    numelems = floored.sum(-1)
    res /= numelems

    intpart = floored.long()
    ranged = torch.arange(w.shape[-1], dtype=intpart.dtype, device=w.device) * out

    # ===== Repeat the integers and transform to correct ===== #
    modded = ranged.repeat_interleave(intpart)
    aslong = numelems.long()

    out[:aslong] = modded

    if numelems == w.shape[-1]:
        return out

    out[aslong:] = torch.multinomial(res, w.shape[-1] - aslong, replacement=True)

    return out 
開發者ID:tingiskhan,項目名稱:pyfilter,代碼行數:43,代碼來源:resampling.py

示例14: sample

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def sample(self, num=1, device=None):
        assert self.natural_ordering
        assert self.input_bins and self.nout > self.nin
        with torch.no_grad():
            sampled = torch.zeros((num, self.nin), device=device)
            indices = np.cumsum(self.input_bins)
            for i in range(self.nin):
                logits = self.forward(sampled)
                s = torch.multinomial(
                    torch.softmax(self.logits_for_i(i, logits), -1), 1)
                sampled[:, i] = s.view(-1,)
        return sampled 
開發者ID:naru-project,項目名稱:naru,代碼行數:14,代碼來源:made.py

示例15: __iter__

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import multinomial [as 別名]
def __iter__(self):
        return iter(torch.multinomial(self.weights, self.num_samples, self.replacement)) 
開發者ID:XiaLiPKU,項目名稱:EMANet,代碼行數:4,代碼來源:sampler.py


注:本文中的torch.multinomial方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。