Problem when running TF.js with Jest (unit testing)
See original GitHub issueTensorFlow.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:
- According to the docs, I can pass a
tf.scalar
totf.squaredDifference
; however, I get a Typescript error when passing atf.scalar
unless I cast toany
. Seems it’s expecting aTensor
. Are the docs wrong, or is the Typescript definition too restrictive here? - 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:
- Created 5 years ago
- Reactions:3
- Comments:13 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Can confirm that running with
jest
can cause this error to throw: