Пример #1
0
 def test_relu_node(self):
     node_def = helper.make_node(
         "Relu", ["X"], ["Y"])
     X = np.random.randn(3, 2).astype(np.float32)
     # Testing with a list
     output = c2.run_node(
         node_def, [X])
     Y_ref = np.clip(X, 0, np.inf)
     np.testing.assert_almost_equal(output["Y"], Y_ref)
     # Testing with a dictionary
     output = c2.run_node(
         node_def, {"X": X})
     Y_ref = np.clip(X, 0, np.inf)
     np.testing.assert_almost_equal(output["Y"], Y_ref)
Пример #2
0
    def test_gemm(self):
        # simple
        A = np.random.randn(3, 2).astype(np.float32)
        B = np.random.randn(2, 4).astype(np.float32)
        C = np.random.randn(3, 4).astype(np.float32)
        node_def = make_node('Gemm', ['A', 'B', 'C'], ["Y"])
        output = c2.run_node(node_def, [A, B, C])
        np.testing.assert_almost_equal(output["Y"], np.dot(A, B) + C)

        # transA
        A = np.transpose(A)
        node_def = make_node('Gemm', ['A', 'B', 'C'], ["Y"], transA=True)
        output = c2.run_node(node_def, [A, B, C])
        np.testing.assert_almost_equal(output["Y"],
                                       np.dot(np.transpose(A), B) + C)
        # revert A
        A = np.transpose(A)

        # transB
        B = np.transpose(B)
        node_def = make_node('Gemm', ['A', 'B', 'C'], ["Y"], transB=True)
        output = c2.run_node(node_def, [A, B, C])
        np.testing.assert_almost_equal(output["Y"],
                                       np.dot(A, np.transpose(B)) + C)
        # revert A
        B = np.transpose(B)

        # scale
        alpha = np.random.random()
        beta = np.random.random()
        node_def = make_node('Gemm', ['A', 'B', 'C'], ["Y"],
                             alpha=alpha,
                             beta=beta)
        output = c2.run_node(node_def, [A, B, C])
        np.testing.assert_almost_equal(output["Y"],
                                       alpha * np.dot(A, B) + beta * C)

        # broadcast
        C = np.random.randn(4).astype(np.float32)
        node_def = make_node('Gemm', ['A', 'B', 'C'], ["Y"],
                             alpha=alpha,
                             beta=beta,
                             broadcast=1)
        output = c2.run_node(node_def, [A, B, C])
        np.testing.assert_almost_equal(output["Y"],
                                       alpha * np.dot(A, B) + beta * C)
Пример #3
0
    def test_relu_graph(self):
        X = np.random.randn(3, 2).astype(np.float32)
        Y_ref = np.clip(X, 0, np.inf)

        node_def = make_node(
            "Relu", ["X"], ["Y"])
        output = c2.run_node(
            node_def, {"X": X})
        np.testing.assert_almost_equal(output.Y, Y_ref)

        graph_def = make_graph(
            [node_def],
            name="test",
            inputs=[make_tensor_value_info("X", onnx.TensorProto.FLOAT, [3, 2])],
            outputs=[make_tensor_value_info("Y", onnx.TensorProto.FLOAT, [3, 2])])
        c2_rep = c2.prepare(make_model(graph_def))
        output = c2_rep.run(X)
        np.testing.assert_almost_equal(output.Y, Y_ref)
Пример #4
0
    def test_relu_node_inplace(self):
        node_def = helper.make_node(
            "Relu", ["X"], ["Y"], consumed_inputs=[1])
        X = np.random.randn(3, 2).astype(np.float32)
        output = c2.run_node(
            node_def, {"X": X})
        graph_def = helper.make_graph(
            [node_def],
            name="test",
            inputs=["X"],
            outputs=["X", "Y"])
        Y_ref = np.clip(X, 0, np.inf)
        c2_rep = c2.prepare(graph_def)
        output = c2_rep.run({"X": X})
        # With the inplace change from Zach, there shouldn't be Y
        # np.testing.assert_almost_equal(output["Y"], Y_ref)

        # ensure  we wrote over X
        np.testing.assert_almost_equal(output["X"], Y_ref)