![]() |
NeuZephyr
Simple DL Framework
|
Represents a scalar multiplication operation node in a computational graph. More...
Public Member Functions | |
ScalarMulNode (Node *input, Tensor::value_type scalar) | |
Constructor to initialize a ScalarMulNode for scalar multiplication. | |
void | forward () override |
Forward pass for the ScalarMulNode to perform scalar multiplication. | |
void | backward () override |
Backward pass for the ScalarMulNode 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 multiplication operation node in a computational graph.
The ScalarMulNode
class performs element-wise multiplication of a tensor by a scalar value. It is used in computational graphs to implement scalar scaling of tensors, which is common in various machine learning and numerical computing tasks.
Key features:
output
tensor.output
tensor back to the input tensor by scaling the gradient with the scalar value.This class is part of the nz::nodes
namespace and is used for scalar-tensor operations in a computational graph.
nz::nodes::calc::ScalarMulNode::ScalarMulNode | ( | Node * | input, |
Tensor::value_type | scalar ) |
Constructor to initialize a ScalarMulNode
for scalar multiplication.
The constructor initializes a ScalarMulNode
, which performs element-wise multiplication of the output tensor of the input node by a scalar value. It sets up the node's input connections, determines the gradient tracking requirement, and prepares the output tensor with the appropriate shape and properties.
input | A pointer to the input node. Its output tensor will be multiplied by the scalar value. |
scalar | The scalar value to multiply with the input tensor. |
ScalarMulNode
by adding the input node to the inputs
vector.output
tensor with the same shape as the input tensor and determines whether the output tensor should track gradients based on the input tensor's gradient requirement.ScalarMulNode
instance and used during the forward and backward passes.
|
overridevirtual |
Backward pass for the ScalarMulNode
to propagate gradients.
The backward()
method computes the gradient of the loss with respect to the input tensor by scaling the gradient of the output tensor using the scalar value. This operation ensures that the gradients are correctly propagated back through the computational graph.
ScalarMul
) is launched to scale the gradient of the output tensor and accumulate the result in the gradient of the input tensor.grad_input[i] = grad_output[i] * scalar
for each element in the tensor, where grad_output
is the gradient of the output
tensor.Implements nz::nodes::Node.
Definition at line 200 of file Nodes.cu.
|
overridevirtual |
Forward pass for the ScalarMulNode
to perform scalar multiplication.
The forward()
method computes the element-wise multiplication of the input tensor by the scalar value. It uses CUDA kernels to perform the computation in parallel on the GPU, ensuring efficient execution.
ScalarMul
) to compute the scalar multiplication.output[i] = input[i] * scalar
for each element in the tensor.Implements nz::nodes::Node.
Definition at line 194 of file Nodes.cu.