In this short tutorial, we’ll make use of the following functions for the examples:
make_xxx()
to make different types of protobufs for attributes, nodes, graphs, etc.check()
method that can check whether a protobuf in a particular type is valid.print_readable()
method that can print out the human-readable representation of the proto object.For detailed definitions of each type of ONNX protobufs, please checkout ONNX intermediate representation spec. A list of available operators, e.g. FC
or Relu
used in the following examples to define the nodes, can be found here.
Define a node protobuf and check whether it’s valid:
library(onnx)
<- make_node("Relu", list("X"), list("Y"))
node_def check(node_def)
> node_def
: "X"
input: "Y"
output: "Relu" op_type
Define an attribute protobuf and check whether it’s valid:
<- make_attribute("this_is_an_int", 123L)
attr_def check(attr_def)
> attr_def
: "this_is_an_int"
name: 123
i: INT type
Define a graph protobuf and check whether it’s valid:
<- make_graph(
graph_def nodes = list(
make_node("FC", list("X", "W1", "B1"), list("H1")),
make_node("Relu", list("H1"), list("R1")),
make_node("FC", list("R1", "W2", "B2"), list("Y"))
),name = "MLP",
inputs = list(
make_tensor_value_info('X' , onnx$TensorProto$FLOAT, list(1L)),
make_tensor_value_info('W1', onnx$TensorProto$FLOAT, list(1L)),
make_tensor_value_info('B1', onnx$TensorProto$FLOAT, list(1L)),
make_tensor_value_info('W2', onnx$TensorProto$FLOAT, list(1L)),
make_tensor_value_info('B2', onnx$TensorProto$FLOAT, list(1L))
),outputs = list(
make_tensor_value_info('Y', onnx$TensorProto$FLOAT, list(1L))
)
)check(graph_def)
You can use print_readable()
to print out the human-readable representation of the graph definition:
> print_readable(graph_def)
MLP (
graph 1]
%X[FLOAT, 1]
%W1[FLOAT, 1]
%B1[FLOAT, 1]
%W2[FLOAT, 1]
%B2[FLOAT,
) {%H1 = FC(%X, %W1, %B1)
%R1 = Relu(%H1)
%Y = FC(%R1, %W2, %B2)
return %Y }
Or simply print it out to see the detailed graph definition containing nodes, inputs, and outputs:
> graph_def
node {: "X"
input: "W1"
input: "B1"
input: "H1"
output: "FC"
op_type
}
node {: "H1"
input: "R1"
output: "Relu"
op_type
}
node {: "R1"
input: "W2"
input: "B2"
input: "Y"
output: "FC"
op_type
}: "MLP"
name
input {: "X"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
}
}
input {: "W1"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
}
}
input {: "B1"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
}
}
input {: "W2"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
}
}
input {: "B2"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
}
}
output {: "Y"
name
type {
tensor_type {: FLOAT
elem_type
shape {
dim {: 1
dim_value
}
}
}
} }