Skip to content

Commit c3419e1

Browse files
ClementVidaldsmilkov
authored andcommitted
Potential fix for tfjs issue 545 (tensorflow#1503)
BUG Fixes tensorflow/tfjs#545. When testing TensorFlow models using jest and **tfjs-node**, errors like the one reported by [tfjs issue 545](tensorflow/tfjs#545) occurs. I think they comes from the fact that jest patch several default objects like Array which make subsequent call to instanceof fail. Probably because the Array object available at "creation time" is not the same that the one used at "test time". Note that **this fix actually resolve the `tensor1d() requires values to be a flat/TypedArray` error** of me and @smith-kyle in [tfjs issue 545](tensorflow/tfjs#545)
1 parent 759b446 commit c3419e1

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/ops/norm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function normImpl(
8787

8888
// vector
8989
if (x.rank === 1 || typeof axis === 'number' ||
90-
axis instanceof Array && axis.length === 1) {
90+
Array.isArray(axis) && axis.length === 1) {
9191
if (p === 1) {
9292
return x.abs().sum(axis);
9393
}
@@ -106,7 +106,7 @@ function normImpl(
106106
}
107107

108108
// matrix (assumption axis[0] < axis[1])
109-
if (axis instanceof Array && axis.length === 2) {
109+
if (Array.isArray(axis) && axis.length === 2) {
110110
if (p === 1) {
111111
return x.abs().sum(axis[0]).max(axis[1] - 1);
112112
}

src/tensor_util_env.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export function inferShape(val: TensorLike): number[] {
3131
}
3232
const shape: number[] = [];
3333

34-
while (firstElem instanceof Array || isTypedArray(firstElem)) {
34+
while (Array.isArray(firstElem) || isTypedArray(firstElem)) {
3535
shape.push(firstElem.length);
3636
firstElem = firstElem[0];
3737
}
38-
if (val instanceof Array && ENV.get('TENSORLIKE_CHECK_SHAPE_CONSISTENCY')) {
38+
if (Array.isArray(val) && ENV.get('TENSORLIKE_CHECK_SHAPE_CONSISTENCY')) {
3939
deepAssertShapeConsistency(val, shape, []);
4040
}
4141

@@ -45,7 +45,7 @@ export function inferShape(val: TensorLike): number[] {
4545
function deepAssertShapeConsistency(
4646
val: TensorLike, shape: number[], indices: number[]) {
4747
indices = indices || [];
48-
if (!(val instanceof Array) && !isTypedArray(val)) {
48+
if (!(Array.isArray(val)) && !isTypedArray(val)) {
4949
assert(
5050
shape.length === 0,
5151
() => `Element arr[${indices.join('][')}] is a primitive, ` +

src/test_util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ export function expectArraysEqual(
127127
expected: Tensor|TypedArray|number[]|boolean[]|string[]) {
128128
if (actual instanceof Tensor && actual.dtype === 'string' ||
129129
expected instanceof Tensor && expected.dtype === 'string' ||
130-
actual instanceof Array && isString(actual[0]) ||
131-
expected instanceof Array && isString(expected[0])) {
130+
Array.isArray(actual) && isString(actual[0]) ||
131+
Array.isArray(expected) && isString(expected[0])) {
132132
// tslint:disable-next-line:triple-equals
133133
return expectArraysPredicate(actual, expected, (a, b) => a == b);
134134
}

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export function isNumber(value: {}): boolean {
440440
}
441441

442442
export function inferDtype(values: TensorLike): DataType {
443-
if (values instanceof Array) {
443+
if (Array.isArray(values)) {
444444
return inferDtype(values[0]);
445445
}
446446
if (values instanceof Float32Array) {

0 commit comments

Comments
 (0)