如何使用预训练的Hugging Face all-MiniLM-L6-v2模型,使用Java Deep Java Library。

huangapple go评论61阅读模式
英文:

How to use Pretrained Hugging face all-MiniLM-L6-v2 mode using java Deep Java Library

问题

如何使用预训练的Hugging Face all-MiniLM-L6-v2模型,使用Java语言。能够加载模型,但在进行预测时遇到问题。尝试用字符串输入和浮点输出编写自定义的翻译器,但未成功。有关Translator的任何示例将会有所帮助。

英文:

How to use Pretrained Hugging face all-MiniLM-L6-v2 mode using java. Was able to load the model but facing issues when predicting.Tried writing a custom translator with String input and float output but didnt work .Any examples with Translator would help.

答案1

得分: 1

你可以使用DJL的内置TextEmbeddingTranslatorFactory

String text = "这是一个示例句子";

Criteria<String, float[]> criteria = Criteria.builder()
        .setTypes(String.class, float[].class)
        .optModelUrls("djl://ai.djl.huggingface.pytorch/sentence-transformers/all-MiniLM-L6-v2")
        .optEngine("PyTorch")
        .optTranslatorFactory(new TextEmbeddingTranslatorFactory())
        .build();

try (ZooModel<String, float[]> model = criteria.loadModel();
        Predictor<String, float[]> predictor = model.newPredictor()) {
     float[] res = predictor.predict(text);
     System.out.println("嵌入: " + Arrays.toString(res));
}

更多huggingface示例,请查看djl-demo

英文:

You can use DJL's built-in TextEmbeddingTranslatorFactory:

String text = &quot;This is an example sentence&quot;;

Criteria&lt;String, float[]&gt; criteria = Criteria.builder()
        .setTypes(String.class, float[].class)
        .optModelUrls(&quot;djl://ai.djl.huggingface.pytorch/sentence-transformers/all-MiniLM-L6-v2&quot;)
        .optEngine(&quot;PyTorch&quot;)
        .optTranslatorFactory(new TextEmbeddingTranslatorFactory())
        .build();

try (ZooModel&lt;String, float[]&gt; model = criteria.loadModel();
        Predictor&lt;String, float[]&gt; predictor = model.newPredictor()) {
     float[] res = predictor.predict(text);
     System.out.println(&quot;Embedding: &quot; + Arrays.toString(res));
}

See djl-demo for more huggingface examples.

huangapple
  • 本文由 发表于 2023年5月18日 13:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定