File size: 1,909 Bytes
dc2106c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

import numpy as np

import onnx
from onnx.backend.test.case.base import Base
from onnx.backend.test.case.node import expect


def bernoulli_reference_implementation(x, dtype):  # type: ignore
    # binomial n = 1 equal bernoulli
    # This example and test-case is for informational purpose. The generator operator is
    # non-deterministic and may not produce the same values in different implementations
    # even if a seed is specified.
    return np.random.binomial(1, p=x).astype(dtype)


class Bernoulli(Base):
    @staticmethod
    def export_bernoulli_without_dtype() -> None:
        node = onnx.helper.make_node(
            "Bernoulli",
            inputs=["x"],
            outputs=["y"],
        )

        x = np.random.uniform(0.0, 1.0, 10).astype(float)
        y = bernoulli_reference_implementation(x, float)
        expect(node, inputs=[x], outputs=[y], name="test_bernoulli")

    @staticmethod
    def export_bernoulli_with_dtype() -> None:
        node = onnx.helper.make_node(
            "Bernoulli",
            inputs=["x"],
            outputs=["y"],
            dtype=onnx.TensorProto.DOUBLE,
        )

        x = np.random.uniform(0.0, 1.0, 10).astype(np.float32)
        y = bernoulli_reference_implementation(x, float)
        expect(node, inputs=[x], outputs=[y], name="test_bernoulli_double")

    @staticmethod
    def export_bernoulli_with_seed() -> None:
        seed = float(0)
        node = onnx.helper.make_node(
            "Bernoulli",
            inputs=["x"],
            outputs=["y"],
            seed=seed,
        )

        x = np.random.uniform(0.0, 1.0, 10).astype(np.float32)
        y = bernoulli_reference_implementation(x, np.float32)
        expect(node, inputs=[x], outputs=[y], name="test_bernoulli_seed")