def test_sqrt_out_of_place(self): elements = 30 output_shape = (3, elements) number_range = ht.arange(elements, dtype=ht.float32) output_buffer = ht.zeros(output_shape, dtype=ht.float32) # square roots float32_sqrt = ht.sqrt(number_range, out=output_buffer) comparison = torch.arange(elements, dtype=torch.float32).sqrt() # check whether the input range remain unchanged self.assertIsInstance(number_range, ht.tensor) self.assertEqual(number_range.sum(axis=0), 190) # gaussian sum self.assertEqual(number_range.gshape, (elements,)) # check whether the output buffer still has the correct shape self.assertIsInstance(float32_sqrt, ht.tensor) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertEqual(float32_sqrt._tensor__array.shape, output_shape) for row in range(output_shape[0]): self.assertTrue((float32_sqrt._tensor__array[row] == comparison).all()) # exception with self.assertRaises(TypeError): ht.sqrt(number_range, 'hello world')
def _normalized_symmetric_L(self, A): degree = ht.sum(A, axis=1) degree.resplit_(axis=None) # Find stand-alone vertices with no connections temp = torch.ones(degree.shape, dtype=degree.larray.dtype, device=degree.device.torch_device) degree.larray = torch.where(degree.larray == 0, temp, degree.larray) L = A / ht.sqrt(ht.expand_dims(degree, axis=1)) L = L / ht.sqrt(ht.expand_dims(degree, axis=0)) L = L * (-1.0) L.fill_diagonal(1.0) return L
def cg(A, b, x0, out=None): """ Conjugate gradients method for solving a system of linear equations Ax = b Parameters ---------- A : ht.DNDarray 2D symmetric, positive definite Matrix b : ht.DNDarray 1D vector x0 : ht.DNDarray Arbitrary 1D starting vector out : ht.DNDarray, optional Output Vector Returns ------- ht.DNDarray Returns the solution x of the system of linear equations. If out is given, it is returned """ if (not isinstance(A, ht.DNDarray) or not isinstance(b, ht.DNDarray) or not isinstance(x0, ht.DNDarray)): raise TypeError( "A, b and x0 need to be of type ht.dndarra, but were {}, {}, {}". format(type(A), type(b), type(x0))) if not A.numdims == 2: raise RuntimeError("A needs to be a 2D matrix") if not b.numdims == 1: raise RuntimeError("b needs to be a 1D vector") if not x0.numdims == 1: raise RuntimeError("c needs to be a 1D vector") r = b - ht.matmul(A, x0) p = r rsold = ht.matmul(r, r) x = x0 for i in range(len(b)): Ap = ht.matmul(A, p) alpha = rsold / ht.matmul(p, Ap) x = x + alpha * p r = r - alpha * Ap rsnew = ht.matmul(r, r) if ht.sqrt(rsnew).item() < 1e-10: print("Residual reaches tolerance in it = {}".format(i)) if out is not None: out = x return out return x p = r + ((rsnew / rsold) * p) rsold = rsnew if out is not None: out = x return out return x
def test_sqrt_method(self): elements = 25 tmp = torch.arange(elements, dtype=torch.float64, device=self.device.torch_device).sqrt() comparison = ht.array(tmp) # square roots of float32 float32_sqrt = ht.arange(elements, dtype=ht.float32).sqrt() self.assertIsInstance(float32_sqrt, ht.DNDarray) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertTrue( ht.allclose(float32_sqrt, comparison.astype(ht.float32), 1e-05)) # square roots of float64 float64_sqrt = ht.arange(elements, dtype=ht.float64).sqrt() self.assertIsInstance(float64_sqrt, ht.DNDarray) self.assertEqual(float64_sqrt.dtype, ht.float64) self.assertEqual(float64_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(float64_sqrt, comparison, 1e-05)) # square roots of ints, automatic conversion to intermediate floats int32_sqrt = ht.arange(elements, dtype=ht.int32).sqrt() self.assertIsInstance(int32_sqrt, ht.DNDarray) self.assertEqual(int32_sqrt.dtype, ht.float64) self.assertEqual(int32_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(int32_sqrt, comparison, 1e-05)) # square roots of longs, automatic conversion to intermediate floats int64_sqrt = ht.arange(elements, dtype=ht.int64).sqrt() self.assertIsInstance(int64_sqrt, ht.DNDarray) self.assertEqual(int64_sqrt.dtype, ht.float64) self.assertEqual(int64_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(int64_sqrt, comparison, 1e-05)) # check exceptions with self.assertRaises(TypeError): ht.sqrt([1, 2, 3]) with self.assertRaises(TypeError): ht.sqrt("hello world")
def rmse(self, gt: DNDarray, yest: DNDarray) -> DNDarray: """ Root mean square error (RMSE) Parameters ---------- gt : DNDarray Input model data, Shape = (1,) yest : DNDarray Thresholded model data, Shape = (1,) """ return ht.sqrt((ht.mean((gt - yest)**2))).larray.item()
def rmse(self, gt, yest): """ Root mean square error (RMSE) Parameters ---------- gt : HeAT tensor, shape (1,) Input model data yest : HeAT tensor, shape (1,) Thresholded model data """ return ht.sqrt((ht.mean((gt - yest)**2))).larray.item()
def test_lasso(self): # ToDo: add additional tests # get some test data X = ht.load_hdf5( os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="x", device=ht_device, split=0, ) y = ht.load_hdf5( os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="y", device=ht_device, split=0, ) # normalize dataset X = X / ht.sqrt((ht.mean(X ** 2, axis=0))) m, n = X.shape # HeAT lasso instance estimator = ht.regression.lasso.Lasso(max_iter=100, tol=None) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertTrue(estimator.theta is None) self.assertTrue(estimator.n_iter is None) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_, None) self.assertEqual(estimator.intercept_, None) estimator.fit(X, y) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertIsInstance(estimator.theta, ht.DNDarray) self.assertEqual(estimator.n_iter, 100) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_.shape, (n - 1, 1)) self.assertEqual(estimator.intercept_.shape, (1,)) yest = estimator.predict(X) # check whether the results are correct self.assertIsInstance(yest, ht.DNDarray) self.assertEqual(yest.shape, (m, 1)) with self.assertRaises(ValueError): estimator.fit(X, ht.zeros((3, 3, 3))) with self.assertRaises(ValueError): estimator.fit(ht.zeros((3, 3, 3)), ht.zeros((3, 3)))
def test_sqrt(self): elements = 25 comparison = ht.arange(elements, dtype=ht.float64).sqrt() # square roots of float32 float32_tensor = ht.arange(elements, dtype=ht.float32) float32_sqrt = ht.sqrt(float32_tensor) self.assertIsInstance(float32_sqrt, ht.tensor) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertEqual(float32_sqrt.dtype, ht.float32) self.assertTrue(ht.allclose(float32_sqrt, comparison.astype(ht.float32), 1e-06)) # square roots of float64 float64_tensor = ht.arange(elements, dtype=ht.float64) float64_sqrt = ht.sqrt(float64_tensor) self.assertIsInstance(float64_sqrt, ht.tensor) self.assertEqual(float64_sqrt.dtype, ht.float64) self.assertEqual(float64_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(float64_sqrt, comparison, 1e-06)) # square roots of ints, automatic conversion to intermediate floats int32_tensor = ht.arange(elements, dtype=ht.int32) int32_sqrt = ht.sqrt(int32_tensor) self.assertIsInstance(int32_sqrt, ht.tensor) self.assertEqual(int32_sqrt.dtype, ht.float64) self.assertEqual(int32_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(int32_sqrt, comparison, 1e-06)) # square roots of longs, automatic conversion to intermediate floats int64_tensor = ht.arange(elements, dtype=ht.int64) int64_sqrt = ht.sqrt(int64_tensor) self.assertIsInstance(int64_sqrt, ht.tensor) self.assertEqual(int64_sqrt.dtype, ht.float64) self.assertEqual(int64_sqrt.dtype, ht.float64) self.assertTrue(ht.allclose(int64_sqrt, comparison, 1e-06)) # check exceptions with self.assertRaises(TypeError): ht.sqrt([1, 2, 3]) with self.assertRaises(TypeError): ht.sqrt('hello world')
def test_lasso(self): # ToDo: add additional tests # get some test data X = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="x") y = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="y") # normalize dataset X = X / ht.sqrt((ht.mean(X**2, axis=0))) m, n = X.shape # HeAT lasso instance estimator = ht.core.regression.lasso.HeatLasso(max_iter=100, tol=None) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertTrue(estimator.theta is None) self.assertTrue(estimator.n_iter is None) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_, None) self.assertEqual(estimator.intercept_, None) estimator.fit(X, y) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertIsInstance(estimator.theta, ht.DNDarray) self.assertEqual(estimator.n_iter, 100) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_.shape, (n - 1, 1)) self.assertEqual(estimator.intercept_.shape, (1, )) yest = estimator.predict(X) # check whether the results are correct self.assertIsInstance(yest, ht.DNDarray) self.assertEqual(yest.shape, (m, )) X = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="x") y = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="y") # Now the same stuff again in PyTorch X = torch.tensor(X._DNDarray__array) y = torch.tensor(y._DNDarray__array) # normalize dataset X = X / torch.sqrt((torch.mean(X**2, 0))) m, n = X.shape estimator = ht.core.regression.lasso.PytorchLasso(max_iter=100, tol=None) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertTrue(estimator.theta is None) self.assertTrue(estimator.n_iter is None) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_, None) self.assertEqual(estimator.intercept_, None) estimator.fit(X, y) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertIsInstance(estimator.theta, torch.Tensor) self.assertEqual(estimator.n_iter, 100) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_.shape, (n - 1, 1)) self.assertEqual(estimator.intercept_.shape, (1, )) yest = estimator.predict(X) # check whether the results are correct self.assertIsInstance(yest, torch.Tensor) self.assertEqual(yest.shape, (m, )) X = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="x") y = ht.load_hdf5(os.path.join(os.getcwd(), "heat/datasets/data/diabetes.h5"), dataset="y") # Now the same stuff again in PyTorch X = X._DNDarray__array.numpy() y = y._DNDarray__array.numpy() # normalize dataset X = X / np.sqrt((np.mean(X**2, axis=0, keepdims=True))) m, n = X.shape estimator = ht.core.regression.lasso.NumpyLasso(max_iter=100, tol=None) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertTrue(estimator.theta is None) self.assertTrue(estimator.n_iter is None) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_, None) self.assertEqual(estimator.intercept_, None) estimator.fit(X, y) # check whether the results are correct self.assertEqual(estimator.lam, 0.1) self.assertIsInstance(estimator.theta, np.ndarray) self.assertEqual(estimator.n_iter, 100) self.assertEqual(estimator.max_iter, 100) self.assertEqual(estimator.coef_.shape, (n - 1, 1)) self.assertEqual(estimator.intercept_.shape, (1, )) yest = estimator.predict(X) # check whether the results are correct self.assertIsInstance(yest, np.ndarray) self.assertEqual(yest.shape, (m, ))
import heat as ht from matplotlib import pyplot as plt from sklearn import datasets import heat.ml.regression.lasso as lasso import plotfkt # read scikit diabetes data set diabetes = datasets.load_diabetes() # load diabetes dataset from hdf5 file X = ht.load_hdf5("../../heat/datasets/data/diabetes.h5", dataset="x", split=0) y = ht.load_hdf5("../../heat/datasets/data/diabetes.h5", dataset="y", split=0) # normalize dataset #DoTO this goes into the lasso fit routine soon as issue #106 is solved X = X / ht.sqrt((ht.mean(X**2, axis=0))) # HeAT lasso instance estimator = lasso.HeatLasso(max_iter=100) # List lasso model parameters theta_list = list() # Range of lambda values lamda = np.logspace(0, 4, 10) / 10 # compute the lasso path for l in lamda: estimator.lam = l estimator.fit(X, y) theta_list.append(estimator.theta.numpy().flatten())