lasagne.js 7.1 KB

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