TensorFlow.js Models (Cont.)


Collecting Data
Create a tensor (xs) with 5 x values:

const xs = tf.tensor( [0, 1, 2, 3, 4] );

Create a tensor (ys) with 5 correct y answers (multiply xs with 1.2 and add 5):

const ys = xs.mul( 1.2 ).add( 5 );

Creating a Model
Create a sequential model, where the output from one layer is the input to the next layer:

const model = tf.sequential( );

Adding a Layer
Add one dense layer to the model. In a dense layer, every node is connected to every node in the preceding layer. The layer is only one unit (tensor) and the shape is 1 (one dimentional):

model.add( tf.layers.dense( { units:1, inputShape:[1] } ) );

Compiling the Model
Compile the model using meanSquaredError as loss function and sgd (stochastic gradient descent) as optimizer function:

model.compile( { loss:'meanSquaredError', optimizer:'sgd' } );

An Example of Tensorflow Projects

     




      Knowledge is knowing a tomato is a fruit.    
      Wisdom is not putting it in a fruit salad.