// matrix multiply, with meprop top-k selection in back propagation, for simplification trainging of mesimp // in foward propagation, the columns of r in outinds are not used (masking the neurons) // in backward propagation, only top-k of the output (row-wise) will be back propagated, and the indices will be collected by the record public virtual Tensor MultiplyTopRecord(Tensor l, Tensor r, int k, Record re, int[] outinds) { var res = new Tensor(l.Row, r.Col); res.W.AddMultiply(outinds, l.W, r.W); if (!IsTrain) { return(res); } if (l.DW != null && r.DW != null) { AddBackOp(delegate { var inds = GetAbsTopsHeap(res.DW, k); re.Store(inds); l.DW.AddMultiplyTransB(res.DW, inds, r.W); r.DW.AddMultiplyTransA(l.W, res.DW, inds); }); } else if (l.DW != null) { AddBackOp(delegate { var inds = GetAbsTopsHeap(res.DW, k); re.Store(inds); l.DW.AddMultiplyTransB(res.DW, inds, r.W); }); } else if (r.DW != null) { AddBackOp(delegate { var inds = GetAbsTopsHeap(res.DW, k); re.Store(inds); r.DW.AddMultiplyTransA(l.W, res.DW, inds); }); } return(res); }
public static void Main(string[] args) { Storage db = StorageFactory.Instance.CreateStorage(); db.Open("testmod.dbs", pagePoolSize); Indices root = (Indices) db.GetRoot(); if (root == null) { root = new Indices(); root.strIndex = db.CreateIndex(typeof(string), true); root.intIndex = db.CreateIndex(typeof(long), true); db.SetRoot(root); } Index intIndex = root.intIndex; Index strIndex = root.strIndex; long start = (DateTime.Now.Ticks - 621355968000000000) / 10000; long key = 1999; int i; for (i = 0; i < nRecords; i++) { Record rec = new Record(); key = (3141592621L * key + 2718281829L) % 1000000007L; rec.intKey = key; rec.strKey = Convert.ToString(key); intIndex.Put(new Key(rec.intKey), rec); strIndex.Put(new Key(rec.strKey), rec); } db.Commit(); Console.Out.WriteLine("Elapsed time for inserting " + nRecords + " records: " + ((DateTime.Now.Ticks - 621355968000000000) / 10000 - start) + " milliseconds"); start = (DateTime.Now.Ticks - 621355968000000000) / 10000; for (int j = 0; j < nIterations; j++) { key = 1999; for (i = 0; i < nRecords; i++) { key = (3141592621L * key + 2718281829L) % 1000000007L; Record rec = (Record) intIndex.Get(new Key(key)); rec.strKey = reverseString(rec.strKey); if ((i & 255) == 0) { rec.Store(); } else { rec.Modify(); } } Console.Out.WriteLine("Iteration " + j); db.Commit(); } Console.Out.WriteLine("Elapsed time for performing " + nRecords * nIterations + " updates: " + ((DateTime.Now.Ticks - 621355968000000000) / 10000 - start) + " milliseconds"); start = (DateTime.Now.Ticks - 621355968000000000) / 10000; key = 1999; for (i = 0; i < nRecords; i++) { key = (3141592621L * key + 2718281829L) % 1000000007L; Record rec1 = (Record) intIndex.Get(new Key(key)); Record rec2 = (Record) strIndex.Get(new Key(Convert.ToString(key))); Assert.That(rec1 != null && rec1 == rec2); } Console.Out.WriteLine("Elapsed time for performing " + nRecords * 2 + " index searches: " + ((DateTime.Now.Ticks - 621355968000000000) / 10000 - start) + " milliseconds"); start = (DateTime.Now.Ticks - 621355968000000000) / 10000; key = 1999; for (i = 0; i < nRecords; i++) { key = (3141592621L * key + 2718281829L) % 1000000007L; Record rec = (Record) intIndex.Get(new Key(key)); intIndex.Remove(new Key(key)); strIndex.Remove(new Key(Convert.ToString(key)), rec); rec.Deallocate(); } Console.Out.WriteLine("Elapsed time for deleting " + nRecords + " records: " + ((DateTime.Now.Ticks - 621355968000000000) / 10000 - start) + " milliseconds"); db.Close(); }