
Implementing a Feedforward Neural Network (FNN) in TensorFlow.js
Learn how to build a Feedforward Neural Network (FNN) using TensorFlow.js, focusing on defining the model architecture, training it, and making predictions.
· tutorials · 2 minutes
Building a Feedforward Neural Network (FNN) in TensorFlow.js
A Feedforward Neural Network (FNN) is one of the most fundamental neural network architectures. It processes data in one direction—from input to output—and is commonly used for tasks like regression and classification. TensorFlow.js makes it easy to create and train FNNs directly in JavaScript for both browser and Node.js applications.
Key Steps in Building an FNN
- Define the Model Architecture
- Compile the Model
- Prepare the Data
- Train the Model
- Evaluate the Model
- Make Predictions
Example: Building an FNN for Binary Classification
This example demonstrates how to build an FNN to predict trading outcomes (win
or lose
) based on the provided trading data.
import * as tf from '@tensorflow/tfjs';
// Step 1: Define the Model Architectureconst model = tf.sequential();model.add(tf.layers.dense({ units: 16, activation: 'relu', inputShape: [5] })); // Hidden layer with 16 neuronsmodel.add(tf.layers.dense({ units: 8, activation: 'relu' })); // Second hidden layermodel.add(tf.layers.dense({ units: 1, activation: 'sigmoid' })); // Output layer for binary classification
// Step 2: Compile the Modelmodel.compile({ optimizer: 'adam', // Adaptive optimizer loss: 'binaryCrossentropy', // Loss function for binary classification metrics: ['accuracy'], // Performance metric});
// Step 3: Prepare the Dataconst rawData = [ { features: [0.24, 0.12, 1.68, -0.08, 7], label: 1 }, // Example: Features from dataset and label (win) { features: [0.05, 0.18, 1.52, -0.1, 7], label: 1 }, { features: [0.02, 0.47, -5.62, -0.1, 7], label: 0 }, // Label (lose)];
// Convert raw data into tensorsconst xs = tf.tensor2d(rawData.map(d => d.features), [rawData.length, 5]); // 5 features per sampleconst ys = tf.tensor2d(rawData.map(d => d.label), [rawData.length, 1]); // Binary labels
// Step 4: Train the Model(async () => { await model.fit(xs, ys, { epochs: 50, // Number of iterations batchSize: 4, // Number of samples per batch verbose: 1, // Display training logs });
// Step 5: Make Predictions const testSample = tf.tensor2d([[0.14, 0.08, 5.04, -0.12, 7]], [1, 5]); // Example test data const prediction = model.predict(testSample); prediction.print(); // Outputs a probability between 0 and 1})();
Key Concepts to Remember
-
Feedforward Architecture:
- Information flows in one direction (input → hidden → output).
-
Activation Functions:
relu
: Introduces non-linearity in hidden layers.sigmoid
: Outputs probabilities in the range [0, 1].
-
Loss Function:
binaryCrossentropy
: Measures how well the model predicts binary outcomes.
-
Optimizer:
adam
: Adapts the learning rate for efficient weight updates.
More posts
-
Exploring Tensor Representation in TensorFlow
Understand how TensorFlow represents data using tensors, and learn about the concepts of rank, shape, and data type that define tensors.
-
Reshaping tensors in Tensorflow TensorFlow.js
Learn the process of reshaping tensors in Tensorflow.js and understand why its useful for machine learning tasks.A simple guides with examples.
-
Implementing a Convolutional Neural Network (CNN) in TensorFlow.js Using Tabular Data
Learn how to implement a convolutional neural network (CNN) in TensorFlow.js using a tabular dataset. This step-by-step guide covers data preprocessing, model architecture, training, and evaluation for binary classification tasks.