![]() |
NeuZephyr
Simple DL Framework
|
Represents a scalar addition operation node in a computational graph. More...
Public Member Functions | |
ScalarAddNode (Node *input, Tensor::value_type scalar) | |
Constructor to initialize a ScalarAddNode for scalar addition. | |
void | forward () override |
Forward pass for the ScalarAddNode to perform scalar addition. | |
void | backward () override |
Backward pass for the ScalarAddNode to propagate gradients. | |
![]() | |
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 a scalar addition operation node in a computational graph.
The ScalarAddNode
class performs element-wise addition of a tensor and a scalar value. It is commonly used in computational graphs for offsetting tensor values or applying a bias term in various operations.
Key features:
output
tensor.output
tensor back to the input tensor without modification, as the derivative of addition with respect to the input is 1.output
tensor.This class is part of the nz::nodes
namespace and facilitates scalar-tensor addition operations in computational graphs.
nz::nodes::calc::ScalarAddNode::ScalarAddNode | ( | Node * | input, |
Tensor::value_type | scalar ) |
Constructor to initialize a ScalarAddNode
for scalar addition.
The constructor initializes a ScalarAddNode
, which performs element-wise addition of a tensor and a scalar value. It establishes the connection between the input node and this node, prepares the output tensor with the appropriate shape and properties, and stores the scalar value for use during forward and backward passes.
input | A pointer to the input node. Its output tensor will be added to the scalar value. |
scalar | The scalar value to add to each element of the input tensor. |
inputs
vector to establish the connection in the computational graph.output
tensor is initialized with the same shape as the input tensor, and the requires_grad
property is determined based on the input tensor's gradient requirements.
|
overridevirtual |
Backward pass for the ScalarAddNode
to propagate gradients.
The backward()
method propagates the gradient of the loss from the output tensor back to the input tensor. Since the derivative of addition with respect to the input is 1, the gradient from the output tensor is directly copied to the input tensor's gradient.
output
tensor is copied to the gradient of the input tensor using cudaMemcpy
.output
tensor is already computed and properly initialized.Implements nz::nodes::Node.
Definition at line 251 of file Nodes.cu.
|
overridevirtual |
Forward pass for the ScalarAddNode
to perform scalar addition.
The forward()
method computes the element-wise addition of the input tensor and the scalar value. It uses a CUDA kernel to efficiently execute the operation in parallel on the GPU.
ScalarAdd
) is launched to add the scalar value to each element of the input tensor.output
tensor.output[i] = input[i] + scalar
for each element of the tensor.Implements nz::nodes::Node.
Definition at line 245 of file Nodes.cu.