Lesson · 40 min · Free
Build Your First Neural Network with Keras
Build Your First Neural Network with Keras body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto
AI & Machine Learning Foundations
Build Your First Neural Network with Keras
Welcome to this foundational lesson on building your first neural network! As future innovators in pharmacy and biotechnology, understanding neural networks is becoming increasingly critical. From predicting drug-target interactions to analyzing genomic data for disease biomarkers, these powerful algorithms are transforming how we approach scientific discovery and patient care. In this lesson, we'll demystify the process by using Keras, a user-friendly neural network API written in Python, which runs on top of TensorFlow. At its core, a neural network is a series of algorithms that endeavors to recognize underlying relationships in a set of data through a process that mimics the way the human brain operates. It consists of interconnected 'neurons' organized in layers: an input layer, one or more hidden layers, and an output layer. Each connection has a 'weight' associated with it, and each neuron has an 'activation function' that determines its output based on the weighted sum of its inputs. Keras simplifies the creation of neural networks significantly. We'll start with a simple, fully connected (dense) neural network, often called a Multi-Layer Perceptron (MLP). For this example, we'll imagine a hypothetical dataset where we're trying to predict a binary outcome (e.g., whether a compound is active or inactive against a specific target) based on a few numerical features (e.g., molecular descriptors like logP, molecular weight, etc.).
Setting up Keras and Your First Model
First, ensure you have TensorFlow and Keras installed. If not, you can install them via pip: pip install tensorflow keras . Keras is now integrated directly into TensorFlow, so you'll usually import it as tensorflow.keras . Let's define a simple sequential model. A sequential model is a linear stack of layers. We'll use Dense layers, which are standard fully connected neural network layers. import numpy as np from tensorflow import keras from tensorflow.keras import layers # 1. Prepare your data (synthetic example for demonstration) # Imagine 100 samples, each with 5 features, and a binary outcome X_train = np.random.rand(100, 5) # 100 samples, 5 features y_train = np.random.randint(0, 2, 100) # 100 binary outcomes (0 or 1) # 2. Define the model architecture model = keras.Sequential([ # Input layer: 5 features, 16 neurons in the first hidden layer, ReLU activation layers.Dense(16, activation='relu', input_shape=(5,)), # Hidden layer: 8 neurons, ReLU activation layers.Dense(8, activation='relu'), # Output layer: 1 neuron for binary classification, sigmoid activation layers.Dense(1, activation='sigmoid') ]) # 3. Compile the model # optimizer: how the model updates weights based on loss # loss: function to quantify error between predicted and actual values # metrics: what to monitor during training model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Display a summary of the model's layers model.summary() In the code above: layers.Dense(16, activation='relu', input_shape=(5,)) : This is our first hidden layer. It has 16 neurons. activation='relu' (Rectified Linear Unit) is a common activation function that helps the network learn complex patterns. input_shape=(5,) tells Keras that our input data has 5 features per sample. layers.Dense(8, activation='relu') : Another hidden layer with 8 neurons and ReLU activation. layers.Dense(1, activation='sigmoid') : This is the output layer. For binary classification (predicting 0 or 1), a single neuron with a sigmoid activation function is standard. The sigmoid function squashes the output to a value between 0 and 1, which can be interpreted as a probability. model.compile(...) : Before training, we need to configure the learning process. optimizer='adam' is an efficient algorithm for optimizing the network's weights. loss='binary_crossentropy' is the appropriate loss function for binary classification problems. metrics=['accuracy'] tells us to monitor the accuracy during training.
Training Your Model
Once the model is defined and compiled, the next step is to train it using your data. This is where the model learns the patterns. The model.fit() method handles this process. # 4. Train the model # epochs: number of times the model will iterate over the entire training dataset # batch_size: number of samples per gradient update print("\nTraining the model...") history = model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=1) print("\nModel training complete. Evaluation metrics:") print(f"Final training accuracy: {history.history['accuracy'][-1]:.4f}") print(f"Final training loss: {history.history['loss'][-1]:.4f}") # 5. Make predictions (on new, unseen data) X_new = np.random.rand(5, 5) # 5 new samples with 5 features each predictions = model.predict(X_new) print("\nPredictions for new data:") for i, pred in enumerate(predictions): print(f"Sample {i+1}: Probability of being active = {pred[0]:.4f}") if pred[0] > 0.5: print(f" -> Predicted: Active (1)") else: print(f" -> Predicted: Inactive (0)") In this training phase: epochs=10 : The model will go through the entire training dataset 10 times. Each pass is called an epoch. batch_size=32 : The training data will be divided into chunks of 32 samples. The model's weights will be updated after processing each batch. verbose=1 : Displays a progress bar during training. After training, we also demonstrate how to use the trained model to make predictions on new, unseen data ( X_new ). The output of the sigmoid activation is a probability, so we typically apply a threshold (e.g., 0.5) to classify it into 0 or 1.
Key Takeaways
Neural networks are powerful tools for pattern recognition, with applications across pharmacy and biotech. Keras provides a high-level, user-friendly API for building and training neural networks. A sequential model is a linear stack of layers, commonly used for simple network architectures. Dense layers are fully connected layers where every neuron in one layer is connected to every neuron in the next. Activation functions (like ReLU and Sigmoid) introduce non-linearity, allowing the network to learn complex relationships. Model compilation involves defining the optimizer , loss function, and metrics . Training ( model.fit() ) involves iterating over the data multiple epochs , updating weights in batches .
Practice Exercise
Modify the provided code to build a neural network for a slightly different scenario. Imagine you are predicting the binding affinity (a continuous numerical value, not binary) of a compound. Change the output layer to reflect this: use a single Dense neuron with no activation function (or activation='linear' , which is the default) and change the loss function to 'mean_squared_error' (a common choice for regression tasks). Keep the input features and hidden layers the same for simplicity. Train this modified model and observe the output.
Watch the full lesson — free
This topic is part of AI & Machine Learning Foundations, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →