-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var sweet = require('sweet.js'); | ||
var fs = require('fs'); | ||
var cp = require('child_process'); | ||
|
||
var adRequireStr = "var ad = require('adnn/ad');\n"; | ||
var macros = undefined; | ||
function getMacros() { | ||
if (macros === undefined) { | ||
macros = sweet.loadNodeModule(__dirname, './macros.sjs'); | ||
} | ||
return macros; | ||
} | ||
|
||
// Macro transform some code | ||
function macroTransform(code) { | ||
var compiledCode = sweet.compile(code, { | ||
modules: getMacros(), | ||
readableNames: true | ||
}).code; | ||
return adRequireStr + compiledCode; | ||
} | ||
|
||
// Macro transform a module before requiring it. | ||
// Optionally cache the transformed module code on disk. | ||
// NOTE: Once cached, it won't be recompiled (unless you macroRequire the same | ||
// module again with 'cacheOnDisk' set to false). So this isn't like 'make', | ||
// which will detect changes and recompile for you. Thus, 'cacheOnDisk' is | ||
// best used for modules which aren't going to change (or which change very | ||
// infrequently). | ||
function macroRequire(modulename, cacheOnDisk) { | ||
var filename = require.resolve(modulename); | ||
var adfilename = filename + '.ad'; | ||
var m = require.cache[adfilename]; | ||
if (m !== undefined) { | ||
m = m.exports; | ||
} else { | ||
if (!cacheOnDisk || !fs.existsSync(adfilename)) { | ||
var code = fs.readFileSync(filename); | ||
var compiledCode = macroTransform(code); | ||
fs.writeFileSync(adfilename, compiledCode); | ||
} | ||
m = require(adfilename); | ||
if (!cacheOnDisk) { | ||
cp.execSync('rm -f ' + adfilename); | ||
} | ||
} | ||
return m; | ||
} | ||
|
||
module.exports = { | ||
macroTransform: macroTransform, | ||
macroRequire: macroRequire | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "adnn", | ||
"version": "0.0.1", | ||
"description": "Javascript neural networks on top of general scalar/tensor reverse-mode automatic differentiation.", | ||
"author": "dritchie", | ||
"license": "MIT", | ||
"dependencies": { | ||
"sweet.js": "^0.7.4" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dritchie/adnn" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/dritchie/adnn/issues" | ||
}, | ||
"engines": { | ||
"node": ">=0.12.4" | ||
}, | ||
"keywords": [ | ||
"automatic differentiation", | ||
"neural networks" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function foo(x, y) { | ||
return Math.sqrt(x*x + y*y); | ||
} | ||
|
||
module.exports = { | ||
foo: foo | ||
}; |