|
| 1 | + |
| 2 | +(* |
| 3 | +
|
| 4 | +deeptemporalmemoryC.m |
| 5 | +
|
| 6 | +
|
| 7 | +Mathematica wrapper for the deeptemporalmemory command line program. |
| 8 | +Copyright (c) 2022 Peter Overmann |
| 9 | +
|
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 11 | +and associated documentation files (the “Software”), to deal in the Software without restriction, |
| 12 | +including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | +and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 14 | +subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all copies or substantial |
| 17 | +portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
| 20 | +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 21 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 23 | +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +
|
| 25 | +
|
| 26 | +*) |
| 27 | + |
| 28 | +(*) |
| 29 | +
|
| 30 | +Requires an installation of /usr/local/bin/deeptemporalmemory. Add to environment path using: |
| 31 | +SetEnvironment[ "PATH" -> Environment["PATH"] <> ":/usr/local/bin"]; |
| 32 | +
|
| 33 | +*) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +TemporalMemory[f_Symbol, {n_Integer,p_Integer} ]:= |
| 39 | + |
| 40 | + Module[ {process, tostr}, |
| 41 | + |
| 42 | + (* random sparse vector with dimension n and sparse population p *) |
| 43 | + f[] := SparseArray[RandomSample[ Range[n], p] -> Table[1, p], {n}]; |
| 44 | + |
| 45 | + process = StartProcess[ {"deeptemporalmemory", ToString[n], ToString[p]}]; Pause[5]; |
| 46 | + |
| 47 | + tostr[x_SparseArray] := StringJoin[ ToString[#] <> " " & /@ Sort[Flatten[x["NonzeroPositions"]]]] ; |
| 48 | + |
| 49 | + (* send x, return prediction *) |
| 50 | + f[x_SparseArray] := Module[ {a}, |
| 51 | + WriteLine[ process, tostr[x] ]; |
| 52 | + a = ToExpression /@ StringSplit[ReadLine[process]]; SparseArray[ a -> Table[1, Length[a]], {n}] |
| 53 | + ]; |
| 54 | + |
| 55 | + |
| 56 | + ]; |
| 57 | + |
| 58 | + |
0 commit comments