我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用math.abs()。
def _test_btrisolve(self, cast): a = torch.FloatTensor((((1.3722, -0.9020), (1.8849, 1.9169)), ((0.7187, -1.1695), (-0.0139, 1.3572)), ((-1.6181, 0.7148), (1.3728, 0.1319)))) b = torch.FloatTensor(((4.02, 6.19), (-1.56, 4.00), (9.81, -4.09))) a, b = cast(a), cast(b) info = cast(torch.IntTensor()) LU_data, pivots = a.btrifact(info=info) self.assertEqual(info.abs().sum(), 0) x = torch.btrisolve(b, LU_data, pivots) b_ = torch.bmm(a, x.unsqueeze(2)).squeeze() self.assertEqual(b_, b)
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 test_view(self): tensor = torch.rand(15) template = torch.rand(3, 5) empty = torch.Tensor() target = template.size() self.assertEqual(tensor.view_as(template).size(), target) self.assertEqual(tensor.view(3, 5).size(), target) self.assertEqual(tensor.view(torch.Size([3, 5])).size(), target) self.assertEqual(tensor.view(-1, 5).size(), target) self.assertEqual(tensor.view(3, -1).size(), target) tensor_view = tensor.view(5, 3) tensor_view.fill_(random.uniform(0, 1)) self.assertEqual((tensor_view - tensor).abs().max(), 0) self.assertEqual(empty.view_as(empty), empty) self.assertEqual(empty.view(0), empty) self.assertRaises(RuntimeError, lambda: tensor.view(15, 0)) self.assertRaises(RuntimeError, lambda: tensor.view(7, -1)) self.assertRaises(RuntimeError, lambda: tensor.view(15, -1, -1))
def test_view(self): tensor = torch.rand(15) template = torch.rand(3, 5) empty = torch.Tensor() target = template.size() self.assertEqual(tensor.view_as(template).size(), target) self.assertEqual(tensor.view(3, 5).size(), target) self.assertEqual(tensor.view(torch.Size([3, 5])).size(), target) self.assertEqual(tensor.view(-1, 5).size(), target) self.assertEqual(tensor.view(3, -1).size(), target) tensor_view = tensor.view(5, 3) tensor_view.fill_(random.uniform(0, 1)) # suppress broadcastable warning with warnings.catch_warnings(record=True): self.assertEqual((tensor_view - tensor).abs().max(), 0) self.assertEqual(empty.view_as(empty), empty) self.assertEqual(empty.view(0), empty) self.assertRaises(RuntimeError, lambda: tensor.view(15, 0)) self.assertRaises(RuntimeError, lambda: tensor.view(7, -1)) self.assertRaises(RuntimeError, lambda: tensor.view(15, -1, -1))
def test_asin(self): self._testMath(torch.asin, lambda x: math.asin(x) if abs(x) <= 1 else float('nan'))
def test_acos(self): self._testMath(torch.acos, lambda x: math.acos(x) if abs(x) <= 1 else float('nan'))
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_view(self): tensor = torch.rand(15) template = torch.rand(3, 5) empty = torch.Tensor() target = template.size() self.assertEqual(tensor.view_as(template).size(), target) self.assertEqual(tensor.view(3, 5).size(), target) self.assertEqual(tensor.view(torch.Size([3, 5])).size(), target) self.assertEqual(tensor.view(-1, 5).size(), target) self.assertEqual(tensor.view(3, -1).size(), target) tensor_view = tensor.view(5, 3) tensor_view.fill_(random.uniform(0, 1)) self.assertEqual((tensor_view-tensor).abs().max(), 0) self.assertEqual(empty.view_as(empty), empty) self.assertEqual(empty.view(0), empty)
def test_repeat(self): result = torch.Tensor() tensor = torch.rand(8, 4) size = (3, 1, 1) torchSize = torch.Size(size) target = [3, 8, 4] self.assertEqual(tensor.repeat(*size).size(), target, 'Error in repeat') self.assertEqual(tensor.repeat(torchSize).size(), target, 'Error in repeat using LongStorage') result = tensor.repeat(*size) self.assertEqual(result.size(), target, 'Error in repeat using result') result = tensor.repeat(torchSize) self.assertEqual(result.size(), target, 'Error in repeat using result and LongStorage') self.assertEqual((result.mean(0).view(8, 4)-tensor).abs().max(), 0, 'Error in repeat (not equal)')
def _test_btrifact(self, cast): a = torch.FloatTensor((((1.3722, -0.9020), (1.8849, 1.9169)), ((0.7187, -1.1695), (-0.0139, 1.3572)), ((-1.6181, 0.7148), (1.3728, 0.1319)))) a = cast(a) info = cast(torch.IntTensor()) a_LU = a.btrifact(info=info) self.assertEqual(info.abs().sum(), 0) P, a_L, a_U = torch.btriunpack(*a_LU) a_ = torch.bmm(P, torch.bmm(a_L, a_U)) self.assertEqual(a_, a)
def test_repeat(self): result = torch.Tensor() tensor = torch.rand(8, 4) size = (3, 1, 1) torchSize = torch.Size(size) target = [3, 8, 4] self.assertEqual(tensor.repeat(*size).size(), target, 'Error in repeat') self.assertEqual(tensor.repeat(torchSize).size(), target, 'Error in repeat using LongStorage') result = tensor.repeat(*size) self.assertEqual(result.size(), target, 'Error in repeat using result') result = tensor.repeat(torchSize) self.assertEqual(result.size(), target, 'Error in repeat using result and LongStorage') self.assertEqual((result.mean(0).view(8, 4) - tensor).abs().max(), 0, 'Error in repeat (not equal)')
def Mass_Diffusion_Centrality(G): #??? Mass_Diffusion_Centrality = {} for nid in G.nodes(): Mass_Diffusion_Centrality[nid] = 1.0 #end for #????????? def Iterative_Update_Mass_Diffusion(G, Resource_Centrality): Count = 0 while Count > 0.95 * len(G.nodes()): for nid in G.nodes(): neighbors = G.neighbors(nid) Updated_Resource_Nid = 0.0 for ngb in neighbors: Updated_Resource_Nid = Updated_Resource_Nid + Resource_Centrality[ngb] * (1.0/G.degree(ngb)) #????????????? #end for #Update if math.abs(Resource_Centrality[nid] - Updated_Resource_Nid) < 0.000001: Count = Count + 1 Resource_Centrality[nid] = Updated_Resource_Nid #end for #end while return Resource_Centrality #????Mass Diffusion?? Mass_Diffusion_Centrality = Iterative_Update_Mass_Diffusion(G, Mass_Diffusion_Centrality) #print sorted(Mass_Diffusion_Centrality.iteritems(), key=lambda d:d[1], reverse = True) return Mass_Diffusion_Centrality #**********************************************************************************
def _testSelection(self, torchfn, mathfn): # contiguous m1 = torch.randn(100,100) res1 = torchfn(m1) res2 = m1[0,0] for i, j in iter_indices(m1): res2 = mathfn(res2, m1[i,j]) self.assertEqual(res1, res2) # non-contiguous m1 = torch.randn(10,10,10) m2 = m1[:,4] res1 = torchfn(m2) res2 = m2[0,0] for i, j in iter_indices(m2): res2 = mathfn(res2, m2[i][j]) self.assertEqual(res1, res2) # with indices m1 = torch.randn(100,100) res1val, res1ind = torchfn(m1, 1) res2val = m1[:,0:1].clone() res2ind = res1ind.clone().fill_(0) for i, j in iter_indices(m1): if mathfn(res2val[i,0], m1[i,j]) != res2val[i,0]: res2val[i,0] = m1[i,j] res2ind[i,0] = j maxerr = 0 for i in range(res1val.size(0)): maxerr = max(maxerr, abs(res1val[i][0] - res2val[i][0])) self.assertEqual(res1ind[i][0], res2ind[i][0]) self.assertLessEqual(abs(maxerr), 1e-5) # NaNs for index in (0, 4, 99): m1 = torch.randn(100) m1[index] = float('nan') res1val, res1ind = torch.max(m1, 0) self.assertNotEqual(res1val[0], res1val[0]) self.assertEqual(res1ind[0], index) res1val = torchfn(m1) self.assertNotEqual(res1val, res1val)
def test_conv3(self): x = torch.rand(math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40))) k = torch.rand(math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10))) imvc = torch.conv3(x, k) imvc2 = torch.conv3(x, k, 'V') imfc = torch.conv3(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] imvx = torch.xcorr3(x, ki) imvx2 = torch.xcorr3(x, ki, 'V') imfx = torch.xcorr3(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv3') self.assertEqual(imvc, imvx, 0, 'torch.conv3') self.assertEqual(imvc, imvx2, 0, 'torch.conv3') self.assertEqual(imfc, imfx, 0, 'torch.conv3') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr3(x, x)[0][0][0]), 4e-10, 'torch.conv3') xx = torch.Tensor(2, x.size(1), x.size(2), x.size(3)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2), k.size(3)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv3(xx, kk) immvc2 = torch.conv3(xx, kk, 'V') immfc = torch.conv3(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv3') self.assertEqual(immvc[0], imvc, 0, 'torch.conv3') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv3') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv3') self.assertEqual(immfc[0], imfc, 0, 'torch.conv3')
def _testSelection(self, torchfn, mathfn): # contiguous m1 = torch.randn(100, 100) res1 = torchfn(m1) res2 = m1[0, 0] for i, j in iter_indices(m1): res2 = mathfn(res2, m1[i, j]) self.assertEqual(res1, res2) # non-contiguous m1 = torch.randn(10, 10, 10) m2 = m1[:, 4] res1 = torchfn(m2) res2 = m2[0, 0] for i, j in iter_indices(m2): res2 = mathfn(res2, m2[i][j]) self.assertEqual(res1, res2) # with indices m1 = torch.randn(100, 100) res1val, res1ind = torchfn(m1, 1) res2val = m1[:, 0:1].clone() res2ind = res1ind.clone().fill_(0) for i, j in iter_indices(m1): if mathfn(res2val[i, 0], m1[i, j]) != res2val[i, 0]: res2val[i, 0] = m1[i, j] res2ind[i, 0] = j maxerr = 0 for i in range(res1val.size(0)): maxerr = max(maxerr, abs(res1val[i][0] - res2val[i][0])) self.assertEqual(res1ind[i][0], res2ind[i][0]) self.assertLessEqual(abs(maxerr), 1e-5) # NaNs for index in (0, 4, 99): m1 = torch.randn(100) m1[index] = float('nan') res1val, res1ind = torch.max(m1, 0) self.assertNotEqual(res1val[0], res1val[0]) self.assertEqual(res1ind[0], index) res1val = torchfn(m1) self.assertNotEqual(res1val, res1val)
def test_conv3(self): x = torch.rand(math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40))) k = torch.rand(math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10))) imvc = torch.conv3(x, k) imvc2 = torch.conv3(x, k, 'V') imfc = torch.conv3(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] imvx = torch.xcorr3(x, ki) imvx2 = torch.xcorr3(x, ki, 'V') imfx = torch.xcorr3(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv3') self.assertEqual(imvc, imvx, 0, 'torch.conv3') self.assertEqual(imvc, imvx2, 0, 'torch.conv3') self.assertEqual(imfc, imfx, 0, 'torch.conv3') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr3(x, x)[0][0][0]), 4e-10, 'torch.conv3') xx = torch.Tensor(2, x.size(1), x.size(2), x.size(3)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2), k.size(3)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv3(xx, kk) immvc2 = torch.conv3(xx, kk, 'V') immfc = torch.conv3(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv3') self.assertEqual(immvc[0], imvc, 0, 'torch.conv3') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv3') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv3') self.assertEqual(immfc[0], imfc, 0, 'torch.conv3')
def _testSelection(self, torchfn, mathfn): # contiguous m1 = torch.randn(100, 100) res1 = torchfn(m1) res2 = m1[0, 0] for i, j in iter_indices(m1): res2 = mathfn(res2, m1[i, j]) self.assertEqual(res1, res2) # non-contiguous m1 = torch.randn(10, 10, 10) m2 = m1[:, 4] res1 = torchfn(m2) res2 = m2[0, 0] for i, j in iter_indices(m2): res2 = mathfn(res2, m2[i][j]) self.assertEqual(res1, res2) # with indices m1 = torch.randn(100, 100) res1val, res1ind = torchfn(m1, 1, False) res2val = m1[:, 0:1].clone().squeeze() res2ind = res1ind.clone().fill_(0) for i, j in iter_indices(m1): if mathfn(res2val[i], m1[i, j]) != res2val[i]: res2val[i] = m1[i, j] res2ind[i] = j maxerr = 0 for i in range(res1val.size(0)): maxerr = max(maxerr, abs(res1val[i] - res2val[i])) self.assertEqual(res1ind[i], res2ind[i]) self.assertLessEqual(abs(maxerr), 1e-5) # NaNs for index in (0, 4, 99): m1 = torch.randn(100) m1[index] = float('nan') res1val, res1ind = torch.max(m1, 0) self.assertNotEqual(res1val[0], res1val[0]) self.assertEqual(res1ind[0], index) res1val = torchfn(m1) self.assertNotEqual(res1val, res1val)