def test_tensor_product_2(k1, k2): k = TensorProduct(x=k1, y=k2) mirror = eval(repr(k)) # representation meaningness test assert(mirror.theta == k.theta) for i1, j1 in [(0, 0), (0, 1.5), (-1, 1), (-1.0, 0)]: for i2, j2 in [(0, 0), (0, 1.5), (-1, 1), (-1.0, 0)]: ''' default and corner cases ''' assert(k(dict(x=i1, y=i2), dict(x=j1, y=j2)) == k1(i1, j1) * k2(i2, j2)) assert(k(dict(x=i1, y=i2), dict(x=j1, y=j2)) == mirror(dict(x=i1, y=i2), dict(x=j1, y=j2))) for _ in range(10000): i1 = random.paretovariate(0.1) j1 = random.paretovariate(0.1) i2 = random.paretovariate(0.1) j2 = random.paretovariate(0.1) ''' check by definition ''' assert(k(dict(x=i1, y=j1), dict(x=i2, y=j2)) == k1(i1, i2) * k2(j1, j2)) assert(k(dict(x=i1, y=i2), dict(x=j1, y=j2)) == mirror(dict(x=i1, y=i2), dict(x=j1, y=j2))) ''' hyperparameter retrieval ''' assert(k1.theta in k.theta) assert(k2.theta in k.theta) k.theta = k.theta ''' representation generation ''' assert(len(str(k).split('⊗')) == 2) assert(str(k1) in str(k)) assert(str(k2) in str(k)) assert(repr(k1) in repr(k)) assert(repr(k2) in repr(k)) ''' C++ code generation ''' assert(k.dtype.isalignedstruct) assert(isinstance(k.gencode('x', 'y'), str))
def test_tensor_product_3(k1, k2, k3): k = TensorProduct(x=k1, y=k2, z=k3) mirror = eval(repr(k)) # representation meaningness test assert(mirror.theta == k.theta) ''' default and corner cases only ''' for x1, y1, z1 in [(0, 0, 0), (0, 1, -1), (-1, 1, 0.5), (0, -42., 1)]: for x2, y2, z2 in [(0, 0, 0), (0, 1, -1), (-1, 1, 0.5), (0, -42., 1)]: ''' default and corner cases ''' assert(k(dict(x=x1, y=y1, z=z1), dict(x=x2, y=y2, z=z2)) == (k1(x1, x2) * k2(y1, y2) * k3(z1, z2))) assert(k(dict(x=x1, y=y1, z=z1), dict(x=x2, y=y2, z=z2)) == mirror(dict(x=x1, y=y1, z=z1), dict(x=x2, y=y2, z=z2))) ''' hyperparameter retrieval ''' assert(k1.theta in k.theta) assert(k2.theta in k.theta) assert(k3.theta in k.theta) k.theta = k.theta ''' representation generation ''' assert(len(str(k).split('⊗')) == 3) assert(str(k1) in str(k)) assert(str(k2) in str(k)) assert(str(k3) in str(k)) assert(repr(k1) in repr(k)) assert(repr(k2) in repr(k)) assert(repr(k3) in repr(k)) ''' C++ code generation ''' assert(k.dtype.isalignedstruct) assert(isinstance(k.gencode('x', 'y'), str))
def _makekernel(self, **kwargs): self.kernel = MarginalizedGraphKernel( TensorProduct(element=KroneckerDelta(self.element_prior, 1.0)), TensorProduct(length=SquareExponential(self.edge_length_scale)), q=self.stopping_probability, p=self.starting_probability, **kwargs)
# {1.0, 1} --[1.0]-- {2.0, 1} # \ / # [0.5] [2.0] # \ / # {1.0, 2} g3 = nx.Graph() g3.add_node(0, radius=1.0, category=1) g3.add_node(1, radius=2.0, category=1) g3.add_node(2, radius=1.0, category=2) g3.add_edge(0, 1, w=1.0) g3.add_edge(0, 2, w=0.5) g3.add_edge(1, 2, w=2.0) # define node and edge kernelets knode = TensorProduct(radius=SquareExponential(0.5), category=KroneckerDelta(0.5)) kedge = Constant(1.0) # compose the marginalized graph kernel and compute pairwise similarity mlgk = MarginalizedGraphKernel(knode, kedge, q=0.05) R = mlgk([Graph.from_networkx(g, weight='w') for g in [g1, g2, g3]]) # normalize the similarity matrix d = np.diag(R)**-0.5 K = np.diag(d).dot(R).dot(np.diag(d)) print(K)
Graph.from_networkx(unlabeled_graph1), Graph.from_networkx(unlabeled_graph2) ], 'knode': Constant(1.0), 'kedge': Constant(1.0), 'q': [0.01, 0.05, 0.1, 0.5] }, 'labeled': { 'graphs': [ Graph.from_networkx(labeled_graph1), Graph.from_networkx(labeled_graph2) ], 'knode': TensorProduct(hybridization=KroneckerDelta(0.3, 1.0), charge=SquareExponential(1.0)), 'kedge': TensorProduct(order=KroneckerDelta(0.3, 1.0), length=SquareExponential(0.05)), 'q': [0.01, 0.05, 0.1, 0.5] }, 'weighted': { 'graphs': [ Graph.from_networkx(weighted_graph1, weight='w'), Graph.from_networkx(weighted_graph2, weight='w') ], 'knode': TensorProduct(hybridization=KroneckerDelta(0.3, 1.0), charge=SquareExponential(1.0)), 'kedge': TensorProduct(order=KroneckerDelta(0.3, 1.0),
'CC', # ethane 'CCO', # acetic acid 'CCN', # ethylamine 'C=C', # ethene 'CC=C', # propene 'CC=CC', # 2-n-butene ] # convert to molecular graphs # nodes(atoms) has 'aromatic', 'charge', 'element', 'hcount' attributes # edges(bonds) has the 'order' attribute graphs = [Graph.from_smiles(smi) for smi in smiles_list] # define node and edge kernelets knode = TensorProduct(aromatic=KroneckerDelta(0.8, 1.0), charge=SquareExponential(1.0), element=KroneckerDelta(0.5, 1.0), hcount=SquareExponential(1.0)) kedge = TensorProduct(order=KroneckerDelta(0.5, 1.0)) # compose the marginalized graph kernel and compute pairwise similarity kernel = MarginalizedGraphKernel(knode, kedge, q=0.05) R = kernel(graphs) # normalize the similarity matrix and then print d = np.diag(R)**-0.5 K = np.diag(d).dot(R).dot(np.diag(d)) print(pd.DataFrame(K, columns=smiles_list, index=smiles_list))
# \ / # \ / # {2.0}[0.5] 1.0}[2.0] # \ / # \ / # {1.0, 2} g3 = nx.Graph() g3.add_node(0, radius=1.0, category=1) g3.add_node(1, radius=2.0, category=1) g3.add_node(2, radius=1.0, category=2) g3.add_edge(0, 1, w=1.0, length=1.5) g3.add_edge(0, 2, w=0.5, length=2.0) g3.add_edge(1, 2, w=2.0, length=1.0) # define node and edge kernelets knode = TensorProduct(radius=SquareExponential(1.0), category=KroneckerDelta(0.5)) kedge = TensorProduct(length=SquareExponential(1.0)) # compose the marginalized graph kernel and compute pairwise similarity mlgk = MarginalizedGraphKernel(knode, kedge, q=0.05) R = mlgk([Graph.from_networkx(g, weight='w') for g in [g1, g2, g3]]) # normalize the similarity matrix d = np.diag(R)**-0.5 K = np.diag(d).dot(R).dot(np.diag(d)) print(K)