我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.floor()。
def test_abs(self): size = 1000 max_val = 1000 original = torch.rand(size).mul(max_val) # Tensor filled with values from {-1, 1} switch = torch.rand(size).mul(2).floor().mul(2).add(-1) types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor'] for t in types: data = original.type(t) switch = switch.type(t) res = torch.mul(data, switch) self.assertEqual(res.abs(), data, 1e-16) # Checking that the right abs function is called for LongTensor bignumber = 2^31 + 1 res = torch.LongTensor((-bignumber,)) self.assertGreater(res.abs()[0], 0)
def test_abs(self): size = 1000 max_val = 1000 original = torch.rand(size).mul(max_val) # Tensor filled with values from {-1, 1} switch = torch.rand(size).mul(2).floor().mul(2).add(-1) types = ['torch.DoubleTensor', 'torch.FloatTensor', 'torch.LongTensor', 'torch.IntTensor'] for t in types: data = original.type(t) switch = switch.type(t) res = torch.mul(data, switch) # abs is used in assertEqual so we use the slow version instead self.assertTensorsSlowEqual(res.abs(), data, 1e-16) # Checking that the right abs function is called for LongTensor bignumber = 2 ^ 31 + 1 res = torch.LongTensor((-bignumber,)) self.assertGreater(res.abs()[0], 0)
def min_max_quantize(input, bits): assert bits >= 1, bits if bits == 1: return torch.sign(input) - 1 min_val, max_val = input.min(), input.max() if isinstance(min_val, Variable): max_val = float(max_val.data.cpu().numpy()[0]) min_val = float(min_val.data.cpu().numpy()[0]) input_rescale = (input - min_val) / (max_val - min_val) n = math.pow(2.0, bits) - 1 v = torch.floor(input_rescale * n + 0.5) / n v = v * (max_val - min_val) + min_val return v
def train(self, lr, iters, batch_size = 256): optimizer = optim.Adam(self.parameters(), lr=lr) t = trange(iters) for i in t: optimizer.zero_grad() inds = torch.floor(torch.rand(batch_size) * self.M).long().cuda() # bug: floor(rand()) sometimes gives 1 inds[inds >= self.M] = self.M - 1 inds = Variable(inds) loss = self.forward(inds) # print loss.data[0] t.set_description( str(loss.data[0]) ) loss.backward() optimizer.step() return self.state_model, self.goal_model
def _get_batch(self, inputs, targets, batch_size = None, volatile = False): data_size = self._get_size(inputs) if batch_size == None: batch_size = self.batch_size inds = torch.floor(torch.rand(batch_size) * data_size).long().cuda() # bug: floor(rand()) sometimes gives 1 inds[inds >= data_size] = data_size - 1 if type(inputs) == tuple: inp = tuple([Variable( i.index_select(0, inds).cuda(), volatile=volatile ) for i in inputs]) else: inp = Variable( inputs.index_select(0, inds).cuda(), volatile=volatile ) if type(targets) == list: targ = [targets[ind] for ind in inds] elif targets != None: targ = Variable( targets.index_select(0, inds).cuda(), volatile=volatile ) else: targ = None return inp, targ
def test_floor(self): self._testMathByName('floor')
def test_renorm(self): m1 = torch.randn(10,5) res1 = torch.Tensor() def renorm(matrix, value, dim, max_norm): m1 = matrix.transpose(dim, 0).contiguous() # collapse non-dim dimensions. m2 = m1.clone().resize_(m1.size(0), int(math.floor(m1.nelement() / m1.size(0)))) norms = m2.norm(value, 1) # clip new_norms = norms.clone() new_norms[torch.gt(norms, max_norm)] = max_norm new_norms.div_(norms.add_(1e-7)) # renormalize m1.mul_(new_norms.expand_as(m1)) return m1.transpose(dim, 0) # note that the axis fed to torch.renorm is different (2~=1) maxnorm = m1.norm(2, 1).mean() m2 = renorm(m1, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) self.assertEqual(m1, m2, 1e-5) self.assertEqual(m1.norm(2, 0), m2.norm(2, 0), 1e-5) m1 = torch.randn(3, 4, 5) m2 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) maxnorm = m2.norm(2, 0).mean() m2 = renorm(m2, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) m3 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) self.assertEqual(m3, m2) self.assertEqual(m3.norm(2, 0), m2.norm(2, 0))
def test_median(self): for size in (155, 156): x = torch.rand(size, size) x0 = x.clone() res1val, res1ind = torch.median(x) res2val, res2ind = torch.sort(x) ind = int(math.floor((size+1)/2) - 1) self.assertEqual(res2val.select(1, ind), res1val.select(1, 0), 0) self.assertEqual(res2val.select(1, ind), res1val.select(1, 0), 0) # Test use of result tensor res2val = torch.Tensor() res2ind = torch.LongTensor() torch.median(res2val, res2ind, x) self.assertEqual(res2val, res1val, 0) self.assertEqual(res2ind, res1ind, 0) # Test non-default dim res1val, res1ind = torch.median(x, 0) res2val, res2ind = torch.sort(x, 0) self.assertEqual(res1val[0], res2val[ind], 0) self.assertEqual(res1ind[0], res2ind[ind], 0) # input unchanged self.assertEqual(x, x0, 0)
def test_conv2(self): x = torch.rand(math.floor(torch.uniform(50, 100)), math.floor(torch.uniform(50, 100))) k = torch.rand(math.floor(torch.uniform(10, 20)), math.floor(torch.uniform(10, 20))) imvc = torch.conv2(x, k) imvc2 = torch.conv2(x, k, 'V') imfc = torch.conv2(x, k, 'F') ki = k.clone() ks = k.storage() kis = ki.storage() for i in range(ks.size()-1, 0, -1): kis[ks.size()-i+1] = ks[i] #for i=ks.size(), 1, -1 do kis[ks.size()-i+1]=ks[i] end imvx = torch.xcorr2(x, ki) imvx2 = torch.xcorr2(x, ki, 'V') imfx = torch.xcorr2(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv2') self.assertEqual(imvc, imvx, 0, 'torch.conv2') self.assertEqual(imvc, imvx2, 0, 'torch.conv2') self.assertEqual(imfc, imfx, 0, 'torch.conv2') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr2(x, x)[0][0]), 1e-10, 'torch.conv2') xx = torch.Tensor(2, x.size(1), x.size(2)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv2(xx, kk) immvc2 = torch.conv2(xx, kk, 'V') immfc = torch.conv2(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv2') self.assertEqual(immvc[0], imvc, 0, 'torch.conv2') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv2') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv2') self.assertEqual(immfc[0], imfc, 0, 'torch.conv2')
def _test_conv_corr_eq(self, fn, fn_2_to_3): ix = math.floor(random.randint(20, 40)) iy = math.floor(random.randint(20, 40)) iz = math.floor(random.randint(20, 40)) kx = math.floor(random.randint(5, 10)) ky = math.floor(random.randint(5, 10)) kz = math.floor(random.randint(5, 10)) x = torch.rand(ix, iy, iz) k = torch.rand(kx, ky, kz) o3 = fn(x, k) o32 = torch.zeros(o3.size()) fn_2_to_3(x, k, o3, o32) self.assertEqual(o3, o32)
def test_masked_select(self): num_src = 10 src = torch.randn(num_src) mask = torch.rand(num_src).clamp(0, 1).mul(2).floor().byte() dst = src.masked_select(mask) dst2 = [] for i in range(num_src): if mask[i]: dst2 += [src[i]] self.assertEqual(dst, torch.Tensor(dst2), 0)
def test_masked_fill(self): num_dest = 10 dst = torch.randn(num_dest) mask = torch.rand(num_dest).mul(2).floor().byte() val = random.random() dst2 = dst.clone() dst.masked_fill_(mask, val) for i in range(num_dest): if mask[i]: dst2[i] = val self.assertEqual(dst, dst2, 0)
def psi(x, linearized=False): """ linearized=False -> psi from paper linearized=True -> piecewise linear psi (which makes more sense to me) """ if not linearized: ks = torch.floor(x / np.pi) return (1 - 2 * (ks % 2)) * x.cos() - (2 * ks) else: return torch.minimum(np.pi / 2 - x, x.cos())
def test_renorm(self): m1 = torch.randn(10, 5) res1 = torch.Tensor() def renorm(matrix, value, dim, max_norm): m1 = matrix.transpose(dim, 0).contiguous() # collapse non-dim dimensions. m2 = m1.clone().resize_(m1.size(0), int(math.floor(m1.nelement() / m1.size(0)))) norms = m2.norm(value, 1) # clip new_norms = norms.clone() new_norms[torch.gt(norms, max_norm)] = max_norm new_norms.div_(norms.add_(1e-7)) # renormalize m1.mul_(new_norms.expand_as(m1)) return m1.transpose(dim, 0) # note that the axis fed to torch.renorm is different (2~=1) maxnorm = m1.norm(2, 1).mean() m2 = renorm(m1, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) self.assertEqual(m1, m2, 1e-5) self.assertEqual(m1.norm(2, 0), m2.norm(2, 0), 1e-5) m1 = torch.randn(3, 4, 5) m2 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) maxnorm = m2.norm(2, 0).mean() m2 = renorm(m2, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) m3 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) self.assertEqual(m3, m2) self.assertEqual(m3.norm(2, 0), m2.norm(2, 0))
def test_median(self): for size in (155, 156): x = torch.rand(size, size) x0 = x.clone() res1val, res1ind = torch.median(x) res2val, res2ind = torch.sort(x) ind = int(math.floor((size + 1) / 2) - 1) self.assertEqual(res2val.select(1, ind), res1val.select(1, 0), 0) self.assertEqual(res2val.select(1, ind), res1val.select(1, 0), 0) # Test use of result tensor res2val = torch.Tensor() res2ind = torch.LongTensor() torch.median(x, out=(res2val, res2ind)) self.assertEqual(res2val, res1val, 0) self.assertEqual(res2ind, res1ind, 0) # Test non-default dim res1val, res1ind = torch.median(x, 0) res2val, res2ind = torch.sort(x, 0) self.assertEqual(res1val[0], res2val[ind], 0) self.assertEqual(res1ind[0], res2ind[ind], 0) # input unchanged self.assertEqual(x, x0, 0)
def test_conv2(self): x = torch.rand(math.floor(torch.uniform(50, 100)), math.floor(torch.uniform(50, 100))) k = torch.rand(math.floor(torch.uniform(10, 20)), math.floor(torch.uniform(10, 20))) imvc = torch.conv2(x, k) imvc2 = torch.conv2(x, k, 'V') imfc = torch.conv2(x, k, 'F') ki = k.clone() ks = k.storage() kis = ki.storage() for i in range(ks.size() - 1, 0, -1): kis[ks.size() - i + 1] = ks[i] # for i=ks.size(), 1, -1 do kis[ks.size()-i+1]=ks[i] end imvx = torch.xcorr2(x, ki) imvx2 = torch.xcorr2(x, ki, 'V') imfx = torch.xcorr2(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv2') self.assertEqual(imvc, imvx, 0, 'torch.conv2') self.assertEqual(imvc, imvx2, 0, 'torch.conv2') self.assertEqual(imfc, imfx, 0, 'torch.conv2') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr2(x, x)[0][0]), 1e-10, 'torch.conv2') xx = torch.Tensor(2, x.size(1), x.size(2)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv2(xx, kk) immvc2 = torch.conv2(xx, kk, 'V') immfc = torch.conv2(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv2') self.assertEqual(immvc[0], imvc, 0, 'torch.conv2') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv2') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv2') self.assertEqual(immfc[0], imfc, 0, 'torch.conv2')
def test_renorm(self): m1 = torch.randn(10, 5) res1 = torch.Tensor() def renorm(matrix, value, dim, max_norm): m1 = matrix.transpose(dim, 0).contiguous() # collapse non-dim dimensions. m2 = m1.clone().resize_(m1.size(0), int(math.floor(m1.nelement() / m1.size(0)))) norms = m2.norm(value, 1, True) # clip new_norms = norms.clone() new_norms[torch.gt(norms, max_norm)] = max_norm new_norms.div_(norms.add_(1e-7)) # renormalize m1.mul_(new_norms.expand_as(m1)) return m1.transpose(dim, 0) # note that the axis fed to torch.renorm is different (2~=1) maxnorm = m1.norm(2, 1).mean() m2 = renorm(m1, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) self.assertEqual(m1, m2, 1e-5) self.assertEqual(m1.norm(2, 0), m2.norm(2, 0), 1e-5) m1 = torch.randn(3, 4, 5) m2 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) maxnorm = m2.norm(2, 0).mean() m2 = renorm(m2, 2, 1, maxnorm) m1.renorm_(2, 1, maxnorm) m3 = m1.transpose(1, 2).contiguous().clone().resize_(15, 4) self.assertEqual(m3, m2) self.assertEqual(m3.norm(2, 0), m2.norm(2, 0))
def test_median(self): for size in (155, 156): x = torch.rand(size, size) x0 = x.clone() res1val, res1ind = torch.median(x, keepdim=False) res2val, res2ind = torch.sort(x) ind = int(math.floor((size + 1) / 2) - 1) self.assertEqual(res2val.select(1, ind), res1val, 0) self.assertEqual(res2val.select(1, ind), res1val, 0) # Test use of result tensor res2val = torch.Tensor() res2ind = torch.LongTensor() torch.median(x, keepdim=False, out=(res2val, res2ind)) self.assertEqual(res2val, res1val, 0) self.assertEqual(res2ind, res1ind, 0) # Test non-default dim res1val, res1ind = torch.median(x, 0, keepdim=False) res2val, res2ind = torch.sort(x, 0) self.assertEqual(res1val, res2val[ind], 0) self.assertEqual(res1ind, res2ind[ind], 0) # input unchanged self.assertEqual(x, x0, 0)
def test_median(self): for size in (155, 156): x = torch.rand(size, size) x0 = x.clone() nelem = x.nelement() res1val = torch.median(x) res2val, _ = torch.sort(x.view(nelem)) ind = int(math.floor((nelem + 1) / 2) - 1) self.assertEqual(res2val[ind], res1val, 0) res1val, res1ind = torch.median(x, dim=1, keepdim=False) res2val, res2ind = torch.sort(x) ind = int(math.floor((size + 1) / 2) - 1) self.assertEqual(res2val.select(1, ind), res1val, 0) self.assertEqual(res2val.select(1, ind), res1val, 0) # Test use of result tensor res2val = torch.Tensor() res2ind = torch.LongTensor() torch.median(x, keepdim=False, out=(res2val, res2ind)) self.assertEqual(res2val, res1val, 0) self.assertEqual(res2ind, res1ind, 0) # Test non-default dim res1val, res1ind = torch.median(x, 0, keepdim=False) res2val, res2ind = torch.sort(x, 0) self.assertEqual(res1val, res2val[ind], 0) self.assertEqual(res1ind, res2ind[ind], 0) # input unchanged self.assertEqual(x, x0, 0)
def test_nonzero(self): num_src = 12 types = [ 'torch.ByteTensor', 'torch.CharTensor', 'torch.ShortTensor', 'torch.IntTensor', 'torch.FloatTensor', 'torch.DoubleTensor', 'torch.LongTensor', ] shapes = [ torch.Size((12,)), torch.Size((12, 1)), torch.Size((1, 12)), torch.Size((6, 2)), torch.Size((3, 2, 2)), ] for t in types: while True: tensor = torch.rand(num_src).mul(2).floor().type(t) if tensor.sum() > 0: break for shape in shapes: tensor = tensor.clone().resize_(shape) dst1 = torch.nonzero(tensor) dst2 = tensor.nonzero() dst3 = torch.LongTensor() torch.nonzero(tensor, out=dst3) if len(shape) == 1: dst = [] for i in range(num_src): if tensor[i] != 0: dst += [i] self.assertEqual(dst1.select(1, 0), torch.LongTensor(dst), 0) self.assertEqual(dst2.select(1, 0), torch.LongTensor(dst), 0) self.assertEqual(dst3.select(1, 0), torch.LongTensor(dst), 0) elif len(shape) == 2: # This test will allow through some False positives. It only checks # that the elements flagged positive are indeed non-zero. for i in range(dst1.size(0)): self.assertNotEqual(tensor[dst1[i, 0], dst1[i, 1]], 0) elif len(shape) == 3: # This test will allow through some False positives. It only checks # that the elements flagged positive are indeed non-zero. for i in range(dst1.size(0)): self.assertNotEqual(tensor[dst1[i, 0], dst1[i, 1], dst1[i, 2]], 0)
def th_bilinear_interp2d(input, coords): """ bilinear interpolation in 2d """ x = th.clamp(coords[:,:,0], 0, input.size(1)-2) x0 = x.floor() x1 = x0 + 1 y = th.clamp(coords[:,:,1], 0, input.size(2)-2) y0 = y.floor() y1 = y0 + 1 stride = th.LongTensor(input.stride()) x0_ix = x0.mul(stride[1]).long() x1_ix = x1.mul(stride[1]).long() y0_ix = y0.mul(stride[2]).long() y1_ix = y1.mul(stride[2]).long() input_flat = input.view(input.size(0),-1) vals_00 = input_flat.gather(1, x0_ix.add(y0_ix)) vals_10 = input_flat.gather(1, x1_ix.add(y0_ix)) vals_01 = input_flat.gather(1, x0_ix.add(y1_ix)) vals_11 = input_flat.gather(1, x1_ix.add(y1_ix)) xd = x - x0 yd = y - y0 xm = 1 - xd ym = 1 - yd x_mapped = (vals_00.mul(xm).mul(ym) + vals_10.mul(xd).mul(ym) + vals_01.mul(xm).mul(yd) + vals_11.mul(xd).mul(yd)) return x_mapped.view_as(input)
def linear_quantize(input, sf, bits): assert bits >= 1, bits if bits == 1: return torch.sign(input) - 1 delta = math.pow(2.0, -sf) bound = math.pow(2.0, bits-1) min_val = - bound max_val = bound - 1 rounded = torch.floor(input / delta + 0.5) clipped_value = torch.clamp(rounded, min_val, max_val) * delta return clipped_value
def tanh_quantize(input, bits): assert bits >= 1, bits if bits == 1: return torch.sign(input) input = torch.tanh(input) # [-1, 1] input_rescale = (input + 1.0) / 2 #[0, 1] n = math.pow(2.0, bits) - 1 v = torch.floor(input_rescale * n + 0.5) / n v = 2 * v - 1 # [-1, 1] v = 0.5 * torch.log((1 + v) / (1 - v)) # arctanh return v
def __get_batch(self, inputs, targets): data_size = targets.size(0) inds = torch.floor(torch.rand(self.batch_size) * data_size).long().cuda() # bug: floor(rand()) sometimes gives 1 inds[inds >= data_size] = data_size - 1 if type(inputs) == tuple: inp = tuple([Variable( i.index_select(0, inds).cuda() ) for i in inputs]) else: inp = Variable( inputs.index_select(0, inds).cuda() ) targ = Variable( targets.index_select(0, inds).cuda() ) return inp, targ