在 java 中使用估算器 loading/serving 张量流模型时发出问题
Issue while loading/serving tensorflow model in java using estimators
我使用了人口普查数据,并使用 tensorflow 中的估计器 api 创建了一个广泛而深入的模型。在 Java 中加载模型时,似乎出现错误,导致模型无法加载。异常看起来像
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
Java使用代码如下:
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
当您在 tf.contrib
模块中使用操作时,它们不被视为实验性的,因此不属于 stable TensorFlow API 并且不包含在其他语言发行版中。
但是,在 TensorFlow 1.4 及更高版本中,您可以使用 TensorFlow.loadLibrary()
.
在 Java 中显式加载共享库
为此,首先您需要找到包含您感兴趣的 tf.contrib
操作的实现的共享库的位置。在本例中,它似乎是 tf.contrib.layers
,所以你会做这样的事情:
python -c "import tensorflow; print(tensorflow.contrib.layers.__path__)"
这会打印出如下内容:
['/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers']
然后您将使用类似以下内容找到该路径中的所有共享库:
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:
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
文件与您的程序打包并调整对 [=16= 的调用] 适当。
确保您为 Python 和 Java (1.4)
使用相同的 TensorFlow 版本
希望对您有所帮助。
我使用了人口普查数据,并使用 tensorflow 中的估计器 api 创建了一个广泛而深入的模型。在 Java 中加载模型时,似乎出现错误,导致模型无法加载。异常看起来像
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
Java使用代码如下:
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
当您在 tf.contrib
模块中使用操作时,它们不被视为实验性的,因此不属于 stable TensorFlow API 并且不包含在其他语言发行版中。
但是,在 TensorFlow 1.4 及更高版本中,您可以使用 TensorFlow.loadLibrary()
.
为此,首先您需要找到包含您感兴趣的 tf.contrib
操作的实现的共享库的位置。在本例中,它似乎是 tf.contrib.layers
,所以你会做这样的事情:
python -c "import tensorflow; print(tensorflow.contrib.layers.__path__)"
这会打印出如下内容:
['/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers']
然后您将使用类似以下内容找到该路径中的所有共享库:
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:
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
文件与您的程序打包并调整对 [=16= 的调用] 适当。确保您为 Python 和 Java (1.4)
使用相同的 TensorFlow 版本
希望对您有所帮助。