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.tensorflow;
018
019import com.oracle.labs.mlrg.olcut.config.Configurable;
020import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
021import com.oracle.labs.mlrg.olcut.provenance.Provenancable;
022import org.tribuo.Example;
023import org.tribuo.ImmutableFeatureMap;
024import org.tribuo.Output;
025import org.tribuo.math.la.SparseVector;
026import org.tensorflow.Tensor;
027
028import java.io.Serializable;
029import java.util.List;
030
031/**
032 * TensorFlow support is experimental, and may change without a major version bump.
033 * <p>
034 * Transforms an {@link Example}, extracting the features from it as a {@link Tensor}.
035 * <p>
036 * This usually densifies the example, so can be a lot larger than the input example.
037 */
038public interface ExampleTransformer<T extends Output<T>> extends Configurable, Provenancable<ConfiguredObjectProvenance>, Serializable {
039
040    /**
041     * Converts an {@link Example} into a {@link Tensor} suitable for supplying as an input to a graph.
042     * <p>
043     * It generates it as a single example minibatch.
044     * @param example The example to convert.
045     * @param featureIDMap The id map to convert feature names into id numbers.
046     * @return A dense Tensor representing this example.
047     */
048    public Tensor<?> transform(Example<T> example, ImmutableFeatureMap featureIDMap);
049
050    /**
051     * Converts a batch of {@link Example}s into a single {@link Tensor} suitable for supplying as
052     * an input to a graph.
053     * @param example The examples to convert.
054     * @param featureIDMap THe id map to convert feature names into id numbers.
055     * @return A dense Tensor representing this minibatch.
056     */
057    public Tensor<?> transform(List<Example<T>> example, ImmutableFeatureMap featureIDMap);
058
059    /**
060     * Converts a {@link SparseVector} representing the features into a {@link Tensor}.
061     * <p>
062     * It generates it as a single example minibatch.
063     * @param vector The features to convert.
064     * @return A dense Tensor representing this vector.
065     */
066    public Tensor<?> transform(SparseVector vector);
067
068    /**
069     * Converts a list of {@link SparseVector}s representing a batch of features into a {@link Tensor}.
070     * <p>
071     * @param vectors The batch of features to convert.
072     * @return A dense Tensor representing this minibatch.
073     */
074    public Tensor<?> transform(List<SparseVector> vectors);
075
076}