@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
89 lines • 3.56 kB
JavaScript
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '../index';
import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
import { expectArraysClose } from '../test_util';
describeWithFlags('sigmoid', ALL_ENVS, () => {
it('basic', async () => {
const values = [1, -3, 2, 7, -4];
const a = tf.tensor1d(values);
const result = tf.sigmoid(a);
const expected = [];
for (let i = 0; i < a.size; i++) {
expected[i] = 1 / (1 + Math.exp(-values[i]));
}
expectArraysClose(await result.data(), expected);
});
it('6D', async () => {
const a = tf.ones([2, 2, 2, 2, 2, 2]);
const result = tf.sigmoid(a);
const expected = [];
for (let i = 0; i < a.size; i++) {
expected[i] = 1 / (1 + Math.exp(-1.0));
}
expectArraysClose(await result.data(), expected);
});
it('propagates NaNs', async () => {
const a = tf.tensor1d([3, NaN]);
const res = tf.sigmoid(a);
expectArraysClose(await res.data(), [1 / (1 + Math.exp(-3)), NaN]);
});
it('gradients: Tensor1D', async () => {
const a = tf.tensor1d([1, 2, -3, 5]);
const dy = tf.tensor1d([1, 2, 3, 4]);
const da = tf.grad(a => tf.sigmoid(a))(a, dy);
const aVals = await a.array();
const dyVals = await dy.array();
const expected = [];
for (let i = 0; i < a.size; i++) {
const y = 1 / (1 + Math.exp(-aVals[i]));
expected[i] = dyVals[i] * y * (1 - y);
}
expectArraysClose(await da.data(), expected);
});
it('gradient with clones', async () => {
const a = tf.tensor1d([1, 2, -3, 5]);
const dy = tf.tensor1d([1, 2, 3, 4]);
const da = tf.grad(a => tf.sigmoid(a.clone()).clone())(a, dy);
const aVals = await a.array();
const dyVals = await dy.array();
const expected = [];
for (let i = 0; i < a.size; i++) {
const y = 1 / (1 + Math.exp(-aVals[i]));
expected[i] = dyVals[i] * y * (1 - y);
}
expectArraysClose(await da.data(), expected);
});
it('throws when passed a non-tensor', () => {
expect(() => tf.sigmoid({}))
.toThrowError(/Argument 'x' passed to 'sigmoid' must be a Tensor/);
});
it('accepts a tensor-like object', async () => {
const values = [1, -3, 2, 7, -4];
const result = tf.sigmoid(values);
const expected = [];
for (let i = 0; i < values.length; i++) {
expected[i] = 1 / (1 + Math.exp(-values[i]));
}
expectArraysClose(await result.data(), expected);
});
it('throws for string tensor', () => {
expect(() => tf.sigmoid('q'))
.toThrowError(/Argument 'x' passed to 'sigmoid' must be numeric/);
});
});
//# sourceMappingURL=sigmoid_test.js.map