當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。