You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@singa.apache.org by wa...@apache.org on 2023/09/07 02:07:54 UTC
[singa] branch dev-postgresql updated: Add the SumErrorLayer implementation
This is an automated email from the ASF dual-hosted git repository.
wangwei pushed a commit to branch dev-postgresql
in repository https://gitbox.apache.org/repos/asf/singa.git
The following commit(s) were added to refs/heads/dev-postgresql by this push:
new 2cf2a5be Add the SumErrorLayer implementation
new e6cb95f7 Merge pull request #1088 from liuchangshiye/model-selection-psql-msmlp
2cf2a5be is described below
commit 2cf2a5be9c855693f77c09cee87647f20e53874f
Author: liuchangshiye <li...@gmail.com>
AuthorDate: Wed Sep 6 16:40:56 2023 +0800
Add the SumErrorLayer implementation
---
examples/model_selection_psql/msmlp/model.py | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/examples/model_selection_psql/msmlp/model.py b/examples/model_selection_psql/msmlp/model.py
index 81ccd5b0..be898b29 100644
--- a/examples/model_selection_psql/msmlp/model.py
+++ b/examples/model_selection_psql/msmlp/model.py
@@ -32,9 +32,7 @@ np_dtype = {"float16": np.float16, "float32": np.float32}
singa_dtype = {"float16": tensor.float16, "float32": tensor.float32}
-#### self-defined loss begin
-
-### reference from autograd.py
+### refer to autograd.py
class SumError(Operator):
def __init__(self):
@@ -52,7 +50,7 @@ class SumError(Operator):
# self.n *= s
# loss /= self.n
return loss
-
+
def backward(self, dy=1.0):
# dx = self.err
dev = device.get_default_device()
@@ -62,12 +60,21 @@ class SumError(Operator):
dx *= dy
return dx
-### called in the MSMLP class for sum error loss gradients
def se_loss(x):
- # assert x.shape == t.shape, "input and target shape different: %s, %s" % (
- # x.shape, t.shape)
return SumError()(x)[0]
+### refer to layer.py
+class SumErrorLayer(Layer):
+ """
+ Generate a SumError Layer
+ """
+
+ def __init__(self):
+ super(SumErrorLayer, self).__init__()
+
+ def forward(self, x):
+ return se_loss(x)
+
class MSMLP(model.Model):
def __init__(self, data_size=10, perceptron_size=100, num_classes=10):
@@ -79,8 +86,8 @@ class MSMLP(model.Model):
self.linear1 = layer.Linear(perceptron_size)
self.linear2 = layer.Linear(num_classes)
self.softmax_cross_entropy = layer.SoftMaxCrossEntropy()
- self.sum_error = SumErrorLayer()
-
+ self.sum_error = SumErrorLayer() # for synflow backward
+
def forward(self, inputs):
y = self.linear1(inputs)
y = self.relu(y)