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

Represents a Swish activation function node in a computational graph. More...

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

Public Member Functions

 SwishNode (Node *input)
 Constructor to initialize a SwishNode for applying the Swish activation function.
 
void forward () override
 Forward pass for the SwishNode to apply the Swish activation function.
 
void backward () override
 Backward pass for the SwishNode 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 Swish activation function node in a computational graph.

The SwishNode class applies the Swish activation function to the input tensor. The Swish function is defined as:

Swish(x) = x / (1 + exp(-x))
void Swish(dim3 gridDim, dim3 blockDim, float *out, float *in, unsigned long long n)
Kernel function to apply the Swish activation function on the GPU.

It is a smooth, non-monotonic activation function that often outperforms ReLU in deep learning tasks.

Key features:

  • Forward Pass: Applies the Swish activation function element-wise to the input tensor, blending the input with its sigmoid output.
  • Backward Pass: Computes the gradient of the loss with respect to the input tensor using the derivative of the Swish function:
    Swish'(x) = Sigmoid(x) + x * Sigmoid(x) * (1 - Sigmoid(x))
  • 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 advanced deep learning models to enhance performance over traditional activation functions.

Note
  • The Swish function is applied element-wise, and it smoothly maps input values while retaining non-linearity.
  • Efficient GPU computations are performed for both forward and backward passes.

Usage Example:

// Example: Using SwishNode 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
SwishNode swish_node(&input); // Apply Swish activation
swish_node.forward(); // Perform the forward pass
swish_node.backward(); // Propagate gradients in the backward pass
std::cout << "Output: " << *swish_node.output << std::endl; // Print the result
SwishNode(Node *input)
Constructor to initialize a SwishNode for applying the Swish activation function.
Definition Nodes.cu:431
See also
forward() for the Swish 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 2504 of file Nodes.cuh.

Constructor & Destructor Documentation

◆ SwishNode()

nz::nodes::calc::SwishNode::SwishNode ( Node * input)
explicit

Constructor to initialize a SwishNode for applying the Swish activation function.

The constructor initializes a SwishNode, which applies the Swish activation function to an input tensor. It establishes a connection to the input node, initializes the output tensor, and sets the type of the node to "Swish".

Parameters
inputA pointer to the input node. Its output tensor will have the Swish activation applied.
  • 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 node's type is set to "Swish" to reflect its operation.
Note
  • The Swish activation function is defined as:
    Swish(x) = x / (1 + exp(-x))
  • 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 431 of file Nodes.cu.

Member Function Documentation

◆ backward()

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

Backward pass for the SwishNode to compute gradients.

The backward() method computes the gradient of the loss with respect to the input tensor by applying the derivative of the Swish activation function. The gradient computation is based on the formula:

Swish'(x) = Sigmoid(x) + x * Sigmoid(x) * (1 - Sigmoid(x))

where Sigmoid(x) = 1 / (1 + exp(-x)).

  • A CUDA kernel (SwishBackward) is launched to compute the gradients in parallel on the GPU.
  • The derivative of the Swish 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 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 Swish computation in the forward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Implements nz::nodes::Node.

Definition at line 444 of file Nodes.cu.

Here is the call graph for this function:

◆ forward()

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

Forward pass for the SwishNode to apply the Swish activation function.

The forward() method applies the Swish activation function element-wise to the input tensor. The result is stored in the output tensor. The Swish function is defined as:

Swish(x) = x / (1 + exp(-x))
  • A CUDA kernel (Swish) 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 optimize GPU performance.
  • The computed values are stored in the output tensor for use in subsequent layers or operations.
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 438 of file Nodes.cu.

Here is the call graph for this function:

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