英文:
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 = "This is an example sentence";
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("Embedding: " + Arrays.toString(res));
}
See djl-demo for more huggingface examples.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论