From fc9e3e4bda6a4ceaa1ae9e45eb3ef522382bf8e3 Mon Sep 17 00:00:00 2001 From: zlx Date: Fri, 16 Jun 2017 14:29:16 +0800 Subject: [PATCH] explain the sparsity ratio --- paddle/parameter/ParameterUpdaterHook.cpp | 6 +++--- proto/ParameterConfig.proto | 3 ++- python/paddle/trainer_config_helpers/attrs.py | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/paddle/parameter/ParameterUpdaterHook.cpp b/paddle/parameter/ParameterUpdaterHook.cpp index 44fac592002..1cc91b727ad 100644 --- a/paddle/parameter/ParameterUpdaterHook.cpp +++ b/paddle/parameter/ParameterUpdaterHook.cpp @@ -61,7 +61,7 @@ public: maskTemp_ = Vector::create(para->getSize(), false); maskTemp_->zeroMem(); real* dataPtr = maskTemp_->getData(); - size_t sparsityNum = para->getSize() * (1 - sparsityRatio_); + size_t nonZeroNum = para->getSize() * (1 - sparsityRatio_); VectorPtr vecCpu = Vector::create(para->getSize(), false); vecCpu->copyFrom(*vec); @@ -71,10 +71,10 @@ public: param.push_back(std::make_pair(fabs(vecCpu->getData()[i]), i)); std::partial_sort(param.begin(), - param.begin() + sparsityNum, + param.begin() + nonZeroNum, param.end(), sortPairAscend); - for (size_t i = 0; i < sparsityNum; i++) dataPtr[param[i].second] = 1.0; + for (size_t i = 0; i < nonZeroNum; i++) dataPtr[param[i].second] = 1.0; // Currently just use a mask vector for hack. if (para->useGpu()) { diff --git a/proto/ParameterConfig.proto b/proto/ParameterConfig.proto index 360342bac6f..580d6632460 100644 --- a/proto/ParameterConfig.proto +++ b/proto/ParameterConfig.proto @@ -27,7 +27,8 @@ enum ParameterInitStrategy { message ParameterUpdaterHookConfig { // hook type such as 'pruning' required string type = 1; - optional double sparsity_ratio = 2 [default = 0.8]; + // this represents the ratio of zero element to be set by the Parameter + optional double sparsity_ratio = 2 [default = 0.6]; } message ParameterConfig { diff --git a/python/paddle/trainer_config_helpers/attrs.py b/python/paddle/trainer_config_helpers/attrs.py index 2e4e082efb0..bf12ad644dc 100644 --- a/python/paddle/trainer_config_helpers/attrs.py +++ b/python/paddle/trainer_config_helpers/attrs.py @@ -65,7 +65,8 @@ class HookAttribute(object): :param type: Hook type, eg: 'pruning' :type type: string - :param sparsity_ratio: Must be specified if hook type is 'pruning' + :param sparsity_ratio: Must be specified if hook type is 'pruning', + it represents the ratio of the zero elements to be set by the Parameter. :type sparsity_ratio: float or None """ -- GitLab