TensorFlow.js: Are those two Tensors equal? - javascript

I am reading the TensorflowJS documentation. In their example code they state
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
See here
I am confused since they use a 2 dimensional array here. Does anyone know why?
For completeness, here is the full code snippet.
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
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).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
model.predict(tf.tensor2d([5], [1, 1])).print();
});
Would it not be simpler to use a 1 dimensional array here, since the array does not make use of a second dimension anyways?
const xs = tf.tensor1d([1, 2, 3, 4]);

The features xs tensor is a two dimensionnal tensor. More generally the features tensor is one dimension bigger than the inputShape of the first layer. The added dimension is the batches dimension which indicates how many elements of shape the inputShape, the model are either trained with or predicted.
In the example, the inputShape being of size [1], the features shape need to be of shape [b,1], therefore a two dimensionnal tensor

Related

How do I scale up a tensor's size by a given integer scale factor in Tensorflow.js?

I'm aware that one could implement a scaling function in vanilla JS, but if I were to have a tensor such as this:
a = tf.tensor([[1,2],[3,4]])
a.print();
/*
Tensor
[[1, 2],
[3, 4]]
*/
Does Tensorflow.js have a native way of scaling a tensor up without converting it into a vanilla JS data structure first? For example, if I scaled the above tensor up by 2 in all dimensions, I'd get this:
Tensor
[[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4]]
How can I scale a Tensorflow.js tensor up without first converting it into a vanilla JS data structure?
mirrorPad will do just that
x = tf.tensor([[1,2],[3,4]])
x.mirrorPad([[1, 1], [1, 1]], 'symmetric').print();

Constant Array in JavaScript

How can I keep an array of constants in JavaScript?
And likewise, when comparing, it gives me the correct result.
Example,
const vector = [1, 2, 3, 4];
vector[2] = 7; // An element is not constant!
console.log(JSON.stringify(vector));
// [1,2,7,4] ... Was edited
// OR
const mirror = [1, 2, 7, 4];
console.log(`are equals? ${vector == mirror}`);
// false !
With Object.freeze you can prevent values from being added or changed on the object:
'use strict';
const vector = Object.freeze([1, 2, 3, 4]);
vector[2] = 7; // An element is not constant!
'use strict';
const vector = Object.freeze([1, 2, 3, 4]);
vector.push(5);
That said, this sort of code in professional JS is unusual and a bit overly defensive IMO. A common naming convention for absolute constants is to use ALL_CAPS, eg:
const VECTOR =
Another option for larger projects (that I prefer) is to use TypeScript to enforce these sorts of rules at compile-time without introducing extra runtime code. There, you can do:
const VECTOR: ReadonlyArray<Number> = [1, 2, 3, 4];
or
const VECTOR = [1, 2, 3, 4] as const;
I have not investigated thoroughly, but it is inferred that JS will have immutable native types, here is a presentation:
https://2ality.com/2020/05/records-tuples-first-look.html
const vector = #[1, 2, 3, 4]; // immutable
The const keyword can be confusing, since it allows for mutability. What you would need is immutability. You can do this with a library like Immutable or Mori, or with Object.freeze:
const array = [1,2,3]
Object.freeze(array)
array[0] = 4
array // [1,2,3]

Error when creating a model using Core API

I tried to follow the tutorial on TensorFlow official site to create a machine learning model based on Core API. But I got the following error:
Error: Argument 'b' passed to 'matMul' must be a Tensor or TensorLike, but got 'null'
I am working on windows 10 and with tfjs#1.5.2
const tf = require('#tensorflow/tfjs');
const tfcore = require('#tensorflow/tfjs-core')
const w1 = tf.variable(tf.randomNormal([784, 32]));
const b1 = tf.variable(tf.randomNormal([32]));
const w2 = tf.variable(tf.randomNormal([32, 10]));
const b2 = tf.variable(tf.randomNormal([10]));
function model(x) {
console.log(w1);
return x.matMul(w1).add(b1).relu().matMul(w2).add(b2).softmax();
}
model(tfcore);
Could you please help me with this error?
as #edkeveked stated you need to supply two tensors for tf.matMul:
approach 1
const a = tf.tensor2d([1, 2], [1, 2]);
const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);
a.matMul(b);
or approach 2
const a = tf.tensor2d([1, 2], [1, 2]);
const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);
tf.matMul(a, b);
in your example, by passing in tfcore to model() you are using approach 2 and therefore need to pass a second tensor to matMul. however, if you pass a tensor to model() it should work like approach 1.
The error says it all, you are multiplying a tensor by null (the second parameter defaults to null).
tf.matMul(tensor)
You need to supply a second tensor for the matrix multiplication

Javascript recursion: pull all unique combinations of single items from N arrays of M length

I am looking to create a recursive function (or use loops), to select all possible combinations of single items of N arrays, each having M length.
I want to pull out each combination and find the product of the items from the arrays and store the result. So in other words, the order of the items pulled out doesn't matter (for the example, 1, 1, 4 pulled from the first index starting with array1 would be considered the same as 4, 1, 1 pulled from the first index starting with array3 and working backwards).
//Example:
//in this case, N = 3 and M = 5, 3, 4 for array1, array2, and array3, respectively
array1 = [1, 3, 5, 6, 7];
array2 = [1, 5, 3];
array3 = [4, 3, 7, 9];
//Example output using arrays above:
[1, 1, 4]
[3, 1, 4]
[5, 1, 4]
[6, 1, 4]
[7, 1, 4]
[1, 5, 4]
[1, 3, 4]
[3, 5, 4]
//etc...
I expect the output of each recursive call to be one item from each Array, resulting in a unique combination of items.
For example, each call would output an array of N length, with one value from each array. The function should run until all unique combinations have been looked at.
UPDATE: to clarify, the final end solution that I am trying to get at is to find the Minimum product, by selecting one item from each array and multiplying them together. But I also need to evaluate, manipulate and store each combination, before determining this minimum.
Cou could get the cartesian product first and then get unique products from this values.
The raw array contains possible duplicates.
const multiply = (a, b) => a * b;
var array1 = [1, 3, 5, 6, 7],
array2 = [1, 5, 3],
array3 = [4, 3, 7, 9],
data = [array1, array2, array3],
raw = data.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), [])),
unique = Array.from(new Set(raw.map(a => a.reduce(multiply))));
console.log(unique);
console.log(raw.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I prefer to write this sort of thing as a composition of functions. I'm a big fan of Ramda (disclaimer: I'm a Ramda author) and with Ramda, this is a one-liner 1:
const uniqueProduct = pipe (xprod, map (product), unique)
Of course you wouldn't want to pull in an external library for a single simple problem, but it's also easy to write our own versions of the functions used here:
// Utility functions
const pipe = (...fns) => (args) =>
fns .reduce ((a, f) => f (a), args)
const map = (fn) => (xs) =>
xs.map(fn)
const product = (xs) =>
xs .reduce ((a, x) => a * x, 1)
const crossproduct = (xss) =>
xss.reduce(
(ps, xs) => ps.reduce((r, p) => [...r, ...(xs.map((x) => [...p, x]))], []),
[[]]
)
const unique = (xs) =>
Array .from (new Set (xs))
// Main function
const uniqueProduct = pipe (crossproduct, map (product), unique)
// Demonstration
const data = [[1, 3, 5, 6, 7], [1, 5, 3], [4, 3, 7, 9]]
console .log (
uniqueProduct (data)
)
Every function here, except for uniqueProduct is common enough that there's already a version available in Ramda. But these versions are also potentially useful across a great deal of your code. And outside crossproduct, they are trivial-to-write functions.
Note that there is no difference here in algorithm from Nina Scholz's answer; it's only structured differently. I like the fact that, by storing the common functions in my own library or by using a public one, this code can be written so simply.
1 This is actually a bit of an exaggeration. Ramda's xprod function only works on two arrays; I happen to keep handy one that works on an array of arrays in my own personal utility library built atop Ramda.

How to rollback/replay some operations conditionally?

I want to get a stream which emits numbers and each number is distinct from the previous one or the all previous numbers.
The stream below outputs may be [0, 1, 1, 1, 2, 1, 0, ...], I expect some modifications to get [0, 2, 1, 2, 0, 1, ...], no two or more adjacent numbers are same in the sequence:
const number$ = Rx.Observable.interval(500)
.map(() => Math.floor(Math.random() * 3))
number$.subscribe(console.log)
The stream below outputs may be [3, 3, 1, 4, 3], I expect some modifications to get [2, 0, 3, 1, 4], all numbers are distinct:
const number$ = Rx.Observable.interval(500)
.map(() => Math.floor(Math.random() * 5))
.take(5)
number$.subscribe(console.log)
Simply using distinct and distinctUntilChanged will make some 'holes', it isn't desired. Pre-generate the determinated sequence(ex: using lodash.shuffle) is also not the desired solution, the output number is may generated by a remote rand generator service, means that we cloud retry number fetching while get duplicated numbers.
I think if there are some ways to get the comparison result of distinct and distinctUntilChanged and using it to conditionally rollback/replay the last (one or some) operation(s) can solve my problem.

Categories

Resources