Baunsgaard commented on code in PR #1848:
URL: https://github.com/apache/systemds/pull/1848#discussion_r1273203554


##########
src/main/python/tests/nn/test_affine.py:
##########
@@ -0,0 +1,134 @@
+# -------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# -------------------------------------------------------------
+
+import unittest
+
+import numpy as np
+from numpy.testing import assert_almost_equal
+
+from systemds.context import SystemDSContext
+from systemds.script_building.script import DMLScript
+from systemds.operator.nn.affine import Affine
+
+dim = 6
+n = 5
+m = 6
+X = np.array([[9., 2., 5., 5., 9., 6.],
+              [0., 8., 8., 0., 5., 7.],
+              [2., 2., 6., 3., 4., 3.],
+              [3., 5., 2., 6., 6., 0.],
+              [3., 8., 5., 2., 5., 2.]])
+
+W = np.array([[8., 3., 7., 2., 0., 1.],
+              [6., 5., 1., 2., 6., 1.],
+              [2., 4., 7., 7., 6., 4.],
+              [3., 8., 9., 3., 5., 6.],
+              [3., 8., 0., 5., 7., 9.],
+              [7., 9., 7., 4., 5., 7.]])
+dout = np.array([[9., 5., 4., 0., 4., 1.],
+                 [1., 2., 2., 3., 3., 9.],
+                 [7., 4., 0., 8., 7., 0.],
+                 [8., 7., 0., 6., 0., 9.],
+                 [1., 6., 5., 8., 8., 9.]])
+
+
+class TestAffine(unittest.TestCase):
+    sds: SystemDSContext = None
+
+    @classmethod
+    def setUpClass(cls):
+        cls.sds = SystemDSContext()
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.sds.close()
+
+    def test_init(self):
+        affine = Affine(self.sds, dim, m, 10)
+        w = affine.weight.compute()
+        self.assertEqual(len(w), 6)
+        self.assertEqual(len(w[0]), 6)
+
+    def test_forward(self):
+        Xm = self.sds.from_numpy(X)
+        Wm = self.sds.from_numpy(W)
+        bm = self.sds.full((1, 6), 0)
+
+        # test class method
+        affine = Affine(self.sds, dim, m, 10)
+        out = affine.forward(Xm).compute()
+        self.assertEqual(len(out), 5)
+        self.assertEqual(len(out[0]), 6)
+
+        # test static method
+        out = Affine.forward(Xm, Wm, bm).compute()
+        expected = np.matmul(X, W)
+        assert_almost_equal(out, expected)
+
+    def test_backward(self):
+        Xm = self.sds.from_numpy(X)
+        Wm = self.sds.from_numpy(W)
+        bm = self.sds.full((1, 6), 0)
+        doutm = self.sds.from_numpy(dout)

Review Comment:
   Then we should leave a TODO specifying to use an loss function.
   But i think it should be possible to naively use the outputs of the forward 
call.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@systemds.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to