Tensor &ret = context.getOutgoingDerivative(SINGLE_INOUT_IDX);
Tensor &out = context.getOutput(SINGLE_INOUT_IDX);
- ret = acti_func.run_prime_fn(out, ret, deriv);
+ acti_func.run_prime_fn(out, ret, deriv);
}
void ActivationLayer::setProperty(const std::vector<std::string> &values) {
if (!outputs[idx]->hasGradient())
throw std::invalid_argument(
"Requesting gradient for a non-trainable tensor.");
+ return getOutputGradUnsafe(idx);
+ }
+
+ /**
+ * @brief Get the Output Grad tensor object
+ *
+ * @param idx Identifier of the output
+ * @return Tensor& Reference to the output grad tensor
+ *
+ * @note recommended to NOT use this function as a layer developer but rather
+ * use getOutputGrad().
+ */
+ Tensor &getOutputGradUnsafe(unsigned int idx) {
return outputs[idx]->getGradientRef();
}
run_context.setBatch(batch);
layer->setBatch(run_context, batch);
} else {
- for (auto &dim : input_dim)
- dim.batch(batch);
init_context.setBatch(batch);
layer->setBatch(init_context, batch);
}
* @brief Get number of inputs
* @retval number of inputs
*/
- unsigned int getNumInputs() const { return input_dim.size(); }
+ unsigned int getNumInputs() const { return init_context.getNumInputs(); }
/**
* @brief Get number of outputs
* @brief Get the Weight object
*
* @param idx Identifier of the weight
- * @return Tensor& Reference to the weight tensor
+ * @return Weight& Reference to the weight
*/
Weight getWeightWrapper(unsigned int idx) {
if (layerv1 == nullptr) {
}
}
+ /**
+ * @brief Get the Output Grad unsafe
+ *
+ * @param idx Identifier of the output
+ * @return Tensor& Reference to the output grad tensor
+ */
+ Tensor &getOutputGradUnsafe(unsigned int idx) {
+ if (layerv1 == nullptr) {
+ return run_context.getOutputGradUnsafe(idx);
+ } else {
+ return getLayer()->getOutputRef()[idx]->getGradientRef();
+ }
+ }
+
/**
* @brief read layer Weight & Bias data from file
* @param file input file stream
<< " requirements size: " << layer_node->getNumOutputs();
for (unsigned int i = 0; i < layer_node->getNumOutputs(); i++) {
- layer_node->getOutputGrad(i) = *label[i];
+ layer_node->getOutputGradUnsafe(i) = *label[i];
}
};
auto clear_label = [](auto const &layer_node) {
for (unsigned int i = 0; i < layer_node->getNumOutputs(); i++) {
- layer_node->getOutputGrad(i) = Tensor();
+ layer_node->getOutputGradUnsafe(i) = Tensor();
}
};
dim(v.getDim()),
var(std::make_shared<Tensor>(v.getSharedDataTensor(dim, 0, false))),
grad(std::make_shared<Tensor>(g.getSharedDataTensor(dim, 0, false))),
- trainable(!g.uninitialized()),
+ need_gradient(!g.uninitialized()),
alloc_now(v.isAllocated()),
name(n) {}
initializeVariable(var_preallocated);
if (gtrain)
initializeGradient(grad_preallocated);
+ else
+ grad = std::make_shared<Tensor>();
}
/**