本文整理汇总了Python中torch.transpose方法的典型用法代码示例。如果您正苦于以下问题:Python torch.transpose方法的具体用法?Python torch.transpose怎么用?Python torch.transpose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.transpose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: m_ggnn
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def m_ggnn(self, h_v, h_w, e_vw, opt={}):
m = Variable(torch.zeros(h_w.size(0), h_w.size(1), self.args['out']).type_as(h_w.data))
for w in range(h_w.size(1)):
if torch.nonzero(e_vw[:, w, :].data).size():
for i, el in enumerate(self.args['e_label']):
ind = (el == e_vw[:,w,:]).type_as(self.learn_args[0][i])
parameter_mat = self.learn_args[0][i][None, ...].expand(h_w.size(0), self.learn_args[0][i].size(0),
self.learn_args[0][i].size(1))
m_w = torch.transpose(torch.bmm(torch.transpose(parameter_mat, 1, 2),
torch.transpose(torch.unsqueeze(h_w[:, w, :], 1),
1, 2)), 1, 2)
m_w = torch.squeeze(m_w)
m[:,w,:] = ind.expand_as(m_w)*m_w
return m
示例2: r_duvenaud
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def r_duvenaud(self, h):
# layers
aux = []
for l in range(len(h)):
param_sz = self.learn_args[l].size()
parameter_mat = torch.t(self.learn_args[l])[None, ...].expand(h[l].size(0), param_sz[1],
param_sz[0])
aux.append(torch.transpose(torch.bmm(parameter_mat, torch.transpose(h[l], 1, 2)), 1, 2))
for j in range(0, aux[l].size(1)):
# Mask whole 0 vectors
aux[l][:, j, :] = nn.Softmax()(aux[l][:, j, :].clone())*(torch.sum(aux[l][:, j, :] != 0, 1) > 0).expand_as(aux[l][:, j, :]).type_as(aux[l])
aux = torch.sum(torch.sum(torch.stack(aux, 3), 3), 1)
return self.learn_modules[0](torch.squeeze(aux))
示例3: max_pool1d
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def max_pool1d(inputs, kernel_size, stride=1, padding='same'):
'''
inputs: [N, T, C]
outputs: [N, T // stride, C]
'''
inputs = inputs.transpose(1, 2) # [N, C, T]
if padding == 'same':
left = (kernel_size - 1) // 2
right = (kernel_size - 1) - left
pad = (left, right)
else:
pad = (0, 0)
inputs = F.pad(inputs, pad)
outputs = F.max_pool1d(inputs, kernel_size, stride) # [N, C, T]
outputs = outputs.transpose(1, 2) # [N, T, C]
return outputs
示例4: cmmd
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def cmmd(source, target, s_label, t_label, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
s_label = s_label.cpu()
s_label = s_label.view(32,1)
s_label = torch.zeros(32, 31).scatter_(1, s_label.data, 1)
s_label = Variable(s_label).cuda()
t_label = t_label.cpu()
t_label = t_label.view(32, 1)
t_label = torch.zeros(32, 31).scatter_(1, t_label.data, 1)
t_label = Variable(t_label).cuda()
batch_size = int(source.size()[0])
kernels = guassian_kernel(source, target,
kernel_mul=kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma)
loss = 0
XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
loss += torch.mean(torch.mm(s_label, torch.transpose(s_label, 0, 1)) * XX +
torch.mm(t_label, torch.transpose(t_label, 0, 1)) * YY -
2 * torch.mm(s_label, torch.transpose(t_label, 0, 1)) * XY)
return loss
示例5: forward
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def forward(self, input_):
"""
:param input_: sequences
:return: outputs
"""
batch_size = input_.size()[0]
if self.time_dim == 2:
input_ = input_.transpose(1, 2).contiguous()
input_ = input_.view(-1, self.input_size)
out = self.linear(input_).view(batch_size, -1, self.output_size)
if self.time_dim == 2:
out = out.contiguous().transpose(1, 2)
return out
示例6: intersection_area
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def intersection_area(yx_min1, yx_max1, yx_min2, yx_max2):
"""
Calculates the intersection area of two lists of bounding boxes.
:author 申瑞珉 (Ruimin Shen)
:param yx_min1: The top left coordinates (y, x) of the first list (size [N1, 2]) of bounding boxes.
:param yx_max1: The bottom right coordinates (y, x) of the first list (size [N1, 2]) of bounding boxes.
:param yx_min2: The top left coordinates (y, x) of the second list (size [N2, 2]) of bounding boxes.
:param yx_max2: The bottom right coordinates (y, x) of the second list (size [N2, 2]) of bounding boxes.
:return: The matrix (size [N1, N2]) of the intersection area.
"""
ymin1, xmin1 = torch.split(yx_min1, 1, -1)
ymax1, xmax1 = torch.split(yx_max1, 1, -1)
ymin2, xmin2 = torch.split(yx_min2, 1, -1)
ymax2, xmax2 = torch.split(yx_max2, 1, -1)
max_ymin = torch.max(ymin1.repeat(1, ymin2.size(0)), torch.transpose(ymin2, 0, 1).repeat(ymin1.size(0), 1)) # PyTorch's bug
min_ymax = torch.min(ymax1.repeat(1, ymax2.size(0)), torch.transpose(ymax2, 0, 1).repeat(ymax1.size(0), 1)) # PyTorch's bug
height = torch.clamp(min_ymax - max_ymin, min=0)
max_xmin = torch.max(xmin1.repeat(1, xmin2.size(0)), torch.transpose(xmin2, 0, 1).repeat(xmin1.size(0), 1)) # PyTorch's bug
min_xmax = torch.min(xmax1.repeat(1, xmax2.size(0)), torch.transpose(xmax2, 0, 1).repeat(xmax1.size(0), 1)) # PyTorch's bug
width = torch.clamp(min_xmax - max_xmin, min=0)
return height * width
示例7: batch_intersection_area
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def batch_intersection_area(yx_min1, yx_max1, yx_min2, yx_max2):
"""
Calculates the intersection area of two lists of bounding boxes for N independent batches.
:author 申瑞珉 (Ruimin Shen)
:param yx_min1: The top left coordinates (y, x) of the first lists (size [N, N1, 2]) of bounding boxes.
:param yx_max1: The bottom right coordinates (y, x) of the first lists (size [N, N1, 2]) of bounding boxes.
:param yx_min2: The top left coordinates (y, x) of the second lists (size [N, N2, 2]) of bounding boxes.
:param yx_max2: The bottom right coordinates (y, x) of the second lists (size [N, N2, 2]) of bounding boxes.
:return: The matrics (size [N, N1, N2]) of the intersection area.
"""
ymin1, xmin1 = torch.split(yx_min1, 1, -1)
ymax1, xmax1 = torch.split(yx_max1, 1, -1)
ymin2, xmin2 = torch.split(yx_min2, 1, -1)
ymax2, xmax2 = torch.split(yx_max2, 1, -1)
max_ymin = torch.max(ymin1.repeat(1, 1, ymin2.size(1)), torch.transpose(ymin2, 1, 2).repeat(1, ymin1.size(1), 1)) # PyTorch's bug
min_ymax = torch.min(ymax1.repeat(1, 1, ymax2.size(1)), torch.transpose(ymax2, 1, 2).repeat(1, ymax1.size(1), 1)) # PyTorch's bug
height = torch.clamp(min_ymax - max_ymin, min=0)
max_xmin = torch.max(xmin1.repeat(1, 1, xmin2.size(1)), torch.transpose(xmin2, 1, 2).repeat(1, xmin1.size(1), 1)) # PyTorch's bug
min_xmax = torch.min(xmax1.repeat(1, 1, xmax2.size(1)), torch.transpose(xmax2, 1, 2).repeat(1, xmax1.size(1), 1)) # PyTorch's bug
width = torch.clamp(min_xmax - max_xmin, min=0)
return height * width
示例8: birkhoff_case
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def birkhoff_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.BirkhoffPolytope]
ex = torch.randn(*shape, dtype=torch.float64).abs()
ev = torch.randn(*shape, dtype=torch.float64)
max_iter = 100
eps = 1e-12
tol = 1e-5
iter = 0
c = 1.0 / (torch.sum(ex, dim=-2, keepdim=True) + eps)
r = 1.0 / (torch.matmul(ex, c.transpose(-1, -2)) + eps)
while iter < max_iter:
iter += 1
cinv = torch.matmul(r.transpose(-1, -2), ex)
if torch.max(torch.abs(cinv * c - 1)) <= tol:
break
c = 1.0 / (cinv + eps)
r = 1.0 / ((ex @ c.transpose(-1, -2)) + eps)
x = ex * (r @ c)
v = proju_original(x, ev)
manifold = geoopt.manifolds.BirkhoffPolytope()
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例9: sphere_compliment_case
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def sphere_compliment_case():
torch.manual_seed(42)
shape = manifold_shapes[geoopt.manifolds.Sphere]
complement = torch.rand(shape[-1], 1, dtype=torch.float64)
Q, _ = geoopt.linalg.batch_linalg.qr(complement)
P = -Q @ Q.transpose(-1, -2)
P[..., torch.arange(P.shape[-2]), torch.arange(P.shape[-2])] += 1
ex = torch.randn(*shape, dtype=torch.float64)
ev = torch.randn(*shape, dtype=torch.float64)
x = (ex @ P.t()) / torch.norm(ex @ P.t())
v = (ev - (x @ ev) * x) @ P.t()
manifold = geoopt.Sphere(complement=complement)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
manifold = geoopt.SphereExact(complement=complement)
x = geoopt.ManifoldTensor(x, manifold=manifold)
case = UnaryCase(shape, x, ex, v, ev, manifold)
yield case
示例10: channel_shuffle
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def channel_shuffle(x, groups):
batchsize, num_channels, height, width = x.data.size()
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups,
channels_per_group, height, width)
# transpose
# - contiguous() required if transpose() is used before view().
# See https://github.com/pytorch/pytorch/issues/764
x = torch.transpose(x, 1, 2).contiguous()
# flatten
x = x.view(batchsize, -1, height, width)
return x
示例11: convolutional_layer
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def convolutional_layer(self, inputs):
convolution_all = []
conv_wts = []
for i in range(self.seq_len):
convolution_one_month = []
for j in range(self.pad_size):
convolution = self.conv(torch.unsqueeze(inputs[:, i, j], dim=1))
convolution_one_month.append(convolution)
convolution_one_month = torch.stack(convolution_one_month)
convolution_one_month = torch.squeeze(convolution_one_month, dim=3)
convolution_one_month = torch.transpose(convolution_one_month, 0, 1)
convolution_one_month = torch.transpose(convolution_one_month, 1, 2)
convolution_one_month = torch.squeeze(convolution_one_month, dim=1)
convolution_one_month = self.func_tanh(convolution_one_month)
convolution_one_month = torch.unsqueeze(convolution_one_month, dim=1)
vec = torch.bmm(convolution_one_month, inputs[:, i])
convolution_all.append(vec)
conv_wts.append(convolution_one_month)
convolution_all = torch.stack(convolution_all, dim=1)
convolution_all = torch.squeeze(convolution_all, dim=2)
conv_wts = torch.squeeze(torch.stack(conv_wts, dim=1), dim=2)
return convolution_all, conv_wts
示例12: get_loss
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def get_loss(pred, y, criterion, mtr, a=0.5):
"""
To calculate loss
:param pred: predicted value
:param y: actual value
:param criterion: nn.CrossEntropyLoss
:param mtr: beta matrix
"""
mtr_t = torch.transpose(mtr, 1, 2)
aa = torch.bmm(mtr, mtr_t)
loss_fn = 0
for i in range(aa.size()[0]):
aai = torch.add(aa[i, ], Variable(torch.neg(torch.eye(mtr.size()[1]))))
loss_fn += torch.trace(torch.mul(aai, aai).data)
loss_fn /= aa.size()[0]
loss = torch.add(criterion(pred, y), Variable(torch.FloatTensor([loss_fn * a])))
return loss
示例13: similarity
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def similarity(self, s1, l1, s2, l2):
"""
:param s1: [B, t1, D]
:param l1: [B]
:param s2: [B, t2, D]
:param l2: [B]
:return:
"""
batch_size = s1.size(0)
t1 = s1.size(1)
t2 = s2.size(1)
S = torch.bmm(s1, s2.transpose(1,
2)) # [B, t1, D] * [B, D, t2] -> [B, t1, t2] S is the similarity matrix from biDAF paper. [B, T1, T2]
s_mask = S.data.new(*S.size()).fill_(1).byte() # [B, T1, T2]
# Init similarity mask using lengths
for i, (l_1, l_2) in enumerate(zip(l1, l2)):
s_mask[i][:l_1, :l_2] = 0
s_mask = Variable(s_mask)
S.data.masked_fill_(s_mask.data.byte(), -math.inf)
return S
示例14: __init__
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def __init__(self, n_classes, n_blocks, pyramids, class_emb):
super(DeepLabV2, self).__init__()
if class_emb is not None:
self.emb_size = class_emb.shape[1]
self.class_emb = torch.transpose(class_emb, 1, 0).float().cuda()
self.add_module(
"layer1",
nn.Sequential(
OrderedDict(
[
("conv1", _ConvBatchNormReLU(3, 64, 7, 2, 3, 1)),
("pool", nn.MaxPool2d(3, 2, 1, ceil_mode=True)),
]
)
),
)
self.add_module("layer2", _ResBlock(n_blocks[0], 64, 64, 256, 1, 1))
self.add_module("layer3", _ResBlock(n_blocks[1], 256, 128, 512, 2, 1))
self.add_module("layer4", _ResBlock(n_blocks[2], 512, 256, 1024, 1, 2))
self.add_module("layer5", _ResBlock(n_blocks[3], 1024, 512, 2048, 1, 4))
self.add_module("aspp", _ASPPModule(2048, n_classes, pyramids))
print("DeepLab Outputs: {}".format(n_classes))
示例15: _dropping
# 需要导入模块: import torch [as 别名]
# 或者: from torch import transpose [as 别名]
def _dropping(self, delta):
weight = self.conv.weight * self.mask
### Sum up all kernels
### Assume only apply to 1x1 conv to speed up
assert weight.size()[-1] == 1
weight = weight.abs().squeeze()
assert weight.size()[0] == self.out_channels
assert weight.size()[1] == self.in_channels
d_out = self.out_channels // self.groups
### Shuffle weight
weight = weight.view(d_out, self.groups, self.in_channels)
weight = weight.transpose(0, 1).contiguous()
weight = weight.view(self.out_channels, self.in_channels)
### Sort and drop
for i in range(self.groups):
wi = weight[i * d_out:(i + 1) * d_out, :]
### Take corresponding delta index
di = wi.sum(0).sort()[1][self.count:self.count + delta]
for d in di.data:
self._mask[i::self.groups, d, :, :].fill_(0)
self.count = self.count + delta