コード例 #1
0
 def backward(ctx, grad_output):
     X, Y = ctx.saved_tensors
     m = ctx.m
     result = iisignature.sigjoinbackprop(grad_output.numpy(),
                                          X.detach().numpy(),
                                          Y.detach().numpy(), m)
     return torch.FloatTensor(result[0]), torch.FloatTensor(result[1]), None
コード例 #2
0
ファイル: test_sig.py プロジェクト: Y8N-Haotian/iisignature
    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")
コード例 #3
0
ファイル: test_sig.py プロジェクト: bottler/iisignature
    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")
コード例 #4
0
 def perform(self,node,inputs_storage,out):
     s=inputs_storage[0]
     x=inputs_storage[1]
     y=inputs_storage[2]
     m=inputs_storage[3]
     fixed=inputs_storage[4]
     if report_contig:
         contig_check([s,x,y])
     o=iisignature.sigjoinbackprop(s,x,y,m,fixed)
     if(nancheck):
         if contains_nan(s):
             raise RuntimeError("nan in s")
         if contains_nan(x):
             raise RuntimeError("nan in x")
         if contains_nan(y):
             raise RuntimeError("nan in y")
         if contains_nan(o[0]) or contains_nan(o[1]):
             raise RuntimeError("nan in output")
     out[0][0]=o[0]
     out[1][0]=o[1]
コード例 #5
0
 def perform(self,node,inputs_storage,out):
     s=inputs_storage[0]
     x=inputs_storage[1]
     y=inputs_storage[2]
     m=inputs_storage[3]
     fixed=inputs_storage[4]
     if report_contig:
         contig_check([s,x,y])
     o=iisignature.sigjoinbackprop(s,x,y,m,fixed)
     if(nancheck):
         if contains_nan(s):
             raise RuntimeError("nan in s")
         if contains_nan(x):
             raise RuntimeError("nan in x")
         if contains_nan(y):
             raise RuntimeError("nan in y")
         if contains_nan(o[0]) or contains_nan(o[1]):
             raise RuntimeError("nan in output")
     out[0][0]=o[0]
     out[1][0]=o[1]
     out[2][0]=np.array(0.0 if np.isnan(fixed) else o[2], dtype="float64")
コード例 #6
0
ファイル: test_sig.py プロジェクト: Y8N-Haotian/iisignature
    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))
コード例 #7
0
ファイル: leak_check.py プロジェクト: bottler/iisignature
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)
 
コード例 #8
0
 def backward(self, grad_output):
     X, Y, fixed = self.saved_tensors
     result = iisignature.sigjoinbackprop(grad_output.numpy(), X.numpy(),
                                          Y.numpy(), self.m, fixed.numpy())
     return torch.FloatTensor(result[0]), torch.FloatTensor(
         result[1]), torch.FloatTensor([result[2]])
コード例 #9
0
 def backward(self, grad_output):
     X,Y,fixed = self.saved_tensors
     result = iisignature.sigjoinbackprop(grad_output.numpy(),X.numpy(),Y.numpy(),self.m, fixed.numpy())
     return torch.FloatTensor(result[0]),torch.FloatTensor(result[1]),torch.FloatTensor([result[2]])
コード例 #10
0
def _sigJoinGradFixedImp(g,x,y,m,fixedlast):
    o= iisignature.sigjoinbackprop(g,x,y,m,fixedlast)
    return o[0],o[1],_zero,np.array(o[2],dtype="float32")
コード例 #11
0
def _sigJoinGradImp(g,x,y,m):
    o= iisignature.sigjoinbackprop(g,x,y,m)
    return o[0],o[1],_zero
コード例 #12
0
def _sigJoinGradFixedImp(g, x, y, m, fixedlast):
    o = iisignature.sigjoinbackprop(g, x, y, m, fixedlast)
    return o[0], o[1], _zero, np.array(o[2], dtype="float32")
コード例 #13
0
def _sigJoinGradImp(g, x, y, m):
    o = iisignature.sigjoinbackprop(g, x, y, m)
    return o[0], o[1], _zero
コード例 #14
0
ファイル: test_sig.py プロジェクト: bottler/iisignature
    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))
コード例 #15
0
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)