Example #1
0
    def testa(self):
        numpy.random.seed(775)
        d = 3
        m = 5
        pathLength = 5
        numberToDo = 2
        paths = numpy.random.uniform(size=(numberToDo,pathLength,d))
        sigs = numpy.vstack([iisignature.sig(i,m) for i in paths])
        scales = numpy.random.uniform(0.5,0.97,size=(numberToDo,d))
        scaledPaths = paths * scales[:,numpy.newaxis,:]
        scaledSigs = numpy.vstack([iisignature.sig(i,m) for i in scaledPaths])
        scaledSigsCalc = iisignature.sigscale(sigs,scales,m)
        self.assertEqual(scaledSigs.shape,scaledSigsCalc.shape)
        self.assertLess(diff(scaledSigs,scaledSigsCalc),0.0000001)

        bumpedScales = 1.001 * scales
        bumpedSigs = 1.001 * sigs
        base = numpy.sum(scaledSigsCalc)
        bump1 = numpy.sum(iisignature.sigscale(bumpedSigs,scales,m))
        bump2 = numpy.sum(iisignature.sigscale(sigs,bumpedScales,m))
        derivsOfSum = numpy.ones_like(scaledSigsCalc)
        calculated = iisignature.sigscalebackprop(derivsOfSum,sigs,scales,m)
        diff1 = (bump1 - base) - numpy.sum(calculated[0] * (bumpedSigs - sigs))
        diff2 = (bump2 - base) - numpy.sum(calculated[1] * (bumpedScales - scales))
        #print(calculated[1].shape,bumpedScales.shape,scales.shape)
        #print(calculated[1][0,0],bump2,base,bumpedScales[0,0],scales[0,0])
        #print (bump1,bump2,base,diff1,diff2)
        self.assertLess(numpy.abs(diff1),0.0000001)
        self.assertLess(numpy.abs(diff2),0.0000001)
Example #2
0
 def backward(ctx, grad_output):
     X, Y = ctx.saved_tensors
     m = ctx.m
     result = iisignature.sigscalebackprop(grad_output.numpy(),
                                           X.detach().numpy(),
                                           Y.detach().numpy(), m)
     return torch.FloatTensor(result[0]), torch.FloatTensor(result[1]), None
Example #3
0
    def testa(self):
        numpy.random.seed(775)
        d = 3
        m = 5
        pathLength = 5
        numberToDo = 2
        paths = numpy.random.uniform(size=(numberToDo,pathLength,d))
        sigs = numpy.vstack([iisignature.sig(i,m) for i in paths])
        scales = numpy.random.uniform(0.5,0.97,size=(numberToDo,d))
        scaledPaths = paths * scales[:,numpy.newaxis,:]
        scaledSigs = numpy.vstack([iisignature.sig(i,m) for i in scaledPaths])
        scaledSigsCalc = iisignature.sigscale(sigs,scales,m)
        self.assertEqual(scaledSigs.shape,scaledSigsCalc.shape)
        self.assertLess(diff(scaledSigs,scaledSigsCalc),0.0000001)

        bumpedScales = 1.001 * scales
        bumpedSigs = 1.001 * sigs
        base = numpy.sum(scaledSigsCalc)
        bump1 = numpy.sum(iisignature.sigscale(bumpedSigs,scales,m))
        bump2 = numpy.sum(iisignature.sigscale(sigs,bumpedScales,m))
        derivsOfSum = numpy.ones_like(scaledSigsCalc)
        calculated = iisignature.sigscalebackprop(derivsOfSum,sigs,scales,m)
        diff1 = (bump1 - base) - numpy.sum(calculated[0] * (bumpedSigs - sigs))
        diff2 = (bump2 - base) - numpy.sum(calculated[1] * (bumpedScales - scales))
        #print(calculated[1].shape,bumpedScales.shape,scales.shape)
        #print(calculated[1][0,0],bump2,base,bumpedScales[0,0],scales[0,0])
        #print (bump1,bump2,base,diff1,diff2)
        self.assertLess(numpy.abs(diff1),0.0000001)
        self.assertLess(numpy.abs(diff2),0.0000001)
Example #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]
     if report_contig:
         contig_check([s,x,y])
     o=iisignature.sigscalebackprop(s,x,y,m)
     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]
 def perform(self,node,inputs_storage,out):
     s=inputs_storage[0]
     x=inputs_storage[1]
     y=inputs_storage[2]
     m=inputs_storage[3]
     if report_contig:
         contig_check([s,x,y])
     o=iisignature.sigscalebackprop(s,x,y,m)
     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]
Example #6
0
    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))
Example #7
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)
 
 def backward(self, grad_output):
     X, Y = self.saved_tensors
     result = iisignature.sigscalebackprop(grad_output.numpy(), X.numpy(),
                                           Y.numpy(), self.m)
     return tuple(torch.FloatTensor(i) for i in result)
Example #9
0
 def backward(self, grad_output):
     X,Y = self.saved_tensors
     result = iisignature.sigscalebackprop(grad_output.numpy(),X.numpy(),Y.numpy(),self.m)
     return tuple(torch.FloatTensor(i) for i in result)
def _sigScaleGradImp(g,x,y,m):
    o= iisignature.sigscalebackprop(g,x,y,m)
    return o[0],o[1],_zero
Example #11
0
def _sigScaleGradImp(g, x, y, m):
    o = iisignature.sigscalebackprop(g, x, y, m)
    return o[0], o[1], _zero
Example #12
0
    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))
Example #13
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)