![]() |
NeuZephyr
Simple DL Framework
|
Represents an input node in a computational graph. More...
Public Member Functions | |
InputNode (const Tensor::shape_type &shape, bool requires_grad=false) | |
Constructor to initialize an InputNode with a specified shape and gradient requirement. | |
InputNode (const Tensor &tensor) | |
Constructor to initialize an InputNode with an existing Tensor . | |
InputNode (const Tensor::shape_type &shape, Tensor::value_type *data, bool requires_grad=false, bool host=false) | |
Constructs an InputNode object with specified tensor shape, data, gradient requirement, and data location. | |
InputNode (const Tensor::shape_type &shape, const std::initializer_list< Tensor::value_type > &data, bool requires_grad=false) | |
Constructs an InputNode object with a specified tensor shape, initializer list data, and gradient requirement. | |
void | forward () override |
Forward pass for the InputNode . | |
void | backward () override |
Backward pass for the InputNode . | |
![]() | |
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. | |
Represents an input node in a computational graph.
The InputNode
class is a subclass of Node
, representing a node that holds the input data for a neural network or computational graph. It is designed to store the input tensor and pass it forward through the graph. InputNode
does not perform any computations in the forward or backward passes; its main role is to provide input data to the network.
Key features:
InputNode
stores a Tensor
as its output, which is initialized either from a shape or an existing tensor.forward()
and backward()
methods are implemented as empty, since this node only provides input data and does not modify the network during these passes.This class is part of the nz::nodes
namespace and serves as a fundamental part of the computational graph, providing input data to the network during the forward pass.
forward()
and backward()
methods are implemented but do not perform any operations for the InputNode
.
|
explicit |
Constructor to initialize an InputNode
with a specified shape and gradient requirement.
This constructor initializes an InputNode
that holds a tensor with the specified shape. The tensor is created with the specified shape, and it can optionally track gradients if requires_grad
is set to true
. The InputNode
does not perform any computations; its primary role is to hold and provide input data to the computational graph.
shape | The shape of the tensor to be stored in the InputNode . This defines the dimensions of the input data. |
requires_grad | A boolean flag indicating whether the tensor should track gradients. Defaults to false . |
The InputNode
will store a Tensor
object, initialized with the given shape and gradient tracking setting. This tensor can then be used as input for subsequent nodes in the computational graph.
InputNode
class does not perform any computations during the forward or backward passes. It simply stores and provides input data.requires_grad
determines whether the input tensor will store gradients, which is useful if the input data is part of the network's parameters.
|
explicit |
Constructor to initialize an InputNode
with an existing Tensor
.
This constructor initializes an InputNode
using an existing Tensor
object. The Tensor
object is directly assigned to the output
of the InputNode
, allowing the node to use the provided tensor as its input data. The InputNode
does not perform any computations but simply holds and provides the given tensor data to the computational graph.
tensor | The existing Tensor object to be used as the input for the InputNode . This tensor contains the input data to be passed through the network. |
The InputNode
stores the provided tensor in its output
member, which will be used by other nodes in the graph.
InputNode
does not modify the given tensor, it simply holds a reference to it.
|
explicit |
Constructs an InputNode object with specified tensor shape, data, gradient requirement, and data location.
shape | A reference to the shape of the output tensor of the input node (host-to-device). It defines the dimensions of the tensor. |
data | A pointer to the initial data of the output tensor. The data can be either on the host or device depending on the host parameter. |
requires_grad | A boolean indicating whether the output tensor requires gradient computation. |
host | A boolean indicating whether the data pointed to by data is on the host or device. If true, data is on the host; otherwise, it is on the device. |
This constructor initializes an InputNode
object. It creates a new Tensor
object using the provided shape
, data
, requires_grad
, and host
parameters and stores a shared pointer to this tensor in the output
member variable.
In terms of memory management, the std::shared_ptr
in output
takes care of the memory of the Tensor
object. When the last reference to the Tensor
object held by a std::shared_ptr
is destroyed, the Tensor
object will be automatically deleted.
Regarding exception handling, this constructor does not explicitly catch any exceptions thrown by the Tensor
constructor. If the Tensor
constructor fails (e.g., due to insufficient memory or invalid input), the exception will propagate to the caller.
This constructor is a fundamental part of the InputNode
class as it initializes the output tensor of the input node.
None | explicitly, but the Tensor constructor may throw exceptions, such as std::bad_alloc if memory allocation fails. |
data
pointer is valid and points to enough data to fill the tensor according to the specified shape.std::shared_ptr
and O(n) for the Tensor
constructor, where n is the total number of elements in the tensor.
|
explicit |
Constructs an InputNode object with a specified tensor shape, initializer list data, and gradient requirement.
shape | A reference to the shape of the output tensor of the input node (host-to-device). It determines the dimensions and total size of the tensor. |
data | A std::initializer_list containing the initial data for the output tensor (host-to-device). |
requires_grad | A boolean indicating whether the output tensor requires gradient computation. |
This constructor initializes an InputNode object. It creates a new Tensor object using the provided shape, initializer list data, and gradient requirement, and stores a shared pointer to this tensor in the output member variable.
For memory management, the std::shared_ptr in output takes care of the Tensor object's memory. When the last reference to the Tensor object held by a std::shared_ptr is destroyed, the Tensor object will be automatically deleted.
Regarding exception handling, this constructor does not explicitly catch any exceptions thrown by the Tensor constructor. If the Tensor constructor fails (e.g., due to insufficient memory or an invalid initializer list size), the exception will propagate to the caller.
This constructor is an important part of the InputNode class as it provides a convenient way to initialize the output tensor of the input node with an initializer list.
None | explicitly, but the Tensor constructor may throw exceptions such as std::invalid_argument if the initializer list size is insufficient or std::bad_alloc if memory allocation fails. |
|
overridevirtual |
Backward pass for the InputNode
.
The backward()
method for the InputNode
is a no-op (no operation) because the input node does not participate in the backpropagation process. It does not have any parameters or gradients to update, as its role is simply to provide the input data to the computational graph.
Since the InputNode
does not modify any data during the backward pass, the backward()
method does not need to perform any operations, and it is left empty. Gradients will not be propagated from this node to any other nodes, as the InputNode
does not require gradients.
Node
class, but it does not perform any operations for InputNode
. It serves as a placeholder to conform to the Node
class interface.InputNode
is used to provide data to the computational graph, but since it doesn't have parameters, there is no need to propagate gradients through it.Implements nz::nodes::Node.
|
overridevirtual |
Forward pass for the InputNode
.
The forward()
method for the InputNode
is a no-op (no operation) because the input node does not perform any computations during the forward pass. Its primary role is to provide the input data to the computational graph.
Since the InputNode
does not modify its data or perform any calculations, the forward()
method does not need to be implemented with any functionality, and it is left empty. The tensor stored in the output
member will simply be passed along to the next nodes in the computational graph during the forward pass.
Node
class, but it does not perform any operations for InputNode
. It serves as a placeholder to conform to the Node
class interface.InputNode
only holds input data, and its forward()
method ensures that the data is available for the subsequent nodes in the graph.Implements nz::nodes::Node.