Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve reproducibility of results in Dfmc and Dfmf #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions skfusion/fusion/decomposition/dfmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def fuse(self, fusion_graph):
if not isinstance(self.random_state, np.random.RandomState):
self.random_state = np.random.RandomState(self.random_state)

object_types = set([ot for ot in self.fusion_graph.object_types])
object_type2rank = {ot: int(ot.rank) for ot in self.fusion_graph.object_types}

R, T, M = {}, {}, {}
Expand Down Expand Up @@ -95,7 +94,7 @@ def fuse(self, fusion_graph):

parallelizer = Parallel(n_jobs=self.n_jobs, max_nbytes=1e3, verbose=self.verbose)
task_iter = (delayed(parallel_dfmc_wrapper)(
R=R, M=M, Theta=T, obj_types=object_types,
R=R, M=M, Theta=T, obj_types=self.fusion_graph.object_types,
Copy link
Author

@mzganec mzganec May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iteration order of obj_types in matrix initialization is not conserved if obj_types is a set. Consequently, matrix initialization is not always reproducible even if the same random_state is used, leading to irreproducibility of results.

Uniqueness of keys in self.fusion_graph.object_types is guaranteed already by its type, so conversion into a set is redundant.

obj_type2rank=object_type2rank, max_iter=self.max_iter, init_type=self.init_type,
stopping=self.stopping, stopping_system=self.stopping_system, verbose=self.verbose,
compute_err=self.compute_err, callback=self.callback, random_state=self.random_state,
Expand Down
7 changes: 3 additions & 4 deletions skfusion/fusion/decomposition/dfmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def fuse(self, fusion_graph):
if not isinstance(self.random_state, np.random.RandomState):
self.random_state = np.random.RandomState(self.random_state)

object_types = set([ot for ot in self.fusion_graph.object_types])
object_type2rank = {ot: int(ot.rank) for ot in self.fusion_graph.object_types}

R, T = {}, {}
Expand All @@ -86,9 +85,9 @@ def fuse(self, fusion_graph):

parallelizer = Parallel(n_jobs=self.n_jobs, max_nbytes=1e3, verbose=self.verbose)
task_iter = (delayed(parallel_dfmf_wrapper)(
R=R, Theta=T, obj_types=object_types, obj_type2rank=object_type2rank,
max_iter=self.max_iter, init_type=self.init_type, stopping=self.stopping,
stopping_system=self.stopping_system, verbose=self.verbose,
R=R, Theta=T, obj_types=self.fusion_graph.object_types,
obj_type2rank=object_type2rank, max_iter=self.max_iter, init_type=self.init_type,
stopping=self.stopping, stopping_system=self.stopping_system, verbose=self.verbose,
compute_err=self.compute_err, callback=self.callback, random_state=self.random_state,
n_jobs=self.n_jobs)
for _ in range(self.n_run))
Expand Down
13 changes: 13 additions & 0 deletions skfusion/tests/test_dfmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ def postprocessor(data):
np.testing.assert_almost_equal(fuser.complete(relation), trnf)
np.testing.assert_equal(fusion_graph.get_relation('R').data, R12)

def test_reproducibility(self):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this fix test_reproducibility occasionally fails depending on the iteration order of obj_types in matrix initialization. With the proposed fix this test always finishes successfully.

rnds = np.random.RandomState(0)
R12 = rnds.rand(5, 3)

t1 = ObjectType('type1', 3)
t2 = ObjectType('type2', 2)
relation = Relation(R12, t1, t2)
fusion_graph = FusionGraph()
fusion_graph.add_relation(relation)

fuser = Dfmc(init_type='random', random_state=rnds).fuse(fusion_graph)
np.testing.assert_almost_equal(fuser.complete(relation).sum(), 9.02321)


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions skfusion/tests/test_dfmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def postprocessor(data):
trnf = relation.data - np.mean(relation.data)
np.testing.assert_almost_equal(fuser.complete(relation), trnf)

def test_reproducibility(self):
rnds = np.random.RandomState(0)
R12 = rnds.rand(5, 3)

t1 = ObjectType('type1', 3)
t2 = ObjectType('type2', 2)
relation = Relation(R12, t1, t2)
fusion_graph = FusionGraph()
fusion_graph.add_relation(relation)

fuser = Dfmf(init_type='random', random_state=rnds).fuse(fusion_graph)
np.testing.assert_almost_equal(fuser.complete(relation).sum(), 9.02321)


if __name__ == "__main__":
unittest.main()