Move name as an identifier to the tensor.
This name has been mvoed from the Var_Grad object.
Var_Grad now does not contain name itself.
The semantics of name for Tensor currently follows from var-grad:
- empty name is allowed
- repititive names are allowed
Restrictions can and will be added over time.
Resolves #1485
Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
return rng;
}();
-Tensor::Tensor(const TensorDim &d, bool alloc_now, Tensor::Initializer init) :
- Tensor() {
+Tensor::Tensor(const TensorDim &d, bool alloc_now, Tensor::Initializer init,
+ std::string name_) :
+ Tensor(name_) {
if (d.getDataLen() != 0) {
dim = d;
strides = d.computeStrides();
}
Tensor Tensor::getSharedDataTensor(const TensorDim dim_, unsigned int offset,
- bool reset_stride) const {
+ bool reset_stride,
+ const std::string &name_) const {
Tensor ret = *this;
ret.dim = dim_;
+ if (!name_.empty())
+ ret.name = name_;
if (dim_.getDataLen() + offset > dim.getDataLen())
throw std::invalid_argument(
/**
* @brief Basic Constructor of Tensor
*/
- Tensor() :
+ Tensor(std::string name_ = "") :
dim(TensorDim()),
strides(dim.computeStrides()),
is_contiguous(true),
initializer(Initializer::NONE),
+ name(name_),
data(nullptr),
src_tensor() {}
* @brief Constructor of Tensor with dimension, possibly lazily
* @param d Tensor dim for this tensor
* @param alloc_now If the memory of the tensor must be allocated
+ * @param init Initializer for the tensor
+ * @param name Name of the tensor
*/
Tensor(const TensorDim &d, bool alloc_now,
- Initializer init = Initializer::NONE);
+ Initializer init = Initializer::NONE, std::string name = "");
/**
* @brief Constructor of Tensor with dimension/buf
std::swap(lhs.is_contiguous, rhs.is_contiguous);
std::swap(lhs.initializer, rhs.initializer);
std::swap(lhs.data, rhs.data);
+ std::swap(lhs.name, rhs.name);
}
/**
* tensor.
*/
Tensor getSharedDataTensor(const TensorDim dim, unsigned int offset,
- bool reset_stride = true) const;
+ bool reset_stride = true,
+ const std::string &name_ = "") const;
/**
* @brief make this tensor share memory with given tensor
return (b * strides[0] + c * strides[1] + h * strides[2] + w * strides[3]);
}
+ /**
+ * @brief Get name of the tensor
+ *
+ * @return name of the tensor
+ */
+ void setName(const std::string &name_) { name = name_; }
+
+ /**
+ * @brief Get name of the tensor
+ *
+ * @return name of the tensor
+ */
+ const std::string &getName() const { return name; }
+
static constexpr float epsilon = 1e-5;
private:
std::array<unsigned int, TensorDim::MAXDIM> strides;
bool is_contiguous;
Tensor::Initializer initializer;
+ std::string name; /**< name of the tensor */
std::shared_ptr<float> data;
bool ng, bool alloc_now_, const std::string &name) :
dim(dim),
need_gradient(ng),
- alloc_now(alloc_now_),
- name(name) {
- var = std::make_shared<Tensor>(dim, alloc_now, init);
+ alloc_now(alloc_now_) {
+ var = std::make_shared<Tensor>(dim, alloc_now, init, name);
+
+ std::string grad_name = name + grad_suffix;
if (need_gradient)
/**
* @todo gradient initializer should be none, and then they should be set
* zero right before using by the user itself.
*/
- grad = std::make_shared<Tensor>(dim, alloc_now, Tensor::Initializer::ZEROS);
+ grad = std::make_shared<Tensor>(dim, alloc_now, Tensor::Initializer::ZEROS,
+ grad_name);
else
- grad = std::make_shared<Tensor>();
+ grad = std::make_shared<Tensor>(grad_name);
}
void Var_Grad::initializeVariable(const Tensor &preallocated) {
need_gradient = ng;
if (need_gradient && grad->empty()) {
bool alloc_now_ = var->isAllocated();
- grad =
- std::make_shared<Tensor>(dim, alloc_now_, Tensor::Initializer::ZEROS);
+ grad = std::make_shared<Tensor>(dim, alloc_now_, 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))),
- grad(std::make_shared<Tensor>()),
+ 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()),
- name(n) {
+ alloc_now(v.isAllocated()) {
if (need_gradient)
- grad = std::make_shared<Tensor>(g.getSharedDataTensor(dim, 0, false));
+ grad = std::make_shared<Tensor>(
+ g.getSharedDataTensor(dim, 0, false, n + grad_suffix));
}
/**
if (gtrain)
initializeGradient(grad_preallocated);
else
- grad = std::make_shared<Tensor>();
+ grad = std::make_shared<Tensor>(grad->getName());
}
/**
*
* @return std::string name
*/
- const std::string &getName() const { return name; }
+ const std::string &getName() const { return var->getName(); }
/**
- * @brief Get the variable tensor (by name)
+ * @brief Get the variable tensor
*
* @return Tensor Variable tensor
*/
Tensor getVariable() const { return *var.get(); }
/**
- * @brief Get the Gradient tensor (by name)
+ * @brief Get the Gradient tensor
*
* @return Tensor Gradient tensor
*/
*/
bool hasGradient() const { return need_gradient && !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 */
- std::string name; /**< name of the parameter */
+ bool alloc_now; /**< if the tensor should be allocated instantly */
};
} // namespace nntrainer