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


Python torch.Tensors方法代碼示例

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


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

示例1: conf2hist

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def conf2hist(self, array, bins=100):
        error_msg = "Confidence must contain torch.Tensors or numpy.ndarray; found {}"
        if isinstance(array, torch.Tensor):
            array = array.clone().detach().cpu().numpy()
        elif isinstance(array, np.ndarray):
            array = array.copy()
        else:
            raise TypeError((error_msg.format(type(array))))

        length = len(array.shape)
        assert length >= 2
        if length == 4:
            array = array[0, 0, :, :]
        elif length == 3:
            array = array[0, :, :]

        # for interval [bin_edges[i], bin_edges[i+1]], it has counts[i] numbers.
        counts, bin_edges = np.histogram(array, bins=bins)
        return counts, bin_edges

    # return a plt.figure() 
開發者ID:DeepMotionAIResearch,項目名稱:DenseMatchingBenchmark,代碼行數:23,代碼來源:show_result.py

示例2: global_device

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def global_device():
    """Returns the global device that torch.Tensors should be placed on.

    Note: The global device is set by using the function
        `garage.torch._functions.set_gpu_mode.`
        If this functions is never called
        `garage.torch._functions.device()` returns None.

    Returns:
        `torch.Device`: The global device that newly created torch.Tensors
            should be placed on.

    """
    # pylint: disable=global-statement
    global _DEVICE
    return _DEVICE 
開發者ID:rlworkgroup,項目名稱:garage,代碼行數:18,代碼來源:_functions.py

示例3: __call__

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def __call__(self):

        # calls pyro.param so that params are exposed and constraints applied
        # should not create any new torch.Tensors after __init__
        self.initialize_params()

        N_c = self.config["sizes"]["group"]
        N_s = self.config["sizes"]["individual"]

        log_prob = Tensor(torch.tensor(0.), OrderedDict())

        plate_g = Tensor(torch.zeros(N_c), OrderedDict([("g", bint(N_c))]))
        plate_i = Tensor(torch.zeros(N_s), OrderedDict([("i", bint(N_s))]))

        if self.config["group"]["random"] == "continuous":
            eps_g_dist = plate_g + dist.Normal(**self.params["eps_g"])(value="eps_g")
            log_prob += eps_g_dist

        # individual-level random effects
        if self.config["individual"]["random"] == "continuous":
            eps_i_dist = plate_g + plate_i + dist.Normal(**self.params["eps_i"])(value="eps_i")
            log_prob += eps_i_dist

        return log_prob 
開發者ID:pyro-ppl,項目名稱:funsor,代碼行數:26,代碼來源:model.py

示例4: initialize_observations

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def initialize_observations(self):
        """
        Convert raw observation tensors into funsor.tensor.Tensors
        """
        batch_inputs = OrderedDict([
            ("i", bint(self.config["sizes"]["individual"])),
            ("g", bint(self.config["sizes"]["group"])),
            ("t", bint(self.config["sizes"]["timesteps"])),
        ])

        observations = {}
        for name, data in self.config["observations"].items():
            observations[name] = Tensor(data[..., :self.config["sizes"]["timesteps"]], batch_inputs)

        self.observations = observations
        return self.observations 
開發者ID:pyro-ppl,項目名稱:funsor,代碼行數:18,代碼來源:model.py

示例5: initialize_raggedness_masks

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def initialize_raggedness_masks(self):
        """
        Convert raw raggedness tensors into funsor.tensor.Tensors
        """
        batch_inputs = OrderedDict([
            ("i", bint(self.config["sizes"]["individual"])),
            ("g", bint(self.config["sizes"]["group"])),
            ("t", bint(self.config["sizes"]["timesteps"])),
        ])

        raggedness_masks = {}
        for name in ("individual", "timestep"):
            data = self.config[name]["mask"]
            if len(data.shape) < len(batch_inputs):
                while len(data.shape) < len(batch_inputs):
                    data = data.unsqueeze(-1)
                data = data.expand(tuple(v.dtype for v in batch_inputs.values()))
            data = data.to(self.config["observations"]["step"].dtype)
            raggedness_masks[name] = Tensor(data[..., :self.config["sizes"]["timesteps"]],
                                            batch_inputs)

        self.raggedness_masks = raggedness_masks
        return self.raggedness_masks 
開發者ID:pyro-ppl,項目名稱:funsor,代碼行數:25,代碼來源:model.py

示例6: set_ilink

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def set_ilink(self, ilink: Optional[Callable], overwrite: bool = False, **kwargs):
        """
        Set the inverse-link function that will translate value-assignments/adjustments for an element of the
        design-matrix into their final value.

        :param ilink: A callable that is appropriate for torch.Tensors (e.g. torch.exp). If None, then the identity
          link is assumed.
        :param overwrite: If False (default) then cannot re-assign if already assigned; if True will overwrite.
        :param kwargs: The names of the dimensions.
        """
        key = self._get_key(kwargs)
        if key not in self._assignments:
            raise ValueError(f"Tried to set ilink for {key} but must `assign` first.")
        if key in self._ilinks and not overwrite:
            raise ValueError(f"Already have ilink for {key}.")
        assert ilink is None or callable(ilink)
        self._ilinks[key] = ilink 
開發者ID:strongio,項目名稱:torch-kalman,代碼行數:19,代碼來源:base.py

示例7: adjust

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def adjust(self, value: DesignMatAdjustment, check_slow_grad: bool, **kwargs):
        """
        Adjust the value of an assignment. The final value for an element will be given by (a) taking the sum of the
        initial value and all adjustments, (b) applying the ilink function from `set_ilink()`.

        :param value: Either (a) a torch.Tensor or (b) a sequence of torch.Tensors (one for each timepoint). The tensor
          should be either scalar, or be 1D with length = self.num_groups.
        :param check_slow_grad: A natural way to create adjustments is to first create a tensor that `requires_grad`,
          then split it into a list of tensors, one for each time-point. This way of creating adjustments should be
          avoided because it leads to a very slow backwards pass. When check_slow_grad is True then a heuristic is used
          to check for this "gotcha". It can lead to false-alarms, so disabling is allowed with `check_slow_grad=False`.
        :param kwargs: The names of the dimensions.
        """
        key = self._get_key(kwargs)
        value = self._validate_adjustment(value, check_slow_grad=check_slow_grad)
        try:
            self._assignments[key].append(value)
        except KeyError:
            raise RuntimeError("Tried to adjust {} (in {}); but need to `assign()` it first.".format(key, self)) 
開發者ID:strongio,項目名稱:torch-kalman,代碼行數:21,代碼來源:base.py

示例8: move_to_device

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def move_to_device(obj, cuda_device: torch.device):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    From ``allenlp.nn.util``
    """
    # pylint: disable=too-many-return-statements
    # pargma: no cover
    # not tested relying on allennlp
    if cuda_device.type == "cpu" or not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.to(cuda_device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, cuda_device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, cuda_device) for item in obj]
    elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
        # This is the best way to detect a NamedTuple, it turns out.
        return obj.__class__(*[move_to_device(item, cuda_device) for item in obj])
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, cuda_device) for item in obj])
    else:
        return obj 
開發者ID:abhinavkashyap,項目名稱:sciwing,代碼行數:26,代碼來源:tensor_utils.py

示例9: move_to_device

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def move_to_device(obj, cuda_device: Union[torch.device, int]):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    from allennlp.common.util import int_to_device

    cuda_device = int_to_device(cuda_device)

    if cuda_device == torch.device("cpu") or not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.cuda(cuda_device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, cuda_device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, cuda_device) for item in obj]
    elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
        # This is the best way to detect a NamedTuple, it turns out.
        return obj.__class__(*(move_to_device(item, cuda_device) for item in obj))
    elif isinstance(obj, tuple):
        return tuple(move_to_device(item, cuda_device) for item in obj)
    else:
        return obj 
開發者ID:allenai,項目名稱:allennlp,代碼行數:26,代碼來源:util.py

示例10: prepare_batch

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def prepare_batch(self, batch, device):
        """Prepare batch data for training by performing device conversion.

        Args:
            batch (tuple of 2 torch.Tensors: (input, target)): The input and
                target tensors to move on the required device.
            device (str or torch.device): The target device for the tensors.

        Returns:
            tuple of 2 torch.Tensors: (input, target): The resulted tensors on
            the required device.

        """
        input, target = batch
        input = deep_to(input, device, non_blocking=True)
        target = deep_to(target, device, non_blocking=True)
        return input, target 
開發者ID:lRomul,項目名稱:argus,代碼行數:19,代碼來源:model.py

示例11: forward

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of images.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
開發者ID:aimagelab,項目名稱:novelty-detection,代碼行數:23,代碼來源:LSA_cifar10.py

示例12: forward

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of patches.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
開發者ID:aimagelab,項目名稱:novelty-detection,代碼行數:23,代碼來源:LSA_shanghaitech.py

示例13: var2link

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def var2link(var):
    """
    Function. Constructs a PartialLink from variables, numbers, numpy arrays, tensors or a combination of variables and
    PartialLinks.

    Args:
        var: brancher.Variables, numbers, numpy.ndarrays, torch.Tensors, or List/Tuple of brancher.Variables and
        brancher.PartialLinks.

    Retuns: brancher.PartialLink
    """
    if isinstance(var, Variable):
        vars = {var}
        fn = lambda values: values[var]
    elif isinstance(var, (numbers.Number, np.ndarray, torch.Tensor)):
        vars = set()
        fn = lambda values: var
    elif isinstance(var, (tuple, list)) and all([isinstance(v, (Variable, PartialLink)) for v in var]):
        vars = join_sets_list([{v} if isinstance(v, Variable) else v.vars for v in var])
        fn = lambda values: tuple([values[v] if isinstance(v, Variable) else v.fn(values) for v in var])
    else:
        return var
    return PartialLink(vars=vars, fn=fn, links=set(), string=str(var)) 
開發者ID:AI-DI,項目名稱:Brancher,代碼行數:25,代碼來源:variables.py

示例14: move_to_device

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def move_to_device(obj, device):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    if not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.to(device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, device) for item in obj]
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, device) for item in obj])
    else:
        return obj 
開發者ID:jcyk,項目名稱:gtos,代碼行數:19,代碼來源:environment.py

示例15: batchify

# 需要導入模塊: import torch [as 別名]
# 或者: from torch import Tensors [as 別名]
def batchify(
    data: List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """custom collate_fn for DataLoader
    Args:
        data (List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]): list of tuples of torch.Tensors
    Returns:
        qpair (Tuple[torch.Tensor, torch.Tensor, torch.Tensor]): tuple of torch.Tensors
    """
    data = list(zip(*data))
    queries_a, queries_b, is_duplicates = data
    queries_a = pad_sequence(queries_a, batch_first=True, padding_value=1)
    queries_b = pad_sequence(queries_b, batch_first=True, padding_value=1)
    is_duplicates = torch.stack(is_duplicates, 0)

    return queries_a, queries_b, is_duplicates 
開發者ID:aisolab,項目名稱:nlp_classification,代碼行數:18,代碼來源:data.py


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