Published
Edited
Apr 28, 2018
2 stars
Insert cell
Insert cell
Insert cell
tf = require('@tensorflow/tfjs')
Insert cell
Insert cell
{
const shape = [2,3]; // 2 row, 3 column
const a = tf.tensor([1,2,3,10,20,30],shape)
// return a.toString()
const b = tf.tensor([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]);
// return b.toString();
const c=tf.tensor2d([[1,2,3],[10,20,30]]);
// return c.toString()
const zeros = tf.zeros([3,5])
return zeros.toString();
}
Insert cell
Insert cell
{
const initialValues = tf.zeros([5]);
const biases = tf.variable(initialValues)
const updatedValues = tf.tensor1d([0,1,0,1,0]);
biases.assign(updatedValues);
return biases.toString();
}
Insert cell
Insert cell
//square
{
const d=tf.tensor2d([[1,2],[3,4]]);
const d_squared = d.square();
return d_squared.toString();
}
Insert cell
// Binary operations
{
const e = tf.tensor2d([[1,2],[3,4]]);
const f = tf.tensor2d([[5,6],[7,8]]);
const e_plus_f = e.add(f);
return e_plus_f.toString();
}
Insert cell
// chainable api
{
const e = tf.tensor2d([[1,2],[3,4]]);
const f = tf.tensor2d([[5,6],[7,8]]);
const sq_sum = e.add(f).square();
return sq_sum.toString()
}
Insert cell
Insert cell
{
function predict(input){
// y = a*x^2+b*x+c
return tf.tidy(()=>{
const x = tf.scalar(input); // 2
const ax2 = a.mul(x.square()); // 4 * 2 = 8
const bx = b.mul(x); // 4 * 2 = 8;
const y = ax2.add(bx).add(c); // 8 + 8 + 8 = 24
return y;
});
}
const a = tf.scalar(2);
const b = tf.scalar(4);
const c = tf.scalar(8);
const result = predict(2);
return result.toString()
}
Insert cell
Insert cell
{
const x = tf.tensor2d([[0,2],[4,6]]);
const x_squared = x.square();
x.dispose();
x_squared.dispose();
}
Insert cell
{
const average = tf.tidy(()=>{
const y = tf.tensor1d([1,2,3,4]) ;
const z = tf.ones([4])
return y.sub(z).square().mean();
})
return average.toString();
}
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