本文整理汇总了Python中torch.Tensor.transpose方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.transpose方法的具体用法?Python Tensor.transpose怎么用?Python Tensor.transpose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.Tensor
的用法示例。
在下文中一共展示了Tensor.transpose方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: l2_distance
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def l2_distance(x: torch.Tensor, y: torch.Tensor) \
-> torch.Tensor:
"""Compute the Gram matrix holding all ||.||_2 distances."""
xTy = 2 * x.matmul(y.transpose(0, 1))
x2 = torch.sum(x ** 2, dim=1)[:, None]
y2 = torch.sum(y ** 2, dim=1)[None, :]
K = x2 + y2 - xTy
return K
示例2: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def forward(self, input: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
layer = (Recurrent(PeepholeLSTMCell), Recurrent(PeepholeLSTMCell, reverse=True))
func = StackedRNN(layer, 1, 2)
input = input.transpose(0, 1)
hidden = (torch.zeros(2, input.shape[1], self.hidden_size).to(input.device),
torch.zeros(2, input.shape[1], self.hidden_size).to(input.device))
hidden, output = func(input, hidden, self.all_weights)
output = output.transpose(0, 1)
return output, hidden
示例3: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def forward(self,
inputs: torch.Tensor,
mask: torch.Tensor) -> Dict[str, torch.Tensor]:
"""
Compute context insensitive token embeddings for ELMo representations.
Parameters
----------
inputs:
Shape ``(batch_size, num_tokens, embedding_dim)``
of character embeddings representing the current batch.
mask:
Shape ``(batch_size, num_tokens)``
mask for the current batch.
Returns
-------
``encoding``:
Shape ``(batch_size, sequence_length, embedding_dim2)`` tensor
with context-insensitive token representations. If bos_characters and eos_characters
are being added, the second dimension will be ``sequence_length + 2``.
"""
# pylint: disable=arguments-differ
# convolutions want (batch_size, embedding_dim, num_tokens)
inputs = inputs.transpose(1, 2)
convolutions = []
for i in range(len(self._convolutions)):
char_conv_i = getattr(self, f"char_conv_{i}")
convolved = char_conv_i(inputs)
# (batch_size, n_filters for this width)
convolved, _ = torch.max(convolved, dim=-1)
convolved = self._activation(convolved)
convolutions.append(convolved)
# (batch_size, n_filters)
token_embedding = torch.cat(convolutions, dim=-1)
if self._projection_location == 'after_cnn':
token_embedding = self._projection(token_embedding)
# apply the highway layers (batch_size, highway_dim)
token_embedding = self._highways(token_embedding)
if self._projection_location == 'after_highway':
# final projection (batch_size, embedding_dim)
token_embedding = self._projection(token_embedding)
# Apply layer norm if appropriate
token_embedding = self._layer_norm(token_embedding, mask)
return token_embedding
示例4: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def forward(self, inputs: torch.Tensor, mask: torch.Tensor) -> Dict[str, torch.Tensor]: # pylint: disable=unused-argument
"""
Compute context insensitive token embeddings for ELMo representations.
Parameters
----------
inputs:
Shape ``(batch_size, num_characters, embedding_dim)``
Character embeddings representing the current batch.
mask:
Shape ``(batch_size, num_characters)``
Currently unused. The mask for characters is implicit. See TokenCharactersEncoder.forward.
Returns
-------
``encoding``:
Shape ``(batch_size, projection_dim)`` tensor with context-insensitive token representations.
"""
# pylint: disable=arguments-differ
# convolutions want (batch_size, embedding_dim, num_characters)
inputs = inputs.transpose(1, 2)
convolutions = []
for i in range(len(self._convolutions)):
char_conv_i = getattr(self, f"char_conv_{i}")
convolved = char_conv_i(inputs)
# (batch_size, n_filters for this width)
convolved, _ = torch.max(convolved, dim=-1)
convolved = self._activation(convolved)
convolutions.append(convolved)
# (batch_size, n_filters)
token_embedding = torch.cat(convolutions, dim=-1)
if self._projection_location == 'after_cnn':
token_embedding = self._projection(token_embedding)
# apply the highway layers (batch_size, highway_dim)
token_embedding = self._highways(token_embedding)
if self._projection_location == 'after_highway':
# final projection (batch_size, projection_dim)
token_embedding = self._projection(token_embedding)
# Apply layer norm if appropriate
token_embedding = self._layer_norm(token_embedding)
return token_embedding
示例5: attention
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def attention(query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
mask: torch.Tensor = None,
dropout: Callable = None) -> Tuple[torch.Tensor, torch.Tensor]:
"""Compute 'Scaled Dot Product Attention'"""
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
p_attn = F.softmax(scores, dim=-1)
if dropout is not None:
p_attn = dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
示例6: _input_likelihood
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def _input_likelihood(self, logits: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
"""
Computes the (batch_size,) denominator term for the log-likelihood, which is the
sum of the likelihoods across all possible state sequences.
"""
batch_size, sequence_length, num_tags = logits.size()
# Transpose batch size and sequence dimensions
mask = mask.float().transpose(0, 1).contiguous()
logits = logits.transpose(0, 1).contiguous()
# Initial alpha is the (batch_size, num_tags) tensor of likelihoods combining the
# transitions to the initial states and the logits for the first timestep.
if self.include_start_end_transitions:
alpha = self.start_transitions.view(1, num_tags) + logits[0]
else:
alpha = logits[0]
# For each i we compute logits for the transitions from timestep i-1 to timestep i.
# We do so in a (batch_size, num_tags, num_tags) tensor where the axes are
# (instance, current_tag, next_tag)
for i in range(1, sequence_length):
# The emit scores are for time i ("next_tag") so we broadcast along the current_tag axis.
emit_scores = logits[i].view(batch_size, 1, num_tags)
# Transition scores are (current_tag, next_tag) so we broadcast along the instance axis.
transition_scores = self.transitions.view(1, num_tags, num_tags)
# Alpha is for the current_tag, so we broadcast along the next_tag axis.
broadcast_alpha = alpha.view(batch_size, num_tags, 1)
# Add all the scores together and logexp over the current_tag axis
inner = broadcast_alpha + emit_scores + transition_scores
# In valid positions (mask == 1) we want to take the logsumexp over the current_tag dimension
# of ``inner``. Otherwise (mask == 0) we want to retain the previous alpha.
alpha = (util.logsumexp(inner, 1) * mask[i].view(batch_size, 1) +
alpha * (1 - mask[i]).view(batch_size, 1))
# Every sequence needs to end with a transition to the stop_tag.
if self.include_start_end_transitions:
stops = alpha + self.end_transitions.view(1, num_tags)
else:
stops = alpha
# Finally we log_sum_exp along the num_tags dim, result is (batch_size,)
return util.logsumexp(stops)
示例7: multi_perspective_match_pairwise
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def multi_perspective_match_pairwise(vector1: torch.Tensor,
vector2: torch.Tensor,
weight: torch.Tensor,
eps: float = 1e-8) -> torch.Tensor:
"""
Calculate multi-perspective cosine matching between each time step of
one vector and each time step of another vector.
Parameters
----------
vector1 : ``torch.Tensor``
A tensor of shape ``(batch, seq_len1, hidden_size)``
vector2 : ``torch.Tensor``
A tensor of shape ``(batch, seq_len2, hidden_size)``
weight : ``torch.Tensor``
A tensor of shape ``(num_perspectives, hidden_size)``
eps : ``float`` optional, (default = 1e-8)
A small value to avoid zero division problem
Returns
-------
A tensor of shape (batch, seq_len1, seq_len2, num_perspectives) consisting
multi-perspective matching results
"""
num_perspectives = weight.size(0)
# (1, num_perspectives, 1, hidden_size)
weight = weight.unsqueeze(0).unsqueeze(2)
# (batch, num_perspectives, seq_len*, hidden_size)
vector1 = weight * vector1.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
vector2 = weight * vector2.unsqueeze(1).expand(-1, num_perspectives, -1, -1)
# (batch, num_perspectives, seq_len*, 1)
vector1_norm = vector1.norm(p=2, dim=3, keepdim=True)
vector2_norm = vector2.norm(p=2, dim=3, keepdim=True)
# (batch, num_perspectives, seq_len1, seq_len2)
mul_result = torch.matmul(vector1, vector2.transpose(2, 3))
norm_value = vector1_norm * vector2_norm.transpose(2, 3)
# (batch, seq_len1, seq_len2, num_perspectives)
return (mul_result / norm_value.clamp(min=eps)).permute(0, 2, 3, 1)
示例8: __init__
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def __init__(
self,
mean: Tensor,
cov: Tensor,
seed: Optional[int] = None,
inv_transform: bool = False,
) -> None:
r"""Engine for qMC sampling from a multivariate Normal `N(\mu, \Sigma)`.
Args:
mean: The mean vector.
cov: The covariance matrix.
seed: The seed with which to seed the random number generator of the
underlying SobolEngine.
inv_transform: If True, use inverse transform instead of Box-Muller.
"""
# validate inputs
if not cov.shape[0] == cov.shape[1]:
raise ValueError("Covariance matrix is not square.")
if not mean.shape[0] == cov.shape[0]:
raise ValueError("Dimension mismatch between mean and covariance.")
if not torch.allclose(cov, cov.transpose(-1, -2)):
raise ValueError("Covariance matrix is not symmetric.")
self._mean = mean
self._normal_engine = NormalQMCEngine(
d=mean.shape[0], seed=seed, inv_transform=inv_transform
)
# compute Cholesky decomp; if it fails, do the eigendecomposition
try:
self._corr_matrix = torch.cholesky(cov).transpose(-1, -2)
except RuntimeError:
eigval, eigvec = torch.symeig(cov, eigenvectors=True)
if not torch.all(eigval >= -1e-8):
raise ValueError("Covariance matrix not PSD.")
eigval_root = eigval.clamp_min(0.0).sqrt()
self._corr_matrix = (eigvec * eigval_root).transpose(-1, -2)
示例9: _forward_internal
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def _forward_internal(self, vector: torch.Tensor, matrix: torch.Tensor) -> torch.Tensor:
intermediate = vector.mm(self._weight_matrix).unsqueeze(1)
return self._activation(intermediate.bmm(matrix.transpose(1, 2)).squeeze(1) + self._bias)
示例10: _joint_likelihood
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def _joint_likelihood(self,
logits: torch.Tensor,
tags: torch.Tensor,
mask: torch.LongTensor) -> torch.Tensor:
"""
Computes the numerator term for the log-likelihood, which is just score(inputs, tags)
"""
batch_size, sequence_length, num_tags = logits.data.shape
# Transpose batch size and sequence dimensions:
logits = logits.transpose(0, 1).contiguous()
mask = mask.float().transpose(0, 1).contiguous()
tags = tags.transpose(0, 1).contiguous()
# Start with the transition scores from start_tag to the first tag in each input
if self.include_start_end_transitions:
score = self.start_transitions.index_select(0, tags[0])
else:
score = 0.0
# Broadcast the transition scores to one per batch element
broadcast_transitions = self.transitions.view(1, num_tags, num_tags).expand(batch_size, num_tags, num_tags)
# Add up the scores for the observed transitions and all the inputs but the last
for i in range(sequence_length - 1):
# Each is shape (batch_size,)
current_tag, next_tag = tags[i], tags[i+1]
# The scores for transitioning from current_tag to next_tag
transition_score = (
broadcast_transitions
# Choose the current_tag-th row for each input
.gather(1, current_tag.view(batch_size, 1, 1).expand(batch_size, 1, num_tags))
# Squeeze down to (batch_size, num_tags)
.squeeze(1)
# Then choose the next_tag-th column for each of those
.gather(1, next_tag.view(batch_size, 1))
# And squeeze down to (batch_size,)
.squeeze(1)
)
# The score for using current_tag
emit_score = logits[i].gather(1, current_tag.view(batch_size, 1)).squeeze(1)
# Include transition score if next element is unmasked,
# input_score if this element is unmasked.
score = score + transition_score * mask[i + 1] + emit_score * mask[i]
# Transition from last state to "stop" state. To start with, we need to find the last tag
# for each instance.
last_tag_index = mask.sum(0).long() - 1
last_tags = tags.gather(0, last_tag_index.view(1, batch_size).expand(sequence_length, batch_size))
# Is (sequence_length, batch_size), but all the columns are the same, so take the first.
last_tags = last_tags[0]
# Compute score of transitioning to `stop_tag` from each "last tag".
if self.include_start_end_transitions:
last_transition_score = self.end_transitions.index_select(0, last_tags)
else:
last_transition_score = 0.0
# Add the last input if it's not masked.
last_inputs = logits[-1] # (batch_size, num_tags)
last_input_score = last_inputs.gather(1, last_tags.view(-1, 1)) # (batch_size, 1)
last_input_score = last_input_score.squeeze() # (batch_size,)
score = score + last_transition_score + last_input_score * mask[-1]
return score
示例11: forward
# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import transpose [as 别名]
def forward(self, matrix_1: torch.Tensor, matrix_2: torch.Tensor) -> torch.Tensor:
return matrix_1.bmm(matrix_2.transpose(2, 1))