question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem when running TF.js with Jest (unit testing)

See original GitHub issue

TensorFlow.js version

0.12.3

Browser version

Node.js 10.6.0 (no browser) Typescript 2.7.1

Describe the problem or feature request

I’m trying to use tf.sum() and tf.squaredDifference() (see below). Two issues – some or all might be my fault:

  1. According to the docs, I can pass a tf.scalar to tf.squaredDifference; however, I get a Typescript error when passing a tf.scalar unless I cast to any. Seems it’s expecting a Tensor. Are the docs wrong, or is the Typescript definition too restrictive here?
  2. When I run the below code to sum my tensor (vector), I get the error tensor1d() requires values to be a flat/TypedArray. This already should be a 1D tensor/vector, so why is tfjs complaining here? But moreover, the docs suggest I can sum with higher-order tensors as well, so not sure what the 1D restriction is about. Any ideas would be welcome!

Thanks in advance!

Code to reproduce the bug / link to feature request

    // state is a tf.Tensor1D
    console.log(state.squaredDifference(tf.scalar(0) as any).sum());

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ClementVidalcommented, Jan 23, 2019

After some investigation i think i found something interesting:

When using the NodeJSKernelBackend (tf.setBackend("tensorflow"))

In tfjs-core/src/tensor_util_env.ts:34 in function inferShape(), if i change to use Array.isArray() instead of instanceof, the @smith-kyle error goes away and everything work perfectly.

while ( Array.isArray( firstElem ) || isTypedArray(firstElem)) {

I suspect that there is a problem with jest patching some builtin objects like Array thus invalidating tests using instanceof.

I think Array.isArray() should be enforced instead of instanceof

PR 1503 aim to fix that.

3reactions
smith-kylecommented, Aug 4, 2018

Can confirm that running with jest can cause this error to throw:

tensor1d() requires values to be a flat/TypedArray
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using jest with tensorflow.js produces error - Stack Overflow
I import the node backend and it immediately errors. If I run normally (not using jest) then I get no such error. FAIL...
Read more >
Speeding Unit Testing with Jest Runner - IMELGRAT.ME
While running tests may not be a problem when few tests are used, it can be a tedious, time-consuming task when running several...
Read more >
jest failed to parse a file. this happens e.g. when your code or its ...
First of all, what tf does this the command mean?? ... FAIL src/App.test.js ○ Test suite failed to run Jest encountered an unexpected...
Read more >
TensorFlow testing best practices
For any code you write, you should also write its unit tests. ... TF has a lot of subtleties when running tests. We...
Read more >
Test and debug resolvers (VTL) - AWS AppSync
Testing and debugging resolvers in AWS AppSync. ... At the top of the Edit Resolver page, choose Run Test. For a more practical...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found