我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用torch.eig()。
def test_eig(self): a = torch.Tensor(((1.96, 0.00, 0.00, 0.00, 0.00), (-6.49, 3.80, 0.00, 0.00, 0.00), (-0.47, -6.39, 4.17, 0.00, 0.00), (-7.20, 1.50, -1.51, 5.70, 0.00), (-0.65, -6.34, 2.67, 1.80, -7.10))).t().contiguous() e = torch.eig(a)[0] ee, vv = torch.eig(a, True) te = torch.Tensor() tv = torch.Tensor() eee, vvv = torch.eig(te, tv, a, True) self.assertEqual(e, ee, 1e-12) self.assertEqual(ee, eee, 1e-12) self.assertEqual(ee, te, 1e-12) self.assertEqual(vv, vvv, 1e-12) self.assertEqual(vv, tv, 1e-12) # test reuse X = torch.randn(4,4) X = torch.mm(X.t(), X) e, v = torch.zeros(4,2), torch.zeros(4,4) torch.eig(e, v, X, True) Xhat = torch.mm(torch.mm(v, torch.diag(e.select(1, 0))), v.t()) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong') self.assertFalse(v.is_contiguous(), 'V is contiguous') torch.eig(e, v, X, True) Xhat = torch.mm(v, torch.mm(e.select(1, 0).diag(), v.t())) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong') self.assertFalse(v.is_contiguous(), 'V is contiguous') # test non-contiguous X = torch.randn(4, 4) X = torch.mm(X.t(), X) e = torch.zeros(4, 2, 2)[:,1] v = torch.zeros(4, 2, 4)[:,1] self.assertFalse(v.is_contiguous(), 'V is contiguous') self.assertFalse(e.is_contiguous(), 'E is contiguous') torch.eig(e, v, X, True) Xhat = torch.mm(torch.mm(v, torch.diag(e.select(1, 0))), v.t()) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong')
def test_eig(self): a = torch.Tensor(((1.96, 0.00, 0.00, 0.00, 0.00), (-6.49, 3.80, 0.00, 0.00, 0.00), (-0.47, -6.39, 4.17, 0.00, 0.00), (-7.20, 1.50, -1.51, 5.70, 0.00), (-0.65, -6.34, 2.67, 1.80, -7.10))).t().contiguous() e = torch.eig(a)[0] ee, vv = torch.eig(a, True) te = torch.Tensor() tv = torch.Tensor() eee, vvv = torch.eig(a, True, out=(te, tv)) self.assertEqual(e, ee, 1e-12) self.assertEqual(ee, eee, 1e-12) self.assertEqual(ee, te, 1e-12) self.assertEqual(vv, vvv, 1e-12) self.assertEqual(vv, tv, 1e-12) # test reuse X = torch.randn(4, 4) X = torch.mm(X.t(), X) e, v = torch.zeros(4, 2), torch.zeros(4, 4) torch.eig(X, True, out=(e, v)) Xhat = torch.mm(torch.mm(v, torch.diag(e.select(1, 0))), v.t()) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong') self.assertFalse(v.is_contiguous(), 'V is contiguous') torch.eig(X, True, out=(e, v)) Xhat = torch.mm(v, torch.mm(e.select(1, 0).diag(), v.t())) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong') self.assertFalse(v.is_contiguous(), 'V is contiguous') # test non-contiguous X = torch.randn(4, 4) X = torch.mm(X.t(), X) e = torch.zeros(4, 2, 2)[:, 1] v = torch.zeros(4, 2, 4)[:, 1] self.assertFalse(v.is_contiguous(), 'V is contiguous') self.assertFalse(e.is_contiguous(), 'E is contiguous') torch.eig(X, True, out=(e, v)) Xhat = torch.mm(torch.mm(v, torch.diag(e.select(1, 0))), v.t()) self.assertEqual(X, Xhat, 1e-8, 'VeV\' wrong')