Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jul 9, 2021
1 parent 52fe52f commit 2830c62
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
63 changes: 63 additions & 0 deletions _doc/sphinxdoc/source/blog/2021/2021-07-09_csharp.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

.. blogpost::
:title: ONNX from C#
:keywords: ONNX, C#
:date: 2021-07-09
:categories: runtime

This example shows how to compute the predictions of a model
using C#.

::

using System.Collections.Generic;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;

namespace ConsoleAppOnnx
{
class Program
{
static void Main(string[] args)
{
// Loads the model.
var opts = new SessionOptions();
string model_path = "model.onnx";
var session = new InferenceSession(model_path, opts);

// Creating an input tensor (assuming there is only one).
// Get the name of the input and the number of features.
string name = string.Empty;
int n_features = -1;
foreach (var inp in session.InputMetadata)
{
name = inp.Key;
n_features = inp.Value.Dimensions[1];
break;
}

// Creates an empty input.
var dims = new int[] { 1, n_features };
var t = new DenseTensor<float>(dims);
for (int i = 0; i < dims[1]; ++i)
t.SetValue(i, 1.0f / (dims[1] + 1));
var tensor = NamedOnnxValue.CreateFromTensor(name, t);

// Runs the inference.
var inputs = new List<NamedOnnxValue>() { tensor };
using (var outputs = session.Run(inputs))
{
foreach (var o in outputs)
{
DenseTensor<float> to = o.AsTensor<float>().ToDenseTensor();
var values = new float[to.Length];
to.Buffer.CopyTo(values);
// values contains the results.
foreach (var i in values)
System.Console.Write(string.Format("{0}, ", i));
System.Console.WriteLine();
}
}
}
}
}
32 changes: 32 additions & 0 deletions _doc/sphinxdoc/source/blog/2021/2021-07-09_lightgbm.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

.. blogpost::
:title: Convert a Lightgbm dump
:keywords: ONNX, lightgbm, onnxmltools
:date: 2021-07-09
:categories: converters

This example shows how to convert a :epkg:`lightgbm` model
dumped as a text file. It uses :epkg:`lightgbm` to restore
the model, converts it and checks the discrepencies.

::

import numpy
from numpy.testing import assert_almost_equal
import lightgbm
from onnxruntime import InferenceSession
from onnxmltools import convert_lightgbm
from skl2onnx.common.data_types import FloatTensorType

booster = lightgbm.Booster(model_file="model.txt")
n = booster.num_feature()

onx = convert_lightgbm(booster, initial_types=[('input', FloatTensorType([None, n]))])

sess = InferenceSession(onx.SerializeToString())
rnd = numpy.random.random((1, n)).astype(numpy.float32)

expected = booster.predict(rnd)
got = sess.run(None, {'input': rnd})[0]

assert_almost_equal(expected, got.ravel(), decimal=4)

0 comments on commit 2830c62

Please sign in to comment.