本文整理匯總了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)
示例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