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


Python modules.Module方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: from torch.nn import modules [as 別名]
# 或者: from torch.nn.modules import Module [as 別名]
def __init__(self, module_or_grads_list):
        if isinstance(module_or_grads_list, Module):
            self.module = module_or_grads_list
            flat_dist_call([param.data for param in self.module.parameters()], dist.broadcast, (0,) )

        else:
            self.module = None
            self.grads = []
            extract_tensors(module_or_grads_list, self.grads) 
開發者ID:NVIDIA,項目名稱:apex,代碼行數:11,代碼來源:distributed.py

示例2: plot_losses

# 需要導入模塊: from torch.nn import modules [as 別名]
# 或者: from torch.nn.modules import Module [as 別名]
def plot_losses(
    losses: Union[nn.Module, List[nn.Module]],
    visdom_server: Optional["visdom.Visdom"] = None,
    env: Optional[str] = None,
    win: Optional[str] = None,
    title: str = "",
) -> Any:
    """Constructs a plot of specified losses as function of y * f(x). The losses
    are a list of nn.Module losses. Optionally, the environment, window handle,
    and title for the visdom plot can be specified.
    """

    if visdom_server is None and visdom_connected():
        visdom_server = vis[-1]

    # return if we are not connected to visdom server:
    if not visdom_server or not visdom_server.check_connection():
        print("WARNING: Not connected to visdom. Skipping plotting.")
        return

    # assertions:
    if isinstance(losses, nn.Module):
        losses = [losses]
    assert type(losses) == list
    assert all(isinstance(loss, nn.Module) for loss in losses)
    if any(isinstance(loss, UNSUPPORTED_LOSSES) for loss in losses):
        raise NotImplementedError("loss function not supported")

    # loop over all loss functions:
    for idx, loss in enumerate(losses):

        # construct scores and targets:
        score = torch.arange(-5.0, 5.0, 0.005)
        if idx == 0:
            loss_val = torch.FloatTensor(score.size(0), len(losses))
        if isinstance(loss, REGRESSION_LOSSES):
            target = torch.FloatTensor(score.size()).fill_(0.0)
        else:
            target = torch.LongTensor(score.size()).fill_(1)

        # compute loss values:
        for n in range(0, score.nelement()):
            loss_val[n][idx] = loss(
                score.narrow(0, n, 1), target.narrow(0, n, 1)
            ).item()

    # show plot:
    title = str(loss) if title == "" else title
    legend = [str(loss) for loss in losses]
    opts = {"title": title, "xlabel": "Score", "ylabel": "Loss", "legend": legend}
    win = visdom_server.line(loss_val, score, env=env, win=win, opts=opts)
    return win 
開發者ID:facebookresearch,項目名稱:ClassyVision,代碼行數:54,代碼來源:visualize.py


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