from icosagon.data import Data, \ _equal from icosagon.model import Model from icosagon.trainprep import PreparedData, \ PreparedRelationFamily, \ PreparedRelationType, \ TrainValTest, \ norm_adj_mat_one_node_type import torch from icosagon.input import OneHotInputLayer from icosagon.convlayer import DecagonLayer from icosagon.declayer import DecodeLayer import pytest def _is_identity_function(f): for x in range(-100, 101): if f(x) != x: return False return True def test_model_01(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) fam.add_relation_type('Dummy Rel', torch.rand(10, 10).round()) m = Model(d) assert m.data == d assert m.layer_dimensions == [32, 64] assert (m.ratios.train, m.ratios.val, m.ratios.test) == (.8, .1, .1) assert m.keep_prob == 1. assert _is_identity_function(m.rel_activation) assert m.layer_activation == torch.nn.functional.relu assert _is_identity_function(m.dec_activation) assert m.lr == 0.001 assert m.loss == torch.nn.functional.binary_cross_entropy_with_logits assert m.batch_size == 100 assert isinstance(m.prep_d, PreparedData) assert isinstance(m.seq, torch.nn.Sequential) assert isinstance(m.opt, torch.optim.Optimizer) def test_model_02(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel', mat) m = Model(d, ratios=TrainValTest(1., 0., 0.)) assert isinstance(m.prep_d, PreparedData) assert isinstance(m.prep_d.relation_families, list) assert len(m.prep_d.relation_families) == 1 assert isinstance(m.prep_d.relation_families[0], PreparedRelationFamily) assert len(m.prep_d.relation_families[0].relation_types) == 1 assert isinstance(m.prep_d.relation_families[0].relation_types[0], PreparedRelationType) assert m.prep_d.relation_families[0].relation_types[0].adjacency_matrix_backward is None assert torch.all(_equal(m.prep_d.relation_families[0].relation_types[0].adjacency_matrix, norm_adj_mat_one_node_type(mat))) assert isinstance(m.seq[0], OneHotInputLayer) assert isinstance(m.seq[1], DecagonLayer) assert isinstance(m.seq[2], DecagonLayer) assert isinstance(m.seq[3], DecodeLayer) assert len(m.seq) == 4 def test_model_03(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel', mat) m = Model(d, ratios=TrainValTest(1., 0., 0.)) state_dict = m.opt.state_dict() assert isinstance(state_dict, dict) # print(state_dict['param_groups']) # print(list(m.seq.parameters())) assert len(list(m.seq[0].parameters())) == 1 assert len(list(m.seq[1].parameters())) == 1 assert len(list(m.seq[2].parameters())) == 1 assert len(list(m.seq[3].parameters())) == 2 # print(list(m.seq[1].parameters())) def test_model_04(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel 1', mat) fam.add_relation_type('Dummy Rel 2', mat.clone()) m = Model(d, ratios=TrainValTest(1., 0., 0.)) assert len(list(m.seq[0].parameters())) == 1 assert len(list(m.seq[1].parameters())) == 2 assert len(list(m.seq[2].parameters())) == 2 assert len(list(m.seq[3].parameters())) == 3 def test_model_05(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel 1', mat) fam.add_relation_type('Dummy Rel 2', mat.clone()) fam = d.add_relation_family('Dummy-Dummy 2', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel 2-1', mat) fam.add_relation_type('Dummy Rel 2-2', mat.clone()) m = Model(d, ratios=TrainValTest(1., 0., 0.)) assert len(list(m.seq[0].parameters())) == 1 assert len(list(m.seq[1].parameters())) == 4 assert len(list(m.seq[2].parameters())) == 4 assert len(list(m.seq[3].parameters())) == 6 def test_model_05(): d = Data() d.add_node_type('Dummy', 10) d.add_node_type('Foobar', 20) fam = d.add_relation_family('Dummy-Foobar', 0, 1, True) mat = torch.rand(10, 20).round().to_sparse() fam.add_relation_type('Dummy Rel 1', mat) fam.add_relation_type('Dummy Rel 2', mat.clone()) fam = d.add_relation_family('Dummy-Dummy 2', 0, 0, False) mat = torch.rand(10, 10).round().to_sparse() fam.add_relation_type('Dummy Rel 2-1', mat) fam.add_relation_type('Dummy Rel 2-2', mat.clone()) m = Model(d, ratios=TrainValTest(1., 0., 0.)) assert len(list(m.seq[0].parameters())) == 2 assert len(list(m.seq[1].parameters())) == 6 assert len(list(m.seq[2].parameters())) == 6 assert len(list(m.seq[3].parameters())) == 6 def test_model_06(): d = Data() d.add_node_type('Dummy', 10) fam = d.add_relation_family('Dummy-Dummy', 0, 0, False) fam.add_relation_type('Dummy Rel', torch.rand(10, 10).round()) with pytest.raises(TypeError): m = Model(1) with pytest.raises(TypeError): m = Model(d, layer_dimensions=1) with pytest.raises(TypeError): m = Model(d, ratios=1) with pytest.raises(ValueError): m = Model(d, keep_prob='x') with pytest.raises(TypeError): m = Model(d, rel_activation='x') with pytest.raises(TypeError): m = Model(d, layer_activation='x') with pytest.raises(TypeError): m = Model(d, dec_activation='x') with pytest.raises(ValueError): m = Model(d, lr='x') with pytest.raises(TypeError): m = Model(d, loss=1) with pytest.raises(ValueError): m = Model(d, batch_size='x')