当前位置: 首页>>代码示例>>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;未经允许,请勿转载。