Here's a template for a README.md
file that explains the steps involved in building, training, and evaluating a neural network model on the Iris dataset using TensorFlow/Keras.
This project demonstrates how to build, compile, and train a neural network model on the Iris dataset using TensorFlow and Keras. The model is designed to classify the Iris flowers into three species based on four input features (sepal length, sepal width, petal length, and petal width).
The Iris dataset is a famous dataset from the UCI Machine Learning Repository that is commonly used for classification tasks. In this project, a neural network model is constructed using the Keras Sequential API. The model is trained on the Iris dataset and evaluated for its performance.
The goal is to predict the species of Iris flowers from the input features. The three species are:
- Setosa
- Versicolor
- Virginica
The project includes the following key steps:
- Loading and splitting the dataset.
- Preprocessing the data.
- Building and compiling the neural network model.
- Training the model with the dataset.
- Evaluating the model's performance.
To run this project, you need the following Python libraries:
- TensorFlow: A deep learning framework for building and training neural networks.
- Numpy: For numerical operations.
- Matplotlib: For data visualization (optional).
- Scikit-learn: For dataset loading, splitting, and preprocessing.
You can install these dependencies using pip:
pip install tensorflow numpy matplotlib scikit-learn
The model is built using the Keras Sequential API, consisting of 10 layers:
- Input layer: 64 units with He uniform initialization and biases set to 1.
- Hidden layers:
- Four layers with 128 units each and ReLU activation.
- Four layers with 64 units each and ReLU activation.
- Output layer: 3 units with softmax activation (for multi-class classification).
- Optimizer: Adam optimizer.
- Loss function: Categorical crossentropy, suitable for multi-class classification.
- Metrics: Accuracy.
- He Uniform initializer: Used for the weights of the first layer.
- Bias initializer: Set to 1 for the first layer.
- ReLU activation: Used for all hidden layers.
- Softmax activation: Used in the output layer to output probabilities for each class.
-
Load the Iris Dataset:
- The dataset is loaded from
sklearn.datasets.load_iris()
.
- The dataset is loaded from
-
Split the Data:
- The dataset is split into training (90%) and test (10%) sets using
train_test_split
from Scikit-learn.
- The dataset is split into training (90%) and test (10%) sets using
-
Preprocess the Data:
- Features are scaled using
StandardScaler()
to normalize them before training.
- Features are scaled using
-
Build the Model:
- The neural network is created using the Keras
Sequential
API. - The first layer uses a He uniform initializer for weights and sets biases to 1.
- The neural network is created using the Keras
-
Compile the Model:
- The model is compiled with the Adam optimizer, categorical cross-entropy loss, and accuracy as the metric.
-
Train the Model:
- The model is trained on the training data for 100 epochs, using early stopping to prevent overfitting.
-
Evaluate the Model:
- The model’s performance is evaluated on the test set, and the test accuracy is displayed.
# Load the dataset
from sklearn import datasets
iris = datasets.load_iris()
# Split the data
train_data, test_data, train_targets, test_targets = read_in_and_split_data(iris)
# Build the model
model = get_model(input_shape=(4,))
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(train_data, train_targets, epochs=100, batch_size=32, validation_data=(test_data, test_targets))
# Evaluate the model
test_loss, test_accuracy = model.evaluate(test_data, test_targets)
print(f"Test Accuracy: {test_accuracy:.4f}")
This project is licensed under the MIT License - see the LICENSE file for details.
This README.md
file provides an overview of the project, explains the neural network model, and gives clear instructions on how to run the code and install dependencies. It also includes a small code snippet showing how to train and evaluate the model.