How to use the @tensorflow/tfjs-node.tensor1d function in @tensorflow/tfjs-node

To help you get started, we’ve selected a few @tensorflow/tfjs-node examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github charliegerard / gestures-ml-js / daydream / examples / harry-potter / train.js View on Github external
function convertToTensors(featuresData, labelData, testSplit) {
  if (featuresData.length !== labelData.length) {
    throw new Error('features set and labels set have different numbers of examples');
  }

  const [shuffledFeatures, shuffledLabels] = shuffleData(featuresData, labelData);

  const featuresTensor = tf.tensor2d(shuffledFeatures, [numSamplesPerGesture, totalNumDataPerFile]);

  // Create a 1D `tf.Tensor` to hold the labels, and convert the number label
  // from the set {0, 1, 2} into one-hot encoding (.e.g., 0 --> [1, 0, 0]).
  const labelsTensor = tf.oneHot(tf.tensor1d(shuffledLabels).toInt(), numClasses);

  return split(featuresTensor, labelsTensor, testSplit);
}
github charliegerard / gestures-ml-js / phone / examples / game / train.js View on Github external
function convertToTensors(featuresData, labelData, testSplit) {
  if (featuresData.length !== labelData.length) {
    throw new Error('features set and labels set have different numbers of examples');
  }

  const [shuffledFeatures, shuffledLabels] = shuffleData(featuresData, labelData);

  const featuresTensor = tf.tensor2d(shuffledFeatures, [numSamplesPerGesture, totalNumDataPerFile]);

  // Create a 1D `tf.Tensor` to hold the labels, and convert the number label
  // from the set {0, 1, 2} into one-hot encoding (.e.g., 0 --> [1, 0, 0]).
  const labelsTensor = tf.oneHot(tf.tensor1d(shuffledLabels).toInt(), numClasses);

  return split(featuresTensor, labelsTensor, testSplit);
}
github charliegerard / gestures-ml-js / daydream / examples / game / train.js View on Github external
function convertToTensors(featuresData, labelData, testSplit) {
  if (featuresData.length !== labelData.length) {
    throw new Error('features set and labels set have different numbers of examples');
  }

  const [shuffledFeatures, shuffledLabels] = shuffleData(featuresData, labelData);

  const featuresTensor = tf.tensor2d(shuffledFeatures, [numSamplesPerGesture, totalNumDataPerFile]);

  // Create a 1D `tf.Tensor` to hold the labels, and convert the number label
  // from the set {0, 1, 2} into one-hot encoding (.e.g., 0 --> [1, 0, 0]).
  const labelsTensor = tf.oneHot(tf.tensor1d(shuffledLabels).toInt(), numClasses);

  return split(featuresTensor, labelsTensor, testSplit);
}
github adwellj / node-tfjs-retrain / data.js View on Github external
labels.set([labelIndex], labelsOffset);
                });
                t.dispose();

                embeddingsOffset += embeddingsFlatSize;
                labelsOffset += 1;
            }
            console.timeLog("Loading Training Data", {
                label: element.label,
                count: element.images.length
            });
        }

        this.dataset = {
            images: tf.tensor4d(embeddings, embeddingsShape),
            labels: tf.oneHot(tf.tensor1d(labels, "int32"), numClasses)
        };
    }
}
github microsoft / 0xDeCA10B / demo / client / src / ml-models / hot_dog-not / train-classifier.js View on Github external
model.weights = tf.tidy(_ => {
            return normalize1d(tf.tensor1d(model.weights));
        });
github microsoft / 0xDeCA10B / demo / client / src / ml-models / hot_dog-not / train-classifier.js View on Github external
async function getEmbedding(sample) {
    let result = embeddingCache[sample];
    if (result !== undefined) {
        result = tf.tensor1d(result);
    } else {
        const img = await loadImage(path.join(dataPath, sample));
        const canvas = createCanvas(img.width, img.height);
        const ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0);
        const emb = await encoder.infer(canvas, { embedding: true });
        if (emb.shape[1] !== EMB_SIZE) {
            throw new Error(`Expected embedding to have ${EMB_SIZE} dimensions. Got shape: ${emb.shape}.`);
        }
        result = tf.tidy(_ => {
            let result = emb.gather(0);
            embeddingCache[sample] = result.arraySync();
            if (REDUCE_EMBEDDINGS) {
                result = EMB_MAPPER.dot(result);
            }
            return result;
github microsoft / 0xDeCA10B / demo / client / src / ml-models / hot_dog-not / train-classifier.js View on Github external
Object.entries(model).forEach(([intent, centroidInfo]) => {
            const centroid = tf.tensor1d(centroidInfo.centroid);
            const distance = centroid.sub(emb).pow(2).sum();
            if (distance.less(minDistance).dataSync()[0]) {
                result = intent;
                minDistance = distance;
            }
        });
    });