UNPKG

3.55 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2018 Google LLC. 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 });
33// Register all kernels.
34require("./register_all_kernels");
35var tf = require("@tensorflow/tfjs");
36var path = require("path");
37var callbacks_1 = require("./callbacks");
38var file_system_1 = require("./io/file_system");
39var nodeIo = require("./io/index");
40var nodejs_kernel_backend_1 = require("./nodejs_kernel_backend");
41var nodeVersion = require("./version");
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) + ". \nPlease run command " +
51 "'npm rebuild @tensorflow/tfjs-node build-addon-from-source' to " +
52 "rebuild the native addon module. \nIf you have problem with building " +
53 "the addon module, please check " +
54 "https://github.com/tensorflow/tfjs/blob/master/tfjs-node/" +
55 "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');
67// Side effects for default initialization of Node backend.
68tf.registerBackend('tensorflow', function () {
69 return new nodejs_kernel_backend_1.NodeJSKernelBackend(bindings, pjson.name);
70}, 3 /* priority */);
71var success = tf.setBackend('tensorflow');
72if (!success) {
73 throw new Error("Could not initialize TensorFlow backend.");
74}
75// Register the model saving and loading handlers for the 'file://' URL scheme.
76tf.io.registerLoadRouter(file_system_1.nodeFileSystemRouter);
77tf.io.registerSaveRouter(file_system_1.nodeFileSystemRouter);
78// Register the ProgbarLogger for Model.fit() at verbosity level 1.
79tf.registerCallbackConstructor(1, callbacks_1.ProgbarLogger);