Text Classification
Transformers.js
ONNX
bert
feature-extraction
linguistic-classification
polarity
tense
relation-type
multilingual
onnxruntime-web
Instructions to use cp500/infon-heads with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers.js
How to use cp500/infon-heads with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('text-classification', 'cp500/infon-heads');
Upload js/src/softmax.ts with huggingface_hub
Browse files- js/src/softmax.ts +27 -0
js/src/softmax.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Numerically stable softmax + argmax-with-confidence.
|
| 3 |
+
*
|
| 4 |
+
* Pure JS — no ORT, no DOM. The classifier ONNX emits raw logits
|
| 5 |
+
* because softmax-inside-graph would erase the per-class info we
|
| 6 |
+
* use to surface ``confidence`` to downstream callers.
|
| 7 |
+
*/
|
| 8 |
+
|
| 9 |
+
/** Argmax + softmax probability of the winning class. Numerically
|
| 10 |
+
* stable (subtract max before exp). */
|
| 11 |
+
export function argmaxWithProb(
|
| 12 |
+
logits: Float32Array | number[],
|
| 13 |
+
): { idx: number; prob: number } {
|
| 14 |
+
let maxLogit = -Infinity;
|
| 15 |
+
let argmax = 0;
|
| 16 |
+
for (let i = 0; i < logits.length; i++) {
|
| 17 |
+
if (logits[i] > maxLogit) {
|
| 18 |
+
maxLogit = logits[i];
|
| 19 |
+
argmax = i;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
let z = 0;
|
| 23 |
+
for (let i = 0; i < logits.length; i++) {
|
| 24 |
+
z += Math.exp(logits[i] - maxLogit);
|
| 25 |
+
}
|
| 26 |
+
return { idx: argmax, prob: 1 / z };
|
| 27 |
+
}
|