@@ -27,12 +27,37 @@ def sample_sptensor():
2727 return data , sptensorInstance
2828
2929
30+ @pytest .fixture ()
31+ def sample_ttensor ():
32+ """Simple TTENSOR to verify by hand"""
33+ core = ttb .tensor (np .ones ((2 , 3 )))
34+ factors = [
35+ np .ones ((5 , 2 )),
36+ np .ones ((2 , 3 )),
37+ ]
38+ ttensorInstance = ttb .ttensor (core , factors )
39+ return ttensorInstance
40+
41+
42+ @pytest .fixture ()
43+ def random_ttensor ():
44+ """Arbitrary TTENSOR to verify consistency between alternative operations"""
45+ core = ttb .tensor (np .random .random ((2 , 3 , 4 )))
46+ factors = [
47+ np .random .random ((5 , 2 )),
48+ np .random .random ((2 , 3 )),
49+ np .random .random ((4 , 4 )),
50+ ]
51+ ttensorInstance = ttb .ttensor (core , factors )
52+ return ttensorInstance
53+
54+
3055@pytest .mark .indevelopment
3156def test_cp_als_tensor_default_init (capsys , sample_tensor ):
3257 (data , T ) = sample_tensor
3358 (M , Minit , output ) = ttb .cp_als (T , 2 )
3459 capsys .readouterr ()
35- assert pytest .approx (output ["fit" ], 1 ) == 0
60+ assert pytest .approx (output ["fit" ]) == 1
3661
3762
3863@pytest .mark .indevelopment
@@ -49,7 +74,7 @@ def test_cp_als_tensor_ktensor_init(capsys, sample_tensor):
4974 KInit = ttb .ktensor .from_function (np .random .random_sample , T .shape , 2 )
5075 (M , Minit , output ) = ttb .cp_als (T , 2 , init = KInit )
5176 capsys .readouterr ()
52- assert pytest .approx (output ["fit" ], 1 ) == 0
77+ assert pytest .approx (output ["fit" ]) == 1
5378
5479
5580@pytest .mark .indevelopment
@@ -76,7 +101,7 @@ def test_cp_als_sptensor_default_init(capsys, sample_sptensor):
76101 (data , T ) = sample_sptensor
77102 (M , Minit , output ) = ttb .cp_als (T , 2 )
78103 capsys .readouterr ()
79- assert pytest .approx (output ["fit" ], 1 ) == 0
104+ assert pytest .approx (output ["fit" ]) == 1
80105
81106
82107@pytest .mark .indevelopment
@@ -93,7 +118,26 @@ def test_cp_als_sptensor_ktensor_init(capsys, sample_sptensor):
93118 KInit = ttb .ktensor .from_function (np .random .random_sample , T .shape , 2 )
94119 (M , Minit , output ) = ttb .cp_als (T , 2 , init = KInit )
95120 capsys .readouterr ()
96- assert pytest .approx (output ["fit" ], 1 ) == 0
121+ assert pytest .approx (output ["fit" ]) == 1
122+
123+
124+ @pytest .mark .indevelopment
125+ def test_cp_als_ttensor_default_init (capsys , sample_ttensor ):
126+ T = sample_ttensor
127+ (M , Minit , output ) = ttb .cp_als (T , 1 )
128+ capsys .readouterr ()
129+ assert pytest .approx (output ["fit" ]) == 1
130+
131+
132+ @pytest .mark .indevelopment
133+ def test_cp_als_ttensor_default_init_consistency (capsys , random_ttensor ):
134+ T = random_ttensor
135+ KInit = ttb .ktensor .from_function (np .random .random_sample , T .shape , 2 )
136+ _ , _ , output = ttb .cp_als (T , 2 , init = KInit )
137+ capsys .readouterr ()
138+ _ , _ , dense_output = ttb .cp_als (T .full (), 2 , init = KInit )
139+ capsys .readouterr ()
140+ assert pytest .approx (output ["fit" ]) == dense_output ["fit" ]
97141
98142
99143@pytest .mark .indevelopment
@@ -102,19 +146,15 @@ def test_cp_als_tensor_dimorder(capsys, sample_tensor):
102146
103147 # default dimorder
104148 dimorder = [i for i in range (T .ndims )]
105- print (dimorder )
106- print (dimorder .__class__ )
107149 (M , Minit , output ) = ttb .cp_als (T , 2 , dimorder = dimorder )
108150 capsys .readouterr ()
109- assert pytest .approx (output ["fit" ], 1 ) == 0
151+ assert pytest .approx (output ["fit" ]) == 1
110152
111153 # reverse should work
112154 dimorder = [T .ndims - i - 1 for i in range (T .ndims )]
113- print (dimorder )
114- print (dimorder .__class__ )
115155 (M , Minit , output ) = ttb .cp_als (T , 2 , dimorder = dimorder )
116156 capsys .readouterr ()
117- assert pytest .approx (output ["fit" ], 1 ) == 0
157+ assert pytest .approx (output ["fit" ]) == 1
118158
119159 # dimorder not a list
120160 with pytest .raises (AssertionError ) as excinfo :
0 commit comments