Make softmax optional
authorjijoong.moon <jijoong.moon@samsung.com>
Sun, 15 Mar 2020 23:40:27 +0000 (08:40 +0900)
committer문지중/On-Device Lab(SR)/Principal Engineer/삼성전자 <jijoong.moon@samsung.com>
Sun, 15 Mar 2020 23:55:15 +0000 (08:55 +0900)
make softmax optional when the output is calculated

Resolves:

**Self evaluation:**
1. Build test:  [X]Passed [ ]Failed [ ]Skipped
2. Run test:  [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
src/layers.cpp
src/tensor.cpp

index 21fdfe5..fed84da 100644 (file)
@@ -289,7 +289,9 @@ Tensor OutputLayer::forwarding(Tensor input, Tensor output) {
   Input = input;
   hidden = input.dot(Weight).add(Bias).applyFunction(activation);
   Tensor Y2 = output;
-  Tensor Y = hidden.softmax();
+  Tensor Y = hidden;
+  if (softmax)
+    Y = Y.softmax();
   float lossSum = 0.0;
 
   switch (cost) {
@@ -385,11 +387,10 @@ void OutputLayer::setOptimizer(Optimizer opt) {
 Tensor OutputLayer::backwarding(Tensor label, int iteration) {
   float lossSum = 0.0;
   Tensor Y2 = label;
-  Tensor Y;
+  Tensor Y = hidden;
   if (softmax)
-    Y = hidden.softmax();
-  else
-    Y = hidden;
+    Y = Y.softmax();
+
   Tensor ret;
   Tensor dJdB;
 
index fec5001..610f405 100644 (file)
@@ -531,12 +531,19 @@ Tensor Tensor::softmax() const {
     }
   }
 
+  for (int k = 0; k < batch; ++k) {
+    int index = k * height;
+    for (int i = 1; i < height; ++i) {
+      divisor.data[index] += divisor.data[index + i];
+    }
+  }
+
   for (int k = 0; k < batch; k++) {
     int index = k * height;
     for (int i = 0; i < height; i++) {
       for (int j = 0; j < width; j++) {
         int id = k * height * width + i * width + j;
-        result.data[id] = exp(this->data[id]) / divisor.data[index + i];
+        result.data[id] = exp(this->data[id]) / divisor.data[index];
       }
     }
   }