chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from prml import nn
|
||||
from prml.nn.array.broadcast import broadcast_to
|
||||
|
||||
|
||||
class TestBroadcastTo(unittest.TestCase):
|
||||
|
||||
def test_broadcast(self):
|
||||
x = nn.Parameter(np.ones((1, 1)))
|
||||
shape = (5, 2, 3)
|
||||
y = broadcast_to(x, shape)
|
||||
self.assertEqual(y.shape, shape)
|
||||
y.backward(np.ones(shape))
|
||||
self.assertTrue((x.grad == np.ones((1, 1)) * 30).all())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from prml import nn
|
||||
|
||||
|
||||
class TestFlatten(unittest.TestCase):
|
||||
|
||||
def test_flatten(self):
|
||||
self.assertRaises(TypeError, nn.flatten, "abc")
|
||||
self.assertRaises(ValueError, nn.flatten, np.ones(1))
|
||||
|
||||
x = np.random.rand(5, 4)
|
||||
p = nn.Parameter(x)
|
||||
y = p.flatten()
|
||||
self.assertTrue((y.value == x.flatten()).all())
|
||||
y.backward(np.ones(20))
|
||||
self.assertTrue((p.grad == np.ones((5, 4))).all())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from prml import nn
|
||||
|
||||
|
||||
class TestReshape(unittest.TestCase):
|
||||
|
||||
def test_reshape(self):
|
||||
self.assertRaises(ValueError, nn.reshape, 1, (2, 3))
|
||||
|
||||
x = np.random.rand(2, 6)
|
||||
p = nn.Parameter(x)
|
||||
y = p.reshape(3, 4)
|
||||
self.assertTrue((x.reshape(3, 4) == y.value).all())
|
||||
y.backward(np.ones((3, 4)))
|
||||
self.assertTrue((p.grad == np.ones((2, 6))).all())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from prml import nn
|
||||
|
||||
|
||||
class TestSplit(unittest.TestCase):
|
||||
|
||||
def test_split(self):
|
||||
x = np.random.rand(10, 7)
|
||||
a = nn.Parameter(x)
|
||||
b, c = nn.split(a, (3,), axis=-1)
|
||||
self.assertTrue((b.value == x[:, :3]).all())
|
||||
self.assertTrue((c.value == x[:, 3:]).all())
|
||||
b.backward(np.ones((10, 3)))
|
||||
self.assertIs(a.grad, None)
|
||||
c.backward(np.ones((10, 4)))
|
||||
self.assertTrue((a.grad == np.ones((10, 7))).all())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,28 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from prml import nn
|
||||
|
||||
|
||||
class TestTranspose(unittest.TestCase):
|
||||
|
||||
def test_transpose(self):
|
||||
arrays = [
|
||||
np.random.normal(size=(2, 3)),
|
||||
np.random.normal(size=(2, 3, 4))
|
||||
]
|
||||
axes = [
|
||||
None,
|
||||
(2, 0, 1)
|
||||
]
|
||||
|
||||
for arr, ax in zip(arrays, axes):
|
||||
arr = nn.Parameter(arr)
|
||||
arr_t = nn.transpose(arr, ax)
|
||||
self.assertEqual(arr_t.shape, np.transpose(arr.value, ax).shape)
|
||||
da = np.random.normal(size=arr_t.shape)
|
||||
arr_t.backward(da)
|
||||
self.assertEqual(arr.grad.shape, arr.shape)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user