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 2020/06/25 08:38:55 UTC
[singa] branch dev updated: issue 707 additional input check for
autograd operator
This is an automated email from the ASF dual-hosted git repository.
wangwei pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/singa.git
The following commit(s) were added to refs/heads/dev by this push:
new 04b2a7b issue 707 additional input check for autograd operator
new ea873fe Merge pull request #751 from dcslin/operator_input_check
04b2a7b is described below
commit 04b2a7bdefe47239a7ced755a623f056adf5101d
Author: dcslin <13...@users.noreply.github.com>
AuthorDate: Thu Jun 25 07:48:04 2020 +0000
issue 707 additional input check for autograd operator
---
python/singa/autograd.py | 17 +++++++++++++++++
test/python/test_operation.py | 13 +++++++++++++
2 files changed, 30 insertions(+)
diff --git a/python/singa/autograd.py b/python/singa/autograd.py
index ddbe46b..3ba09c9 100644
--- a/python/singa/autograd.py
+++ b/python/singa/autograd.py
@@ -711,6 +711,9 @@ def add_bias(x, b, axis=0):
Return:
the result Tensor
"""
+ assert x.ndim() == 2, "1st arg required 2d tensor. got shape: %s" % (x.shape)
+ assert b.ndim() == 1, "2nd arg required 1d tensor. got shape: %s" % (b.shape)
+ assert axis in [0, 1], "allowed axis: 0 or 1"
return AddBias(axis)(x, b)[0]
@@ -1221,6 +1224,8 @@ class QALSTMLoss(Operator):
def qa_lstm_loss(pos, neg, M=0.2):
+ assert pos.shape == neg.shape, "input and target shape different: %s, %s" % (
+ pos.shape, neg.shape)
return QALSTMLoss(M)(pos, neg)[0]
@@ -1244,6 +1249,12 @@ class SoftMaxCrossEntropy(Operator):
def softmax_cross_entropy(x, t):
+ assert x.shape == t.shape, "input and target shape different: %s, %s" % (
+ x.shape, t.shape)
+ assert x.ndim() == 2, "1st arg required 2d tensor. got shape: %s" % (
+ x.shape)
+ assert t.ndim() == 2, "2nd arg required 2d tensor. got shape: %s" % (
+ t.shape)
# x is the logits and t is the ground truth; both are 2D.
return SoftMaxCrossEntropy(t)(x)[0]
@@ -1268,6 +1279,12 @@ class MeanSquareError(Operator):
def mse_loss(x, t):
+ assert x.shape == t.shape, "input and target shape different: %s, %s" % (
+ x.shape, t.shape)
+ assert x.ndim() == 2, "2d input required, input shapes: %s, %s" % (x.shape,
+ t.shape)
+ assert t.ndim() == 2, "2d input required, input shapes: %s, %s" % (x.shape,
+ t.shape)
return MeanSquareError()(x, t)[0]
diff --git a/test/python/test_operation.py b/test/python/test_operation.py
index 30d2661..f7f0b0b 100755
--- a/test/python/test_operation.py
+++ b/test/python/test_operation.py
@@ -3220,6 +3220,19 @@ class TestPythonOperation(unittest.TestCase):
def test_upsample_gpu(self):
self.upsample_helper(gpu_dev)
+ def test_invalid_inputs(self,dev=cpu_dev):
+ _1d = tensor.Tensor((10,), dev)
+ _2d = tensor.Tensor((10,10), dev)
+ _3d = tensor.Tensor((10,10,10), dev)
+ self.assertRaises(AssertionError, autograd.softmax_cross_entropy, _2d,
+ _3d)
+ self.assertRaises(AssertionError, autograd.mse_loss, _2d,
+ _3d)
+ self.assertRaises(AssertionError, autograd.add_bias, _2d,
+ _1d, 3)
+ self.assertRaises(AssertionError, autograd.qa_lstm_loss, _2d,
+ _1d)
+
if __name__ == '__main__':
unittest.main()