UNPKG

4.36 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2019 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 */
18Object.defineProperty(exports, "__esModule", { value: true });
19var tfjs_1 = require("@tensorflow/tfjs");
20var nodejs_kernel_backend_1 = require("./nodejs_kernel_backend");
21var SummaryFileWriter = /** @class */ (function () {
22 function SummaryFileWriter(resourceHandle) {
23 this.resourceHandle = resourceHandle;
24 nodejs_kernel_backend_1.ensureTensorflowBackend();
25 this.backend = nodejs_kernel_backend_1.nodeBackend();
26 }
27 /**
28 * Write a scalar summary.
29 *
30 * @param name A name of the summary. The summary tag for TensorBoard will be
31 * this name.
32 * @param value A real numeric scalar value, as `tf.Scalar` or a JavaScript
33 * `number`.
34 * @param step Required `int64`-castable, monotically-increasing step value.
35 * @param description Optinal long-form description for this summary, as a
36 * `string`. *Not implemented yet*.
37 */
38 SummaryFileWriter.prototype.scalar = function (name, value, step, description) {
39 // N.B.: Unlike the Python TensorFlow API, step is a required parameter,
40 // because the construct of global step does not exist in TensorFlow.js.
41 if (description != null) {
42 throw new Error('scalar() does not support description yet');
43 }
44 this.backend.writeScalarSummary(this.resourceHandle, step, name, value);
45 };
46 /**
47 * Force summary writer to send all buffered data to storage.
48 */
49 SummaryFileWriter.prototype.flush = function () {
50 this.backend.flushSummaryWriter(this.resourceHandle);
51 };
52 return SummaryFileWriter;
53}());
54exports.SummaryFileWriter = SummaryFileWriter;
55/**
56 * Use a cache for `SummaryFileWriter` instance.
57 *
58 * Using multiple instances of `SummaryFileWriter` pointing to the same
59 * logdir has potential problems. Using this cache avoids those problems.
60 */
61var summaryFileWriterCache = {};
62/**
63 * Create a summary file writer for TensorBoard.
64 *
65 * Example:
66 * ```js
67 * const tf = require('@tensorflow/tfjs-node');
68 *
69 * const summaryWriter = tf.node.summaryFileWriter('/tmp/tfjs_tb_logdir');
70 *
71 * for (let step = 0; step < 100; ++step) {
72 * summaryWriter.scalar('dummyValue', Math.sin(2 * Math.PI * step / 8), step);
73 * }
74 * ```
75 *
76 * @param logdir Log directory in which the summary data will be written.
77 * @param maxQueue Maximum queue length (default: `10`).
78 * @param flushMillis Flush every __ milliseconds (default: `120e3`, i.e,
79 * `120` seconds).
80 * @param filenameSuffix Suffix of the protocol buffer file names to be
81 * written in the `logdir` (default: `.v2`).
82 * @returns An instance of `SummaryFileWriter`.
83 */
84/**
85 * @doc {heading: 'TensorBoard', namespace: 'node'}
86 */
87function summaryFileWriter(logdir, maxQueue, flushMillis, filenameSuffix) {
88 if (maxQueue === void 0) { maxQueue = 10; }
89 if (flushMillis === void 0) { flushMillis = 120000; }
90 if (filenameSuffix === void 0) { filenameSuffix = '.v2'; }
91 tfjs_1.util.assert(logdir != null && typeof logdir === 'string' && logdir.length > 0, function () {
92 return "Invalid logdir: " + logdir + ". Expected a non-empty string for logdir.";
93 });
94 if (!(logdir in summaryFileWriterCache)) {
95 nodejs_kernel_backend_1.ensureTensorflowBackend();
96 var backend = nodejs_kernel_backend_1.nodeBackend();
97 var writerResource = backend.summaryWriter(logdir);
98 backend.createSummaryFileWriter(writerResource, logdir, maxQueue, flushMillis, filenameSuffix);
99 summaryFileWriterCache[logdir] = new SummaryFileWriter(writerResource);
100 }
101 return summaryFileWriterCache[logdir];
102}
103exports.summaryFileWriter = summaryFileWriter;