本文整理汇总了Python中torch.Tensor.new方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.new方法的具体用法?Python Tensor.new怎么用?Python Tensor.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.Tensor
的用法示例。
在下文中一共展示了Tensor.new方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: neg_hartmann6
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import new [as 别名]
def neg_hartmann6(X: Tensor) -> Tensor:
r"""Negative Hartmann6 test function.
Six-dimensional function (typically evaluated on `[0, 1]^6`)
`H(x) = - sum_{i=1}^4 ALPHA_i exp( - sum_{j=1}^6 A_ij (x_j - P_ij)**2 )`
H has a 6 local minima and a global minimum at
`z = (0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573)`
with `H(z) = -3.32237`
Args:
X: A Tensor of size `6` or `k x 6` (k batch evaluations).
Returns:
`-H(X)`, the negative value of the standard Hartmann6 function.
"""
batch = X.ndimension() > 1
X = X if batch else X.unsqueeze(0)
inner_sum = torch.sum(X.new(A) * (X.unsqueeze(1) - 0.0001 * X.new(P)) ** 2, dim=2)
H = -torch.sum(X.new(ALPHA) * torch.exp(-inner_sum), dim=1)
result = -H
return result if batch else result.squeeze(0)
示例2: _greedy_decode
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import new [as 别名]
def _greedy_decode(self,
head_tag_representation: torch.Tensor,
child_tag_representation: torch.Tensor,
attended_arcs: torch.Tensor,
mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Decodes the head and head tag predictions by decoding the unlabeled arcs
independently for each word and then again, predicting the head tags of
these greedily chosen arcs indpendently. Note that this method of decoding
is not guaranteed to produce trees (i.e. there maybe be multiple roots,
or cycles when children are attached to their parents).
Parameters
----------
head_tag_representation : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
child_tag_representation : ``torch.Tensor``, required
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
attended_arcs : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, sequence_length) used to generate
a distribution over attachements of a given word to all other words.
Returns
-------
heads : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length) representing the
greedily decoded heads of each word.
head_tags : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length) representing the
dependency tags of the greedily decoded heads of each word.
"""
# Mask the diagonal, because the head of a word can't be itself.
attended_arcs = attended_arcs + torch.diag(attended_arcs.new(mask.size(1)).fill_(-numpy.inf))
# Mask padded tokens, because we only want to consider actual words as heads.
if mask is not None:
minus_mask = (1 - mask).byte().unsqueeze(2)
attended_arcs.masked_fill_(minus_mask, -numpy.inf)
# Compute the heads greedily.
# shape (batch_size, sequence_length)
_, heads = attended_arcs.max(dim=2)
# Given the greedily predicted heads, decode their dependency tags.
# shape (batch_size, sequence_length, num_head_tags)
head_tag_logits = self._get_head_tags(head_tag_representation,
child_tag_representation,
heads)
_, head_tags = head_tag_logits.max(dim=2)
return heads, head_tags
示例3: _greedy_decode
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import new [as 别名]
def _greedy_decode(arc_scores: torch.Tensor,
arc_tag_logits: torch.Tensor,
mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Decodes the head and head tag predictions by decoding the unlabeled arcs
independently for each word and then again, predicting the head tags of
these greedily chosen arcs independently.
Parameters
----------
arc_scores : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, sequence_length) used to generate
a distribution over attachments of a given word to all other words.
arc_tag_logits : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, sequence_length, num_tags) used to
generate a distribution over tags for each arc.
mask : ``torch.Tensor``, required.
A mask of shape (batch_size, sequence_length).
Returns
-------
arc_probs : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length, sequence_length) representing the
probability of an arc being present for this edge.
arc_tag_probs : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length, sequence_length, sequence_length)
representing the distribution over edge tags for a given edge.
"""
# Mask the diagonal, because we don't self edges.
inf_diagonal_mask = torch.diag(arc_scores.new(mask.size(1)).fill_(-numpy.inf))
arc_scores = arc_scores + inf_diagonal_mask
# shape (batch_size, sequence_length, sequence_length, num_tags)
arc_tag_logits = arc_tag_logits + inf_diagonal_mask.unsqueeze(0).unsqueeze(-1)
# Mask padded tokens, because we only want to consider actual word -> word edges.
minus_mask = (1 - mask).byte().unsqueeze(2)
arc_scores.masked_fill_(minus_mask, -numpy.inf)
arc_tag_logits.masked_fill_(minus_mask.unsqueeze(-1), -numpy.inf)
# shape (batch_size, sequence_length, sequence_length)
arc_probs = arc_scores.sigmoid()
# shape (batch_size, sequence_length, sequence_length, num_tags)
arc_tag_probs = torch.nn.functional.softmax(arc_tag_logits, dim=-1)
return arc_probs, arc_tag_probs
示例4: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import new [as 别名]
def forward(self, # pylint: disable=arguments-differ
inputs: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
state_accumulator: torch.Tensor,
memory_accumulator: torch.Tensor,
dropout_mask: torch.Tensor,
lengths: torch.Tensor,
gates: torch.Tensor) -> Tuple[torch.Tensor, None]:
sequence_length, batch_size, input_size = inputs.size()
tmp_i = inputs.new(batch_size, 6 * self.hidden_size)
tmp_h = inputs.new(batch_size, 5 * self.hidden_size)
is_training = 1 if self.train else 0
highway_lstm_layer.highway_lstm_forward_cuda(input_size, # type: ignore # pylint: disable=no-member
self.hidden_size,
batch_size,
self.num_layers,
sequence_length,
inputs,
lengths,
state_accumulator,
memory_accumulator,
tmp_i,
tmp_h,
weight,
bias,
dropout_mask,
gates,
is_training)
self.save_for_backward(inputs, lengths, weight, bias, state_accumulator,
memory_accumulator, dropout_mask, gates)
# The state_accumulator has shape: (num_layers, sequence_length + 1, batch_size, hidden_size)
# so for the output, we want the last layer and all but the first timestep, which was the
# initial state.
output = state_accumulator[-1, 1:, :, :]
return output, state_accumulator[:, 1:, :, :]