Showing posts with label Example 1. Show all posts
Showing posts with label Example 1. Show all posts

Saturday, December 21, 2024

Example 1 Training

 

Training Function

async function trainModel(model, inputs, labels, surface) {
  const batchSize = 25;
  const epochs = 100;
  const callbacks = tfvis.show.fitCallbacks(surface, ['loss'], {callbacks:['onEpochEnd']})
  return await model.fit(inputs, labels,
    {batchSize, epochs, shuffle:true, callbacks:callbacks}
  );
}

epochs defines how many iterations (loops) the model will do.

model.fit is the function that runs the loops.

callbacks defines the callback function to call when the model wants to redraw the graphics.


Test the Model

When a model is trained, it is important to test and evaluate it.

We do this by inspecting what the model predicts for a range of different inputs.

But, before we can do that, we have to un-normalize the data:

Un Normalize

let unX = tf.linspace(01100);
let unY = model.predict(unX.reshape([1001]));

const unNormunX = unX.mul(inputMax.sub(inputMin)).add(inputMin);
const unNormunY = unY.mul(labelMax.sub(labelMin)).add(labelMin);

unX = unNormunX.dataSync();
unY = unNormunY.dataSync();

Then we can look at the result:

Plot the Result

const predicted = Array.from(unX).map((val, i) => {
return {x: val, y: unY[i]}
});

// Plot the Result
tfPlot([values, predicted], surface1)

Example 1 Model

 

Shuffle Data

Always shuffle data before training.

When a model is trained, the data is divided into small sets (batches). Each batch is then fed to the model. Shuffling is important to prevent the model getting the same data over again. If using the same data twice, the model will not be able to generalize the data and give the right output. Shuffling gives a better variety of data in each batch.

Example

tf.util.shuffle(data);

TensorFlow Tensors

To use TensorFlow, input data needs to be converted to tensor data:

// Map x values to Tensor inputs
const inputs = values.map(obj => obj.x);
// Map y values to Tensor labels
const labels = values.map(obj => obj.y);

// Convert inputs and labels to 2d tensors
const inputTensor = tf.tensor2d(inputs, [inputs.length1]);
const labelTensor = tf.tensor2d(labels, [labels.length1]);

Data Normalization

Data should be normalized before being used in a neural network.

A range of 0 - 1 using min-max are often best for numerical data:

const inputMin = inputTensor.min();
const inputMax = inputTensor.max();
const labelMin = labelTensor.min();
const labelMax = labelTensor.max();
const nmInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
const nmLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin))

Tensorflow Model

Machine Learning Model is an algorithm that produces output from input.

This example uses 3 lines to define a ML Model:

const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [1], units: 1, useBias: true}));
model.add(tf.layers.dense({units: 1, useBias: true}));

Sequential ML Model

const model = tf.sequential(); creates a Sequential ML Model.

In a sequential model, the input flows directly to the output. Other models can have multiple inputs and multiple outputs. Sequential is the easiest ML model. It allows you to build a model layer by layer, with weights that correspond to the next layer.

TensorFlow Layers

model.add() is used to add two layers to the model.

tf.layer.dense is a layer type that works in most cases. It multiplies its inputs by a weight-matrix and adds a number (bias) to the result.

Shapes and Units

inputShape: [1] because we have 1 input (x = horsepower).

units: 1 defines the size of the weight matrix: 1 weight for each input (x value).


Compiling a Model

Compile the model with a specified optimizer and loss function:

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

The compiler is set to use the sgd optimizer. It is simple to use and quite effective.

meanSquaredError is the function we want to use to compare model predictions and true values.

Example 1 Data

 

TensorFlow Data Collection

The data used in Example 1, is a list of car objects like this:

{
  "Name""chevrolet chevelle malibu",
  "Miles_per_Gallon"18,
  "Cylinders"8,
  "Displacement"307,
  "Horsepower"130,
  "Weight_in_lbs"3504,
  "Acceleration"12,
  "Year""1970-01-01",
  "Origin""USA"
},
{
  "Name""buick skylark 320",
  "Miles_per_Gallon"15,
  "Cylinders"8,
  "Displacement"350,
  "Horsepower"165,
  "Weight_in_lbs"3693,
  "Acceleration"11.5,
  "Year""1970-01-01",
  "Origin""USA"
},

The dataset is a JSON file stored at:

https://storage.googleapis.com/tfjs-tutorials/carsData.json


Cleaning Data

When preparing for machine learning, it is always important to:

  • Remove the data you don't need
  • Clean the data from errors

Remove Data

A smart way to remove unnecessary data, it to extract only the data you need.

This can be done by iterating (looping over) your data with a map function.

The function below takes an object and returns only x and y from the object's Horsepower and Miles_per_Gallon properties:

function extractData(obj) {
  return {x:obj.Horsepower, y:obj.Miles_per_Gallon};
}

TensorFlow Example 1

 Here is a basic example of using TensorFlow for creating a simple neural network to classify the famous MNIST dataset of handwritten digits. This will provide a basic introduction to TensorFlow and how to work with it.

Step 1: Install TensorFlow

If you haven't installed TensorFlow yet, you can install it using pip:

pip install tensorflow

Step 2: Import Necessary Libraries

import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
import matplotlib.pyplot as plt

Step 3: Load and Preprocess the Data

MNIST is a dataset of 60,000 training images and 10,000 test images of handwritten digits (0–9). We will use TensorFlow's keras API to load it.

# Load the MNIST dataset
mnist = tf.keras.datasets.mnist

# Split the dataset into training and test data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Normalize the image data to values between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

# The images are 28x28 pixels, and we need to reshape them to (28, 28, 1) to fit the CNN input format
train_images = train_images.reshape((train_images.shape[0], 28, 28, 1))
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1))

# Check the shape of the data
print("Train data shape:", train_images.shape)
print("Test data shape:", test_images.shape)

Step 4: Build the Neural Network Model

In this example, we'll create a simple Convolutional Neural Network (CNN) model to classify the digits.

# Build the CNN model
model = models.Sequential()

# Add a convolutional layer with 32 filters and a 3x3 kernel
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))

# Add a second convolutional layer
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

# Flatten the output of the previous layer
model.add(layers.Flatten())

# Add a fully connected layer with 64 units
model.add(layers.Dense(64, activation='relu'))

# Output layer with 10 units (for 10 classes: digits 0–9) and softmax activation
model.add(layers.Dense(10, activation='softmax'))

# Display the model summary
model.summary()

Step 5: Compile the Model

Now, we'll compile the model by specifying the optimizer, loss function, and evaluation metric.

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Step 6: Train the Model

Now, we will train the model using the training data.

# Train the model
history = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)

Step 7: Evaluate the Model

After training, we can evaluate the model on the test set to see how well it performs.

# Evaluate the model on the test data
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc:.4f}")

Step 8: Make Predictions (Optional)

You can make predictions using the trained model and visualize the results.

# Make predictions on the test set
predictions = model.predict(test_images)

# Visualize the first test image and its predicted label
plt.imshow(test_images[0].reshape(28, 28), cmap='gray')
plt.title(f"Predicted Label: {np.argmax(predictions[0])}, Actual Label: {test_labels[0]}")
plt.show()

Conclusion:

In this example, you have:

  • Loaded and preprocessed the MNIST dataset.
  • Built a simple CNN model using tensorflow.keras.
  • Compiled and trained the model on the training data.
  • Evaluated its performance on the test data.
  • Made predictions and visualized results.

You can experiment with different architectures, add dropout layers, change hyperparameters, or try using other datasets. TensorFlow is flexible and allows you to easily extend this simple example for more complex problems.

How will AI transform your life in the next 5 years?

 AI is already transforming how we live and work, and over the next 5 years, this transformation is expected to accelerate in several key ar...