使用估计器在Java中加载/服务Tensorflow模型时出现的问题 [英] Issue while loading/serving tensorflow model in java using estimators

查看:58
本文介绍了使用估计器在Java中加载/服务Tensorflow模型时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了人口普查数据,并使用了Tensorflow中的estimators API创建了一个广泛而深入的模型.在用Java加载模型时,似乎出现了一个错误,该错误不允许加载模型.异常看起来像

I used the census data and created a wide and deep model using the estimators api in tensorflow. While loading the model in Java, there seems to be an error which doesn't let the model to be loaded. Exception looks like

Exception in thread "main" org.tensorflow.TensorFlowException: Op type not 
registered 'SparseFeatureCross' in binary running on gmalhotra-mba-2.local. 
Make sure the Op and Kernel are registered in the binary running in this 
process.
at org.tensorflow.SavedModelBundle.load(Native Method)
at org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:39)
at deeplearning.DeepLearningTest.main(DeepLearningTest.java:32)

请找到以下用于保存模型的python代码: https://gist.github.com/gaganmalhotra/cd6a5898b9caf9005a05c8831a9b9153

Please find the python code below used for saving the model: https://gist.github.com/gaganmalhotra/cd6a5898b9caf9005a05c8831a9b9153

使用的Java代码如下:

Java code used is as follows:

    public static void main(String[] args) {
          try (SavedModelBundle b = SavedModelBundle.load("/Users/gagandeep.malhotra/Documents/SampleTF_projects/temporaryModel/1510624417/", "serve")) {


    Session sess = b.session();

                //Create the input sensor 
                  float[][] mat=new float[1][1];
                  mat[0]=new float[]{0.5f};

                // create tensors specific to inputs ....

                Tensor<?> x = (Tensor<?>) Tensor.create(mat);

                //run the model 
                float[][] y = sess.runner()
                        .feed("input", x)
                        .fetch("output")
                        .run()
                        .get(0)
                        .copyTo(new float[1][1]);               

               //print the result
                System.out.println(y[0][0]);
}

PS:使用的Tensorflow版本:1.3

PS : Tensorflow version used: 1.3

推荐答案

tf.contrib 模块中使用操作时,这些操作不被认为是实验性的,因此不属于<一个href ="https://www.tensorflow.org/programmers_guide/version_compat" rel ="noreferrer">稳定的TensorFlow API ,并且未包含在其他语言版本中.

When you use operations in the tf.contrib module, they are not considered to be experimental, thus are not part of the stable TensorFlow API and aren't included in other language distributions.

但是,在TensorFlow 1.4及更高版本中,您可以使用

However, in TensorFlow 1.4 and above you can explicitly load the shared library in Java using TensorFlow.loadLibrary().

为此,首先您需要找到包含您感兴趣的 tf.contrib 操作的实现的共享库的位置.在这种情况下,它看起来像是 tf.contrib.layers ,因此您将执行以下操作:

To do that, first you need to find the location of the shared library that contains the implementation of the tf.contrib operation you're interested in. In this case it seems like it's tf.contrib.layers, so you'd do something like this:

python -c "import tensorflow; print(tensorflow.contrib.layers.__path__)"

将打印以下内容:

['/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers']

然后,您将使用以下方式找到该路径中的所有共享库:

Then you'd find all the shared libraries in that path using something like:

find /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers -name "*.so"

那会是这样的:

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/ops/_sparse_feature_cross_op.so

好的,现在您有了该库,您可以使用以下命令在Java中加载它:

Alright, now you have that library, you can load it in Java using:

public static void main(String[] args) {
    TensorFlow.loadLibrary("/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/ops/_sparse_feature_cross_op.so");

    // And now load the model etc.
}

注意事项:

  • 如果要在其他计算机上运行,​​则希望将上面的 .so 文件与程序打包在一起,并调整对 TensorFlow.loadLibrary()的调用.

  • If you want to run on a different machine, you'd want to package the .so file above with your program and adjust the call to TensorFlow.loadLibrary() appropriately.

确保针对Python和Java(1.4)使用相同的TensorFlow版本

Make sure you're using the same TensorFlow version for Python and Java (1.4)

希望有帮助.

这篇关于使用估计器在Java中加载/服务Tensorflow模型时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆