Public
Edited
Oct 26, 2022
Fork of Tensors
Insert cell
Insert cell
`The TF.js version used in this notebook is: ${tf.version.tfjs}`
Insert cell
Insert cell
myTensor = tf.tensor([1, 2, 3, 10, 20, 30], [2, 3], 'int32')
Insert cell
Insert cell
Insert cell
Insert cell
myT=tf.tensor([1,2,3,4,5,6],[2,3])
Insert cell
Insert cell
myA = myT.array()

Insert cell
myA[0][0]=9
Insert cell
myA
Insert cell
myT.toString()
Insert cell
myT.print()
Insert cell
Insert cell
myTensor.toString()
Insert cell
Insert cell
`
This tensor's dimension is: ${myTensor.rank}.
The shape of this tensor is: [${myTensor.shape}].
The number of elements in this tensor is: ${myTensor.size}.
The data type of this tensor is: ${myTensor.dtype}.
`
Insert cell
Insert cell
myTensor.array()
Insert cell
myTensor.data()
Insert cell
Insert cell
tf.memory()
Insert cell
Insert cell
{
let numTensorsStart = tf.memory().numTensors;
let numTensorsInsideTidy, numTensorsOutsideTidy, numTensorsClean, numTensorsOrphaned;
let toKeep, toReturn, toCollect, toBatman;

toBatman = tf.tensor([1, 2, 3]);
tf.tidy(() => {
toKeep = tf.tensor([4, 5, 6]);
toReturn = tf.tensor([7, 8, 9]);
toCollect = tf.tensor([10, 11, 12]);

numTensorsInsideTidy = tf.memory().numTensors - numTensorsStart;
tf.keep(toKeep);
return toReturn;
});
numTensorsOutsideTidy = tf.memory().numTensors - numTensorsStart;

toKeep.dispose();
toReturn.dispose();
numTensorsClean = tf.memory().numTensors - numTensorsStart;
toBatman = toBatman.asType('int32'); // original Float32 is orphaned; source of memory leak
toBatman.dispose();
numTensorsOrphaned = tf.memory().numTensors - numTensorsStart;
return `
After all four tensors were created, the number of new tensors inside tidy() was: ${numTensorsInsideTidy}.
After tidy() garbage collected the toCollect tensor, the number of tensors just outside tidy() was: ${numTensorsOutsideTidy}.
After disposing off toKeep and toReturn, the number of tensors was: ${numTensorsClean}.
After orphaning toBatman, the number of tensors was: ${numTensorsOrphaned}.
`
}
Insert cell
Insert cell
myTensorBuffer = myTensor.clone().buffer()
Insert cell
Insert cell
myTensorBuffer.get(0, 2)
Insert cell
Insert cell
myTensorBuffer.set(999, 0, 0)
Insert cell
Insert cell
myModifiedTensor = myTensorBuffer.toTensor()
Insert cell
myModifiedTensor.toString()
Insert cell
Insert cell
numHoursStudied = [0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50,
2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.50]
Insert cell
Insert cell
studentPassedExam = [0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1]
Insert cell
Insert cell
predictor = tf.tensor(numHoursStudied, [numHoursStudied.length, 1])
Insert cell
predictor.shape
Insert cell
predictor.toString()
Insert cell
Insert cell
ones = tf.onesLike(predictor)
Insert cell
ones.toString()
Insert cell
ones.shape
Insert cell
Insert cell
x = tf.concat([ones, predictor], 1)
Insert cell
x.toString()
Insert cell
x.shape
Insert cell
Insert cell
betas = [-4.1, 1.5]
Insert cell
Insert cell
b = tf.tensor(betas, [1, betas.length])
Insert cell
b.toString()
Insert cell
b.shape
Insert cell
Insert cell
logits = tf.matMul(x, b.transpose())
Insert cell
logits.toString()
Insert cell
Insert cell
yPred = tf.sigmoid(logits)
Insert cell
Insert cell
yPred.toString()
Insert cell
Insert cell
predictedOutcomes = yPred.data()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xRange = tf.linspace(0, 6, 100).reshape([100, 1])
Insert cell
Insert cell
xExtra = tf.concat([tf.onesLike(xRange), xRange], 1)
Insert cell
Insert cell
yPredExtra = tf.sigmoid(tf.matMul(xExtra, b.transpose()))
Insert cell
Insert cell
xRangeArray = xRange.data()
Insert cell
yPredExtraArray = yPredExtra.data()
Insert cell
Insert cell
Insert cell
Insert cell
tf = require('@tensorflow/tfjs@4.0.0/dist/tf.min.js')
Insert cell
Plotly = require("https://cdn.plot.ly/plotly-2.14.0.min.js")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more