summaryrefslogtreecommitdiffstats
path: root/mlplib/batch_test.py
blob: 6eb2ad7d2dfab3cd87cef8e13136e1cee7b8501c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
# -*- coding: utf-8 -*-
#
# Copyright 2021 Michael Büsch <m@bues.ch>
#
# Licensed under the Apache License version 2.0
# or the MIT license, at your option.
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
"""

from mlplib.batch import *
import numpy as np

def test_minibatches():
    x = np.array([[1, 2],
                  [3, 4],
                  [5, 6],
                  [7, 8],
                  [9, 10]])
    y = np.array([[10, 20],
                  [30, 40],
                  [50, 60],
                  [70, 80],
                  [90, 100]])
    m = list(minibatches(x, y, 2))
    assert np.all(m[0][0] == np.array([[1, 2], [3, 4]]))
    assert np.all(m[1][0] == np.array([[5, 6], [7, 8]]))
    assert np.all(m[2][0] == np.array([[9, 10]]))
    assert np.all(m[0][1] == np.array([[10, 20], [30, 40]]))
    assert np.all(m[1][1] == np.array([[50, 60], [70, 80]]))
    assert np.all(m[2][1] == np.array([[90, 100]]))

def test_proportion_equal():
    a = np.array([[1, 2, 3], [4, 5, 6]])
    b = np.array([[1.1, 2, 3.3], [4, -5, 6]])
    p = proportion_equal(a, b)
    assert p == 0.5

def test_array_to_boolint():
    a = array_to_boolint(np.array([[0.0, 0.1, -0.1, 0.74, 0.75, 0.76, 0.9, 1.0],
                                   [0.9, 0.5, 0.7, 0.6, 0.5, 0.4, 0.8, 0.1]]))
    assert np.all(a == np.array([[0, 0, 0, 0, 1, 1, 1, 1],
                                 [1, 0, 0, 0, 0, 0, 1, 0]]))

def test_collapse_bool_nodes():
    a = collapse_bool_nodes(np.array([[0.9, 0.1, 0.1],
                                      [0.1, 0.9, 0.1],
                                      [0.1, 0.1, 0.9],
                                      [0.9, 0.9, 0.9]]))
    assert np.all(a == np.array([1, 2, 4, 7]))

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface