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


Python torch.functional方法代码示例

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


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

示例1: build_header_str

# 需要导入模块: import torch [as 别名]
# 或者: from torch import functional [as 别名]
def build_header_str(net_name, rgb_mean, rgb_std, im_size, uses_functional,
                     debug_mode):
    """Generate source code header - constructs the header source
    code for the network definition file.

    Args:
        net_name (str): name of the network architecture
        debug_mode (bool): whether to generate additional debugging code
        rgb_mean (List): average rgb image of training data
        rgb_std (List): standard deviation of rgb images in training data
        im_size (List): spatial dimensions of the training input image size
        uses_functional (bool): whether the network requires the
           torch.functional module

    Returns:
        (str) : source code header string.
    """
    imports = '''
import torch
import torch.nn as nn
'''
    if uses_functional:
        imports = imports + '''
import torch.nn.functional as F
'''
    header = imports + '''

class {0}(nn.Module):

    def __init__(self):
        super({0}, self).__init__()
        self.meta = {{'mean': {1},
                     'std': {2},
                     'imageSize': {3}}}
'''
    if debug_mode:
        header = header + '''
        from collections import OrderedDict
        self.debug_feats = OrderedDict() # only used for feature verification
'''
    return header.format(net_name, rgb_mean, rgb_std, im_size) 
开发者ID:albanie,项目名称:pytorch-mcn,代码行数:43,代码来源:source_gen.py

示例2: _compute_conv_grad_sample

# 需要导入模块: import torch [as 别名]
# 或者: from torch import functional [as 别名]
def _compute_conv_grad_sample(layer, A, B, batch_dim=0):
    n = A.shape[0]
    layer_type = get_layer_type(layer)
    # get A and B in shape depending on the Conv layer
    if layer_type == "Conv2d":
        A = torch.nn.functional.unfold(
            A, layer.kernel_size, padding=layer.padding, stride=layer.stride
        )
        B = B.reshape(n, -1, A.shape[-1])
    elif layer_type == "Conv1d":
        # unfold doesn't work for 3D tensors; so force it to be 4D
        A = A.unsqueeze(-2)  # add the H dimension
        # set arguments to tuples with appropriate second element
        A = torch.nn.functional.unfold(
            A,
            (1, layer.kernel_size[0]),
            padding=(0, layer.padding[0]),
            stride=(1, layer.stride[0]),
        )
        B = B.reshape(n, -1, A.shape[-1])
    try:
        # n=batch_sz; o=num_out_channels; p=num_in_channels*kernel_sz
        grad_sample = (
            torch.einsum("noq,npq->nop", B, A)
            if layer.groups == 1
            else torch.einsum("njk,njk->nj", B, A)
        )
        shape = [n] + list(layer.weight.shape)
        _create_or_extend_grad_sample(
            layer.weight, grad_sample.reshape(shape), batch_dim
        )
    except Exception as e:
        raise type(e)(
            f"{e} There is probably a problem with {layer_type}.groups"
            + "It should be either 1 or in_channel"
        )
    if layer.bias is not None:
        _create_or_extend_grad_sample(layer.bias, torch.sum(B, dim=2), batch_dim) 
开发者ID:facebookresearch,项目名称:pytorch-dp,代码行数:40,代码来源:supported_layers_grad_samplers.py


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