D
Deno

help

How to use recent Tensorflow.JS? TF from deno.land/x/tensorflow@v0.21 appears to work, but it's old

BBBogdan Biv11/20/2023
I've only tried the TF package from deno.land/x/tensorflow@v0.21 with the readme page example. I'm hoping there are better options - read it as more recent and maintained ports of TF. Here's the output of my trial of using jsdelivr version in Deno:
> import * as tf from "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js"
undefined
> const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
/*
Uncaught TypeError: Cannot read properties of undefined (reading 'isTypedArray')
at isTypedArray (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:3075:23)
at scalar (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:12493:11)
at SGDOptimizer.setLearningRate (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:25134:23)
at new SGDOptimizer (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:25105:14)
at Function.sgd (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:27624:16)
at Object.SGD (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:41336:28)
at getOptimizer (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:41345:40)
at LayersModel.compile (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:44020:31)
at Sequential.compile (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:46129:20)
at <anonymous>:8:7
*/
> import * as tf from "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js"
undefined
> const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
/*
Uncaught TypeError: Cannot read properties of undefined (reading 'isTypedArray')
at isTypedArray (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:3075:23)
at scalar (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:12493:11)
at SGDOptimizer.setLearningRate (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:25134:23)
at new SGDOptimizer (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:25105:14)
at Function.sgd (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:27624:16)
at Object.SGD (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:41336:28)
at getOptimizer (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:41345:40)
at LayersModel.compile (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:44020:31)
at Sequential.compile (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.13.0/dist/tf.fesm.js:46129:20)
at <anonymous>:8:7
*/
Would there be other options?
DAHDeno AI Helper11/20/2023
Based on the documentation, Deno has experimental support for npm specifiers, which allows you to import npm packages directly in your Deno scripts. However, this feature is still under development and might not work in all scenarios. The error you're encountering seems to be related to the Tensorflow.js library itself when used in the Deno environment. It's possible that the library is trying to access some Node.js-specific APIs that are not available in Deno. As of now, the documentation does not provide a clear solution for using the latest version of Tensorflow.js in Deno. You might want to consider using the version from deno.land/x/tensorflow@v0.21 if it works for your use case, even though it's not the latest version. Alternatively, you could try using the dnt tool mentioned in the documentation to transform your Deno code into a Node.js compatible NPM package. This way, you could use the latest version of Tensorflow.js as you would in a Node.js environment. However, this might not be the ideal solution if you want to stick to Deno. Remember to always use the --unstable and --allow-net flags when running your Deno script with npm specifiers, as they are required for this experimental feature. Please note that this information is not explicitly stated in the documentation and is more of a general advice. 🦕
Rraunioroo11/20/2023
Sorry I don't have any actual experience with Tensorflow, but the AI bot may be at the right tracks. Try using the actual NPM package (import "npm:packagename"). This activates Deno's Node compatibility layer, so you might have better luck with things working out of the box.

Looking for more? Join the community!