UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2019 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 */
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, monotonically-increasing step value.
35 * @param description Optional 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 * Write a histogram summary, for later analysis in TensorBoard's 'Histograms'
48 * and 'Distributions' dashboards (data written using this API will appear in
49 * both places). Like `SummaryFileWriter.scalar` points, each histogram is
50 * associated with a `step` and a `name`. All the histograms with the same
51 * `name` constitute a time series of histograms.
52 *
53 * The histogram is calculated over all the elements of the given `Tensor`
54 * without regard to its shape or rank.
55 *
56 * @param name A name for this summary. The summary tag used for TensorBoard
57 * will be this name.
58 * @param data A Tensor of any shape. The histogram is computed over its
59 * elements, which must be castable to `float32`.
60 * @param step Monotonically-increasing step value.
61 * @param buckets Optional positive `number`. The output will have this many
62 * buckets, except in two edge cases. If there is no data, then there are
63 * no buckets. If there is data but all points have the same value, then
64 * there is one bucket whose left and right endpoints are the same.
65 * @param description Optional long-form description for this summary, as a
66 * `string`. Markdown is supported. Defaults to empty.
67 */
68 SummaryFileWriter.prototype.histogram = function (name, data, step, buckets, description) {
69 this.backend.writeHistogramSummary(this.resourceHandle, step, name, data, buckets, description);
70 };
71 /**
72 * Force summary writer to send all buffered data to storage.
73 */
74 SummaryFileWriter.prototype.flush = function () {
75 this.backend.flushSummaryWriter(this.resourceHandle);
76 };
77 return SummaryFileWriter;
78}());
79exports.SummaryFileWriter = SummaryFileWriter;
80/**
81 * Use a cache for `SummaryFileWriter` instance.
82 *
83 * Using multiple instances of `SummaryFileWriter` pointing to the same
84 * logdir has potential problems. Using this cache avoids those problems.
85 */
86var summaryFileWriterCache = {};
87/**
88 * Create a summary file writer for TensorBoard.
89 *
90 * Example:
91 * ```js
92 * const tf = require('@tensorflow/tfjs-node');
93 *
94 * const summaryWriter = tf.node.summaryFileWriter('/tmp/tfjs_tb_logdir');
95 *
96 * for (let step = 0; step < 100; ++step) {
97 * summaryWriter.scalar('dummyValue', Math.sin(2 * Math.PI * step / 8), step);
98 * }
99 * ```
100 *
101 * @param logdir Log directory in which the summary data will be written.
102 * @param maxQueue Maximum queue length (default: `10`).
103 * @param flushMillis Flush every __ milliseconds (default: `120e3`, i.e,
104 * `120` seconds).
105 * @param filenameSuffix Suffix of the protocol buffer file names to be
106 * written in the `logdir` (default: `.v2`).
107 * @returns An instance of `SummaryFileWriter`.
108 *
109 * @doc {heading: 'TensorBoard', namespace: 'node'}
110 */
111function summaryFileWriter(logdir, maxQueue, flushMillis, filenameSuffix) {
112 if (maxQueue === void 0) { maxQueue = 10; }
113 if (flushMillis === void 0) { flushMillis = 120000; }
114 if (filenameSuffix === void 0) { filenameSuffix = '.v2'; }
115 tfjs_1.util.assert(logdir != null && typeof logdir === 'string' && logdir.length > 0, function () {
116 return "Invalid logdir: " + logdir + ". Expected a non-empty string for logdir.";
117 });
118 if (!(logdir in summaryFileWriterCache)) {
119 nodejs_kernel_backend_1.ensureTensorflowBackend();
120 var backend = nodejs_kernel_backend_1.nodeBackend();
121 var writerResource = backend.summaryWriter(logdir);
122 backend.createSummaryFileWriter(writerResource, logdir, maxQueue, flushMillis, filenameSuffix);
123 summaryFileWriterCache[logdir] = new SummaryFileWriter(writerResource);
124 }
125 return summaryFileWriterCache[logdir];
126}
127exports.summaryFileWriter = summaryFileWriter;