Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
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);
}
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);
}
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)
};
}
}
model.weights = tf.tidy(_ => {
return normalize1d(tf.tensor1d(model.weights));
});
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;
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;
}
});
});