NeuZephyr
Simple DL Framework
nz::nodes::calc::LeakyReLUNode Class Reference

Represents a Leaky Rectified Linear Unit (LeakyReLU) activation function node in a computational graph. More...

Inheritance diagram for nz::nodes::calc::LeakyReLUNode:
Collaboration diagram for nz::nodes::calc::LeakyReLUNode:

Public Member Functions

 LeakyReLUNode (Node *input, Tensor::value_type alpha=0.01f)
 Constructor to initialize a LeakyReLUNode for applying the Leaky ReLU activation function.
 
void forward () override
 Forward pass for the LeakyReLUNode to apply the Leaky ReLU activation function.
 
void backward () override
 Backward pass for the LeakyReLUNode to compute gradients.
 
- Public Member Functions inherited from nz::nodes::Node
virtual void print (std::ostream &os) const
 Prints the type, data, and gradient of the node.
 
void dataInject (Tensor::value_type *data, bool grad=false) const
 Injects data into a relevant tensor object, optionally setting its gradient requirement.
 
template<typename Iterator >
void dataInject (Iterator begin, Iterator end, const bool grad=false) const
 Injects data from an iterator range into the output tensor of the InputNode, optionally setting its gradient requirement.
 
void dataInject (const std::initializer_list< Tensor::value_type > &data, bool grad=false) const
 Injects data from a std::initializer_list into the output tensor of the Node, optionally setting its gradient requirement.
 

Detailed Description

Represents a Leaky Rectified Linear Unit (LeakyReLU) activation function node in a computational graph.

The LeakyReLUNode class applies the Leaky ReLU activation function to the input tensor. Unlike the standard ReLU, Leaky ReLU allows a small, non-zero gradient for negative input values. The function is defined as:

LeakyReLU(x) = x, if x > 0
alpha * x, if x <= 0
void LeakyReLU(dim3 gridDim, dim3 blockDim, float *out, float *in, unsigned long long n, float alpha=0.01f)
Kernel function to apply the Leaky ReLU activation function on the GPU.

where alpha is a small constant that determines the slope for negative values.

Key features:

  • Forward Pass: Applies the Leaky ReLU activation function element-wise to the input tensor. Positive values remain unchanged, while negative values are scaled by alpha.
  • Backward Pass: Computes the gradient of the loss with respect to the input tensor. Gradients for positive values are passed through unchanged, while gradients for negative values are scaled by alpha.
  • Shape Preservation: The output tensor has the same shape as the input tensor.
  • Gradient Management: Automatically tracks gradients if required by the input tensor.

This class is part of the nz::nodes namespace and is commonly used in deep learning to mitigate the "dying ReLU" problem by allowing small gradients for negative inputs.

Note
  • The alpha parameter defaults to 0.01, but can be customized during construction.
  • Efficient GPU computations are performed for both forward and backward passes.

Usage Example:

// Example: Using LeakyReLUNode in a computational graph
InputNode input({3, 3}, true); // Create an input node with shape {3, 3}
float data[] = {-1.0f, 0.0f, 1.0f, 2.0f, -2.0f, 3.0f, -3.0f, 4.0f, -4.0f}; // Sample input values
input.output->dataInject(data); // Copy data to the input tensor
LeakyReLUNode leaky_relu_node(&input, 0.1f); // Apply Leaky ReLU activation with alpha = 0.1
leaky_relu_node.forward(); // Perform the forward pass
leaky_relu_node.backward(); // Propagate gradients in the backward pass
std::cout << "Output: " << *leaky_relu_node.output << std::endl; // Print the result
LeakyReLUNode(Node *input, Tensor::value_type alpha=0.01f)
Constructor to initialize a LeakyReLUNode for applying the Leaky ReLU activation function.
Definition Nodes.cu:408
See also
forward() for the Leaky ReLU computation in the forward pass.
backward() for gradient computation in the backward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Definition at line 2353 of file Nodes.cuh.

Constructor & Destructor Documentation

◆ LeakyReLUNode()

nz::nodes::calc::LeakyReLUNode::LeakyReLUNode ( Node * input,
Tensor::value_type alpha = 0.01f )
explicit

Constructor to initialize a LeakyReLUNode for applying the Leaky ReLU activation function.

The constructor initializes a LeakyReLUNode, which applies the Leaky ReLU activation function to an input tensor. It establishes a connection to the input node, initializes the output tensor, and sets the alpha parameter and node type.

Parameters
inputA pointer to the input node. Its output tensor will have the Leaky ReLU activation applied.
alphaThe slope for negative input values, determining how much they are scaled. Defaults to 0.01.
  • The input node is added to the inputs vector to establish the connection in the computational graph.
  • The output tensor is initialized with the same shape as the input tensor, and its gradient tracking is determined based on the input tensor's requirements.
  • The alpha parameter is stored to control the slope for negative input values during both the forward and backward passes.
  • The node's type is set to "LeakyReLU" to reflect its operation.
Note
  • The Leaky ReLU activation function is defined as:
    LeakyReLU(x) = x, if x > 0
    alpha * x, if x <= 0
  • This node supports automatic gradient tracking if the input tensor requires gradients.
See also
forward() for the forward pass implementation.
backward() for gradient computation in the backward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Definition at line 408 of file Nodes.cu.

Member Function Documentation

◆ backward()

void nz::nodes::calc::LeakyReLUNode::backward ( )
overridevirtual

Backward pass for the LeakyReLUNode to compute gradients.

The backward() method computes the gradient of the loss with respect to the input tensor by applying the derivative of the Leaky ReLU activation function. The gradient computation is defined as:

LeakyReLU'(x) = 1, if x > 0
alpha, if x <= 0
  • A CUDA kernel (LeakyReLUBackward) is launched to compute the gradients in parallel on the GPU.
  • The derivative of the Leaky ReLU function is applied element-wise to the input tensor's data and combined with the gradient of the output tensor to compute the input gradient.
  • The alpha parameter, provided during construction, controls the gradient scale for negative input values.
  • The computed gradient is stored in the gradient tensor of the input node.
Note
  • Gradients are only computed and propagated if the input tensor's requiresGrad property is true.
  • The shape of the gradient tensor matches that of the input tensor.
See also
forward() for the Leaky ReLU computation in the forward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Implements nz::nodes::Node.

Definition at line 422 of file Nodes.cu.

Here is the call graph for this function:

◆ forward()

void nz::nodes::calc::LeakyReLUNode::forward ( )
overridevirtual

Forward pass for the LeakyReLUNode to apply the Leaky ReLU activation function.

The forward() method applies the Leaky ReLU activation function element-wise to the input tensor. Positive input values remain unchanged, while negative input values are scaled by the alpha parameter. The result is stored in the output tensor. The function is defined as:

LeakyReLU(x) = x, if x > 0
alpha * x, if x <= 0
  • A CUDA kernel (LeakyReLU) is launched to compute the activation function in parallel on the GPU.
  • The grid and block dimensions are dynamically calculated based on the size of the output tensor to ensure efficient GPU utilization.
  • The alpha parameter, provided during construction, determines the slope for negative input values.
See also
backward() for the computation of gradients in the backward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Implements nz::nodes::Node.

Definition at line 416 of file Nodes.cu.

Here is the call graph for this function:

The documentation for this class was generated from the following files: