Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about input feature mapping #347

Open
paranjapeved15 opened this issue Jul 25, 2023 · 9 comments
Open

Question about input feature mapping #347

paranjapeved15 opened this issue Jul 25, 2023 · 9 comments
Labels
question General question

Comments

@paranjapeved15
Copy link

Ask the question
I am trying to use a onnx model trained in python to run inference on with tribe. I was providing the input to my onnx model in the following format:
float[][] sourceArray = new float[2][7];
for (int i=0; i < 2; i++){
for (int j=0; j < 7; j++) sourceArray[i][j] = 1.0f;
}
OnnxTensor tensor = OnnxTensor.createTensor(env,sourceArray);
Map<String, OnnxTensor> inputs = new HashMap();
inputs.put("input",tensor);

what is the best way in tribuo to do the same? I checked this tutorial (https://tribuo.org/learn/4.3/tutorials/external-models-tribuo-v4.html#Loading-in-an-ONNX-model) but I am not sure how to structure my feature mapping.

Is your question about a specific ML algorithm or approach?
No

Is your question about a specific Tribuo class?
No

System details

  • Tribuo version: 4.3.1
  • Java version (if appropriate): 17
  • OS/Architecture (if appropriate): Mac OS

Additional context
Add any other context or screenshots about the question

@paranjapeved15 paranjapeved15 added the question General question label Jul 25, 2023
@Craigacp
Copy link
Member

Tribuo needs to know what the mapping is from the feature names it produces to the feature indices that the ONNX model expects. If your model expects a feature vector (rather than an image or some other kind of structured input) then you need to pass in a DenseTransformer to the ONNXExternalModel, along with the string -> int mapping. The feature names are user controlled in some senses, they either come from the headers/field names of the csv/db/json data, or are constructed as the string representation of padded indices (i.e. 000, 001 etc).

For your example with a feature vector of [2][7] is that a batch size of 2 and a 7 element feature vector?

@paranjapeved15
Copy link
Author

Yes @Craigacp, the 2 is a batch. So it really needs a float array with 7 elements as input.

@Craigacp
Copy link
Member

Ok, so you'd want to make a Tribuo example containing those 7 features, with appropriate names. Those can be semantic ones if the features actually have names, or just 00,...,06, then supply the mapping as appropriate.

@paranjapeved15
Copy link
Author

okay, but what about the key "input" do I not need that in tribuo?

@paranjapeved15
Copy link
Author

Oh I see. And at inference time what should be the format of my input? What type of object and what function should I be using to run inference on the Model object? @Craigacp

@Craigacp
Copy link
Member

The model exposes a predict function which accepts Iterable<Example<T>> or Example<T>, returning Prediction<T> which contains the predicted values and any confidence scores produced by the model.

@paranjapeved15
Copy link
Author

Any code example which shows how to build an Example object from raw values?

@Craigacp
Copy link
Member

Not specifically, but you can see how the examples are built in all the DataSource implementations, e.g. this one for loading in LibSVM format data - https://github.com/oracle/tribuo/blob/main/Core/src/main/java/org/tribuo/datasource/LibSVMDataSource.java#L348.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

2 participants