![]() |
NeuZephyr
Simple DL Framework
|
Represents a scalar subtraction operation node in a computational graph. More...
Public Member Functions | |
ScalarSubNode (Node *input, Tensor::value_type scalar) | |
Constructor to initialize a ScalarSubNode for scalar subtraction. | |
void | forward () override |
Forward pass for the ScalarSubNode to perform scalar subtraction. | |
void | backward () override |
Backward pass for the ScalarSubNode 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 subtraction operation node in a computational graph.
The ScalarSubNode
class performs element-wise subtraction of a scalar value from a tensor. It is commonly used in computational graphs to offset tensor values or perform subtraction-based normalization tasks.
Key features:
output
tensor.output
tensor back to the input tensor. Since the derivative of subtraction with respect to the input is 1, the gradient from the output
tensor is directly transferred to the input tensor.output
tensor.This class is part of the nz::nodes
namespace and facilitates scalar-tensor subtraction operations in computational graphs.
nz::nodes::calc::ScalarSubNode::ScalarSubNode | ( | Node * | input, |
Tensor::value_type | scalar ) |
Constructor to initialize a ScalarSubNode
for scalar subtraction.
The constructor initializes a ScalarSubNode
, which performs element-wise subtraction of a scalar value from the elements of the input tensor. It establishes the connection between the input node and this node, prepares the output tensor with the appropriate shape and properties, and stores the negated scalar value for use during forward and backward passes.
input | A pointer to the input node. Its output tensor will have the scalar value subtracted from it. |
scalar | The scalar value to subtract from 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 ScalarSubNode
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 subtraction 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 directly to the gradient of the input tensor using cudaMemcpy
.output
tensor is already computed and properly initialized.Implements nz::nodes::Node.
Definition at line 275 of file Nodes.cu.
|
overridevirtual |
Forward pass for the ScalarSubNode
to perform scalar subtraction.
The forward()
method computes the element-wise subtraction of a scalar value from the input tensor. Internally, it utilizes the addition kernel (ScalarAdd
) by treating the subtraction as addition with a negated scalar value, which was preprocessed during node construction.
ScalarAdd
) is launched to add the negated scalar value to each element of the input tensor.output
tensor.output[i] = input[i] - scalar
, achieved by using output[i] = input[i] + (-scalar)
for efficiency.Implements nz::nodes::Node.
Definition at line 269 of file Nodes.cu.