Building and Training a Basic Neural Network (Simple Perceptron)
In this chapter, we will walk through the process of building, training, and making predictions using a simple neural network. The code presented demonstrates a basic binary classification task: predicting whether a student will pass or fail based on their study hours and previous test scores.
Flow Chart:
Where:
Linear Separation:
If the dataset is not linearly separable, the perceptron might fail to classify all samples correctly. For more complex tasks, consider using a multi-layer perceptron (MLP) instead.
We begin by importing the necessary classes from the Rubix ML library. These include tools for handling datasets (Labeled and Unlabeled), building neural network layers (Dense and Activation), setting optimization strategies (Adam), applying activation functions (ReLU), and using the MultilayerPerceptron classifier.
Finally, we loop through the predictions and display the results. Each prediction corresponds to whether a student is likely to pass or fail.
Full Code:
Full Code of Example
useRubix\ML\Datasets\Labeled;useRubix\ML\Datasets\Unlabeled;useRubix\ML\NeuralNet\Layers\Dense;useRubix\ML\NeuralNet\Layers\Activation;useRubix\ML\NeuralNet\Optimizers\Adam;useRubix\ML\NeuralNet\ActivationFunctions\ReLU;useRubix\ML\Classifiers\MultilayerPerceptron;// Create a simple dataset for binary classification// Example: Predict if a student will pass (1) or fail (0) based on study hours and previous test score$samples = [ [2,65], [1,45], [8,76], [4,75], [7,90], [3,55], [6,78], [5,80], [8,85], [7,88],];$labels = ['fail','fail','pass','pass','pass','fail','pass','pass','pass','pass'];// Create a labeled dataset$dataset =newLabeled($samples, $labels);// Initialize neural network with no hidden layers (simple perceptron)$estimator =newMultilayerPerceptron([newDense(1),// Output layer with single neuronnewActivation(newReLU()),// ReLU activation function],100,// Maximum number of epochsnewAdam(0.01) // Learning rate of 0.01);// Train the network$estimator->train($dataset);// Make predictions using Unlabeled dataset$testSamples = [ [6,82],// New student: 6 hours study, 82% previous score [1,50],// New student: 1 hour study, 50% previous score];// Create an unlabeled dataset for predictions$testDataset =newUnlabeled($testSamples);// Make predictions$predictions = $estimator->predict($testDataset);// Output predictionsforeach ($predictions as $index => $prediction) {echo"Student ". ($index +1) ." prediction: ". $prediction .PHP_EOL;}
From this chapter, we learned how to build and train a simple perceptron for binary classification tasks, such as predicting student performance based on study hours and test scores. We explored dataset preparation, neural network design using layers and activation functions, and the training process with optimization techniques like Adam. We also discovered the perceptron’s limitation with non-linearly separable data and the potential need for multi-layer perceptrons for handling complex scenarios.
Output=step(w1x1+w2x2+b)
x1 = study hours w1,w2 = weights
x2 = previous score b = bias term