def test(self): from topologylayer.nn import LevelSetLayer2D # sublevel set for alg in ['hom', 'hom2', 'cohom']: layer = LevelSetLayer2D(size=(3, 3), maxdim=1, sublevel=False, alg=alg) x = torch.tensor([[2, 1, 1], [1, 0.5, 1], [1, 1, 1]], dtype=torch.float).requires_grad_(True) dgms, issub = layer(x) self.assertEqual(issub, False, "Expected superlevel set layer. alg=" + alg) self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[0]), torch.tensor([[2., -np.inf]]))), True, "unexpected 0-dim barcode. alg=" + alg) self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[1]), torch.tensor([[1., 0.5]]))), True, "unexpected 1-dim barcode. alg=" + alg) d1 = remove_zero_bars(dgms[1]) p = d1[0, 0] - d1[0, 1] p.backward() self.assertEqual(x.grad[1, 1].item(), -1.0, "unexpected gradient. alg=" + alg)
def test(self): from topologylayer.nn import RipsLayer for alg in ['hom', 'hom2', 'cohom']: # superlevel set layer = RipsLayer(4, maxdim=1, alg=alg) x = torch.tensor([[1, 1], [1, -1], [-1, -1], [-1, 1]], dtype=torch.float).requires_grad_(True) dgms, issub = layer(x) self.assertEqual(issub, True, "Expected sublevel set layer. alg=" + alg) self.assertEqual( torch.all( torch.eq( remove_infinite_bars(remove_zero_bars(dgms[0]), issub), torch.tensor([[0., 2.], [0., 2.], [0., 2.]]))), True, "unexpected 0-dim barcode. alg=" + alg) self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[1]), torch.tensor([[2., 2.8284270763397217]]))), True, "unexpected 1-dim barcode. alg=" + alg) d0 = remove_infinite_bars(remove_zero_bars(dgms[0]), issub) p = torch.sum(d0[:, 1] - d0[:, 0]) p.backward() self.assertEqual( torch.all( torch.eq( x.grad, torch.tensor([[1, 1], [1, -1], [-1, 0], [-1, 0]], dtype=torch.float))), True, "unexpected gradient. alg=" + alg)
def test(self): from topologylayer.nn import AlphaLayer # superlevel set layer = AlphaLayer(maxdim=1) x = torch.tensor([[1, 1], [1, -1], [-1, -1], [-1, 1]], dtype=torch.float).requires_grad_(True) dgms, issub = layer(x) self.assertEqual(issub, True, "Expected sublevel set layer") self.assertEqual( torch.all( torch.eq( remove_zero_bars(dgms[0]), torch.tensor([[0., 2.], [0., 2.], [0., 2.], [0., np.inf]]))), True, "unexpected 0-dim barcode") self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[1]), torch.tensor([[2., 2.8284270763397217]]))), True, "unexpected 1-dim barcode") d0 = remove_zero_bars(dgms[0]) p = d0[0, 1] - d0[0, 0] p.backward() self.assertEqual( torch.all( torch.eq( x.grad, torch.tensor([[0, 1], [0, -1], [0, 0], [0, 0]], dtype=torch.float))), True, "unexpected gradient")
def test(self): # first, we build our complex s = SimplicialComplex() # a cycle graph on vertices 1,2,3,4 # cone with vertex 0 s.append([0]) s.append([1]) s.append([2]) s.append([3]) s.append([4]) s.append([0, 1]) s.append([0, 2]) s.append([0, 3]) s.append([0, 4]) s.append([1, 2]) s.append([1, 3]) s.append([4, 2]) s.append([4, 3]) s.append([0, 1, 2]) s.append([0, 1, 3]) s.append([0, 2, 4]) s.append([0, 3, 4]) # initialize internal data structures s.initialize() # function on vertices # we are doing sub-level set persistence # expect single H0 [0,inf] # expect single H1 [0,2] f = torch.Tensor([2., 0., 0., 0., 0.]) # extend filtration to simplical complex s.extendFloat(f) # compute persistence with MAXDIM=1 ret = persistenceForwardCohom(s, 1) self.assertEqual( torch.all( torch.eq(remove_zero_bars(ret[0]), torch.tensor([[0., np.inf]]))), True, "Unexpected 0-Dim persistence") self.assertEqual( torch.all( torch.eq(remove_zero_bars(ret[1]), torch.tensor([[0., 2.]]))), True, "Unexpected 1-Dim persistence")
def forward(self, dgminfo): dgms, issublevel = dgminfo dgm = dgms[self.dim] if self.remove_zero: dgm = remove_zero_bars(dgm) lengths, means = get_barcode_lengths_means(dgm, issublevel) return torch.sum( torch.mul(torch.pow(lengths, self.p), torch.pow(means, self.q)))
def test(self): from topologylayer.nn import LevelSetLayer1D # superlevel set for alg in ['hom', 'hom2', 'cohom']: layer = LevelSetLayer1D(size=3, sublevel=False, alg=alg) y = torch.tensor([1, 0, 1], dtype=torch.float).requires_grad_(True) dgms, issub = layer(y) self.assertEqual(issub, False, "Expected superlevel set layer. alg=" + alg) self.assertEqual( torch.all( torch.eq( remove_infinite_bars(remove_zero_bars(dgms[0]), issub), torch.tensor([[1., 0.]]))), True, "unexpected barcode. alg=" + alg) p = torch.sum( remove_infinite_bars(remove_zero_bars(dgms[0]), issub)[0]) p.backward() self.assertEqual(y.grad[1].item(), 1.0, "unexpected gradient. alg=" + alg)
def test(self): from topologylayer.nn import LevelSetLayer1D # sublevel set for alg in ['hom', 'hom2', 'cohom']: layer = LevelSetLayer1D(size=3, sublevel=True, alg=alg) y = torch.tensor([1, 0, 1], dtype=torch.float).requires_grad_(True) dgms, issub = layer(y) self.assertEqual(issub, True, "Expected sublevel set layer. alg=" + alg) self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[0]), torch.tensor([[0., np.inf]]))), True, "unexpected barcode. alg=" + alg)
def test(self): from topologylayer.nn import LevelSetLayer1D # superlevel set layer = LevelSetLayer1D(size=3, sublevel=False) y = torch.tensor([1, 0, 1], dtype=torch.float).requires_grad_(True) dgms, issub = layer(y) self.assertEqual(issub, False, "Expected superlevel set layer") self.assertEqual( torch.all( torch.eq(remove_zero_bars(dgms[0]), torch.tensor([[1., 0.], [1., -np.inf]]))), True, "unexpected barcode") p = torch.sum(dgms[0][1]) p.backward() self.assertEqual(y.grad[1].item(), 1.0, "unexpected gradient")
s.append([0,4]) s.append([1,2]) s.append([1,3]) s.append([4,2]) s.append([4,3]) s.append([0,1,2]) s.append([0,1,3]) s.append([0,2,4]) s.append([0,3,4]) # initialize internal data structures s.initialize() # function on vertices # we are doing sub-level set persistence # expect single H0 [0,inf] # expect single H1 [0,2] f = torch.Tensor([2., 0., 0., 0., 0.]) # extend filtration to simplical complex s.extendFloat(f) # compute persistence with MAXDIM=1 ret = persistenceForward(s, 1) for k in range(2): print("dimension %d bars" % k) print(remove_zero_bars(ret[k]))