如何通过Java在TensorFlow模型中提供稀疏占位符

时间:2019-03-22 09:24:21

标签: java tensorflow

我正在尝试使用TensorFlow中的kNN算法计算给定地址的最佳匹配,效果很好,但是当我尝试导出模型并在Java环境中使用它时,我陷入了困境从Java提供稀疏的Placholder。

这是python部分的简化版本,它返回测试名称和最佳引用名称之间的最小距离。到目前为止,这项工作符合预期。当我导出模型并将其导入Java程序时,它总是返回相同的值(默认为占位符的距离)。我假设python函数sparse_from_word_vec(word_vec)不在模型中,这对我完全有意义,但是那我应该怎么做这个稀疏张量呢?我的输入是单个字符串,我需要创建一个合适的稀疏张量(值)来计算距离。我还寻找了一种在Java端生成稀疏张量的方法,但没有成功。

import tensorflow as tf
import pandas as pd

d = {'NAME': ['max mustermann', 
              'erika musterfrau', 
              'joseph haydn', 
              'johann sebastian bach', 
              'wolfgang amadeus mozart']}

df = pd.DataFrame(data=d)  

input_name = tf.placeholder_with_default('max musterman',(), name='input_name')
output_dist = tf.placeholder(tf.float32, (), name='output_dist')

test_name = tf.sparse_placeholder(dtype=tf.string)
ref_names = tf.sparse_placeholder(dtype=tf.string)

output_dist = tf.edit_distance(test_name, ref_names, normalize=True)

def sparse_from_word_vec(word_vec):
    num_words = len(word_vec)
    indices = [[xi, 0, yi] for xi,x in enumerate(word_vec) for yi,y in enumerate(x)]
    chars = list(''.join(word_vec))
    return(tf.SparseTensorValue(indices, chars, [num_words,1,1]))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    t_data_names=tf.constant(df['NAME'])
    reference_names = [el.decode('UTF-8') for el in (t_data_names.eval())]

    sparse_ref_names = sparse_from_word_vec(reference_names)
    sparse_test_name = sparse_from_word_vec([str(input_name.eval().decode('utf-8'))]*5)

    feeddict={test_name: sparse_test_name,
              ref_names: sparse_ref_names, 
              }    

    output_dist = sess.run(output_dist, feed_dict=feeddict)
    output_dist = tf.reduce_min(output_dist, 0)
    print(output_dist.eval())

    tf.saved_model.simple_save(sess,
                               "model-simple",
                               inputs={"input_name": input_name},
                               outputs={"output_dist": output_dist})

这是我的Java方法:

public void run(ApplicationArguments args) throws Exception {
  log.info("Loading model...");

  SavedModelBundle savedModelBundle = SavedModelBundle.load("/model", "serve");

  byte[] test_name = "Max Mustermann".toLowerCase().getBytes("UTF-8");


  List<Tensor<?>> output = savedModelBundle.session().runner()
      .feed("input_name", Tensor.<String>create(test_names))
      .fetch("output_dist")
      .run();

  System.out.printl("Nearest distance: " + output.get(0).floatValue());

}

1 个答案:

答案 0 :(得分:1)

我能够使您的示例正常工作。在深入之前,我对您的python代码有一些评论。

在整个代码中,变量output_dist用于3种不同的值类型。我不是python专家,但是我认为这是不好的做法。您实际上也永远不会使用input_name占位符,除非将其导出为输入。最后一个是tf.saved_model.simple_save已过时,您应该改用tf.saved_model.Builder

现在解决。

使用命令libtensorflow查看jar tvf libtensorflow-x.x.x.jar jar文件(感谢this发布),您会发现没有用于创建稀疏张量的有用绑定(也许功能要求?)。因此,我们必须将输入更改为密集的张量,然后向图形添加操作以将其转换为稀疏。在您的原始代码中,稀疏转换位于python端,这意味着Java中加载的图形没有任何操作。

这是新的python代码:

import tensorflow as tf
import pandas as pd

def model():
    #use dense tensors then convert to sparse for edit_distance
    test_name = tf.placeholder(shape=(None, None), dtype=tf.string, name="test_name")
    ref_names = tf.placeholder(shape=(None, None), dtype=tf.string, name="ref_names")

    #Java Does not play well with the empty character so use "/" instead
    test_name_sparse = tf.contrib.layers.dense_to_sparse(test_name, "/")
    ref_names_sparse = tf.contrib.layers.dense_to_sparse(ref_names, "/")

    output_dist = tf.edit_distance(test_name_sparse, ref_names_sparse, normalize=True)

    #output the index to the closest ref name
    min_idx = tf.argmin(output_dist)

    return test_name, ref_names, min_idx

#Python code to be replicated in Java
def pad_string(s, max_len):
    return s + ["/"] * (max_len - len(s))

d = {'NAME': ['joseph haydn', 
              'max mustermann', 
              'erika musterfrau', 
              'johann sebastian bach', 
              'wolfgang amadeus mozart']}

df = pd.DataFrame(data=d)  
input_name = 'max musterman'

#pad dense tensor input
max_len = max([len(n) for n in df['NAME']])

test_input = [list(input_name)]*len(df['NAME'])
#no need to pad, all same length
ref_input = list(map(lambda x: pad_string(x, max_len), [list(n) for n in df['NAME']]))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    test_name, ref_names, min_idx = model()

    #run a test to make sure the model works
    feeddict = {test_name: test_input,
                ref_names: ref_input,
            }
    out = sess.run(min_idx, feed_dict=feeddict)
    print("test output:", out)

    #save the model with the new Builder API
    signature_def_map= {
    "predict": tf.saved_model.signature_def_utils.predict_signature_def(
        inputs= {"test_name": test_name, "ref_names": ref_names},
        outputs= {"min_idx": min_idx})
    }

    builder = tf.saved_model.Builder("model")
    builder.add_meta_graph_and_variables(sess, ["serve"], signature_def_map=signature_def_map)
    builder.save()

这是要加载并运行它的Java。这里可能还有很多改进的余地(java不是我的主要语言),但这可以为您带来想法。

import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;
import org.tensorflow.SavedModelBundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class Test {
    public static byte[][] makeTensor(String s, int padding) throws Exception
    {
        int len = s.length();
        int extra = padding - len;

        byte[][] ret = new byte[len + extra][];
        for (int i = 0; i < len; i++) {
            String cur = "" + s.charAt(i);
            byte[] cur_b = cur.getBytes("UTF-8");
            ret[i] = cur_b;
        }

        for (int i = 0; i < extra; i++) {
            byte[] cur = "/".getBytes("UTF-8");
            ret[len + i] = cur;
        }

        return ret;
    }
    public static byte[][][] makeTensor(List<String> l, int padding) throws Exception
    {
        byte[][][] ret = new byte[l.size()][][];
        for (int i = 0; i < l.size(); i++) {
            ret[i] = makeTensor(l.get(i), padding);
        }

        return ret;
    }
    public static void main(String[] args) throws Exception {
        System.out.println("Loading model...");

        SavedModelBundle savedModelBundle = SavedModelBundle.load("model", "serve");


        List<String> str_test_name = Arrays.asList("Max Mustermann",
            "Max Mustermann",
            "Max Mustermann",
            "Max Mustermann",
            "Max Mustermann");
        List<String> names = Arrays.asList("joseph haydn",
            "max mustermann",
            "erika musterfrau",
            "johann sebastian bach",
            "wolfgang amadeus mozart");

        //get the max length for each array
        int pad1 = str_test_name.get(0).length();
        int pad2 = 0;
        for (String var : names) {
            if(var.length() > pad2)
                pad2 = var.length();
        }


        byte[][][] test_name = makeTensor(str_test_name, pad1);
        byte[][][] ref_names = makeTensor(names, pad2);

        //use a with block so the close method is called
        try(Tensor t_test_name = Tensor.<String>create(test_name))
        {
            try (Tensor t_ref_names = Tensor.<String>create(ref_names))
            {
                List<Tensor<?>> output = savedModelBundle.session().runner()
                    .feed("test_name", t_test_name)
                    .feed("ref_names", t_ref_names)
                    .fetch("ArgMin")
                    .run();

                System.out.println("Nearest distance: " + output.get(0).longValue());
            }
        }
    }
}