CUDA Networks
neural_network_forward.cu
Go to the documentation of this file.
1 /**
2  * @file neural_network_forward.cu
3  * @brief Implementation of the NeuralNetwork::forward method.
4  */
5 #include "neural_network.h"
6 #include <iostream>
7 
8 void NeuralNetwork::forward(const Matrix& X) {
9  // Store the input matrix
10  A = X.copy();
11  // std::cout << "Input matrix A:" << std::endl;
12  // A.print(4);
13 
14  // Compute the pre-activation of the hidden layer
15  Z1 = W1.multiply(A);
16  // std::cout << "Pre-activation of hidden layer Z1:" << std::endl;
17  // Z1.print(4);
18 
19  // Add biases to the pre-activation
20  Z1.add_vector(b1);
21  // std::cout << "Z1 after adding biases:" << std::endl;
22  // Z1.print(4);
23 
24  // Apply ReLU activation to the hidden layer
25  A1 = Z1.relu();
26  // std::cout << "Activation of hidden layer A1:" << std::endl;
27  // A1.print(4);
28 
29  // Compute the pre-activation of the output layer
30  Z2 = W2.multiply(A1);
31  // std::cout << "Pre-activation of output layer Z2:" << std::endl;
32  // Z2.print(4);
33 
34  // Add biases to the pre-activation
35  Z2.add_vector(b2);
36  // std::cout << "Z2 after adding biases:" << std::endl;
37  // Z2.print(4);
38 
39  // Apply softmax activation to the output layer
40  A2 = Z2.softmax();
41  // std::cout << "Activation of output layer A2:" << std::endl;
42  // A2.print(4);
43 }
Represents a matrix with GPU-accelerated operations.
Definition: matrix.h:18
void add_vector(const Vector &v)
Adds a vector to each column of the matrix.
Matrix copy() const
Creates a deep copy of the matrix.
Definition: matrix_copy.cu:59
Matrix multiply(const Matrix &other) const
Multiplies this matrix with another matrix.
Matrix relu() const
Applies the ReLU activation function to the matrix.
Definition: matrix_relu.cu:30
Matrix softmax() const
Applies the softmax function to the matrix column-wise.
void forward(const Matrix &X)
Perform forward propagation through the network.
Defines the NeuralNetwork class for a simple feedforward neural network.