-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem when running TF.js with Jest (unit testing) #545
Comments
Hi @DABH , Can you share your project setup?
|
Thanks for your reply! Sure thing -- the relevant lines in my
I just added these dependencies to my existing project, which previously had no references to anything TensorFlow-related. The other potentially relevant thing is that I'm running my Typescript source file via Aside from that, the relevant imports in my
and then later in the file I have functions which invoke tfjs stuff, like my previous post. If there's any more description I can give, let me know! The line that creates
Seems pretty straightforward so not sure what's going on or if I'm missing something obvious. Thanks again for looking into this! |
Can confirm that running with
|
Hi Kyle, So to confirm the error is happening as soon as you call Can you share the stack trace and see exactly which lines in the code the error originates in? I haven't used jest, so something might be happening in that toolchain that confuses the library. |
Here's the full test file with the stacktrace. This test is pulled straight from the Getting Started page const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs-node");
test("should be able to train a model", () => {
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
return model.fit(xs, ys, { epochs: 10 }).then(() => {
model.predict(tf.tensor2d([5], [1, 1])).print();
});
});
|
@dsmilkov Here's a repo that reproduces the bug: |
Any update on this? My team may have to migrate our tests from jest to mocha if not 😅 |
I found a workaround - it seems the combination of jest with tfjs-node is where this bug manifests. |
Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks! |
Workaround: For me (only) an explicit call to @tensorflow/tfjs: 0.13.5 |
Same problem here.
This issue should be re opened i think , and maybe in the tjs-node repo @tensorflow/tfjs-node: ^0.2.3 |
After some investigation i think i found something interesting: When using the NodeJSKernelBackend ( 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.
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. |
Nice find! Left some comments on your PR. Looks great! |
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)
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()
andtf.squaredDifference()
(see below). Two issues -- some or all might be my fault: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?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
The text was updated successfully, but these errors were encountered: