Lesson · 40 min · Free
MLP and Python Framework
MLP and Python Framework MLP and Python Framework Welcome to this lesson on Multilayer Perceptrons (MLPs) and their implementation using Python frameworks. As students in pharmacy and biotechnology, you are likely famili
MLP and Python Framework
Welcome to this lesson on Multilayer Perceptrons (MLPs) and their implementation using Python frameworks. As students in pharmacy and biotechnology, you are likely familiar with the complexities of biological data, from genomic sequences and protein structures to drug-target interactions and clinical trial outcomes. Machine learning, particularly deep learning models like MLPs, offers powerful tools for pattern recognition, prediction, and classification within these data sets. A Multilayer Perceptron is a class of feedforward artificial neural network. It consists of at least three layers of nodes: an input layer, one or more hidden layers, and an output layer. Each node, or 'neuron', in one layer connects with a certain weight to every node in the subsequent layer. These weights, along with biases, are adjusted during the 'training' phase to minimize the difference between the network's predictions and the actual target values. The 'multilayer' aspect refers to the presence of one or more hidden layers, which allows MLPs to learn complex, non-linear relationships in the data, a capability crucial for many real-world biological and pharmaceutical problems. The activation function applied at each neuron introduces non-linearity, enabling the network to model more intricate patterns than a simple linear regression. Common activation functions include ReLU (Rectified Linear Unit), sigmoid, and tanh. For our purposes, Python frameworks like TensorFlow and PyTorch have become industry standards for building and training deep learning models due to their extensive libraries, GPU acceleration capabilities, and active communities. These frameworks abstract away much of the low-level mathematical operations, allowing researchers to focus on model architecture and data preparation. Let's consider a simple example: predicting the binding affinity of a compound to a target protein based on its molecular descriptors. This is a regression problem where the MLP would take molecular descriptors as input, process them through hidden layers, and output a predicted binding affinity. Alternatively, for a classification task like predicting whether a drug will be toxic based on its chemical structure, the output layer would typically use a sigmoid activation for binary classification or softmax for multi-class classification. Here's a basic example using TensorFlow/Keras to build a simple MLP. We'll simulate some data for demonstration purposes. import numpy as np import tensorflow as tf from tensorflow import keras from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler # 1. Generate synthetic data (e.g., molecular descriptors and a target affinity) # Let's say 1000 samples, 10 molecular descriptors, and one target value (binding affinity) np.random.seed(42) X = np.random.rand(1000, 10) * 100 # 10 features, values between 0-100 y = (X[:, 0] * 2 + X[:, 1] * 0.5 - X[:, 2] * 3 + np.random.randn(1000) * 5).reshape(-1, 1) # Simple linear relation + noise # 2. Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 3. Standardize the features (important for neural networks) scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # 4. Build the MLP model model = keras.Sequential([ keras.layers.Input(shape=(X_train_scaled.shape[1],)), # Input layer, 10 features keras.layers.Dense(64, activation='relu'), # First hidden layer with 64 neurons, ReLU activation keras.layers.Dense(32, activation='relu'), # Second hidden layer with 32 neurons, ReLU activation keras.layers.Dense(1) # Output layer with 1 neuron for regression (binding affinity) ]) # 5. Compile the model model.compile(optimizer='adam', loss='mse', metrics=['mae']) # Adam optimizer, Mean Squared Error loss, Mean Absolute Error metric # 6. Train the model history = model.fit(X_train_scaled, y_train, epochs=50, batch_size=32, validation_split=0.1, verbose=0) # 7. Evaluate the model loss, mae = model.evaluate(X_test_scaled, y_test, verbose=0) print(f"Test Loss (MSE): {loss:.4f}") print(f"Test MAE: {mae:.4f}") # 8. Make predictions sample_data = np.array([[50, 20, 10, 5, 80, 15, 30, 40, 25, 70]]) sample_data_scaled = scaler.transform(sample_data) prediction = model.predict(sample_data_scaled) print(f"Predicted binding affinity for sample: {prediction[0][0]:.2f}") The above code demonstrates the fundamental steps: data preparation (splitting, scaling), model definition (sequential layers, activation functions), compilation (optimizer, loss function), training, and evaluation. For more advanced applications, you might explore recurrent neural networks (RNNs) for sequential data like protein sequences or convolutional neural networks (CNNs) for image data (e.g., microscopy images). Here's another example illustrating a binary classification task, such as predicting drug toxicity (toxic/non-toxic). import numpy as np import tensorflow as tf from tensorflow import keras from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score # 1. Generate synthetic data for binary classification np.random.seed(42) X_clf = np.random.rand(1000, 8) * 100 # 8 features # Simple non-linear relation for classification y_clf = (np.sin(X_clf[:, 0] / 10) + np.cos(X_clf[:, 1] / 5) + X_clf[:, 2] / 50 > 1.5).astype(int).reshape(-1, 1) # 2. Split and scale data X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X_clf, y_clf, test_size=0.2, random_state=42) scaler_clf = StandardScaler() X_train_clf_scaled = scaler_clf.fit_transform(X_train_clf) X_test_clf_scaled = scaler_clf.transform(X_test_clf) # 3. Build the MLP model for binary classification model_clf = keras.Sequential([ keras.layers.Input(shape=(X_train_clf_scaled.shape[1],)), keras.layers.Dense(64, activation='relu'), keras.layers.Dense(32, activation='relu'), keras.layers.Dense(1, activation='sigmoid') # Output layer with sigmoid for binary classification ]) # 4. Compile the model for binary classification model_clf.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 5. Train the model history_clf = model_clf.fit(X_train_clf_scaled, y_train_clf, epochs=50, batch_size=32, validation_split=0.1, verbose=0) # 6. Evaluate the model loss_clf, accuracy_clf = model_clf.evaluate(X_test_clf_scaled, y_test_clf, verbose=0) print(f"\nTest Loss (Binary Crossentropy): {loss_clf:.4f}") print(f"Test Accuracy: {accuracy_clf:.4f}") # 7. Make predictions and compute classification metrics y_pred_proba_clf = model_clf.predict(X_test_clf_scaled) y_pred_clf = (y_pred_proba_clf > 0.5).astype(int) print(f"Precision: {precision_score(y_test_clf, y_pred_clf):.4f}") print(f"Recall: {recall_score(y_test_clf, y_pred_clf):.4f}") print(f"F1-score: {f1_score(y_test_clf, y_pred_clf):.4f}")
Key Takeaways
MLPs are foundational deep learning models: Capable of learning complex, non-linear relationships in data through multiple layers and activation functions. Python frameworks simplify implementation: TensorFlow and PyTorch provide high-level APIs to define, train, and evaluate neural networks efficiently. Data preprocessing is crucial: Scaling features (e.g., using StandardScaler) is often essential for optimal neural network performance. Task dictates architecture and loss: Regression tasks typically use a linear output layer and Mean Squared Error (MSE) loss, while binary classification uses a sigmoid output and binary cross-entropy loss. Hyperparameter tuning is iterative: The number of layers, neurons per layer, activation functions, optimizer, learning rate, and epochs are all hyperparameters that often need to be tuned for optimal performance. Practice Exercise: Imagine you are working with a dataset of patient gene expression profiles and their response to a particular drug (e.g., 'responder' or 'non-responder'). Using the knowledge gained from this lesson and the provided code examples, outline the steps you would take to build, train, and evaluate an MLP model to predict drug response. Specifically, consider: a) the type of problem (regression/classification), b) suitable activation function for the output layer, c) an appropriate loss function, and d) how you would evaluate the model's performance beyond just accuracy. You do not need to write code, but describe the conceptual steps and choices.
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →