Lesson · 40 min · Free
Matrix Multiplication in PyTorch
Matrix Multiplication in PyTorch body { font-family: sans-serif; line-height: 1.6; color: #333; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code {
Matrix Multiplication in PyTorch
Welcome to this lesson on Matrix Multiplication in PyTorch, a fundamental operation that underpins nearly all modern AI and deep learning models, especially those used in drug discovery. Understanding how to perform and interpret matrix multiplication is crucial for building and optimizing neural networks that can analyze complex biological data, predict drug-target interactions, or simulate molecular dynamics. In the context of AI in drug discovery, matrices often represent various forms of data. For instance, a matrix might store gene expression levels across different cell lines, where rows are genes and columns are samples. Another matrix could represent the chemical properties of a library of compounds, with rows as compounds and columns as specific descriptors. Neural networks process these numerical representations through layers of matrix multiplications, transforming raw input data into meaningful outputs, like predicted efficacy or toxicity.
Understanding Matrix Multiplication Basics
Matrix multiplication is not simply element-wise multiplication. For two matrices, A (of size m x n ) and B (of size n x p ), their product C = A * B will be a matrix of size m x p . The key rule is that the number of columns in the first matrix must equal the number of rows in the second matrix. Each element C ij in the resulting matrix is computed as the dot product of the i -th row of A and the j -th column of B . PyTorch, a widely used open-source machine learning library, provides highly optimized functions for performing matrix operations, leveraging GPU acceleration when available. This optimization is critical for handling the large datasets and complex models prevalent in drug discovery research. Let's look at a simple example of matrix multiplication using PyTorch: import torch # Define two tensors (matrices) # Matrix A: 2 rows, 3 columns matrix_a = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) # Matrix B: 3 rows, 2 columns matrix_b = torch.tensor([[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]]) print("Matrix A shape:", matrix_a.shape) print("Matrix B shape:", matrix_b.shape) # Perform matrix multiplication using torch.matmul() result_matrix = torch.matmul(matrix_a, matrix_b) print("\nResult of matrix multiplication:") print(result_matrix) print("Result matrix shape:", result_matrix.shape) # You can also use the '@' operator for matrix multiplication in Python 3.5+ result_matrix_at_op = matrix_a @ matrix_b print("\nResult using '@' operator:") print(result_matrix_at_op) In this example, matrix_a has dimensions 2x3, and matrix_b has dimensions 3x2. Their product result_matrix will therefore have dimensions 2x2. PyTorch's torch.matmul() function (or the @ operator) handles the underlying computations efficiently. Beyond simple 2D matrices, PyTorch also supports matrix multiplication for higher-dimensional tensors, which is common in deep learning for handling batches of data or convolutional layers. For example, if you have a batch of images represented as a 4D tensor (batch_size, channels, height, width), matrix multiplication can be applied to specific dimensions to transform features. Consider an example where we might multiply a feature matrix (e.g., molecular descriptors for a batch of compounds) by a weight matrix from a neural network layer: import torch # Batch of 4 compounds, each with 10 features batch_features = torch.randn(4, 10) # 4x10 tensor (batch_size x num_features) # Weight matrix for a neural network layer, transforming 10 features to 5 outputs weight_matrix = torch.randn(10, 5) # 10x5 tensor (num_features x num_outputs) print("Batch features shape:", batch_features.shape) print("Weight matrix shape:", weight_matrix.shape) # Perform matrix multiplication # The result will be a batch of 4 outputs, each with 5 values output_batch = torch.matmul(batch_features, weight_matrix) print("\nOutput batch after matrix multiplication:") print(output_batch) print("Output batch shape:", output_batch.shape) Here, batch_features represents our input to a neural network layer. The weight_matrix contains the learnable parameters of that layer. Multiplying these two tensors transforms the input features into a new set of features or activations, which are then passed to subsequent layers. This operation is fundamental to how neural networks learn complex patterns in drug discovery data.
Key Takeaways:
Matrix multiplication is a core operation in PyTorch and deep learning, essential for AI in drug discovery. It transforms data represented as tensors, enabling neural networks to learn and make predictions. The number of columns in the first matrix must match the number of rows in the second matrix for multiplication. PyTorch provides torch.matmul() and the @ operator for efficient matrix multiplication. Understanding matrix dimensions and their transformations is crucial for debugging and designing neural network architectures.
Practice Exercise:
Imagine you have a dataset of 5 different drug compounds, and for each compound, you've extracted 8 relevant molecular descriptors (e.g., logP, molecular weight, number of H-bond donors). You want to pass these descriptors through a simple neural network layer that has 3 output neurons. Create two PyTorch tensors: one representing your drug compound descriptors (input) and another representing the weights of the neural network layer. Perform the matrix multiplication to get the output from this layer. What are the shapes of your input, weight, and output tensors?
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 →