Ich versuche, das Huggingface-Modell in Onnx zu konvertieren, um Text in einer Java-App zu klassifizieren, kann aber nicht verstehen, warum ich kein Ergebnis sehe (das Ergebnisarray ist einfach leer). readme.md hat einen Link zum Modell und es funktioniert ziemlich gut, aber ich muss einen anderen verwenden, weil es keine unterstützte Sprache ist, die ich brauche.
Funktionierender Beispiel-Python-Code:< /p>
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model_name = "tabularisai/multilingual-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
def predict_sentiment(texts):
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]
print(predict_sentiment(["I absolutely love the new design of this app!", "The customer service was disappointing."]))
Ich habe versucht, einige Modelle auf zwei Arten zu konvertieren:
python -m optimum.exporters.onnx --model tabularisai/multilingual-sentiment-analysis --task sequence-classification onnx_model
und
from optimum.onnxruntime import ORTModelForFeatureExtraction
model = ORTModelForFeatureExtraction.from_pretrained("tabularisai/multilingual-sentiment-analysis", from_transformers=True)
model.save_pretrained("onnx_model")
Gleiches Ergebnis – Ordner „onnx_model“ mit Modell, Vokabeln usw.
nlptown_bert-base-multilingual-uncased-sentiment – Modell aus der Readme-Datei und es funktioniert wie erwartet Sogar ich verwende eine konvertierte Modellvokabulardatei
Java-Codebeispiel:
public void def() {
try (final DocumentCategorizerDL documentCategorizerDL =
new DocumentCategorizerDL(
new File("onnx_model/model.onnx"),
// new File("nlptown_bert-base-multilingual-uncased-sentiment.onnx"),
new File("onnx_model/vocab.txt"),
getCategories(),
new AverageClassificationScoringStrategy(),
new InferenceOptions())) {
final double[] result = documentCategorizerDL.categorize(new String[] {"I absolutely love the new design of this app!", "The customer service was disappointing."});
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}
private Map getCategories() {
final Map categories = new HashMap();
categories.put(0, "Very Negative");
categories.put(1, "Negative");
categories.put(2, "Neutral");
categories.put(3, "Positive");
categories.put(4, "Very Positive");
return categories;
}
Ich weiß nicht, wie wichtig es ist, aber die Opset-Version des Arbeitsmodells ist 11, meine jedoch 14.
Meine opennlp-dl-Bibliotheksversion ist 2.5.1
Leeres Ergebnis Apache OpenNLP ONNX-Modell ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post