![]() |
NeuZephyr
Simple DL Framework
|
Performs global average pooling operation across spatial dimensions of input tensor. More...
Public Member Functions | |
GlobalAvgPoolNode (Node *input) | |
Constructs a GlobalAvgPoolNode object. | |
void | forward () override |
Performs the forward pass of the global average pooling operation. | |
void | backward () override |
Performs the backward pass of the global average pooling operation. | |
![]() | |
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. | |
Performs global average pooling operation across spatial dimensions of input tensor.
This node reduces each channel's spatial dimensions (H, W) to a single average value, producing output of shape (N, C, 1, 1). Commonly used for final feature aggregation before fully connected layers in CNN architectures.
Core functionality and characteristics:
Key implementation aspects:
Typical use cases:
Critical considerations:
nz::nodes::calc::GlobalAvgPoolNode::GlobalAvgPoolNode | ( | Node * | input | ) |
Constructs a GlobalAvgPoolNode object.
input | A pointer to the input node. The memory of this pointer is assumed to be managed externally and is used in a read - only manner within this constructor (host - to - host). |
This constructor initializes a GlobalAvgPoolNode object. It first adds the provided input node pointer to the inputs
vector. Then, it creates a new shared pointer to a Tensor object for the output
member. The shape of the output tensor is set to have the same batch size and number of channels as the input tensor's output, but with a height and width of 1. The requiresGrad
flag of the output tensor is set to the same value as that of the input tensor's output. Finally, it sets the type
member of the node to "GlobalAvgPool".
Memory management strategy: The constructor does not allocate memory for the input node. It only stores a pointer to it. The output tensor is created using std::make_shared
, which manages the memory automatically. Exception handling mechanism: There is no explicit exception handling in this constructor. If the std::make_shared
call fails to allocate memory for the output tensor, it may throw a std::bad_alloc
exception.
std::bad_alloc | If memory allocation for the output tensor fails. |
|
overridevirtual |
Performs the backward pass of the global average pooling operation.
None |
This function conducts the backward pass of the global average pooling operation. It first checks if the output tensor of the input node requires gradient computation. If it does, the function calls iGlobalAvgPoolBackward
, passing the gradient tensor of the input node's output, the gradient tensor of the output, and the shape information of the input tensor (batch size, number of channels, height, and width). The iGlobalAvgPoolBackward
function computes the gradients and propagates them back to the input.
Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing gradient tensors of the input and output. Exception handling mechanism: There is no explicit exception handling in this function. If the iGlobalAvgPoolBackward
function encounters an error, it may throw an exception, and the specific type of exception depends on the implementation of iGlobalAvgPoolBackward
.
[Exception | type from iGlobalAvgPoolBackward] If the iGlobalAvgPoolBackward function encounters an error during execution. |
Implements nz::nodes::Node.
|
overridevirtual |
Performs the forward pass of the global average pooling operation.
None |
This function conducts the forward pass of the global average pooling operation. It iterates over each sample in the batch (i
loop) and each channel (j
loop) of the input tensor. For each combination of batch and channel, it calculates the sum of all elements in the corresponding 2D matrix of the input tensor using the sum
method. Then, it divides this sum by the total number of elements in the 2D matrix (which is the product of the height and width of the input tensor). The result is then used to fill the corresponding element in the output tensor using the fillMatrix
method.
Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing data tensors of the input and output. Exception handling mechanism: There is no explicit exception handling in this function. If the sum
or fillMatrix
methods encounter an error, they may throw exceptions depending on their implementation.
[Exception | type from sum or fillMatrix] If the sum or fillMatrix methods encounter an error during execution. |
Implements nz::nodes::Node.