Lesson · 40 min · Free
Build First NN with Keras
Build First NN with Keras Build First NN with Keras Welcome to the "AI in Drug Discovery" course! In this lesson, we'll take our first practical steps into the world of neural networks by building a simple one using Kera
Build First NN with Keras
Welcome to the "AI in Drug Discovery" course! In this lesson, we'll take our first practical steps into the world of neural networks by building a simple one using Keras. Keras is a high-level API for building and training deep learning models, known for its user-friendliness and modularity. It runs on top of more powerful backend engines like TensorFlow, abstracting away much of the complexity, making it an excellent choice for beginners and rapid prototyping. As pharmacy and biotech students, you're familiar with complex biological systems and data. Neural networks offer a powerful tool for pattern recognition, prediction, and classification within these domains. While we won't be tackling a drug discovery problem just yet, understanding the fundamentals of building a neural network is crucial. We'll start with a classic "hello world" of machine learning: classifying data that can be linearly separated, or a slightly more complex variant, to illustrate the core components. Before we dive into the code, let's briefly review the essential components of a neural network we'll be using: Sequential Model: The simplest type of Keras model, a linear stack of layers. Dense Layer: A fully connected neural network layer. Each neuron in a dense layer receives input from all neurons in the previous layer. Activation Function: A non-linear function applied to the output of a neuron. Common choices include ReLU (Rectified Linear Unit) for hidden layers and Sigmoid or Softmax for output layers depending on the task. Optimizer: An algorithm used to adjust the weights of the network during training to minimize the loss function. Adam is a popular and effective choice. Loss Function: A measure of how well the model is performing. For binary classification, binary cross-entropy is often used.
Setting Up and Building Your First Model
To begin, you'll need to have Python and TensorFlow (which includes Keras) installed. If you haven't already, you can install them using pip: pip install tensorflow Now, let's write our first Keras model. We'll create a simple binary classification model. Imagine we have a dataset where we want to classify whether a compound is active or inactive based on two simple features. For demonstration, we'll generate some synthetic data. import numpy as np from tensorflow import keras from tensorflow.keras import layers # 1. Generate Synthetic Data # Let's create a simple dataset for binary classification # X will have two features, y will be 0 or 1 np.random.seed(42) X = np.random.rand(100, 2) * 10 # 100 samples, 2 features, values between 0 and 10 y = (X[:, 0] + X[:, 1] > 10).astype(int) # Simple classification rule: if sum of features > 10, then class 1, else class 0 # Split data into training and testing sets (a good practice) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) print(f"X_train shape: {X_train.shape}") print(f"y_train shape: {y_train.shape}") print(f"X_test shape: {X_test.shape}") print(f"y_test shape: {y_test.shape}") # 2. Build the Neural Network Model model = keras.Sequential([ # Input layer: Specify input_shape for the first layer # Our data has 2 features, so input_shape=(2,) layers.Dense(units=4, activation='relu', input_shape=(2,)), # Hidden layer with 4 neurons, ReLU activation layers.Dense(units=1, activation='sigmoid') # Output layer with 1 neuron for binary classification, Sigmoid activation ]) # 3. Compile the Model # Configure the learning process model.compile(optimizer='adam', loss='binary_crossentropy', # Appropriate for binary classification metrics=['accuracy']) # Metric to monitor during training # Print a summary of the model's architecture model.summary() # 4. Train the Model print("\nTraining the model...") history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.1, verbose=0) # epochs: number of times the model will go through the entire training dataset # batch_size: number of samples per gradient update # validation_split: percentage of training data to use for validation during training # 5. Evaluate the Model print("\nEvaluating the model on test data...") loss, accuracy = model.evaluate(X_test, y_test, verbose=0) print(f"Test Loss: {loss:.4f}") print(f"Test Accuracy: {accuracy:.4f}") # 6. Make Predictions print("\nMaking predictions on new data...") new_data = np.array([[1.0, 2.0], [8.0, 9.0], [3.0, 3.0]]) predictions = model.predict(new_data) print(f"Raw predictions: {predictions.flatten()}") # Convert probabilities to class labels (0 or 1) predicted_classes = (predictions > 0.5).astype(int).flatten() print(f"Predicted classes for new data: {predicted_classes}") # Expected: [0, 1, 0] based on our simple rule (sum > 10) Let's break down the code: Data Generation: We create X with two features and y as a binary label. This simulates a very simple scenario where we want to classify based on a combination of two measurements. Model Definition: We use keras.Sequential to stack our layers. The first Dense layer is our hidden layer. It has 4 neurons ( units=4 ) and uses the 'relu' activation function. Crucially, we specify input_shape=(2,) because each sample in our input data X has 2 features. The second Dense layer is the output layer. It has 1 neuron ( units=1 ) because we are performing binary classification (predicting 0 or 1). The 'sigmoid' activation function is perfect for this, as it squashes the output to a value between 0 and 1, which can be interpreted as a probability. Compilation: Before training, the model needs to be compiled. optimizer='adam' : Adam is an adaptive learning rate optimization algorithm that's generally a good default choice. loss='binary_crossentropy' : This is the standard loss function for binary classification problems. It measures the difference between the predicted probabilities and the true labels. metrics=['accuracy'] : We tell Keras to also track accuracy during training and evaluation, which is more intuitive for humans than loss. Training (Fitting): The model.fit() method starts the training process. X_train, y_train : Our training data and their corresponding labels. epochs=50 : The model will iterate over the entire training dataset 50 times. batch_size=32 : The training data is divided into batches of 32 samples. The model's weights are updated after processing each batch. validation_split=0.1 : 10% of the training data is held out to monitor performance on unseen data during training, helping detect overfitting. Evaluation: After training, we evaluate the model's performance on the completely unseen X_test and y_test data using model.evaluate() . Prediction: Finally, we demonstrate how to use the trained model to make predictions on new, unseen data. The output is a probability, which we then convert to a binary class by thresholding at 0.5. This simple example demonstrates the full lifecycle of building, training, and evaluating a neural network in Keras. While the data is synthetic, the principles apply directly to real-world datasets in drug discovery, such as predicting compound activity, toxicity, or even classifying images of cells.
Key Takeaways
Keras provides a high-level, user-friendly API for building neural networks. A Sequential model is a linear stack of layers. Dense layers are fully connected and require input_shape for the first layer. ReLU is a common activation for hidden layers; Sigmoid is used for binary classification output layers. Models are compiled with an optimizer , loss function , and metrics . model.fit() trains the model, while model.evaluate() assesses its performance on new data. model.predict() generates outputs (probabilities for classification) for new inputs.
Practice Exercise
Modify the provided code to: Add another Dense hidden layer to the model. Experiment with different numbers of neurons (e.g., 8, 16). Change the activation function of the first hidden layer from 'relu' to 'tanh'. Increase the number of epochs to 100 and observe if the test accuracy improves or degrades. Generate a slightly more complex synthetic dataset where y = (X[:, 0] * X[:, 1] > 20).astype(int) . Retrain your modified model and see how it performs on this new classification task. Document your observations regarding the changes in accuracy and loss. This hands-on modification will solidify your understanding of how different architectural choices and training parameters affect model performance.
Watch the full lesson — free
This topic is part of AI in Drug Discovery, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →