def forward(ctx, X, Y, m, fixed): result = iisignature.sigjoin(X.detach().numpy(), Y.detach().numpy(), m, fixed.detach().numpy()) ctx.save_for_backward(X, Y, fixed) ctx.m = m return torch.FloatTensor(result)
def testjoining(self): numberToDo = 1 dim = 2 level = 2 siglength = iisignature.siglength(dim,level) for fixedPoint, inputDim, fixed in [(float('nan'),dim,False),(0.1,dim - 1,True)]: pathLength = 10 def makePath(): p = numpy.random.uniform(size=(pathLength,dim)) if fixed: p[:,-1] = fixedPoint * numpy.arange(pathLength) return p paths = [makePath() for i in range(numberToDo)] sig = numpy.vstack([iisignature.sig(path,level) for path in paths]) joinee = numpy.zeros((numberToDo,siglength)) for i in range(1,pathLength): displacements = [path[i:(i + 1),:] - path[(i - 1):i,:] for path in paths] displacement = numpy.vstack(displacements) if fixed: displacement = displacement[:,:-1] joinee = iisignature.sigjoin(joinee,displacement,level,fixedPoint) self.assertLess(diff(sig,joinee),0.0001,"fullSig matches sig" + (" with fixed Dim" if fixed else "")) extra = numpy.random.uniform(size=(numberToDo,inputDim)) bumpedExtra = 1.001 * extra bumpedJoinee = 1.001 * joinee base = numpy.sum(iisignature.sigjoin(joinee,extra,level,fixedPoint)) bump1 = numpy.sum(iisignature.sigjoin(bumpedJoinee,extra,level,fixedPoint)) bump2 = numpy.sum(iisignature.sigjoin(joinee,bumpedExtra,level,fixedPoint)) derivsOfSum = numpy.ones((numberToDo,siglength)) calculated = iisignature.sigjoinbackprop(derivsOfSum,joinee,extra, level,fixedPoint) self.assertEqual(len(calculated),3 if fixed else 2) diff1 = (bump1 - base) - numpy.sum(calculated[0] * (bumpedJoinee - joinee)) diff2 = (bump2 - base) - numpy.sum(calculated[1] * (bumpedExtra - extra)) #print ("\n",bump1,bump2,base,diff1,diff2) self.assertLess(numpy.abs(diff1),0.000001,"diff1 as expected " + (" with fixed Dim" if fixed else "")) self.assertLess(numpy.abs(diff2),0.00001,"diff2 as expected " + (" with fixed Dim" if fixed else "")) if fixed: bumpedFixedPoint = fixedPoint * 1.01 bump3 = numpy.sum(iisignature.sigjoin(joinee,extra, level, bumpedFixedPoint)) diff3 = (bump3-base - numpy.sum(calculated[2] * (bumpedFixedPoint-fixedPoint))) #print("\n",bump3,base, fixedPoint, bumpedFixedPoint, calculated[2]) self.assertLess(numpy.abs(diff3),0.00001, "diff3")
def perform(self,node,inputs_storage,outputs_storage): x=inputs_storage[0] y=inputs_storage[1] m=inputs_storage[2] fixed = inputs_storage[3] outputs_storage[0][0]=iisignature.sigjoin(x,y,m,fixed) if(nancheck): if contains_nan(x): raise RuntimeError("nan in x") if contains_nan(y) : raise RuntimeError("nan in y") if contains_nan(outputs_storage[0][0]) : raise RuntimeError("nan in output")
def test_batch(self): numpy.random.seed(734) d=2 m=2 n=15 paths = [numpy.random.uniform(-1,1,size=(6,d)) for i in range(n)] pathArray15=stack(paths) pathArray1315=numpy.reshape(pathArray15,(1,3,1,5,6,d)) sigs = [iisignature.sig(i,m) for i in paths] sigArray=stack(sigs) sigArray15=iisignature.sig(pathArray15,m) sigArray1315=iisignature.sig(pathArray1315,m) siglength=iisignature.siglength(d,m) self.assertEqual(sigArray1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(sigArray1315.reshape(n,siglength),sigs)) self.assertEqual(sigArray15.shape,(15,siglength)) self.assertTrue(numpy.allclose(sigArray15,sigs)) backsigs=[iisignature.sigbackprop(i,j,m) for i,j in zip(sigs,paths)] backsigArray = stack(backsigs) backsigs1315=iisignature.sigbackprop(sigArray1315,pathArray1315,m) self.assertEqual(backsigs1315.shape,(1,3,1,5,6,d)) self.assertTrue(numpy.allclose(backsigs1315.reshape(n,6,2),backsigArray)) data=[numpy.random.uniform(size=(d,)) for i in range(n)] dataArray1315=stack(data).reshape((1,3,1,5,d)) joined=[iisignature.sigjoin(i,j,m) for i,j in zip(sigs,data)] joined1315=iisignature.sigjoin(sigArray1315,dataArray1315,m) self.assertEqual(joined1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(joined1315.reshape(n,-1),stack(joined))) backjoined=[iisignature.sigjoinbackprop(i,j,k,m) for i,j,k in zip(joined,sigs,data)] backjoinedArrays=[stack([i[j] for i in backjoined]) for j in range(2)] backjoined1315=iisignature.sigjoinbackprop(joined1315,sigArray1315,dataArray1315,m) self.assertEqual(backjoined1315[0].shape,sigArray1315.shape) self.assertEqual(backjoined1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0].reshape(n,-1),backjoinedArrays[0])) self.assertTrue(numpy.allclose(backjoined1315[1].reshape(n,-1),backjoinedArrays[1])) dataAsSigs=[iisignature.sig(numpy.row_stack([numpy.zeros((d,)),i]),m) for i in data] dataArray13151=dataArray1315[:,:,:,:,None,:] dataArray13151=numpy.repeat(dataArray13151,2,4)*[[0.0],[1.0]] dataArrayAsSigs1315=iisignature.sig(dataArray13151,m) combined1315=iisignature.sigcombine(sigArray1315,dataArrayAsSigs1315,d,m) self.assertEqual(joined1315.shape,combined1315.shape) self.assertTrue(numpy.allclose(joined1315,combined1315)) backcombined1315=iisignature.sigcombinebackprop(joined1315,sigArray1315,dataArrayAsSigs1315,d,m) backcombined=[iisignature.sigcombinebackprop(i,j,k,d,m) for i,j,k in zip(joined,sigs,dataAsSigs)] backcombinedArrays=[stack([i[j] for i in backcombined]) for j in range(2)] self.assertEqual(backcombined1315[0].shape,sigArray1315.shape) self.assertEqual(backcombined1315[1].shape,sigArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0],backcombined1315[0])) self.assertTrue(numpy.allclose(backcombined1315[0].reshape(n,-1),backcombinedArrays[0])) self.assertTrue(numpy.allclose(backcombined1315[1].reshape(n,-1),backcombinedArrays[1])) scaled=[iisignature.sigscale(i,j,m) for i,j in zip(sigs,data)] scaled1315=iisignature.sigscale(sigArray1315,dataArray1315,m) self.assertEqual(scaled1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(scaled1315.reshape(n,-1),stack(scaled))) backscaled=[iisignature.sigscalebackprop(i,j,k,m) for i,j,k in zip(scaled,sigs,data)] backscaledArrays=[stack([i[j] for i in backscaled]) for j in range(2)] backscaled1315=iisignature.sigscalebackprop(scaled1315,sigArray1315,dataArray1315,m) self.assertEqual(backscaled1315[0].shape,sigArray1315.shape) self.assertEqual(backscaled1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backscaled1315[0].reshape(n,-1),backscaledArrays[0])) self.assertTrue(numpy.allclose(backscaled1315[1].reshape(n,-1),backscaledArrays[1])) s_s=(iisignature.prepare(d,m,"cosax"),iisignature.prepare(d,m,"cosahx")) for type in ("c","o","s","x","a","ch","oh","sh","ah"): s=s_s[1 if "h" in type else 0] logsigs = [iisignature.logsig(i,s,type) for i in paths] logsigArray=stack(logsigs) logsigArray1315=iisignature.logsig(pathArray1315,s,type) self.assertEqual(logsigArray1315.shape,(1,3,1,5,logsigs[0].shape[0]),type) self.assertTrue(numpy.allclose(logsigArray1315.reshape(n,-1),logsigArray),type) if type in ("s","x","sh"): backlogs = stack(iisignature.logsigbackprop(i,j,s,type) for i,j in zip(logsigs,paths)) backlogs1315 = iisignature.logsigbackprop(logsigArray1315,pathArray1315,s,type) self.assertEqual(backlogs1315.shape,backsigs1315.shape) self.assertTrue(numpy.allclose(backlogs1315.reshape(n,6,d),backlogs),type) a=iisignature.rotinv2dprepare(m,"a") rots=stack([iisignature.rotinv2d(i,a) for i in paths]) rots1315=iisignature.rotinv2d(pathArray1315,a) self.assertEqual(rots1315.shape,(1,3,1,5,rots.shape[1])) self.assertTrue(numpy.allclose(rots1315.reshape(n,-1),rots))
length = 20 dim=3 level=2 npaths=3 paths_ = np.random.uniform(size=(npaths,length,dim)) scale_ = np.random.uniform(size=(npaths,dim)) initialsigs_ = np.random.uniform(size=(npaths,iisignature.siglength(dim,level))) p=iisignature.prepare(dim,level,"cosx") while 0: iisignature.sig(paths[0],level) for i in range(10**10): #copy major parts of the input data, in case we are leaking references to it paths=paths_[:] increment=scale=scale_[:] initialsigs=initialsigs_[:] iisignature.sigjoin(initialsigs,scale,level) iisignature.sigscale(initialsigs,scale,level) iisignature.sigjoinbackprop(initialsigs,initialsigs,scale,level) iisignature.sigscalebackprop(initialsigs,initialsigs,scale,level) iisignature.sig(paths[0,:,:],level) iisignature.sigbackprop(initialsigs[0,:],paths[0,:,:],level) #iisignature.sigjacobian(paths[0,:,:],level) #iisignature.prepare(dim,level,"cosx")#much slower than other functions iisignature.logsig(paths[0,:,:],p,"c") iisignature.logsig(paths[0,:,:],p,"o") iisignature.logsig(paths[0,:,:],p,"s") if i%10000==0: print (i)
def forward(self, X, Y, fixed): result = iisignature.sigjoin(X.numpy(), Y.numpy(), self.m, fixed.numpy()) self.save_for_backward(X, Y, fixed) return torch.FloatTensor(result)
def forward(self,X,Y,fixed): result=iisignature.sigjoin(X.numpy(),Y.numpy(), self.m, fixed.numpy()) self.save_for_backward(X,Y, fixed) return torch.FloatTensor(result)
def _sigJoinFixedImp(x,y,m,fixedLast): return iisignature.sigjoin(x,y,m,fixedLast)
def _sigJoinImp(x,y,m): return iisignature.sigjoin(x,y,m)
def _sigJoinFixedImp(x, y, m, fixedLast): return iisignature.sigjoin(x, y, m, fixedLast)
def _sigJoinImp(x, y, m): return iisignature.sigjoin(x, y, m)
def test_batch(self): numpy.random.seed(734) d=2 m=2 n=15 paths = [numpy.random.uniform(-1,1,size=(6,d)) for i in range(n)] pathArray15=stack(paths) pathArray1315=numpy.reshape(pathArray15,(1,3,1,5,6,d)) sigs = [iisignature.sig(i,m) for i in paths] sigArray=stack(sigs) sigArray15=iisignature.sig(pathArray15,m) sigArray1315=iisignature.sig(pathArray1315,m) siglength=iisignature.siglength(d,m) self.assertEqual(sigArray1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(sigArray1315.reshape(n,siglength),sigs)) self.assertEqual(sigArray15.shape,(15,siglength)) self.assertTrue(numpy.allclose(sigArray15,sigs)) backsigs=[iisignature.sigbackprop(i,j,m) for i,j in zip(sigs,paths)] backsigArray = stack(backsigs) backsigs1315=iisignature.sigbackprop(sigArray1315,pathArray1315,m) self.assertEqual(backsigs1315.shape,(1,3,1,5,6,d)) self.assertTrue(numpy.allclose(backsigs1315.reshape(n,6,2),backsigArray)) data=[numpy.random.uniform(size=(d,)) for i in range(n)] dataArray1315=stack(data).reshape((1,3,1,5,d)) joined=[iisignature.sigjoin(i,j,m) for i,j in zip(sigs,data)] joined1315=iisignature.sigjoin(sigArray1315,dataArray1315,m) self.assertEqual(joined1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(joined1315.reshape(n,-1),stack(joined))) backjoined=[iisignature.sigjoinbackprop(i,j,k,m) for i,j,k in zip(joined,sigs,data)] backjoinedArrays=[stack([i[j] for i in backjoined]) for j in range(2)] backjoined1315=iisignature.sigjoinbackprop(joined1315,sigArray1315,dataArray1315,m) self.assertEqual(backjoined1315[0].shape,sigArray1315.shape) self.assertEqual(backjoined1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backjoined1315[0].reshape(n,-1),backjoinedArrays[0])) self.assertTrue(numpy.allclose(backjoined1315[1].reshape(n,-1),backjoinedArrays[1])) scaled=[iisignature.sigscale(i,j,m) for i,j in zip(sigs,data)] scaled1315=iisignature.sigscale(sigArray1315,dataArray1315,m) self.assertEqual(scaled1315.shape,(1,3,1,5,siglength)) self.assertTrue(numpy.allclose(scaled1315.reshape(n,-1),stack(scaled))) backscaled=[iisignature.sigscalebackprop(i,j,k,m) for i,j,k in zip(scaled,sigs,data)] backscaledArrays=[stack([i[j] for i in backscaled]) for j in range(2)] backscaled1315=iisignature.sigscalebackprop(scaled1315,sigArray1315,dataArray1315,m) self.assertEqual(backscaled1315[0].shape,sigArray1315.shape) self.assertEqual(backscaled1315[1].shape,dataArray1315.shape) self.assertTrue(numpy.allclose(backscaled1315[0].reshape(n,-1),backscaledArrays[0])) self.assertTrue(numpy.allclose(backscaled1315[1].reshape(n,-1),backscaledArrays[1])) s_s=(iisignature.prepare(d,m,"cosax"),iisignature.prepare(d,m,"cosahx")) for type in ("c","o","s","x","a","ch","oh","sh","ah"): s=s_s[1 if "h" in type else 0] logsigs = [iisignature.logsig(i,s,type) for i in paths] logsigArray=stack(logsigs) logsigArray1315=iisignature.logsig(pathArray1315,s,type) self.assertEqual(logsigArray1315.shape,(1,3,1,5,logsigs[0].shape[0]),type) self.assertTrue(numpy.allclose(logsigArray1315.reshape(n,-1),logsigArray),type) if type in ("s","x","sh"): backlogs = stack(iisignature.logsigbackprop(i,j,s,type) for i,j in zip(logsigs,paths)) backlogs1315 = iisignature.logsigbackprop(logsigArray1315,pathArray1315,s,type) self.assertEqual(backlogs1315.shape,backsigs1315.shape) self.assertTrue(numpy.allclose(backlogs1315.reshape(n,6,d),backlogs),type) a=iisignature.rotinv2dprepare(m,"a") rots=stack([iisignature.rotinv2d(i,a) for i in paths]) rots1315=iisignature.rotinv2d(pathArray1315,a) self.assertEqual(rots1315.shape,(1,3,1,5,rots.shape[1])) self.assertTrue(numpy.allclose(rots1315.reshape(n,-1),rots))
length = 20 dim = 3 level = 2 npaths = 3 paths_ = np.random.uniform(size=(npaths, length, dim)) scale_ = np.random.uniform(size=(npaths, dim)) initialsigs_ = np.random.uniform(size=(npaths, iisignature.siglength(dim, level))) p = iisignature.prepare(dim, level, "cosx") while 0: iisignature.sig(paths[0], level) for i in range(10**10): #copy major parts of the input data, in case we are leaking references to it paths = paths_[:] increment = scale = scale_[:] initialsigs = initialsigs_[:] iisignature.sigjoin(initialsigs, scale, level) iisignature.sigscale(initialsigs, scale, level) iisignature.sigjoinbackprop(initialsigs, initialsigs, scale, level) iisignature.sigscalebackprop(initialsigs, initialsigs, scale, level) iisignature.sig(paths[0, :, :], level) iisignature.sigbackprop(initialsigs[0, :], paths[0, :, :], level) #iisignature.sigjacobian(paths[0,:,:],level) #iisignature.prepare(dim,level,"cosx")#much slower than other functions iisignature.logsig(paths[0, :, :], p, "c") iisignature.logsig(paths[0, :, :], p, "o") iisignature.logsig(paths[0, :, :], p, "s") if i % 10000 == 0: print(i)