Exemple #1
0
def test_simple_tree_prediction():
    test_tree = [
        0,
        5,
        [
            1,
            50,
            [3, 500, [[0.1, 0.2, 0.7], None], [[0.7, 0.2, 0.1], None]],
            [0, 3, [[0.2, 0.3, 0.5], None], [[0.5, 0.3, 0.2], None]]
        ],
        [
            2,
            0.5,
            [4, 0.05, [[0.01, 0.02, 0.97], None], [[0.97, 0.01, 0.02], None]],
            [2, 1, [[0.5, 0.1, 0.4], None], [[0.4, 0.5, 0.1], None]]
        ]
    ]

    test_points = [
        [-1.5, -0.07, 0.0, 0.0, -10.0, 42.0],
        [3.0, 23.456, 50.0, 501.501, -100.0, 42.0],
        [3.0, 90, 22.22, 0.0, 0.0, 42.0],
        [5.001, -10000.0, -10000.0, 0.01, 0.01, -0.042],
        [5.0, 90000, 222.22, -5.0, -50.0, -42.0],
        [6.0, 10000.0, 0.5, 45.32, 0.06, -0.42],
        [4567.0, -100.0, 0.77, -50.0, 0.0, 4.2],
        [123456.789, 100.0, 100.0, 0.0004, 99.99, -420.0]
    ]

    results = np.array([
        test_tree[2][2][2][0],
        test_tree[2][2][3][0],
        test_tree[2][3][2][0],
        test_tree[3][2][2][0],
        test_tree[2][3][3][0],
        test_tree[3][2][3][0],
        test_tree[3][3][2][0],
        test_tree[3][3][3][0]
    ], dtype=np.float32)

    # nodes = to_node_list(test_tree, len(results[0]), 0)
    # nodes.sort(key=lambda x: x['node_id'])

    # assert [n['node_id'] for n in nodes] == list(range(len(nodes)))
    # leaves = to_leaf_list(test_tree, [], [])
    # paths = to_paths(leaves)
    # outputs = propagate(paths, np.array(test_points, dtype=np.float32))

    tree = DecisionTree(test_tree)
    preds1 = tree(constant(test_points))
    preds2 = tree(constant(test_points))

    assert np.array_equal(preds1, results), str((preds1, results))
    assert np.array_equal(preds2, results)
Exemple #2
0
    def __init__(self, network):
        metadata = network["metadata"]
        method = metadata["loading_method"]
        mimg = metadata.get("mean_image", None)

        mean, std = IMAGE_STANDARDIZERS[method]

        self._reverse = method == "channelwise_centering"
        self._mean = constant(mean) if mean != 0 else None
        self._stdev = constant(std) if std != 1 else None
        self._mean_image = constant(mimg) if mimg is not None else None
Exemple #3
0
    def __init__(self, network):
        super(ImageLoader, self).__init__()

        metadata = network['metadata']
        method = metadata['loading_method']
        mimg = metadata['mean_image']
        mean, std = IMAGE_STANDARDIZERS[method]

        self._reverse = method == 'channelwise_centering'
        self._mean = constant(mean) if mean != 0 else None
        self._stdev = constant(std) if std != 1 else None
        self._mean_image = constant(mimg) if mimg is not None else None
Exemple #4
0
    def __init__(self, tree):
        super(DecisionTree, self).__init__()

        node_list = []
        outputs = []

        into_arrays(tree, node_list, outputs)

        self._split_indices = constant([n[0] for n in node_list], tf.int32)
        self._split_values = constant([n[1] for n in node_list])
        self._left = constant([n[2] for n in node_list], tf.int32)
        self._right = constant([n[3] for n in node_list], tf.int32)
        self._outputs = constant(outputs)
Exemple #5
0
def test_simple_tree_prediction():
    test_tree = [
        0,
        5,
        [
            1,
            50,
            [3, 500, [[0.1, 0.2, 0.7], None], [[0.7, 0.2, 0.1], None]],
            [0, 3, [[0.2, 0.3, 0.5], None], [[0.5, 0.3, 0.2], None]],
        ],
        [
            2,
            0.5,
            [4, 0.05, [[0.01, 0.02, 0.97], None], [[0.97, 0.01, 0.02], None]],
            [2, 1, [[0.5, 0.1, 0.4], None], [[0.4, 0.5, 0.1], None]],
        ],
    ]

    test_points = [
        [-1.5, -0.07, 0.0, 0.0, -10.0, 42.0],
        [3.0, 23.456, 50.0, 501.501, -100.0, 42.0],
        [3.0, 90, 22.22, 0.0, 0.0, 42.0],
        [5.001, -10000.0, -10000.0, 0.01, 0.01, -0.042],
        [5.0, 90000, 222.22, -5.0, -50.0, -42.0],
        [6.0, 10000.0, 0.5, 45.32, 0.06, -0.42],
        [4567.0, -100.0, 0.77, -50.0, 0.0, 4.2],
        [123456.789, 100.0, 100.0, 0.0004, 99.99, -420.0],
    ]

    results = np.array(
        [
            test_tree[2][2][2][0],
            test_tree[2][2][3][0],
            test_tree[2][3][2][0],
            test_tree[3][2][2][0],
            test_tree[2][3][3][0],
            test_tree[3][2][3][0],
            test_tree[3][3][2][0],
            test_tree[3][3][3][0],
        ],
        dtype=np.float32,
    )

    tree = DecisionForest([test_tree])
    preds1 = tree(constant(test_points))
    preds2 = tree(constant(test_points))

    assert np.array_equal(preds1, results), str((preds1, results))
    assert np.array_equal(preds2, results)
Exemple #6
0
 def build(self, input_shape):
     self._zero_value = constant(self._values[0])
Exemple #7
0
 def build(self, input_shape):
     self._mean = constant(self._moments[0])
     self._stdev = constant(self._moments[1])
Exemple #8
0
 def build(self, input_shape):
     self._output_tensor = tf.reshape(constant(self._outputs), [1, -1])
Exemple #9
0
    def __call__(self, inputs):
        zero_value = constant(self._values[0])
        output = tf.not_equal(inputs, zero_value)

        return tf.cast(tf.reshape(output, [-1, 1]), tf.float32)
Exemple #10
0
 def __call__(self, inputs):
     mean = constant(self._moments[0])
     stdev = constant(self._moments[1])
     output = (inputs - mean) / stdev
     return tf.cast(tf.reshape(output, [-1, 1]), tf.float32)
Exemple #11
0
 def __init__(self, preprocessor):
     super(CategoricalPreprocessor, self).__init__()
     self._values = constant([preprocessor['values']], tf.string)