UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2018 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * =============================================================================
17 */
18var __assign = (this && this.__assign) || function () {
19 __assign = Object.assign || function(t) {
20 for (var s, i = 1, n = arguments.length; i < n; i++) {
21 s = arguments[i];
22 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
23 t[p] = s[p];
24 }
25 return t;
26 };
27 return __assign.apply(this, arguments);
28};
29function __export(m) {
30 for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
31}
32Object.defineProperty(exports, "__esModule", { value: true });
33var tf = require("@tensorflow/tfjs");
34var path = require("path");
35var callbacks_1 = require("./callbacks");
36var file_system_1 = require("./io/file_system");
37var nodeIo = require("./io/index");
38var nodejs_kernel_backend_1 = require("./nodejs_kernel_backend");
39var nodeVersion = require("./version");
40// Import all kernels.
41require("./kernels/all_kernels");
42// tslint:disable-next-line:no-require-imports
43var binary = require('node-pre-gyp');
44var bindingPath = binary.find(path.resolve(path.join(__dirname, '/../package.json')));
45// Check if the node native addon module exists.
46// tslint:disable-next-line:no-require-imports
47var fs = require('fs');
48if (!fs.existsSync(bindingPath)) {
49 throw new Error("The Node.js native addon module (tfjs_binding.node) can not "
50 + "be found at path: " + String(bindingPath)
51 + ". \nPlease run command "
52 + "'npm rebuild @tensorflow/tfjs-node build-addon-from-source' to rebuild "
53 + "the native addon module. \nIf you have problem with building the addon "
54 + "module, please check https://github.com/tensorflow/tfjs/blob/master"
55 + "/tfjs-node/WINDOWS_TROUBLESHOOTING.md or file an issue.");
56}
57// tslint:disable-next-line:no-require-imports
58var bindings = require(bindingPath);
59// Merge version and io namespaces.
60exports.version = __assign({}, tf.version, { 'tfjs-node': nodeVersion.version });
61exports.io = __assign({}, tf.io, nodeIo);
62// Export all union package symbols
63__export(require("@tensorflow/tfjs"));
64__export(require("./node"));
65// tslint:disable-next-line:no-require-imports
66var pjson = require('../package.json');
67tf.registerBackend('tensorflow', function () {
68 return new nodejs_kernel_backend_1.NodeJSKernelBackend(bindings, pjson.name);
69}, 3 /* priority */);
70var success = tf.setBackend('tensorflow');
71if (!success) {
72 throw new Error("Could not initialize TensorFlow backend.");
73}
74// Register the model saving and loading handlers for the 'file://' URL scheme.
75tf.io.registerLoadRouter(file_system_1.nodeFileSystemRouter);
76tf.io.registerSaveRouter(file_system_1.nodeFileSystemRouter);
77// Register the ProgbarLogger for Model.fit() at verbosity level 1.
78tf.registerCallbackConstructor(1, callbacks_1.ProgbarLogger);