001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.interop.onnx;
018
019import ai.onnxruntime.OnnxTensor;
020import ai.onnxruntime.OrtEnvironment;
021import ai.onnxruntime.OrtException;
022import com.oracle.labs.mlrg.olcut.config.Configurable;
023import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
024import com.oracle.labs.mlrg.olcut.provenance.Provenancable;
025import org.tribuo.math.la.SparseVector;
026
027import java.io.Serializable;
028import java.util.List;
029
030/**
031 * Transforms a {@link SparseVector}, extracting the features from it as a {@link OnnxTensor}.
032 * <p>
033 * This usually densifies the example, so can be a lot larger than the input example.
034 * <p>
035 * N.B. ONNX support is experimental, and may change without a major version bump.
036 */
037public interface ExampleTransformer extends Configurable, Provenancable<ConfiguredObjectProvenance>, Serializable {
038
039    /**
040     * Converts a {@link SparseVector} representing the features into a {@link OnnxTensor}.
041     * <p>
042     * It generates it as a single example minibatch.
043     * @param env The OrtEnvironment to create the tensor in.
044     * @param vector The features to convert.
045     * @return A dense OnnxTensor representing this vector.
046     * @throws OrtException if the transformation failed.
047     */
048    public OnnxTensor transform(OrtEnvironment env, SparseVector vector) throws OrtException;
049
050    /**
051     * Converts a list of {@link SparseVector}s representing a batch of features into a {@link OnnxTensor}.
052     *
053     * @param env The OrtEnvironment to create the tensor in.
054     * @param vectors The batch of features to convert.
055     * @return A dense OnnxTensor representing this minibatch.
056     * @throws OrtException if the transformation failed.
057     */
058    public OnnxTensor transform(OrtEnvironment env, List<SparseVector> vectors) throws OrtException;
059
060}