title: Using trained TensorFlow model in Java
body: <p>I have trained a TensorFlow model in Python and would like to use it in Java code. Training the model is done via something like this code:</p>
  
  <pre><code>def input_fn():
      features = {'a': tf.constant([[1],[2]]),
                  'b': tf.constant([[3],[4]]) }
      labels = tf.constant([0, 1])
      return features, labels
  
  feature_a = tf.contrib.layers.sparse_column_with_integerized_feature("a", bucket_size=10)
  feature_b = tf.contrib.layers.sparse_column_with_integerized_feature("b", bucket_size=10)
  feature_columns = [feature_a, feature_b]
  
  model = tf.contrib.learn.LinearClassifier(feature_columns=feature_columns)
  model.fit(input_fn=input_fn, steps=10)
  </code></pre>
  
  <p>Now I want to save this model to use it in Java. It seems that export_savedmodel is the new/preferred way of saving, so I tried:</p>
  
  <pre><code>feature_spec = tf.contrib.layers.create_feature_spec_for_parsing(feature_columns)
  serving_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)
  model.export_savedmodel('export', serving_input_fn, as_text=True)
  </code></pre>
  
  <p>This results in a saved model, which can be loaded from Java with</p>
  
  <pre><code>model = SavedModelBundle.load(dir, "serve");
  model.session().runner()
      .feed("input_example_tensor", input)
      .fetch("linear/binary_logistic_head/predictions/probabilities")
      .run();
  </code></pre>
  
  <p>There is now a problem though: the input_example_tensor should be a Tensor containing Strings/byte[]s, but this is not supported in Java yet (see: Tensor.java#88 "throw new UnsupportedOperationException"). As far as I understand it, the reason that it wants a String is that build_parsing_serving_input_fn wants to parse serialized Example protocol buffers.</p>
  
  <p>Maybe a different serving_input_fn would do better.  <code>input_fn_utils.build_default_serving_input_fn</code> looks promising, but I didn't get that to work.</p>
  
  <p>If I call it like:</p>
  
  <pre><code>features_dict = {'a':feature_a, 'b':feature_b}
  serving_input_fn = input_fn_utils.build_default_serving_input_fn(features)
  </code></pre>
  
  <p>I get "AttributeError: '_SparseColumnIntegerized' object has no attribute 'get_shape'"</p>
  
  <p>If I call it like:</p>
  
  <pre><code>features = {'a': tf.constant([[1],[2]]),
              'b': tf.constant([[3],[4]]) }
  serving_input_fn = input_fn_utils.build_default_serving_input_fn(features)
  </code></pre>
  
  <p>I get "ValueError: 'Const:0' is not a valid scope name".</p>
  
  <p>What is the proper way to use <code>input_fn_utils.build_default_serving_input_fn</code>? I can't find any example that uses it.</p>