<filename>operators/ReduceL2.py<gh_stars>0
#ReduceL2(default_axes_keepdims)

import onnx
from onnx import helper
from onnx import numpy_helper
from onnx import AttributeProto, TensorProto, GraphProto
import numpy as np

# The protobuf definition can be found here:
# https://github.com/onnx/onnx/blob/master/onnx/onnx.proto

X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [3, 2, 2])

# Create one output (ValueInfoProto)
Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 1, 1])

shape = [3, 2, 2]
axes = None
keepdims = 1

# Create a node (NodeProto)
node_def = helper.make_node(
    'ReduceL2',
    inputs=['X'],
    outputs=['Y'],
    keepdims=keepdims
)

# Create the graph (GraphProto)
graph_def = helper.make_graph(
    [node_def],
    'test-model',
    [X],
    [Y],
)

# Create the model (ModelProto)
model_def = helper.make_model(graph_def, producer_name='onnx-reduceL1')

print('The model is:\n{}'.format(model_def))
onnx.checker.check_model(model_def)
print('The model is checked!')

# Save the ONNX model
import os
path = os.getcwd()
new_model_path = os.path.join(path, '../onnx_generated_models/onnx-reduceL1.onnx')
onnx.save(model_def, new_model_path)

print('The model is saved.')



# Preprocessing: load the ONNX model (Loading an already exisisting model)
model_path1 = os.path.join(path, '../onnx_generated_models/onnx-reduceL1.onnx')
onnx_model1 = onnx.load(model_path1)

print('The model is:\n{}'.format(onnx_model1))



import onnxruntime as rt
import numpy as np
sess = rt.InferenceSession("../onnx_generated_models/onnx-reduceL1.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]

y_actual = np.sqrt(np.sum(
    a=np.square(data), axis=axes, keepdims=keepdims == 1))
#print(reduced)
#[[[25.49509757]]]


y_pred = sess.run(
        [label_name], {input_name: data
        })

print("The predicted output for the operation: Reciprocal")
print(y_pred)

y_pred = np.asarray(y_pred) #converting list into an array
print(y_pred.shape)

y_pred = np.squeeze(y_pred, axis=0)
print(y_pred.shape)

compare(y_actual, y_pred)
<filename>operators/ReduceL2.py<gh_stars>0
#ReduceL2(default_axes_keepdims)

import onnx
from onnx import helper
from onnx import numpy_helper
from onnx import AttributeProto, TensorProto, GraphProto
import numpy as np

# The protobuf definition can be found here:
# https://github.com/onnx/onnx/blob/master/onnx/onnx.proto

X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [3, 2, 2])

# Create one output (ValueInfoProto)
Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 1, 1])

shape = [3, 2, 2]
axes = None
keepdims = 1

# Create a node (NodeProto)
node_def = helper.make_node(
    'ReduceL2',
    inputs=['X'],
    outputs=['Y'],
    keepdims=keepdims
)

# Create the graph (GraphProto)
graph_def = helper.make_graph(
    [node_def],
    'test-model',
    [X],
    [Y],
)

# Create the model (ModelProto)
model_def = helper.make_model(graph_def, producer_name='onnx-reduceL1')

print('The model is:\n{}'.format(model_def))
onnx.checker.check_model(model_def)
print('The model is checked!')

# Save the ONNX model
import os
path = os.getcwd()
new_model_path = os.path.join(path, '../onnx_generated_models/onnx-reduceL1.onnx')
onnx.save(model_def, new_model_path)

print('The model is saved.')



# Preprocessing: load the ONNX model (Loading an already exisisting model)
model_path1 = os.path.join(path, '../onnx_generated_models/onnx-reduceL1.onnx')
onnx_model1 = onnx.load(model_path1)

print('The model is:\n{}'.format(onnx_model1))



import onnxruntime as rt
import numpy as np
sess = rt.InferenceSession("../onnx_generated_models/onnx-reduceL1.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
#print(data)
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]

y_actual = np.sqrt(np.sum(
    a=np.square(data), axis=axes, keepdims=keepdims == 1))
#print(reduced)
#[[[25.49509757]]]


y_pred = sess.run(
        [label_name], {input_name: data
        })

print("The predicted output for the operation: Reciprocal")
print(y_pred)

y_pred = np.asarray(y_pred) #converting list into an array
print(y_pred.shape)

y_pred = np.squeeze(y_pred, axis=0)
print(y_pred.shape)

compare(y_actual, y_pred)
#ReduceL2(default_axes_keepdims) # The protobuf definition can be found here: # https://github.com/onnx/onnx/blob/master/onnx/onnx.proto # Create one output (ValueInfoProto) # Create a node (NodeProto) # Create the graph (GraphProto) # Create the model (ModelProto) # Save the ONNX model # Preprocessing: load the ONNX model (Loading an already exisisting model) #print(data) #[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]] #print(reduced) #[[[25.49509757]]] #converting list into an array