namespace nntrainer {
Var_Grad::Var_Grad(const TensorDim &dim, const Tensor::Initializer init,
- bool ng, bool alloc_now_, const std::string &name) :
- dim(dim),
- need_gradient(ng),
- alloc_now(alloc_now_) {
+ bool need_gradient, bool alloc_now,
+ const std::string &name) {
var = std::make_shared<Tensor>(dim, alloc_now, init, name);
std::string grad_name = name + grad_suffix;
void Var_Grad::initializeShared() { grad->makeSharedDataTensor(*var.get()); }
-void Var_Grad::needsGradient(bool ng) {
- need_gradient = ng;
+void Var_Grad::needsGradient(bool need_gradient) {
if (need_gradient && grad->empty()) {
- bool alloc_now_ = var->isAllocated();
- grad = std::make_shared<Tensor>(dim, alloc_now_, Tensor::Initializer::ZEROS,
- grad->getName());
+ grad =
+ std::make_shared<Tensor>(var->getDim(), var->isAllocated(),
+ Tensor::Initializer::ZEROS, grad->getName());
}
}
*/
explicit Var_Grad(const Tensor &v, const Tensor &g,
const std::string &n = "") :
- dim(v.getDim()),
- var(std::make_shared<Tensor>(v.getSharedDataTensor(dim, 0, false, n))),
- grad(std::make_shared<Tensor>(n + grad_suffix)),
- need_gradient(!g.empty()),
- alloc_now(v.isAllocated()) {
- if (need_gradient)
+ var(
+ std::make_shared<Tensor>(v.getSharedDataTensor(v.getDim(), 0, false, n))),
+ grad(std::make_shared<Tensor>(n + grad_suffix)) {
+ if (!g.empty())
grad = std::make_shared<Tensor>(
- g.getSharedDataTensor(dim, 0, false, n + grad_suffix));
+ g.getSharedDataTensor(v.getDim(), 0, false, n + grad_suffix));
}
/**
*
* @return TensorDim Dimension
*/
- TensorDim getDim() const { return dim; }
+ TensorDim getDim() const { return var->getDim(); }
/**
* @brief Get if the Var_Grad is need_gradient
* @retval true if need_gradient
* @retval false is not need_gradient
*/
- bool needsGradient() const { return need_gradient; }
+ bool needsGradient() const { return hasGradient(); }
/**
* @brief set if the Var_Grad should need gradient
*
* @note New dimension must maintain the shape of the variable
*/
- void reset(const TensorDim &tdim, Tensor::Initializer init, bool ng) {
- dim = tdim;
+ void reset(const TensorDim &dim, Tensor::Initializer init, bool ng) {
if (!var->empty())
var->reshape(dim);
var->initialize(init);
- if (!grad->empty())
+ if (ng && !grad->empty())
grad->reshape(dim);
- need_gradient = ng;
+ else
+ grad = std::make_shared<Tensor>(grad->getName());
resetGradient();
}
* @param batch batch size
*/
void setBatchSize(unsigned int batch) {
- dim.batch(batch);
-
if (!var->empty())
var->updateBatch(batch);
if (!grad->empty())
* @note this is can return is the var_grad needs gradient but it not
* empty
*/
- bool hasGradient() const { return need_gradient && !grad->empty(); }
+ bool hasGradient() const { return !grad->empty(); }
inline static const std::string grad_suffix = ":grad";
protected:
- TensorDim dim; /**< dimension of the tensor */
std::shared_ptr<Tensor> var; /**< variable to be updated and used */
std::shared_ptr<Tensor> grad; /**< gradient for the variable */
- bool need_gradient; /**< if this variable needs gradient */
- bool alloc_now; /**< if the tensor should be allocated instantly */
};
} // namespace nntrainer