示例#1
0
    def test_mul(self):

        print('test_mul')

        np.testing.assert_almost_equal(
            (kp.arr(2) * kp.arr(3)).numpy(), 
            [6]
        )
示例#2
0
    def test_sub(self):

        print('test_sub')

        np.testing.assert_almost_equal(
            (kp.arr(2) - kp.arr(3)).numpy(), 
            [-1]
        )
示例#3
0
    def test_div(self):

        print('test_div')

        np.testing.assert_almost_equal(
            (kp.arr(2) / kp.arr(3)).numpy(), 
            [0.6666666666666]
        )
示例#4
0
    def test_inplace_mul(self):

        print('test_inplace_mul')

        a = kp.arr(2);
        a *= kp.arr(1.5)
        
        np.testing.assert_almost_equal(
            a.numpy(), 
            [3]
        )
示例#5
0
    def test_inplace_sub(self):

        print('test_inplace_sub')

        a = kp.arr(2);
        a -= kp.arr(1)

        np.testing.assert_almost_equal(
            a.numpy(), 
            [1],
        )
示例#6
0
    def test_inplace_div(self):

        print('test_inplace_div')

        a = kp.arr(2);
        a /= kp.arr(1.5)
        
        np.testing.assert_almost_equal(
            a.numpy(), 
            [1.33333333]
        )
示例#7
0
    def test_shape_attr(self):
        print('test_shape_attr')

        shape = [3, 4, 5]

        
        with self.assertRaises(AttributeError):
            kp.arr(1).shape = [1, 2]

        self.assertEqual(
            kp.arr(1, shape=shape).shape,
            tuple(shape)
        )
示例#8
0
    def test_subscript(self):
        print('test_subscript')

        a = kp.arr('range', shape=[5, 5, 5, 5, 5])
        b = a.numpy()

        
        with self.assertRaises(kp.KarrayError):
            a[..., ...]

        with self.assertRaises(ValueError):
            a[5]

        with self.assertRaises(TypeError):
            a[1.3]

        with self.assertRaises(ValueError):
            print(a[-7])

        with self.assertRaises(ValueError):
            a[..., 5]


        a[1:1]

        c = kp.arr([1, 2])
        c[[1, 0]]

        subscripts = [(1),
                      (-1),
                      (-3),
                      (-3, -3, slice(-1, 0, -1)),
                      (0, ...),
                      (..., 0),
                      (..., 4),
                      (slice(None),slice(None)),
                      (..., slice(1, 2, 3)),
                      (..., (1, 2, 3)),
                      (1, 2, ..., slice(1, 2, 3)),
                      (slice(None), ..., slice(1, 2, 3)),
                      (..., slice(None), slice(1, 2, 3)),
                      (0, 1, 2, 3, 4)]

        for subscript in subscripts:
            np.testing.assert_almost_equal(
                a[subscript].numpy(), 
                b[subscript],
                err_msg=f"error occured with subscript {subscript}"
            )
示例#9
0
    def test_matmul(self):

        print('test_matmul')
        a = kp.arr('range', shape=[3, 3]).reshape([1, 3, 3]).broadcast([2, 3, 3])
        b = kp.arr('range', shape=[3, 2])

        np.testing.assert_almost_equal(
            (a @ b).numpy(), 
            np.array([[[10., 13.],
                       [28., 40.],
                       [46., 67.]],
                      [[10., 13.],
                       [28., 40.],
                       [46., 67.]]])
        )
示例#10
0
    def step(self, x_batch, y_batch):

        drelu = lambda x: kp.relu(x) / (kp.relu(x) + kp.arr(0.000000000001))

        s1 = self.W1 @ x_batch
        s2 = s1 + self.b
        s3 = kp.relu(s2)
        s4 = self.W2 @ s3
        s5 = kp.softmax(s4.reshape([self.batch_size, self.o
                                    ])).reshape([self.batch_size, self.o, 1])
        loss = (s5 - y_batch)
        x_entropy = (-y_batch * kp.log(s5)).mean()

        dW2 = loss @ s3.reshape([self.batch_size, 1, self.h])

        r1 = loss.reshape([self.batch_size, 1, self.o]) @ self.W2
        r2 = r1.reshape([self.batch_size, self.h, 1]) * drelu(s2)

        db = r2

        dW1 = r2 @ x_batch.reshape([self.batch_size, 1, 784])

        self.W1 -= self.learning_rate * dW1.mean(0)
        self.b -= self.learning_rate * db.mean(0)
        self.W2 -= self.learning_rate * dW2.mean(0)
        return x_entropy.mean()
示例#11
0
    def test_mean(self):

        print('test_mean')

        a = np.random.rand(2, 3, 4).astype(np.float32)
        ka = kp.arr(a)

        np.testing.assert_almost_equal(
            ka.mean().numpy(), 
            a.mean()
        )

        np.testing.assert_almost_equal(
            ka.mean(0).numpy(), 
            a.mean(0)
        )

        np.testing.assert_almost_equal(
            ka.mean(1).numpy(), 
            a.mean(1)
        )

        np.testing.assert_almost_equal(
            ka.mean(2).numpy(), 
            a.mean(2)
        )

        np.testing.assert_almost_equal(
            ka.mean(-1).numpy(), 
            a.mean(-1)
        )
示例#12
0
class LinNN:

    learning_rate = kp.arr(0.5)

    def __init__(self, i, h, o):
        self.i = i
        self.h = h
        self.o = o
        self.W1 = kp.arr('rand', shape=[h, i]) - kp.arr(0.5)
        self.b = kp.arr('rand', shape=[h, 1]) - kp.arr(0.5)
        self.W2 = kp.arr('rand', shape=[o, h]) - kp.arr(0.5)

    def step(self, x_batch, y_batch):

        drelu = lambda x: kp.relu(x) / (kp.relu(x) + kp.arr(0.000000000001))

        s1 = self.W1 @ x_batch
        s2 = s1 + self.b
        s3 = kp.relu(s2)
        s4 = self.W2 @ s3
        s5 = kp.softmax(s4.reshape([self.batch_size, self.o
                                    ])).reshape([self.batch_size, self.o, 1])
        loss = (s5 - y_batch)
        x_entropy = (-y_batch * kp.log(s5)).mean()

        dW2 = loss @ s3.reshape([self.batch_size, 1, self.h])

        r1 = loss.reshape([self.batch_size, 1, self.o]) @ self.W2
        r2 = r1.reshape([self.batch_size, self.h, 1]) * drelu(s2)

        db = r2

        dW1 = r2 @ x_batch.reshape([self.batch_size, 1, 784])

        self.W1 -= self.learning_rate * dW1.mean(0)
        self.b -= self.learning_rate * db.mean(0)
        self.W2 -= self.learning_rate * dW2.mean(0)
        return x_entropy.mean()

    def train(self, train, labels, epochs=1, batch_size=64, verbose=20):
        self.batch_size = batch_size
        for k in range(epochs):
            for batch in range(train.shape[0] // batch_size):
                x_batch = train[batch * batch_size:(batch + 1) * batch_size]
                y_batch = labels[batch * batch_size:(batch + 1) * batch_size]

                if batch % verbose == 0:
                    print(self.step(x_batch, y_batch))

    def accuracy(self, x_test, y_test):

        predictions = kp.softmax(
            self.W2
            @ kp.relu(self.W1 @ x_test.reshape([-1, self.i, 1]) + self.b), -2)

        success = predictions.numpy().argmax(-2) == y_test.argmax(-2)
        return success.mean()
示例#13
0
    def test_broadcast(self):

        print('test_broadcast')
        a = kp.arr('range', shape=[1, 4])
        np.testing.assert_almost_equal(
            a.broadcast([3, 4]).numpy(), 
            np.array([[0, 1, 2, 3],
                      [0, 1, 2, 3],
                      [0, 1, 2, 3]])
        )
示例#14
0
    def test_init(self):
        global v, w, y, z, d

        v = kp.arr('randn', shape=(2,))
        w = kp.arr('randn', shape=(2,))
        y = kp.arr('randn', shape=(2,))
        z = kp.arr('randn', shape=(2,))
        d = kp.arr([[1, .3], [0.5, -1]])

        evaluator = np.random.randn(3, 2)

        def f(x):
            x = x @ ((d + v - w)* y / z)
            return -x

        g = kp.graph(f)
        result = g.compile(kp.arr(evaluator))

        v = v.numpy()
        w = w.numpy()
        y = y.numpy()
        z = z.numpy()
        d = d.numpy()

        np.testing.assert_almost_equal(
            result.numpy(), 
            f(evaluator),
            decimal=5,
            err_msg="behavior of graph and function differs"
        )
示例#15
0
 def __init__(self, i, h, o):
     self.i = i
     self.h = h
     self.o = o
     self.W1 = kp.arr('rand', shape=[h, i]) - kp.arr(0.5)
     self.b = kp.arr('rand', shape=[h, 1]) - kp.arr(0.5)
     self.W2 = kp.arr('rand', shape=[o, h]) - kp.arr(0.5)
示例#16
0
    def test_relu(self):
        print('test_relu')

        for k in range(nb_random_checks):
            shape = rand_shape()

            ka = kp.arr('rand', shape=shape)
            na = ka.numpy()

            np.testing.assert_almost_equal(
                kp.relu(ka).numpy(), 
                na * (na > 0),
                err_msg=f"shape = {shape}"
            )
示例#17
0
    def test_reshape(self):
        print('test_reshape')

        with self.assertRaises(kp.KarrayError):
            kp.arr(1).reshape([2])

        with self.assertRaises(kp.KarrayError):
            kp.arr(1).reshape([])

        with self.assertRaises(kp.KarrayError):
            kp.arr(1).reshape(np.array([]))

        for k in range(nb_random_checks):
            shape, size = rand_shape_and_size()
            np.testing.assert_almost_equal(
                kp.arr('range', shape=[size]).reshape(shape).numpy(), 
                np.array(range(size)).reshape(shape),
                err_msg=f"reshape to {shape}"
            )
示例#18
0
    def test_inplace_add(self):

        print('test_inplace_add')

        ka = kp.arr(1)

        ka += kp.arr(2)
        np.testing.assert_almost_equal(
            ka.numpy(), 
            [3]
        )

        for k in range(nb_random_checks):
            shape = rand_shape()

            a = np.random.rand(*shape).astype(np.float32)
            b = np.random.rand(*shape).astype(np.float32)

            ka = kp.arr(a)
            ka += kp.arr(b)

            np.testing.assert_almost_equal(
                ka.numpy(), 
                a + b
            )

        for sa, sb in self.pair_shapes_one_side:
            na = np.random.rand(*sa)
            nb = np.random.rand(*sb)

            a = kp.arr(na)
            b = kp.arr(nb)

            b += a
            np.testing.assert_almost_equal(
                b.numpy(), 
                na + nb,
                err_msg=f"sa = {sa}, sb = {sb}"
            )
示例#19
0
    def test_add(self):

        print('test_add')

        with self.assertRaises(TypeError):
            kp.arr(1) + ''

        with self.assertRaises(TypeError):
            kp.arr(1) + '33'
            
        with self.assertRaises(TypeError):
            kp.arr(1) + 1
            
        with self.assertRaises(TypeError):
            1 + kp.arr(1)


        np.testing.assert_almost_equal(
            (kp.arr(1) + kp.arr(2)).numpy(), 
            [3]
        )

        for k in range(nb_random_checks):
            shape = rand_shape()

            a = np.random.rand(*shape)
            b = np.random.rand(*shape)
            
            np.testing.assert_almost_equal(
                (kp.arr(a) + kp.arr(b)).numpy(), 
                a + b,
                err_msg=f"shape = {shape}"
            )

        for sa, sb in self.pair_shapes:
            na = np.random.rand(*sa)
            nb = np.random.rand(*sb)

            a = kp.arr(na)
            b = kp.arr(nb)

            np.testing.assert_almost_equal(
                (a + b).numpy(), 
                na + nb,
                err_msg=f"sa = {sa}, sb = {sb}"
            )

            np.testing.assert_almost_equal(
                (b + a).numpy(), 
                na + nb,
                err_msg=f"sa = {sa}, sb = {sb}"
            )
示例#20
0
with np.load("C:/Users/kipr/Downloads/mnist.npz") as data:
    train_examples = (data['x_train'].reshape(-1, 28 * 28, 1) / 255).astype(
        np.float32)
    train_labels = np.zeros((60000, 10))
    np.put_along_axis(train_labels, data['y_train'].reshape(-1, 1), 1, 1)
    train_labels = train_labels.astype(np.float32).reshape(-1, 10, 1)
    test_examples = (data['x_test'].reshape(-1, 28 * 28, 1) / 255).astype(
        np.float32)
    test_labels = np.zeros((10000, 10))
    np.put_along_axis(test_labels, data['y_test'].reshape(-1, 1), 1, 1)
    test_labels = test_labels.astype(np.float32).reshape(-1, 10, 1)

nb_examples = len(train_labels)

train = kp.arr(train_examples)
labels = kp.arr(train_labels)


class LinNN:

    learning_rate = kp.arr(0.5)

    def __init__(self, i, h, o):
        self.i = i
        self.h = h
        self.o = o
        self.W1 = kp.arr('rand', shape=[h, i]) - kp.arr(0.5)
        self.b = kp.arr('rand', shape=[h, 1]) - kp.arr(0.5)
        self.W2 = kp.arr('rand', shape=[o, h]) - kp.arr(0.5)
示例#21
0
    def test_print(self):

        print('test_print')
        print(kp.arr(1, shape=[2, 3]))
示例#22
0
py
import kipr as kp
W1 = kp.arr('randn', shape=[20, 30])
W2 = kp.arr('randn', shape=[3, 20])
b = kp.arr('randn', shape=[20, 1])


@kp.graph
def f(x):
    x = kp.relu(W1 @ x + b)
    return kp.softmax(W2 @ x)


f.compile(kp.arr('randn', shape=[12, 30, 1]))
exit()

py
import kipr as kp
v = kp.arr('randn', shape=(2, ))
w = kp.arr('randn', shape=(2, ))
y = kp.arr('randn', shape=(2, ))
z = kp.arr('randn', shape=(2, ))
d = kp.arr([[1, .3], [0.5, -1]])


def f(x):
    x = x @ ((d + v - w) * y / z)
    return kp.relu(-x)


g = kp.graph(f)
示例#23
0
    def test_init(self):
        print('test_init')

        # at least one argument
        with self.assertRaises(TypeError):
            kp.arr()

        # too deep
        with self.assertRaises(TypeError):
            kp.arr([[[[[[[[[[1]]]]]]]]]])

        # data must be a sequence of numbers
        with self.assertRaises(TypeError):
            kp.arr(['a', 'b'])
        
        # shouldn't be any zeros in shape
        with self.assertRaises(kp.KarrayError):
            kp.arr(1, shape=[0, 1])
        
        # shape len must be > 1
        with self.assertRaises(kp.KarrayError):
            kp.arr(1, shape=[])
        
        # shape must be a sequence
        with self.assertRaises(kp.KarrayError):
            kp.arr(1, shape=2)
        
        # shape must be a sequence
        with self.assertRaises(kp.KarrayError):
            kp.arr(1, shape=np.array([]))

        # BUG
        # self.assertTrue(kp.arr(1, shape=(1)))
        
        # sanity check
        self.assertTrue(kp.arr(1))
        
        np.testing.assert_almost_equal(
            kp.arr(1).numpy(), 
            np.array([1]),
            err_msg="simple 1"
        )

        np.testing.assert_almost_equal(
            kp.arr(range(2), shape=[1, 1, 1, 2]).numpy(), 
            np.arange(2).reshape([1, 1, 1, 2]),
            err_msg="simple 2"
        )


        for k in range(nb_random_checks):
            shape = rand_shape()

            np.testing.assert_almost_equal(
                kp.arr('range', shape=shape).numpy(), 
                np.array(range(np.array(shape).prod())).reshape(shape),
                err_msg=f"shape = {shape}"
            )

        for k in range(nb_random_checks):
            shape = rand_shape()

            a = np.random.rand(*shape)
            np.testing.assert_almost_equal(
                kp.arr(a).numpy(), 
                a,
                err_msg=f"shape = {shape}"
            )
示例#24
0
                      orient="auto-start-reverse">
                      <path d="M 0 0 L 10 5 L 0 10 z" />
                     </marker>"""
        for op in self.ops:
            x, y = op.x * svg_width, (self.depth - op.y) * 100 + 10
            width = (len(op.name) + 20) * 4
            for c in op.children:
                x1, y1 = self.ops[c].x * svg_width, (self.depth -
                                                     self.ops[c].y) * 100 + 5
                result += f'<line x1="{x + 50}" y1="{y}" x2="{x1 + 50}" y2="{y1}" stroke="black" marker-end="url(#arrow)"/>'
            result += f'<rect x="{x}" y="{y}" width="{width}" height="50" rx="15" ry="15" stroke="black" fill="#bdd6ff"/>'
            result += f'<text x="{x + width // 2}" y="{y + 25}" text-anchor="middle">{op.name}</text>'
        return result + '</svg>'


W1 = kp.arr(1)
W2 = kp.arr(1)
b = kp.arr(1)


@weak_graph
def f(x, p):
    y = kp.relu(W1 @ x + b)
    y = kp.softmax(W2 @ x + p + y)
    return y


f

# for n in G.nodes(data=True):
#     print(n)