本文整理匯總了Python中torch.linspace方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.linspace方法的具體用法?Python torch.linspace怎麽用?Python torch.linspace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.linspace方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _fade_in
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _fade_in(self, waveform_length: int) -> Tensor:
fade = torch.linspace(0, 1, self.fade_in_len)
ones = torch.ones(waveform_length - self.fade_in_len)
if self.fade_shape == "linear":
fade = fade
if self.fade_shape == "exponential":
fade = torch.pow(2, (fade - 1)) * fade
if self.fade_shape == "logarithmic":
fade = torch.log10(.1 + fade) + 1
if self.fade_shape == "quarter_sine":
fade = torch.sin(fade * math.pi / 2)
if self.fade_shape == "half_sine":
fade = torch.sin(fade * math.pi - math.pi / 2) / 2 + 0.5
return torch.cat((fade, ones)).clamp_(0, 1)
示例2: _fade_out
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _fade_out(self, waveform_length: int) -> Tensor:
fade = torch.linspace(0, 1, self.fade_out_len)
ones = torch.ones(waveform_length - self.fade_out_len)
if self.fade_shape == "linear":
fade = - fade + 1
if self.fade_shape == "exponential":
fade = torch.pow(2, - fade) * (1 - fade)
if self.fade_shape == "logarithmic":
fade = torch.log10(1.1 - fade) + 1
if self.fade_shape == "quarter_sine":
fade = torch.sin(fade * math.pi / 2 + math.pi / 2)
if self.fade_shape == "half_sine":
fade = torch.sin(fade * math.pi + math.pi / 2) / 2 + 0.5
return torch.cat((ones, fade)).clamp_(0, 1)
示例3: backwarp
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def backwarp(tenInput, tenFlow):
if str(tenFlow.size()) not in backwarp_tenGrid:
tenHorizontal = torch.linspace(-1.0, 1.0, tenFlow.shape[3]).view(1, 1, 1, tenFlow.shape[3]).expand(tenFlow.shape[0], -1, tenFlow.shape[2], -1)
tenVertical = torch.linspace(-1.0, 1.0, tenFlow.shape[2]).view(1, 1, tenFlow.shape[2], 1).expand(tenFlow.shape[0], -1, -1, tenFlow.shape[3])
backwarp_tenGrid[str(tenFlow.size())] = torch.cat([ tenHorizontal, tenVertical ], 1).cuda()
# end
if str(tenFlow.size()) not in backwarp_tenPartial:
backwarp_tenPartial[str(tenFlow.size())] = tenFlow.new_ones([ tenFlow.shape[0], 1, tenFlow.shape[2], tenFlow.shape[3] ])
# end
tenFlow = torch.cat([ tenFlow[:, 0:1, :, :] / ((tenInput.shape[3] - 1.0) / 2.0), tenFlow[:, 1:2, :, :] / ((tenInput.shape[2] - 1.0) / 2.0) ], 1)
tenInput = torch.cat([ tenInput, backwarp_tenPartial[str(tenFlow.size())] ], 1)
tenOutput = torch.nn.functional.grid_sample(input=tenInput, grid=(backwarp_tenGrid[str(tenFlow.size())] + tenFlow).permute(0, 2, 3, 1), mode='bilinear', padding_mode='zeros', align_corners=True)
tenMask = tenOutput[:, -1:, :, :]; tenMask[tenMask > 0.999] = 1.0; tenMask[tenMask < 1.0] = 0.0
return tenOutput[:, :-1, :, :] * tenMask
# end
##########################################################
示例4: test_geodesic_segment_length_property
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def test_geodesic_segment_length_property(a, b, manifold, dtype):
extra_dims = len(a.shape)
segments = 12
t = torch.linspace(0, 1, segments + 1, dtype=dtype).view(
(segments + 1,) + (1,) * extra_dims
)
gamma_ab_t = manifold.geodesic(t, a, b)
gamma_ab_t0 = gamma_ab_t[:-1]
gamma_ab_t1 = gamma_ab_t[1:]
dist_ab_t0mt1 = manifold.dist(gamma_ab_t0, gamma_ab_t1, keepdim=True)
speed = manifold.dist(a, b, keepdim=True).unsqueeze(0).expand_as(dist_ab_t0mt1)
# we have exactly 12 line segments
tolerance = {
torch.float32: dict(rtol=1e-5, atol=5e-3),
torch.float64: dict(rtol=1e-5, atol=5e-3),
}
length = speed / segments
np.testing.assert_allclose(
dist_ab_t0mt1.detach(), length.detach(), **tolerance[dtype]
)
(length + dist_ab_t0mt1).sum().backward()
assert torch.isfinite(a.grad).all()
assert torch.isfinite(b.grad).all()
assert torch.isfinite(manifold.k.grad).all()
示例5: test_geodesic_segement_unit_property
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def test_geodesic_segement_unit_property(a, b, manifold, dtype):
extra_dims = len(a.shape)
segments = 12
t = torch.linspace(0, 1, segments + 1, dtype=dtype).view(
(segments + 1,) + (1,) * extra_dims
)
gamma_ab_t = manifold.geodesic_unit(t, a, b)
gamma_ab_t0 = gamma_ab_t[:1]
gamma_ab_t1 = gamma_ab_t
dist_ab_t0mt1 = manifold.dist(gamma_ab_t0, gamma_ab_t1, keepdim=True)
true_distance_travelled = t.expand_as(dist_ab_t0mt1)
# we have exactly 12 line segments
tolerance = {
torch.float32: dict(atol=2e-4, rtol=5e-5),
torch.float64: dict(atol=1e-10),
}
np.testing.assert_allclose(
dist_ab_t0mt1.detach(), true_distance_travelled.detach(), **tolerance[dtype]
)
(true_distance_travelled + dist_ab_t0mt1).sum().backward()
assert torch.isfinite(a.grad).all()
assert torch.isfinite(b.grad).all()
assert torch.isfinite(manifold.k.grad).all()
示例6: imwrap_BCHW0
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def imwrap_BCHW0(im_src, disp):
# imwrap
bn, c, h, w = im_src.shape
row = torch.linspace(-1, 1, w)
col = torch.linspace(-1, 1, h)
grid = torch.zeros(bn, h, w, 2)
for n in range(bn):
for i in range(h):
grid[n, i, :, 0] = row
for i in range(w):
grid[n, :, i, 1] = col
grid = Variable(grid, requires_grad=True).type_as(im_src)
grid[:, :, :, 0] = grid[:, :, :, 0] - disp.squeeze(1)*2/w
#print disp[-1, -1, -1], grid[-1, -1, -1, 0]
im_src.clamp(min=1e-6)
im_wrap = F.grid_sample(im_src, grid)
return im_wrap
示例7: _init_buffers
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _init_buffers(self):
m_min = 0. if self.f_min == 0 else 2595 * np.log10(1. + (self.f_min / 700))
m_max = 2595 * np.log10(1. + (self.f_max / 700))
m_pts = torch.linspace(m_min, m_max, self.n_mels + 2)
f_pts = (700 * (10**(m_pts / 2595) - 1))
bins = torch.floor(((self.n_fft - 1) * 2) * f_pts / self.sr).long()
fb = torch.zeros(self.n_fft, self.n_mels)
for m in range(1, self.n_mels + 1):
f_m_minus = bins[m - 1].item()
f_m = bins[m].item()
f_m_plus = bins[m + 1].item()
if f_m_minus != f_m:
fb[f_m_minus:f_m, m - 1] = (torch.arange(f_m_minus, f_m) - f_m_minus) / (f_m - f_m_minus)
if f_m != f_m_plus:
fb[f_m:f_m_plus, m - 1] = (f_m_plus - torch.arange(f_m, f_m_plus)) / (f_m_plus - f_m)
self.register_buffer("fb", fb)
示例8: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def __init__(self, max_disp, start_disp=0, dilation=1, alpha=1.0, normalize=True):
super(FasterSoftArgmin, self).__init__()
self.max_disp = max_disp
self.start_disp = start_disp
self.dilation = dilation
self.end_disp = start_disp + max_disp - 1
self.disp_sample_number = (max_disp + dilation - 1) // dilation
self.alpha = alpha
self.normalize = normalize
# compute disparity index: (1 ,1, disp_sample_number, 1, 1)
disp_sample = torch.linspace(
self.start_disp, self.end_disp, self.disp_sample_number
)
disp_sample = disp_sample.repeat(1, 1, 1, 1, 1).permute(0, 1, 4, 2, 3).contiguous()
self.disp_regression = nn.Conv3d(1, 1, (self.disp_sample_number, 1, 1), 1, 0, bias=False)
self.disp_regression.weight.data = disp_sample
self.disp_regression.weight.requires_grad = False
示例9: test_speed
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def test_speed(self):
max_disp = 192
scale = 4
start_disp = 0
dilation = 1
SH, SW = 540, 960
B, C, H, W = 1, 32, SH//scale, SW//scale
reference_fm = torch.rand(B, C, H, W).to(self.device)
target_fm = torch.rand(B, C, H, W).to(self.device)
self.timeTemplate(cat_fms, 'CAT_FMS', reference_fm, target_fm, max_disp//scale, start_disp, dilation)
self.timeTemplate(fast_cat_fms, 'FAST_CAT_FMS', reference_fm, target_fm, max_disp//scale, start_disp, dilation)
print('Test fast_cat_fms with disparity samples')
d = (max_disp + dilation - 1) // dilation
end_disp = start_disp + max_disp - 1
# generate disparity samples
disp_samples = torch.linspace(start_disp, end_disp, d).repeat(1, H, W, 1). \
permute(0, 3, 1, 2).contiguous().to(self.device)
self.timeTemplate(fast_cat_fms, 'FAST_CAT_FMS', reference_fm, target_fm, max_disp//scale, start_disp, dilation, disp_samples)
示例10: _sample_ortho_batch
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _sample_ortho_batch(self, mu, dim):
"""
:param mu: Variable, [batch size, latent dim]
:param dim: scala. =latent dim
:return:
"""
_batch_sz, _lat_dim = mu.size()
assert _lat_dim == dim
squeezed_mu = mu.unsqueeze(1)
v = GVar(torch.randn(_batch_sz, dim, 1)) # TODO random
# v = GVar(torch.linspace(-1, 1, steps=dim))
# v = v.expand(_batch_sz, dim).unsqueeze(2)
rescale_val = torch.bmm(squeezed_mu, v).squeeze(2)
proj_mu_v = mu * rescale_val
ortho = v.squeeze() - proj_mu_v
ortho_norm = torch.norm(ortho, p=2, dim=1, keepdim=True)
y = ortho / ortho_norm
return y
示例11: _sample_orthonormal_to
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _sample_orthonormal_to(self, mu, dim):
"""Sample point on sphere orthogonal to mu.
"""
v = GVar(torch.randn(dim)) # TODO random
# v = GVar(torch.linspace(-1,1,steps=dim))
rescale_value = mu.dot(v) / mu.norm()
proj_mu_v = mu * rescale_value.expand(dim)
ortho = v - proj_mu_v
ortho_norm = torch.norm(ortho)
return ortho / ortho_norm.expand_as(ortho)
#
# a = torch.tensor(10)
# b = torch.ones(1, dtype=torch.float, requires_grad=True)
#
# y = bessel(a, b)
# loss = 1 - y
# print(y)
# loss.backward()
# print(a)
示例12: _sample_orthonormal_to
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def _sample_orthonormal_to(self, mu, dim):
"""Sample point on sphere orthogonal to mu.
"""
v = GVar(torch.randn(dim)) # TODO random
# v = GVar(torch.linspace(-1,1,steps=dim))
rescale_value = mu.dot(v) / mu.norm()
proj_mu_v = mu * rescale_value.expand(dim)
ortho = v - proj_mu_v
ortho_norm = torch.norm(ortho)
return ortho / ortho_norm.expand_as(ortho)
# vmf = vMF_fast(50, 100, 100)
# batchsz = 100
#
# mu = torch.FloatTensor(np.random.uniform(0, 1, 20 * batchsz))
# mu = mu.view(batchsz, -1)
# mu = mu / torch.norm(mu, p=2, dim=1, keepdim=True)
# vmf.sample_cell(mu, None, 100)
# x = vMF(10,lat_dim=50,kappa=50)
示例13: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def forward(self, image, flow):
flow_for_grip = torch.zeros_like(flow)
flow_for_grip[:,0,:,:] = flow[:,0,:,:] / ((flow.size(3) - 1.0) / 2.0)
flow_for_grip[:,1,:,:] = flow[:,1,:,:] / ((flow.size(2) - 1.0) / 2.0)
torchHorizontal = torch.linspace(
-1.0, 1.0, image.size(3)).view(
1, 1, 1, image.size(3)).expand(
image.size(0), 1, image.size(2), image.size(3))
torchVertical = torch.linspace(
-1.0, 1.0, image.size(2)).view(
1, 1, image.size(2), 1).expand(
image.size(0), 1, image.size(2), image.size(3))
grid = torch.cat([torchHorizontal, torchVertical], 1).cuda()
grid = (grid + flow_for_grip).permute(0, 2, 3, 1)
return torch.nn.functional.grid_sample(image, grid)
示例14: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def __init__(self, N_filt,Filt_dim,fs, stride=1, padding=0, is_cuda=False):
super(SincLayer,self).__init__()
# Mel Initialization of the filterbanks
low_freq_mel = 80
high_freq_mel = (2595 * np.log10(1 + (fs / 2) / 700)) # Convert Hz to Mel
mel_points = np.linspace(low_freq_mel, high_freq_mel, N_filt) # Equally spaced in Mel scale
f_cos = (700 * (10**(mel_points / 2595) - 1)) # Convert Mel to Hz
b1=np.roll(f_cos,1)
b2=np.roll(f_cos,-1)
b1[0]=30
b2[-1]=(fs/2)-100
self.freq_scale=fs*1.0
self.filt_b1 = torch.nn.Parameter(torch.from_numpy(b1/self.freq_scale))
self.filt_band = torch.nn.Parameter(torch.from_numpy((b2-b1)/self.freq_scale))
self.N_filt=N_filt
self.Filt_dim=Filt_dim
self.fs=fs
self.stride=stride
self.padding=padding
self.is_cuda = is_cuda
示例15: uniform_grid
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import linspace [as 別名]
def uniform_grid(shape):
'''Uniformly places control points aranged in grid accross normalized image coordinates.
Params
------
shape : tuple
HxW defining the number of control points in height and width dimension
Returns
-------
points: HxWx2 tensor
Control points over [0,1] normalized image range.
'''
H,W = shape[:2]
c = torch.zeros(H, W, 2)
c[..., 0] = torch.linspace(0, 1, W)
c[..., 1] = torch.linspace(0, 1, H).unsqueeze(-1)
return c