lasagne.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* jshint esversion: 6 */
  2. // Experimental
  3. var lasagne = lasagne || {};
  4. lasagne.ModelFactory = class {
  5. match(context) {
  6. const obj = context.open('pkl');
  7. if (obj && obj.__class__ && obj.__class__.__module__ === 'nolearn.lasagne.base' && obj.__class__.__name__ == 'NeuralNet') {
  8. return true;
  9. }
  10. return false;
  11. }
  12. open(context) {
  13. return lasagne.Metadata.open(context).then((metadata) => {
  14. const obj = context.open('pkl');
  15. return new lasagne.Model(metadata, obj);
  16. });
  17. }
  18. };
  19. lasagne.Model = class {
  20. constructor(metadata, model) {
  21. this._graphs = [ new lasagne.Graph(metadata, model) ];
  22. }
  23. get format() {
  24. return 'Lasagne';
  25. }
  26. get graphs() {
  27. return this._graphs;
  28. }
  29. };
  30. lasagne.Graph = class {
  31. constructor(metadata, model) {
  32. this._nodes = [];
  33. this._inputs = [];
  34. this._outputs = [];
  35. const args = new Map();
  36. const arg = (name, type, initializer) => {
  37. if (!args.has(name)) {
  38. args.set(name, new lasagne.Argument(name, type));
  39. }
  40. const value = args.get(name);
  41. if (!value.type && type) {
  42. value.type = type;
  43. }
  44. if (!value.initializer && initializer) {
  45. value.initializer = initializer;
  46. }
  47. return value;
  48. };
  49. for (const pair of model.layers) {
  50. const name = pair[0];
  51. const layer = model.layers_[name];
  52. if (layer && layer.__class__ && layer.__class__.__module__ === 'lasagne.layers.input' && layer.__class__.__name__ === 'InputLayer') {
  53. const type = new lasagne.TensorType(layer.input_var.type.dtype, new lasagne.TensorShape(layer.shape));
  54. this._inputs.push(new lasagne.Parameter(layer.name, [ arg(layer.name, type) ]));
  55. continue;
  56. }
  57. this._nodes.push(new lasagne.Node(metadata, layer, arg));
  58. }
  59. if (model._output_layer) {
  60. const output_layer = model._output_layer;
  61. this._outputs.push(new lasagne.Parameter(output_layer.name, [ arg(output_layer.name) ]));
  62. }
  63. }
  64. get inputs() {
  65. return this._inputs;
  66. }
  67. get outputs() {
  68. return this._outputs;
  69. }
  70. get nodes() {
  71. return this._nodes;
  72. }
  73. };
  74. lasagne.Parameter = class {
  75. constructor(name, args) {
  76. this._name = name;
  77. this._arguments = args;
  78. }
  79. get name() {
  80. return this._name;
  81. }
  82. get arguments() {
  83. return this._arguments;
  84. }
  85. get visible() {
  86. return true;
  87. }
  88. };
  89. lasagne.Argument = class {
  90. constructor(name, type, initializer) {
  91. if (typeof name !== 'string') {
  92. throw new lasagne.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  93. }
  94. this._name= name;
  95. this._type = type || null;
  96. this._initializer = initializer || null;
  97. }
  98. get name() {
  99. return this._name;
  100. }
  101. get type() {
  102. if (this._initializer) {
  103. return this._initializer.type;
  104. }
  105. return this._type;
  106. }
  107. set type(value) {
  108. this._type = value;
  109. }
  110. get initializer() {
  111. return this._initializer;
  112. }
  113. set initializer(value) {
  114. this._initializer = value;
  115. }
  116. };
  117. lasagne.Node = class {
  118. constructor(metadata, layer, arg) {
  119. this._name = layer.name || '';
  120. const type = layer.__class__ ? layer.__class__.__module__ + '.' + layer.__class__.__name__ : '';
  121. this._type = metadata.type(type) || { name: type };
  122. this._inputs = [];
  123. this._outputs = [];
  124. this._attributes = [];
  125. const params = new Map();
  126. for (const key of Object.keys(layer)) {
  127. if (key === 'name' || key === 'params' || key === 'input_layer' || key === 'input_shape') {
  128. continue;
  129. }
  130. const value = layer[key];
  131. if (value && value.__class__ && value.__class__.__module__ === 'theano.tensor.sharedvar' && value.__class__.__name__ === 'TensorSharedVariable') {
  132. params.set(value.name, key);
  133. continue;
  134. }
  135. this._attributes.push(new lasagne.Attribute(null, key, value));
  136. }
  137. if (layer.input_layer && layer.input_layer.name) {
  138. const input_layer = layer.input_layer;
  139. const type = layer.input_shape ? new lasagne.TensorType('?', new lasagne.TensorShape(layer.input_shape)) : undefined;
  140. this._inputs.push(new lasagne.Parameter('input', [ arg(input_layer.name, type) ]));
  141. }
  142. if (layer.params) {
  143. for (const pair of layer.params) {
  144. const param = pair[0];
  145. const param_key = params.get(param.name);
  146. if (param_key) {
  147. const initializer = new lasagne.Tensor(param.container.storage[0]);
  148. this._inputs.push(new lasagne.Parameter(param_key, [ arg(param.name, null, initializer) ]));
  149. }
  150. }
  151. }
  152. this._outputs.push(new lasagne.Parameter('output', [ arg(this.name) ]));
  153. }
  154. get type() {
  155. return this._type;
  156. }
  157. get name() {
  158. return this._name;
  159. }
  160. get inputs() {
  161. return this._inputs;
  162. }
  163. get outputs() {
  164. return this._outputs;
  165. }
  166. get attributes() {
  167. return this._attributes;
  168. }
  169. };
  170. lasagne.Attribute = class {
  171. constructor(metadata, name, value) {
  172. this._name = name;
  173. this._value = value;
  174. if (value && value.__class_) {
  175. this._type = value.__class_.__module__ + '.' + value.__class_.__name__;
  176. }
  177. }
  178. get name() {
  179. return this._name;
  180. }
  181. get value() {
  182. return this._value;
  183. }
  184. get type() {
  185. return this._type;
  186. }
  187. };
  188. lasagne.Metadata = class {
  189. static open(context) {
  190. if (lasagne.Metadata._metadata) {
  191. return Promise.resolve(lasagne.Metadata._metadata);
  192. }
  193. return context.request('lasagne-metadata.json', 'utf-8', null).then((data) => {
  194. lasagne.Metadata._metadata = new lasagne.Metadata(data);
  195. return lasagne.Metadata._metadata;
  196. }).catch(() => {
  197. lasagne.Metadata._metadata = new lasagne.Metadata(null);
  198. return lasagne.Metadata._metadata;
  199. });
  200. }
  201. constructor(data) {
  202. this._map = new Map();
  203. if (data) {
  204. const metadata = JSON.parse(data);
  205. this._map = new Map(metadata.map((item) => [ item.name, item ]));
  206. }
  207. }
  208. type(name) {
  209. return this._map.get(name);
  210. }
  211. attribute(type, name) {
  212. const schema = this.type(type);
  213. if (schema) {
  214. let attributeMap = schema.attributeMap;
  215. if (!attributeMap) {
  216. attributeMap = {};
  217. if (schema.attributes) {
  218. for (const attribute of schema.attributes) {
  219. attributeMap[attribute.name] = attribute;
  220. }
  221. }
  222. schema.attributeMap = attributeMap;
  223. }
  224. const attributeSchema = attributeMap[name];
  225. if (attributeSchema) {
  226. return attributeSchema;
  227. }
  228. }
  229. return null;
  230. }
  231. };
  232. lasagne.TensorType = class {
  233. constructor(dataType, shape) {
  234. this._dataType = dataType;
  235. this._shape = shape;
  236. }
  237. get dataType() {
  238. return this._dataType;
  239. }
  240. get shape() {
  241. return this._shape;
  242. }
  243. toString() {
  244. return this._dataType + this._shape.toString();
  245. }
  246. };
  247. lasagne.TensorShape = class {
  248. constructor(dimensions) {
  249. this._dimensions = dimensions;
  250. }
  251. get dimensions() {
  252. return this._dimensions;
  253. }
  254. toString() {
  255. if (this._dimensions && this._dimensions.length > 0) {
  256. return '[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']';
  257. }
  258. return '';
  259. }
  260. };
  261. lasagne.Tensor = class {
  262. constructor(storage) {
  263. this._type = new lasagne.TensorType(storage.dtype.name, new lasagne.TensorShape(storage.shape));
  264. }
  265. get type() {
  266. return this._type;
  267. }
  268. get state() {
  269. return 'Not implemented.';
  270. }
  271. toString() {
  272. return '';
  273. }
  274. };
  275. lasagne.Error = class extends Error {
  276. constructor(message) {
  277. super(message);
  278. this.name = 'Lasagne Error';
  279. }
  280. };
  281. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  282. module.exports.ModelFactory = lasagne.ModelFactory;
  283. }