You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tf.Estimators的TensorFlow Serving函数从Java调用时出错

Fixing the "input_example_tensor" Serialized Example Error in Java TensorFlow Predictions

Hey there! Let's work through this issue together. The error message is crystal clear—your exported model expects the input_example_tensor placeholder to receive serialized TensorFlow Example protobuf values (since it's dtype string and shape [?]). Here's how to construct that properly in Java using Protocol Buffers:

Step 1: Ensure You Have the Right Dependencies

First, make sure your project includes the necessary TensorFlow and protobuf libraries. If you're using Maven, add these to your pom.xml:

<dependencies>
    <!-- TensorFlow Java Core API -->
    <dependency>
        <groupId>org.tensorflow</groupId>
        <artifactId>tensorflow-core-api</artifactId>
        <version>2.15.0</version> <!-- Use the version matching your model's TF version -->
    </dependency>
    <!-- TensorFlow Protobuf Definitions (for Example/Feature classes) -->
    <dependency>
        <groupId>org.tensorflow</groupId>
        <artifactId>tensorflow-proto</artifactId>
        <version>2.15.0</version>
    </dependency>
</dependencies>

Step 2: Build a TensorFlow Example Object

The Example proto is how TensorFlow represents structured input data. You'll need to define the features your model expects, wrap them in a Features object, then create the Example.

For example, if your model expects a feature named "input_text" (string type) and "input_label" (integer type), here's how to build the Example:

import org.tensorflow.proto.Example;
import org.tensorflow.proto.Feature;
import org.tensorflow.proto.Features;
import org.tensorflow.proto.ByteString;
import org.tensorflow.proto.Int64List;
import org.tensorflow.proto.BytesList;

// 1. Create individual features matching your model's input schema
Feature textFeature = Feature.newBuilder()
    .setBytesList(BytesList.newBuilder()
        .addValue(ByteString.copyFromUtf8("Your input text here"))
        .build())
    .build();

Feature labelFeature = Feature.newBuilder()
    .setInt64List(Int64List.newBuilder()
        .addValue(1)
        .build())
    .build();

// 2. Wrap features into a Features container
Features features = Features.newBuilder()
    .putFeature("input_text", textFeature)
    .putFeature("input_label", labelFeature)
    .build();

// 3. Assemble the final Example object
Example example = Example.newBuilder()
    .setFeatures(features)
    .build();

Step 3: Serialize the Example to Bytes

Convert the Example object into a byte array—this is the raw data the input_example_tensor is expecting:

byte[] serializedExample = example.toByteArray();

Step 4: Create the Tensor for Prediction

Now create a string tensor that holds this serialized example. Since the shape is [?] (variable-length 1D), you can create a tensor with a single element (or multiple for batch prediction):

import org.tensorflow.Tensor;
import org.tensorflow.types.TString;

// For single example prediction
try (Tensor<TString> inputTensor = TString.tensorOfBytes(new byte[][]{serializedExample})) {
    // Pass this tensor to your prediction stub's predict method
    // Example: yourPredictionStub.predict(inputTensor);
}

For batch prediction, just add more serialized example byte arrays to the input array:

byte[][] batchExamples = {serializedExample1, serializedExample2, serializedExample3};
try (Tensor<TString> batchInputTensor = TString.tensorOfBytes(batchExamples)) {
    // Run batch prediction logic here
}

Key Notes

  • Double-check that feature names and data types exactly match what your model was trained with. If your model expects float features, use setFloatList() instead of setBytesList() or setInt64List().
  • Always align your Java TensorFlow dependency version with the version used to export the model—mismatched versions can cause hidden protobuf serialization issues.

内容的提问来源于stack exchange,提问作者gagan malhotra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:53:31