def assertTensorEqual(actual, expected, abs=False): '''Takes an n-dimensional array 'expected' and runs several tests on the tensor 'actual'. For the test description, see assertTensorEqualCompleteDict. ''' expected_dict = nested_list_to_dict(expected) assertTensorEqualCompleteDict(actual, expected_dict, abs)
def setUp(self): self.tensor = DictTensor(2) # Note: this command actually puts 20 values in tensor! self.tensor.update(nested_list_to_dict(svd_2d_test_matrix)) self.svd = self.tensor.svd(k=3) self.incremental = self.tensor.incremental_svd(k=3, niter=200) self.u, self.svals, self.v = self.svd.u, self.svd.svals, self.svd.v
def setUp(self): self.tensor = DictTensor(2) self.tensor.update(nested_list_to_dict( numpy.random.random_sample((10, 12)))) self.normalized_tensor = self.tensor.normalized() self.svd = self.normalized_tensor.svd(k=3) self.u, self.svals, self.v = self.svd.u, self.svd.svals, self.svd.v
def setUp(self): self.tensor = DictTensor(2) self.tensor.update( nested_list_to_dict(numpy.random.random_sample((10, 12)))) self.normalized_tensor = self.tensor.normalized() self.svd = self.normalized_tensor.svd(k=3) self.u, self.svals, self.v = self.svd.u, self.svd.svals, self.svd.v
def testNorm(self): norm_test = [[0,0,0], [0,1,0], [0,5.0,0]] self.tensor.update(nested_list_to_dict(norm_test)) self.assertEqual(self.tensor.norm(), sqrt(26.0)) self.assertEqual(self.tensor.magnitude(), sqrt(26.0))
def test_delete(self): self.tensor.update( nones_removed(nested_list_to_dict(self.slice_testcase))) assertTensorEqual(self.tensor, self.slice_testcase) del self.tensor[0, 0] assertTensorEqual(self.tensor, [[None, None, None], [None, 2, 3], [4, None, None], [None, 5, None]])
def setUp(self): self.tensor = DictTensor(2) # Note: this command actually puts 20 values in tensor! self.tensor.update(nested_list_to_dict(svd_2d_test_matrix)) self.svd = self.tensor.svd(k=3, offset_for_row=offset_for_row, offset_for_col=offset_for_col) self.u, self.svals, self.v = self.svd.u, self.svd.svals, self.svd.v
def setUp(self): self.tensor = DenseTensor(zeros((3, 4))) self.tensor.update(nested_list_to_dict(self.data)) self.normalized = NormalizedView(self.tensor, mode=0) self.randomtensor = DenseTensor(numpy.random.normal(size=(5, 8))) self.randomnormal_0 = NormalizedView(self.randomtensor, mode=0) self.randomnormal_1 = NormalizedView(self.randomtensor, mode=1)
def test_delete(self): self.tensor.update(nones_removed(nested_list_to_dict(self.slice_testcase))) assertTensorEqual(self.tensor, self.slice_testcase) del self.tensor[0,0] assertTensorEqual(self.tensor, [[None, None, None], [None, 2, 3 ], [4, None, None], [None, 5, None]])
def test_slice(self): self.tensor.update( nones_removed(nested_list_to_dict(self.slice_testcase))) # Test end conditions: start index # is included in slice, end index is not slice = self.tensor[1:3, 0:2] assertTensorEqual(slice, [[None, 2], [4, None]]) # Test that slicing on some dims correctly # reduces the dimensionality of the tensor slice = self.tensor[3, :] assertTensorEqual(slice, [None, 5, None]) # Test the step parameter slice = self.tensor[1:4:2, :] assertTensorEqual(slice, [[None, 2, 3], [None, 5, None]])
def test_slice(self): self.tensor.update(nones_removed(nested_list_to_dict(self.slice_testcase))) # Test end conditions: start index # is included in slice, end index is not slice = self.tensor[1:3, 0:2] assertTensorEqual(slice, [[None, 2], [4, None]]) # Test that slicing on some dims correctly # reduces the dimensionality of the tensor slice = self.tensor[3, :] assertTensorEqual(slice, [None, 5, None]) # Test the step parameter slice = self.tensor[1:4:2, :] assertTensorEqual(slice, [[None, 2, 3], [None, 5, None]])
def tensor_from_nested_list(data, tensor_cls=DictTensor): t = tensor_cls(2) t.update(nested_list_to_dict(data)) return t
import numpy as np import unittest from nose.tools import eq_, raises from math import sqrt from csc.divisi.tensor import DictTensor from csc.divisi.util import nested_list_to_dict from tensor_util import assertTensorEqual, zeros_removed data = np.array([[1, 2, 3, 4], [-1, 2, 3, 4], [0, 1, -1, 0]]) tensor = DictTensor(2) tensor.update(zeros_removed(nested_list_to_dict(data))) eq_(len(tensor), 10) # For NumPy, "along an axis" means something different. ms_data = data - data.mean(1)[:, np.newaxis] ms_tensor = DictTensor(2) ms_tensor.update(nested_list_to_dict(ms_data)) def test_means(): means = tensor.means() eq_(len(means), 2) assert np.allclose(means[0], [(1 + 2 + 3 + 4) / 4., (-1 + 2 + 3 + 4) / 4., (0 + 1 + -1 + 0) / 4.]) assert np.allclose( means[1], [0, (2 + 2 + 1) / 3., (3 + 3 - 1) / 3., (4 + 4 + 0) / 3.]) def test_mean_subtracted(): mean_subtracted = tensor.mean_subtracted()
import numpy as np import unittest from nose.tools import eq_, raises from math import sqrt from csc.divisi.tensor import DictTensor from csc.divisi.util import nested_list_to_dict from tensor_util import assertTensorEqual, zeros_removed data = np.array([[1, 2, 3, 4], [-1,2, 3, 4], [0, 1, -1,0]]) tensor = DictTensor(2) tensor.update(zeros_removed(nested_list_to_dict(data))) eq_(len(tensor), 10) # For NumPy, "along an axis" means something different. ms_data = data - data.mean(1)[:,np.newaxis] ms_tensor = DictTensor(2) ms_tensor.update(nested_list_to_dict(ms_data)) def test_means(): means = tensor.means() eq_(len(means), 2) assert np.allclose(means[0], [(1+2+3+4)/4., (-1+2+3+4)/4., (0+1+-1+0)/4.]) assert np.allclose(means[1], [0, (2+2+1)/3., (3+3-1)/3., (4+4+0)/3.]) def test_mean_subtracted(): mean_subtracted = tensor.mean_subtracted() m = np.zeros(data.shape) for (r, c), v in mean_subtracted.iteritems():
def testNorm(self): norm_test = [[0, 0, 0], [0, 1, 0], [0, 5.0, 0]] self.tensor.update(nested_list_to_dict(norm_test)) self.assertEqual(self.tensor.norm(), sqrt(26.0)) self.assertEqual(self.tensor.magnitude(), sqrt(26.0))