def testFewPoints(self): # check sanity of paths with less than 3 points path1=[[4.3,0.8]] path2=numpy.array([[1,2],[2,4]]) m=4 d=2 s=iisignature.prepare(d,m,"cosx") s_a=iisignature.prepare(d,2,"cosx") length=iisignature.siglength(d,m) loglength=iisignature.logsiglength(d,m) loglength_a=iisignature.logsiglength(d,2) blankLogSig=numpy.zeros(loglength) blankLogSig_a=numpy.zeros(loglength_a) blankSig=numpy.zeros(length) self.assertLess(diff(iisignature.sig(path1,m),blankSig),0.000000001) self.assertTrue(numpy.array_equal(iisignature.sig(path1,m,2),numpy.zeros([0,length]))) self.assertLess(diff(iisignature.logsig(path1,s,"C"),blankLogSig),0.000000001) self.assertLess(diff(iisignature.logsig(path1,s,"O"),blankLogSig),0.000000001) self.assertLess(diff(iisignature.logsig(path1,s,"S"),blankLogSig),0.000000001) self.assertLess(diff(iisignature.logsig(path1,s,"X"),blankSig),0.000000001) self.assertLess(diff(iisignature.logsig(path1,s_a,"A"),blankLogSig_a),0.000000001) blankLogSig[:d]=path2[1]-path2[0] blankLogSig_a[:d]=path2[1]-path2[0] blankSig[:d]=path2[1]-path2[0] self.assertLess(diff(iisignature.logsig(path2,s,"C"),blankLogSig),0.000001) self.assertLess(diff(iisignature.logsig(path2,s,"O"),blankLogSig),0.000001) self.assertLess(diff(iisignature.logsig(path2,s,"S"),blankLogSig),0.000001) self.assertLess(diff(iisignature.logsig(path2,s,"X"),blankSig),0.000001) self.assertLess(diff(iisignature.logsig(path2,s_a,"A"),blankLogSig_a),0.000001)
def doplot(n_samples=100, m=5, d=3, nplot=None): x = vanilla_data(n_samples=n_samples, d=d) # do something to make it interesting: if last coord ever hist 1, then zero out the rest hitting_event = np.maximum.accumulate(np.abs(x[:, -1])) > 3 x[hitting_event, :-1] = 3 # x[:,:-1] = np.where(cumulative_maximum > 1, 0, x[:,:-1]) # x = x - x.mean(axis=0) # x = x / x.std(axis=0) # x = np.exp(x) # x = x / x.sum(axis=0) s = iis.prepare(d, m) if nplot is None: nplot = n_samples ii = n_samples // nplot figure(1) clf() ax = subplot(2, 1, 1) plot(x, alpha=0.5) ylabel('path') t = list() data = list() for i in range(ii, n_samples, ii): t.append(i) temp = iis.logsig(x[:i], s) data.append(temp) data = np.array(data) assert data.shape[1] == iis.logsiglength(d, m), 'not match!' t = np.array(t) ax = subplot(2, 1, 2) plot(t, (data.T / (t**0)).T, alpha=0.5) # plot(t, (data[:,-1].T / (t ** 0)).T, alpha=0.5) ylabel('logsig') xlabel('t') show() return locals()
def CLF_grad_Imp(g, path, deg_of_logsig, number_of_segment): """ The implementation of computing the derivatives of gradient from backpropagation. g: the flown gradient from backpropagation path: dimension (sample_size,n, d) number_of_segment: the number of segments deg_of_logsig: the degree of the log-signature """ nT = int(np.shape(path)[1]) dim_path = int(np.shape(path)[-1]) t_vec = np.linspace(1,nT,number_of_segment+1) t_vec = [int(round(x)) for x in t_vec] s = iisignature.prepare(dim_path, deg_of_logsig) MultiLevelBP = [] for k in range(int(np.shape(path)[0])): tmpMultiLevelBP = np.zeros([1,np.shape(path)[-1]]) for i in range(number_of_segment): temp_path = path[k][t_vec[i]-1:t_vec[i+1], :] tempBP = iisignature.logsigbackprop(g[k][i], temp_path , s, None) tmpMultiLevelBP[-1] += tempBP[0] tempBP = np.delete(tempBP, 0, axis=0) tmpMultiLevelBP = np.concatenate((tmpMultiLevelBP, tempBP), axis=0) MultiLevelBP.append(tmpMultiLevelBP) return np.float32(np.array(MultiLevelBP))
def prepare(self, dimension, depth): if (dimension, depth) in self._log_sig_prepare_cache: return self._log_sig_prepare_cache[(dimension, depth)] s = iisignature.prepare(dimension, depth) self._log_sig_prepare_cache[(dimension, depth)] = s return s
def testLevel1(self): m = 1 d = 2 path = numpy.random.uniform(size=(10, d)) rightSig = path[-1, :] - path[0, :] s = iisignature.prepare(d, m, "cosx2") self.assertLess(diff(iisignature.sig(path, m), rightSig), 0.0000001) for type_ in ("C", "O", "S", "X", "A"): self.assertLess(diff(iisignature.logsig(path, s, type_), rightSig), 0.0000001, type_) self.assertLess(diff(rightSig, iisignature.logsigtosig(rightSig, s)), 0.000001) derivs = numpy.array([2.1, 3.2]) pathderivs = numpy.zeros_like(path) pathderivs[-1] = derivs pathderivs[0] = -derivs self.assertLess( diff(iisignature.logsigbackprop(derivs, path, s), pathderivs), 0.00001) self.assertLess( diff(iisignature.logsigbackprop(derivs, path, s, "X"), pathderivs), 0.00001) self.assertLess( diff(iisignature.sigbackprop(derivs, path, m), pathderivs), 0.00001)
def ComputeLogsigFeatures(path, number_of_segment, deg_of_logsig): """ The implementation of computing the log-signature of segments of path. path: dimension (sample_size,n, d) number_of_segment: the number of segments deg_of_logsig: the degree of the log-signature """ nT = int(np.shape(path)[1]) dim_path = int(np.shape(path)[-1]) t_vec = np.linspace(1, nT, number_of_segment + 1) t_vec = [int(round(x)) for x in t_vec] s = iisignature.prepare(dim_path, deg_of_logsig) MultiLevelLogSig = [] for k in range(int(np.shape(path)[0])): tmpMultiLevelLogSig = np.zeros( (number_of_segment, iisignature.logsiglength(dim_path, deg_of_logsig))) for i in range(number_of_segment): temp_path = path[k][t_vec[i] - 1:t_vec[i + 1], :] temp_start = temp_path[0] tmpMultiLevelLogSig[i, :] = iisignature.logsig(temp_path, s) MultiLevelLogSig.append(tmpMultiLevelLogSig) return np.float32(np.array(MultiLevelLogSig))
def doplot(n_samples=100, m=5, d=3, nplot=None): x = vanilla_data(n_samples=n_samples, d=d) # do something to make it interesting: if last coord ever hist 1, then zero out the rest hitting_event = np.maximum.accumulate(np.abs(x[:,-1])) > 3 x[hitting_event,:-1] = 3 # x[:,:-1] = np.where(cumulative_maximum > 1, 0, x[:,:-1]) # x = x - x.mean(axis=0) # x = x / x.std(axis=0) # x = np.exp(x) # x = x / x.sum(axis=0) s = iis.prepare(d, m) if nplot is None: nplot = n_samples ii = n_samples // nplot figure(1) clf() ax = subplot(2,1,1) plot(x, alpha=0.5) ylabel('path') t = list() data = list() for i in range(ii, n_samples, ii): t.append(i) temp = iis.logsig(x[:i], s) data.append(temp) data = np.array(data) assert data.shape[1] == iis.logsiglength(d, m), 'not match!' t = np.array(t) ax = subplot(2,1,2) plot(t, (data.T / (t ** 0)).T, alpha=0.5) # plot(t, (data[:,-1].T / (t ** 0)).T, alpha=0.5) ylabel('logsig') xlabel('t') show() return locals()
def ComputeMultiLevelLogsig1dBM(BM_paths, number_of_segment, depth_of_tensors, T): """ Compute the log-signature of all samples """ no_of_samples = np.shape(BM_paths)[0] if depth_of_tensors == 1: MultiLevelLogSigs = np.zeros( [no_of_samples, 2 * number_of_segment], dtype=float) else: MultiLevelLogSigs = np.zeros([no_of_samples, iisignature.logsiglength( 2, depth_of_tensors) * number_of_segment], dtype=float) MultiStart = np.zeros([no_of_samples, number_of_segment], dtype=float) s = iisignature.prepare(2, depth_of_tensors) print('start computing the logsigs of degree %d:' % depth_of_tensors) for j in tqdm(range(0, no_of_samples, 1), total=no_of_samples): BM = TimeJointPath(BM_paths[j, :], T) result2 = ComputeMultiLevelSig( BM, number_of_segment, depth_of_tensors, s) #MultiLevelSigs[j] = result2['MultiLevelSig'] # print(result2) MultiLevelLogSigs[j] = result2['MultiLevelLogSig'] MultiStart[j] = result2['MultiStart'] n_input = int(np.shape(MultiLevelLogSigs)[1] / number_of_segment) X_logsig_start = MultiLevelLogSigs.reshape( (np.shape(MultiLevelLogSigs)[0], number_of_segment, n_input)) batch_x0 = MultiStart.reshape( (np.shape(MultiLevelLogSigs)[0], number_of_segment, 1)) X_logsig_start = np.concatenate((X_logsig_start, batch_x0), axis=2) return X_logsig_start
def iisignature_prepare(channels, depth, method='d'): """Like iisignature.prepare, but caches every result.""" try: return _iisignature_prepare_cache[(channels, depth, method)] except KeyError: prepared = iisignature.prepare(channels, depth, method) _iisignature_prepare_cache[(channels, depth, method)] = prepared return prepared
def _getPrep(d, m, lyndon=False): hash2use = _prepLyndon if lyndon else _prep key = (d, m) if key in hash2use: return hash2use[key] ans = iisignature.prepare(d, m, "D" if lyndon else "DH") hash2use[key] = ans return ans
def _getPrep(d,m, lyndon=False): hash2use = _prepLyndon if lyndon else _prep key = (d,m) if key in hash2use: return hash2use[key] ans = iisignature.prepare(d,m, "D" if lyndon else "DH") hash2use[key]=ans return ans
def consistency(self, coropa, dim, level): #numpy.random.seed(21) s = iisignature.prepare(dim,level,"coshx" if coropa else "cosx") myinfo = {"level":level, "dimension":dim, "methods": ("COSAX" if level <= 2 else "COSX"), "basis":("Standard Hall" if coropa else "Lyndon")} self.assertEqual(iisignature.info(s),myinfo) path = numpy.random.uniform(size=(10,dim)) basis = iisignature.basis(s) logsig = iisignature.logsig(path,s) sig = iisignature.sig(path,level) #check lengths self.assertEqual(len(basis),iisignature.logsiglength(dim,level)) self.assertEqual((len(basis),),logsig.shape) self.assertEqual(sig.shape,(iisignature.siglength(dim,level),)) #calculate a signature from logsig expanded_logsig = [numpy.zeros(dim ** m) for m in range(1,level + 1)] for coeff, expression in zip(logsig,basis): values, depth = valueOfBracket(expression,dim) expanded_logsig[depth - 1]+=values * coeff calculated_sig = numpy.concatenate(exponentiateTensor(expanded_logsig)) self.assertLess(diff(sig,calculated_sig),0.00001) #calculate a log signature from sig fullLogSig = numpy.concatenate(logTensor(splitConcatenatedTensor(sig,dim,level))) fullLogSigLib = iisignature.logsig(path,s,"x") diff1 = numpy.max(numpy.abs(fullLogSigLib - fullLogSig)) #print #(numpy.vstack([fullLogSig,fullLogSigLib,numpy.abs(fullLogSigLib-fullLogSig)]).transpose()) self.assertLess(diff1,0.00001) basisMatrix = [] zeros = [numpy.zeros(dim ** m) for m in range(1,level + 1)] for expression in basis: values, depth = valueOfBracket(expression, dim) temp = zeros[depth - 1] zeros[depth - 1] = values basisMatrix.append(numpy.concatenate(zeros)) zeros[depth - 1] = temp calculatedLogSig = lstsq(numpy.transpose(basisMatrix),fullLogSig)[0] diff2 = numpy.max(numpy.abs(logsig - calculatedLogSig)) self.assertLess(diff2,0.00001) #check consistency of methods slowLogSig = iisignature.logsig(path,s,"o") diffs = numpy.max(numpy.abs(slowLogSig - calculatedLogSig)) self.assertLess(diffs,0.00001) sigLogSig = iisignature.logsig(path,s,"s") diffs = numpy.max(numpy.abs(sigLogSig - calculatedLogSig)) self.assertLess(diffs,0.00001) if level < 3: areaLogSig = iisignature.logsig(path,s,"a") diffs = numpy.max(numpy.abs(areaLogSig - calculatedLogSig)) self.assertLess(diffs,0.00001)
def testLyndon(self): d=2 m=5 s=iisignature.prepare(d,m,"O") for expression in iisignature.basis(s): word = ''.join(c for c in expression if c not in '[,]') if len(word) > 1: for prefixLength in range(1,len(word)): self.assertLess(word[:prefixLength],word[prefixLength:])
def __init__(self, model, dim, level=2, transform=lambda x: x, **model_args): self.prepared = prepare(dim, level) # iisignature prepare log signature super().__init__(model, level, transform, **model_args)
def tryLogSig(): s=iisignature.prepare(2,2) for expanded in (False, True): inp = Variable(torch.randn(8, 2), requires_grad=True) if expanded: result = LogSig(inp,s,"x") else: result = LogSig(inp, s) print(result.data.numpy()) result.backward(torch.randn(result.size())) print(inp.grad.data)
def tryLogSig(): for expanded in (False, True): sess=tf.Session() path=[[2,3],[3,5],[4,5]] m=2 s=iisignature.prepare(2,m) a = tf.placeholder(tf.float32) out= LogSig(a,s,"x") if expanded else LogSig(a,s) grad = tf.gradients(tf.reduce_sum(out),a)[0] #grad = tf.gradients(out,a)[0] out_vals=(sess.run([out,grad], {a: path})) print (out_vals[0]) print(out_vals[1])
def test_logsigbackwards_can_augment_s(self): numpy.random.seed(291) d=2 m=7 pathLength=3 path = numpy.random.uniform(size=(pathLength,d)) increment = 0.1*numpy.random.uniform(size=(pathLength,d)) dFdlogSig = numpy.ones(iisignature.logsiglength(d,m)) for types in (("x","o","s"),("xh","oh","sh")): ss=[iisignature.prepare(d,m,t) for t in types] backs=[iisignature.logsigbackprop(dFdlogSig,path,s) for s in ss] self.assertTrue(numpy.allclose(backs[0],backs[2]),types[0]) self.assertTrue(numpy.allclose(backs[1],backs[2]),types[1]) fwds=[iisignature.logsig(path,s,"s") for s in ss] self.assertTrue(numpy.allclose(fwds[0],fwds[2]),types[0]) self.assertTrue(numpy.allclose(fwds[1],fwds[2]),types[1])
def testLevel1(self): m=1 d=2 path=numpy.random.uniform(size=(10,d)) rightSig = path[-1,:]-path[0,:] s=iisignature.prepare(d,m,"cosx") self.assertLess(diff(iisignature.sig(path,m),rightSig),0.0000001) for type_ in ("C","O","S","X","A"): self.assertLess(diff(iisignature.logsig(path,s,type_),rightSig),0.0000001,type_) derivs=numpy.array([2.1,3.2]) pathderivs=numpy.zeros_like(path) pathderivs[-1]=derivs pathderivs[0]=-derivs self.assertLess(diff(iisignature.logsigbackprop(derivs,path,s),pathderivs),0.00001) self.assertLess(diff(iisignature.logsigbackprop(derivs,path,s,"X"),pathderivs),0.00001) self.assertLess(diff(iisignature.sigbackprop(derivs,path,m),pathderivs),0.00001)
def logSig(self, type, m=5): numpy.random.seed(291) d=2 pathLength=10 s=iisignature.prepare(d,m,type) path = numpy.random.uniform(size=(pathLength,d)) path = numpy.cumsum(2 * (path - 0.5),0)#makes it more random-walk-ish, less like a scribble increment = 0.01*path increment = 0.1*numpy.random.uniform(size=(pathLength,d)) manualChange = fdDeriv(lambda x:iisignature.logsig(x,s,type),path,increment,4) dFdlogSig = numpy.ones(iisignature.siglength(d,m) if "X"==type else iisignature.logsiglength(d,m)) calculatedChange = numpy.sum(increment*iisignature.logsigbackprop(dFdlogSig,path,s,type)) #print(manualChange, calculatedChange) self.assertLess(numpy.abs(manualChange-calculatedChange),0.0001)
def doL2S(self, coropa): numpy.random.seed(212) d = 3 m = 6 path = numpy.random.uniform(size=(12, d)) sig = iisignature.sig(path, m) s = iisignature.prepare(d, m, "S2H" if coropa else "S2") self.assertTrue(iisignature.info(s)["logsigtosig_supported"]) logsig = iisignature.logsig(path, s) sig_ = iisignature.logsigtosig(logsig, s) self.assertEqual(sig.shape, sig_.shape) self.assertTrue(numpy.allclose(sig, sig_)) #Like the other iisig functions, we check that derivatives #of logsigtosig allow sum(logsigtosig) to be backproped correctly #This is a boring thing to calculate, because after the first level #each of the log signature elements is a lie bracket and so #contributes a net total of 0 to the signature derivsOfSum = numpy.ones((sig.shape[0], ), dtype="float64") bumpedLogSig = 1.01 * logsig calculated = iisignature.logsigtosigbackprop(derivsOfSum, logsig, s) #wantedbackprop = allSensitivities(logsig, lambda l: iisignature.logsigtosig(l,s).sum()) manualChange = fdDeriv(lambda x: iisignature.logsigtosig(x, s), logsig, bumpedLogSig - logsig, 6) calculatedChange = numpy.sum((bumpedLogSig - logsig) * calculated) self.assertLess(numpy.abs(manualChange - calculatedChange), 0.00001) #beyond the first level, all zero if m > 1: self.assertLess(numpy.max(numpy.abs(calculated[d:])), 0.00001) self.assertEqual(calculated.shape, logsig.shape) #Now for a better test, we backprop sum(random*logsigtosig) #specifically calculate the change in it caused by bump two ways random = numpy.random.uniform(size=sig.shape[0], ) derivsOfSum = random calculated = iisignature.logsigtosigbackprop(derivsOfSum, logsig, s) manualChange = fdDeriv( lambda x: iisignature.logsigtosig(x, s) * random, logsig, bumpedLogSig - logsig, 4) calculatedChange = numpy.sum((bumpedLogSig - logsig) * calculated) self.assertLess(numpy.abs(manualChange - calculatedChange), 0.00001) self.assertEqual(calculated.shape, logsig.shape)
def ComputeMultiLevelSig1dBM(BM_paths, number_of_segment, depth_of_tensors, T): """ Compute the signature of all samples """ s = iisignature.prepare(2, depth_of_tensors) no_of_samples = np.shape(BM_paths)[0] MultiLevelSigs = np.zeros([no_of_samples, iisignature.siglength( 2, depth_of_tensors) * number_of_segment - 1], dtype=float) print('start computing the sigs of degree %d:' % depth_of_tensors) for j in tqdm(range(0, no_of_samples, 1), total=no_of_samples): BM = TimeJointPath(BM_paths[j, :], T) result2 = ComputeMultiLevelSig( BM, number_of_segment, depth_of_tensors, s, sig='True') MultiLevelSigs[j] = result2['MultiLevelLogSig'] if number_of_segment > 1: n_input = int(np.shape(MultiLevelSigs)[1] / number_of_segment) X_sig = MultiLevelSigs.reshape( (np.shape(MultiLevelSigs)[0], number_of_segment, n_input)) else: X_sig = MultiLevelSigs return X_sig
def demo(): #1. generate some dummy data d=3 m=6 path1=np.random.uniform(size=(20,d)) path2=np.random.uniform(size=(20,d)) paths = (path1,path2) s=iisignature.prepare(d,m) #2. print the dot product of the log signatures in tensor space #(The "x" means that the log signature is returned in tensor space.) expandedLogSig1,expandedLogSig2=(iisignature.logsig(i,s,"x") for i in paths) target = np.inner(expandedLogSig1,expandedLogSig2) print_ ("Target:", float(target)) #3. use getMatrices to act on the log signatures expressed in a basis # and get the same answer. matrices=getMatrices(s) logsig1,logsig2=(iisignature.logsig(i,s) for i in paths) adjustment=scipy.linalg.block_diag(*matrices) #print(np.dot(adjustment,logsig2).shape) print_ ("We get:", float(np.inner(logsig1,np.dot(adjustment,logsig2))))
def save_plot_sig_logsig_handwritten_data(): for truncation_order in range(1, 16): fig_sig, axs_sig = plt.subplots(10) fig_sig.suptitle( f"Sig of handwritten digits (0 top and 9 bottom); k={truncation_order}" ) fig_logsig, axs_logsig = plt.subplots(10) fig_logsig.suptitle( f"Log_sig of handwritten digits (0 top and 9 bottom); k={truncation_order}" ) for i in range(10): # 1. logsig s = iisignature.prepare(2, truncation_order) log_sig = iisignature.logsig(handwritting_ds.training_X_ordered[i], s) log_sig_df = pd.DataFrame(log_sig) # signatures are rows log_sig_df = log_sig_df.transpose( ) # ; we move them into columns so that time series format axs_logsig[i].plot(log_sig_df.values) fig_logsig.savefig( f"/home/raymess-lin/git/auto-sig-encoder/plots/handwritten_log_sig_k_{truncation_order}.png" ) # 2. sig sig = iisignature.sig(handwritting_ds.training_X_ordered[i], truncation_order) sig_df = pd.DataFrame(sig) # signatures are rows sig_df = sig_df.transpose( ) # ; we move them into columns so that time series format axs_sig[i].plot(sig_df.values) fig_sig.savefig( f"/home/raymess-lin/git/auto-sig-encoder/plots/handwritten_sig_k_{truncation_order}.png" )
'PROTEINS/': 59 } batch_size = bs[dataset_name] test_size = data_len // 10 train_batches = np.ceil((data_len - test_size) / batch_size).astype(int) print('number of batches = ', train_batches, ' batch size = ', batch_size) torch.manual_seed(0) rng_state = torch.get_rng_state( ) #seed init to ensure same initial conditions for each training ### eigenvalue path signature #### if xtra_feat: pslevel = 4 sig_prep = iisignature.prepare(2, pslevel) #xtra_feat_length = iisignature.logsiglength(2, pslevel) siglength = iisignature.logsiglength(2, pslevel) xtra_feat_length = siglength if xxtra: xtra_feat_length += 7 else: xtra_feat_length = 0 ###### preprocess ##### data = [] label = [] mx, mn = -np.inf, np.inf Alist = None Aker = None
def testHighDim(self): for m in [1]: d=1000 path = numpy.random.rand(10,d) s=iisignature.prepare(d,m) iisignature.logsig(path,s,"A")
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))
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))
def testHighDim(self): for m in [1,2]: d=1000 path = numpy.random.rand(10,d) s=iisignature.prepare(d,m) iisignature.logsig(path,s,"A")
import numpy as np import time start_time = time.time() max_frames = 420 min_frames = 130 processes = 12 id_set = get_id_set() size = len(id_set) explored = {} k = 3 d = 69 words, word_index = signature.get_words(d, k) s = prepare(d, k) def similarity(a, b): return log_signature.concatenate_metric(a, b, s) return log_signature.linear_metric(a, b) return signature.inverse_tensor_metric(a, b, words, word_index, k, d) return signature.linear_metric(a, b) return signature.concatenate_group_metric(a, b, words, word_index, k, d) return signature.concatenate_algebra_metric(a, b, words, word_index, k, d) return log_signature.concatenate_group_metric(a, b, s) def explore(id): subject, animation, description = unpack( fetch_animations(1, animation_id=id))
def setup(obj): obj.path = torch.rand(obj.size, dtype=torch.float).numpy() shape = obj.size[-3], iisignature.logsiglength(obj.size[-1], obj.depth) obj.grad = torch.rand(shape).numpy() obj.prepare = iisignature.prepare(obj.path.shape[-1], obj.depth)
import six.moves #add the parent directory, so we find our iisignature build if it was built --inplace sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) import iisignature from iisignature_theano import LogSig, Sig #1: SETUP VARIABLES dim=2 level=3 pathlength=4 timedim=False useLogSig = True s=iisignature.prepare(dim,level) numpy.random.seed(51) target = numpy.random.uniform(size=(pathlength,dim)).astype("float32") #target = numpy.cumsum(2*(target-0.5),0)#makes it more random-walk-ish, less like a scribble targetSig = iisignature.logsig(target,s) if useLogSig else iisignature.sig(target,level) start = numpy.random.uniform(size=(pathlength,dim)).astype("float32") start[0,:] = target[0,:] learnable_mask = numpy.ones((pathlength,dim)).astype("float32") learnable_mask[0,:]=0 #to stop the starting point being modified if timedim: for i in six.moves.xrange(pathlength): target[i,0]=start[i,0]=i*0.2 learnable_mask[i,0]=0 learning_rate_decay = 1.003
def transform(self, X): prepared = prepare(self.dim, self.level) return np.array([logsig(x, prepared) for x in X])
#see their log signatures printed on the console. #based on https://kivy.org/docs/tutorials/firstwidget.html from kivy.app import App from kivy.uix.widget import Widget from kivy.graphics import Color, Ellipse, Line import numpy as np, os, sys #add the parent directory, so we find our iisignature build if it was built --inplace sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) import iisignature np.set_printoptions(suppress=True) m=2 s=iisignature.prepare(2,m) class MyPaintWidget(Widget): def on_touch_down(self, touch): with self.canvas: Color(1, 1, 0) touch.ud['line'] = Line(points=(touch.x, touch.y)) def on_touch_move(self, touch): touch.ud['line'].points += [touch.x, touch.y] def on_touch_up(self, touch): path=np.reshape(touch.ud['line'].points,(-1,2)) print(iisignature.logsig(path,s))
def testIssue16(self): # github-reported crash, issue #16 iisignature.prepare(3, 3, '2')
def setup(obj): obj.path = torch.rand(obj.size, dtype=torch.float).numpy() obj.prepare = iisignature.prepare(obj.path.shape[-1], obj.depth)
import theano, numpy, sys import six.moves #add the parent directory, so we find our iisignature build if it was built --inplace sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) import iisignature from iisignature_theano import LogSig, Sig #1: SETUP VARIABLES dim = 2 level = 3 pathlength = 4 timedim = False useLogSig = True s = iisignature.prepare(dim, level) numpy.random.seed(51) target = numpy.random.uniform(size=(pathlength, dim)).astype("float32") #target = numpy.cumsum(2*(target-0.5),0)#makes it more random-walk-ish, less like a scribble targetSig = iisignature.logsig(target, s) if useLogSig else iisignature.sig( target, level) start = numpy.random.uniform(size=(pathlength, dim)).astype("float32") start[0, :] = target[0, :] learnable_mask = numpy.ones((pathlength, dim)).astype("float32") learnable_mask[0, :] = 0 #to stop the starting point being modified if timedim: for i in six.moves.xrange(pathlength): target[i, 0] = start[i, 0] = i * 0.2 learnable_mask[i, 0] = 0
#This program just continually runs some of the important functionality in #iisignature on small data, so that you can check that memory usage #does not grow. Even a tiny memory leak becomes very visible e.g. in htop. #add the parent directory, so we find our iisignature build if it was built --inplace sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) import 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