![]() |
NeuZephyr
Simple DL Framework
|
Implements max pooling operation for spatial downsampling with feature preservation. More...
Public Member Functions | |
MaxPoolingNode (Node *input, Tensor::size_type poolSize, Tensor::size_type stride, Tensor::size_type padding) | |
Constructs a MaxPoolingNode object. | |
void | forward () override |
Performs the forward pass of the max - pooling operation. | |
void | backward () override |
Performs the backward pass of the max - 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. | |
Implements max pooling operation for spatial downsampling with feature preservation.
This node performs spatial max selection over sliding windows of (poolSize x poolSize) dimensions, maintaining the most prominent features while reducing spatial resolution. Essential for preserving sharp feature responses in CNNs.
Core functionality and characteristics:
Key implementation aspects:
Typical use cases:
Critical considerations:
nz::nodes::calc::MaxPoolingNode::MaxPoolingNode | ( | Node * | input, |
Tensor::size_type | poolSize, | ||
Tensor::size_type | stride, | ||
Tensor::size_type | padding ) |
Constructs a MaxPoolingNode object.
input | A pointer to the input node. Memory of this pointer is assumed to be managed externally and used in a read - only way within the constructor (host - to - host). |
poolSize | The size of the pooling window, of type Tensor::size_type . It is passed by value. |
stride | The stride of the pooling operation, of type Tensor::size_type . It is passed by value. |
padding | The padding applied to the input tensor, of type Tensor::size_type . It is passed by value. |
This constructor initializes a MaxPoolingNode object. It first stores the provided poolSize
, stride
, and padding
values. Then, it adds the input node pointer to the inputs
vector. Next, it creates a new shared pointer to a Tensor object for the output
member. The shape of the output tensor is calculated based on the input tensor's shape, poolSize
, stride
, and padding
using the OUTPUT_DIM
macro. The requiresGrad
flag of the output tensor is set to the same value as that of the input tensor's output. Additionally, it creates a new shared pointer to a Tensor object for the position
member. The shape of the position
tensor is the same as that of the output tensor, and its requiresGrad
flag is set to false
.
Memory management strategy: The constructor does not allocate memory for the input node. It only stores a pointer to it. The output
and position
tensors are created using std::make_shared
, which manages their memory automatically. Exception handling mechanism: There is no explicit exception handling in this constructor. If the std::make_shared
calls fail to allocate memory for the output
or position
tensors, they may throw a std::bad_alloc
exception.
std::bad_alloc | If memory allocation for the output or position tensors fails. |
output
and position
tensors, which has a time complexity of O(1) for the pointer management and O(m) for the tensor data allocation, where m is the number of elements in the tensors.
|
overridevirtual |
Performs the backward pass of the max - pooling operation.
None |
This function conducts the backward pass of the max - pooling operation. It first checks if the output tensor of the input node requires gradient computation. If it does, it calls the iMaxPoolingBackward
function, passing the gradient pointer of the input node's output, the data pointer of the position tensor, the gradient pointer of the output tensor, the pooling parameters (poolSize
, stride
, padding
), and the shape information of the input and output tensors. The iMaxPoolingBackward
function is responsible for computing the gradients and propagating them back to the input tensor.
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, as well as the data tensor of the position. Exception handling mechanism: There is no explicit exception handling in this function. If the iMaxPoolingBackward
function encounters an error, it may throw an exception depending on its implementation.
[Exception | type from iMaxPoolingBackward] If the iMaxPoolingBackward function encounters an error during execution. |
iMaxPoolingBackward
function. The time complexity typically depends on the size of the input tensor and the pooling parameters, and may be O(b * c * h * w) in the worst - case scenario, where b is the batch size, c is the number of channels, h is the height, and w is the width of the input tensor.Implements nz::nodes::Node.
|
overridevirtual |
Performs the forward pass of the max - pooling operation.
None |
This function executes the forward pass of the max - pooling operation. It calls the iMaxPooling
function, passing the data pointers of the output tensor, the position tensor, and the input tensor's output. It also provides the pooling parameters (poolSize
, stride
, padding
) and the shape information of the input and output tensors. The iMaxPooling
function is responsible for computing the max - pooling result and storing it in the output tensor, as well as recording the positions of the maximum values in the position tensor.
Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing data tensors of the input, output, and position. Exception handling mechanism: There is no explicit exception handling in this function. If the iMaxPooling
function encounters an error, it may throw an exception depending on its implementation.
[Exception | type from iMaxPooling] If the iMaxPooling function encounters an error during execution. |
iMaxPooling
function. The time complexity typically depends on the size of the input tensor and the pooling parameters, and may be O(b * c * h * w) in the worst - case scenario, where b is the batch size, c is the number of channels, h is the height, and w is the width of the input tensor.Implements nz::nodes::Node.