Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Convert the labels to one-hot representation
// And embed the query with the universal sentence encoder
const oneHot = tf.oneHot(batchLabels, labels.length);
const embedding = await use.embed(queries);
xsBatchedTensors.push(embedding);
ysBatchedTensors.push(oneHot);
console.timeEnd(
`Converted batch ${i} of ${xsBatches.length} (${batchSize}): `);
}
const xsTensor = tf.concat(xsBatchedTensors);
const ysTensor = tf.concat(ysBatchedTensors);
tf.dispose([xsBatchedTensors, ysBatchedTensors]);
return {
xs: xsTensor,
ys: ysTensor,
};
}
validationData: valData,
batchSize: config.batchSize,
callbacks: config.callback == null ? null : [config.callback]
});
if (config.fineTuningEpochs != null && config.fineTuningEpochs > 0) {
// Fine tuning: unfreeze the second-last dense layer of the base
// model.
const fineTuningHistory = await this.fineTuningUsingTensors(
config, trainXs, trainYs, valData);
return [history, fineTuningHistory];
} else {
return history;
}
} finally {
tf.dispose([xs, ys, trainXs, trainYs, valData]);
}
}
if (outs === null) {
return [];
}
const [boxes, scores, classes] = outs;
const indices = await tf.image.nonMaxSuppressionAsync(boxes, scores, maxBoxes, iouThreshold)
// Pick out data that wasn't filtered out by NMS and put them into
// CPU land to pass back to consumer
const classes_indx_arr = await classes.gather(indices).data();
const keep_scores = await scores.gather(indices).data();
const boxes_arr = await boxes.gather(indices).data();
tf.dispose(outs);
indices.dispose();
const results = [];
classes_indx_arr.forEach((class_indx, i) => {
const classProb = keep_scores[i];
if (classProb < classProbThreshold) {
return;
}
const className = classNames[class_indx];
let [top, left, bottom, right] = [
boxes_arr[4 * i],
boxes_arr[4 * i + 1],
boxes_arr[4 * i + 2],
boxes_arr[4 * i + 3],
// Pre-fill with "NaNs". We use -10 as a NaN value, which will be filtered out in the loss function.
const stateExpectationsTensor = tf.mul(tf.ones([transitions.length, 3]), -10);
// Estimate Q values for resulting states:
const newStateExpectationsTensor = tf.tidy(() => {
const newStates = tf.stack(transitions.map(t => this.stateToTensor(t.newState, t.side)));
return this.model.predict(newStates);
});
// Wait for the computations to be done:
const [stateExpectations, newStateExpectations] = await Promise.all([
stateExpectationsTensor.array(),
newStateExpectationsTensor.array(),
]);
tf.dispose([stateExpectationsTensor, newStateExpectationsTensor]);
for (let i = 0; i < transitions.length; i++) {
const transition = transitions[i];
// Bootstrap the target Q values
const directReward = transition.reward;
const winner = transition.newState && transition.newState.winner;
expectedStateActionValues[i] = stateExpectations[i];
const actionIndex = [-1, 0, 1].indexOf(transition.action);
const nextStateQ = winner ? 0 : Math.max(...newStateExpectations[i]);
const target = directReward + this.gamma * nextStateQ;
expectedStateActionValues[i][actionIndex] = Math.max(-1, Math.min(target, 1));
}
return tf.tensor(expectedStateActionValues);
}
const rewardTensor = tf.tensor1d(batch.map(example => example[2]));
const nextStateTensor = getStateTensor(
batch.map(example => example[4]), this.game.height, this.game.width);
const nextMaxQTensor =
this.targetNetwork.predict(nextStateTensor).max(-1);
const doneMask = tf.scalar(1).sub(
tf.tensor1d(batch.map(example => example[3])).asType('float32'));
const targetQs =
rewardTensor.add(nextMaxQTensor.mul(doneMask).mul(gamma));
return tf.losses.meanSquaredError(targetQs, qs);
});
const grads = tf.variableGrads(lossFunction);
optimizer.applyGradients(grads.grads);
tf.dispose(grads);
// TODO(cais): Return the loss value here?
}
}
if (validationData) {
const valGenerator = validationData();
// Just get all the validation data at once
const valBatch = this._getBatch(valGenerator, Infinity);
const valMetrics = model.evaluate(valBatch.xs, valBatch.ys, { batchSize });
const history = {};
for (let i = 0; i < valMetrics.length; i++) {
const metric = model.metricsNames[i];
history[`validation-${metric}`] = await valMetrics[i].data();
}
this._pushMetrics(history);
tf.dispose(valMetrics);
tf.dispose(valBatch);
}
}
this.props.onTrainEnd(model);
}
const batchedTokens = chunk(uniqueTokens, batchSize);
for (let i = 0; i < batchedTokens.length; i++) {
console.time(
`Converted batch ${i} of ${batchedTokens.length} (${batchSize}): `);
const tokensFromBatch = batchedTokens[i];
const embedding = await use.embed(tokensFromBatch);
const embeddingArr = await embedding.array();
for (let j = 0; j < tokensFromBatch.length; j++) {
serialize.write([tokensFromBatch[j], embeddingArr[j]]);
}
tf.dispose([embedding]);
console.timeEnd(
`Converted batch ${i} of ${batchedTokens.length} (${batchSize}): `);
}
serialize.end();
fs.closeSync(fd);
}
function draw() {
const params = getParams();
console.log('params', params);
const latentSpace = generateLatentSpace(
LATENT_DIMS, params.pointsPerDim, params.start, params.end);
renderLatentSpace(latentSpace);
tf.dispose(latentSpace);
}
async function drawReals() {
const combinedReals = sampleFromMnistData(10);
await tf.browser.toPixels(combinedReals, realCanvas);
tf.dispose(combinedReals);
}
if (shouldFire) {
const freqData = flattenQueue(this.freqDataQueue);
const freqDataTensor = getInputTensorFromFrequencyData(
freqData, [1, this.numFrames, this.columnTruncateLength, 1]);
let timeDataTensor: tf.Tensor;
if (this.includeRawAudio) {
const timeData = flattenQueue(this.timeDataQueue);
timeDataTensor = getInputTensorFromFrequencyData(
timeData, [1, this.numFrames * this.fftSize]);
}
const shouldRest =
await this.spectrogramCallback(freqDataTensor, timeDataTensor);
if (shouldRest) {
this.tracker.suppress();
}
tf.dispose([freqDataTensor, timeDataTensor]);
}
}