How to extract the value of a tensor in tensorflow.js?

Viewed 1004

My model outputs ten predictions from which I take the largest one with argmax. Unfortunately, this also returns a tensor (with single value like this: Tensor: [8]) and I can't figure out how to make this tensor either into integer, or extract the only value.

So far I've tried gather, asType, dataSync, and get.

1 Answers

dataSync returns an array. To get the single element, one needs to use the array indexing as follows

const t = model.predict(tensor)
// Use of argmax and get the value
const v = t.argMax().dataSync()[0]
Related