How to build a simple neural network using Keras
Neural networks, which draw inspiration from the structure and functioning of the human brain, are immensely powerful machine learning tools. They find extensive application in tasks like image classification, natural language processing, and time series analysis. Python's Keras library serves as a high-level neural network API, simplifying the process of constructing and training neural networks.
In this Answer, we will walk through the process of building a simple neural network using Keras.
Step 1: Install the required libraries
First, we have to make sure that the necessary libraries are installed. We'll need NumPy and Keras. Open a terminal or command prompt and run the following commands:
pip install numpypip install keras
Step 2: Import libraries
To start building our neural network, we need to import the required libraries. Open any Python editor and create a new file. Import the following libraries:
import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense
Step 3: Prepare the dataset
Next, we need to prepare the dataset for training our neural network. In this example, we'll generate some dummy data for training. Let's generate the data:
# Generate some dummy data for trainingx_train_data = np.random.random((1000, 10))y_train_data = np.random.randint(2, size=(1000, 1))
Step 4: Build the model
Now, we can build our neural network model. In this example, we'll create a simple feedforward neural network having one hidden layer. Add the following code:
# Building the modelmodel = Sequential()model.add(Dense(10, activation='relu', input_dim=10))model.add(Dense(1, activation='sigmoid'))
Step 5: Compile the model
After creating the model, we need to compile it before training. During compilation, we specify the loss function, optimizer, and evaluation metric. Add the following code:
# Compiling the modelmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Step 6: Train the model
With the model compiled, we can now train it using the prepared dataset. Add the following code:
# Train the modelmodel.fit(x_train_data, y_train_data, epochs=20, batch_size=10)
Step 7: Generate test data
After training the model, we need to generate some dummy test data to evaluate the model. Let's generate the dummy test data by adding the following code:
# Generate some dummy test datax_test_data = np.random.random((100, 10))y_test_data = np.random.randint(2, size=(100, 1))
Step 8: Evaluate the model
To evaluate the performance of our model, we can evaluate it on the test dataset. Add the following code:
# Evaluating the model on the test dataloss, accuracy = model.evaluate(x_test_data, y_test_data)print('Test model loss:', loss)print('Test model accuracy:', accuracy)
Run the neural network
import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense# Generate some dummy data for trainingx_train_data = np.random.random((1000, 10))y_train_data = np.random.randint(2, size=(1000, 1))# Building the modelmodel = Sequential()model.add(Dense(10, activation='relu', input_dim=10))model.add(Dense(1, activation='sigmoid'))# Compiling the modelmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])# Train the modelmodel.fit(x_train_data, y_train_data, epochs=20, batch_size=10)# Generate some dummy test datax_test_data = np.random.random((100, 10))y_test_data = np.random.randint(2, size=(100, 1))# Evaluating the model on the test dataloss, accuracy = model.evaluate(x_test_data, y_test_data)print('Test model loss:', loss)print('Test model accuracy:', accuracy)
Code explanation
Here’s the line-by-line code explanation:
Lines 1–3: Import the necessary libraries.
numpyis imported asnpfor convenience and theSequentialandDenseclasses are imported fromkeras.modelsandkeras.layers, respectively.Lines 6–7: Generate dummy data for training.
x_train_datais a 2D array of shape(1000, 10)with random values between 0 and 1, andy_train_datais a 2D array of shape(1000, 1)with random integer values (0 or 1).Line 10: Create a sequential model using
Sequential(). This represents a linear stack of layers.Line 11: Adding a
Denselayer to the model with 10 units/neurons. The activation function used is Rectified Linear Unit (relu).input_dim=10specifies that the input to this layer has 10 dimensions.Line 12: Add another
Denselayer withsigmoidactivation and a single unit. This is the output layer of the network.Line 15: Compile the model. Here, we specify the optimizer as
adam, which is a popular optimization algorithm. The loss function is set tobinary_crossentropysince we have a binary classification problem.accuracyis used as a training metric to monitor the performance of neural networks.Line 18: Train the model using
fit(). This is where the actual training happens. We pass inx_trainandy_trainas the training data, setepochs=10to train for 10 iterations over the entire dataset, and use a batch size of 10.Lines 21–22: Generate dummy test data similar to the training data.
Lines 25–27: Evaluate the model on the test data using
evaluate(). The test loss and accuracy are computed and printed to the console.
We've successfully built and trained a simple neural network using Keras. Feel free to modify the code and experiment with different architectures, datasets, and hyperparameters to enhance the understanding of neural networks.
Free Resources