-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Revamp
utilities
+ sentiment analysis example (#62)
* chore: bump deps (#60) (#61) * update with tfidf * fix classifier output size * remove dropout * updated model * update tester and analyzer * add commands * remove log * completely revamp utilities * delete split --------- Co-authored-by: Dean Srebnik <[email protected]>
- Loading branch information
Showing
29 changed files
with
396 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,54 @@ | ||
import { CPU, setupBackend, tensor } from "jsr:@denosaurs/[email protected]"; | ||
import { Sequential } from "jsr:@denosaurs/[email protected]/core"; | ||
|
||
import { | ||
useSplit, | ||
ClassificationReport, | ||
MatrixLike, | ||
} from "jsr:@denosaurs/[email protected]/utilities"; | ||
import type { MatrixLike } from "jsr:@denosaurs/[email protected]/utilities"; | ||
|
||
import { CategoricalEncoder } from "jsr:@denosaurs/[email protected]/utilities/encoding"; | ||
import { | ||
CountVectorizer, | ||
SplitTokenizer, | ||
CountVectorizer, | ||
SplitTokenizer, | ||
TfIdfTransformer, | ||
} from "jsr:@denosaurs/[email protected]/utilities/text"; | ||
|
||
import Mappings from "./mappings.json" with {type: "json"} | ||
import Vocab from "./vocab.json" with {type: "json"} | ||
|
||
|
||
console.time("Time Elapsed"); | ||
|
||
console.log("\nImports loaded."); | ||
|
||
import Mappings from "./mappings.json" with { type: "json" }; | ||
import Vocab from "./vocab.json" with { type: "json" }; | ||
import Idf from "./tfidf.json" with { type: "json" }; | ||
|
||
const vocab = new Map(); | ||
|
||
for (const entry of Vocab) { | ||
vocab.set(entry[0], entry[1]) | ||
vocab.set(entry[0], entry[1]); | ||
} | ||
|
||
const tokenizer = new SplitTokenizer({ | ||
skipWords: "english", | ||
vocabulary: vocab, | ||
standardize: { lowercase: true, stripNewlines: true }, | ||
skipWords: "english", | ||
vocabulary: vocab, | ||
standardize: { lowercase: true, stripNewlines: true }, | ||
}); | ||
|
||
const vectorizer = new CountVectorizer(tokenizer.vocabulary.size); | ||
|
||
console.log("\nX vectorized"); | ||
console.timeLog("Time Elapsed"); | ||
const transformer = new TfIdfTransformer({ idf: Float64Array.from(Idf) }); | ||
|
||
const encoder = new CategoricalEncoder<string>(); | ||
const mappings = new Map(); | ||
|
||
for (const entry of Mappings) { | ||
mappings.set(entry[0], entry[1]) | ||
mappings.set(entry[0], entry[1]); | ||
} | ||
|
||
encoder.mapping = mappings; | ||
|
||
console.log("\nCPU Backend Loading"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
await setupBackend(CPU); | ||
|
||
console.log("\nCPU Backend Loaded"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
const net = Sequential.loadFile("examples/sentiment-analysis/sentiment.st") | ||
const net = Sequential.loadFile("examples/sentiment-analysis/sentiment.st"); | ||
|
||
const text = prompt("Text to analyze?") || "hello world" | ||
const text = prompt("Text to analyze?") || "hello world"; | ||
|
||
const predYSoftmax = await net.predict( | ||
tensor(vectorizer.transform(tokenizer.transform([text]), "f32")) | ||
tensor(transformer.transform<"f32">(vectorizer.transform(tokenizer.transform([text]), "f32"))), | ||
); | ||
|
||
CategoricalEncoder.fromSoftmax<"f32">(predYSoftmax as MatrixLike<"f32">); | ||
const predY = encoder.untransform(predYSoftmax as MatrixLike<"f32">); | ||
|
||
console.log(`The sentiment predicted is ${predY[0]}`) | ||
console.log(`The sentiment predicted is ${predY[0]}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,11 @@ import { | |
AdamOptimizer, | ||
Cost, | ||
CPU, | ||
Dropout1DLayer, | ||
Init, | ||
setupBackend, | ||
tensor, | ||
} from "jsr:@denosaurs/[email protected]"; | ||
import { Sequential } from "jsr:@denosaurs/[email protected]/core"; | ||
import { NadamOptimizer } from "jsr:@denosaurs/[email protected]/core/optimizers"; | ||
import { | ||
DenseLayer, | ||
ReluLayer, | ||
|
@@ -18,7 +16,7 @@ import { | |
import { | ||
useSplit, | ||
ClassificationReport, | ||
MatrixLike, | ||
type MatrixLike, | ||
} from "jsr:@denosaurs/[email protected]/utilities"; | ||
|
||
import { CategoricalEncoder } from "jsr:@denosaurs/[email protected]/utilities/encoding"; | ||
|
@@ -103,7 +101,7 @@ Deno.writeTextFileSync( | |
); | ||
Deno.writeTextFileSync( | ||
"examples/sentiment-analysis/tfidf.json", | ||
JSON.stringify(transformer.idf) | ||
JSON.stringify(Array.from(transformer.idf as Float64Array)) | ||
); | ||
|
||
console.log("\nCPU Backend Loading"); | ||
|
@@ -115,7 +113,7 @@ console.log("\nCPU Backend Loaded"); | |
console.timeLog("Time Elapsed"); | ||
|
||
const net = new Sequential({ | ||
size: [4, vecX.nCols], | ||
size: [4, tfidfX.nCols], | ||
layers: [ | ||
DenseLayer({ size: [256], init: Init.Kaiming }), | ||
ReluLayer(), | ||
|
@@ -127,7 +125,6 @@ const net = new Sequential({ | |
ReluLayer(), | ||
DenseLayer({ size: [16], init: Init.Kaiming }), | ||
ReluLayer(), | ||
Dropout1DLayer({ probability: 0.5 }), | ||
DenseLayer({ size: [encoder.mapping.size], init: Init.Kaiming }), | ||
SoftmaxLayer(), | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[["empty",0],["sadness",1],["neutral",2],["worry",3],["surprise",4],["fun",5],["hate",6],["happiness",7],["enthusiasm",8],["love",9],["relief",10],["boredom",11],["anger",12]] | ||
[["empty",0],["sadness",1],["enthusiasm",2],["neutral",3],["worry",4],["surprise",5],["love",6],["fun",7],["hate",8],["happiness",9],["boredom",10],["relief",11],["anger",12]] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,96 +2,79 @@ import { CPU, setupBackend, tensor } from "jsr:@denosaurs/[email protected]"; | |
import { Sequential } from "jsr:@denosaurs/[email protected]/core"; | ||
|
||
import { | ||
useSplit, | ||
ClassificationReport, | ||
MatrixLike, | ||
ClassificationReport, | ||
type MatrixLike, | ||
useSplit, | ||
} from "jsr:@denosaurs/[email protected]/utilities"; | ||
|
||
import { CategoricalEncoder } from "jsr:@denosaurs/[email protected]/utilities/encoding"; | ||
import { | ||
CountVectorizer, | ||
SplitTokenizer, | ||
CountVectorizer, | ||
SplitTokenizer, | ||
TfIdfTransformer, | ||
} from "jsr:@denosaurs/[email protected]/utilities/text"; | ||
|
||
import Mappings from "./mappings.json" with {type: "json"} | ||
import Vocab from "./vocab.json" with {type: "json"} | ||
import Mappings from "./mappings.json" with { type: "json" }; | ||
import Vocab from "./vocab.json" with { type: "json" }; | ||
import Idf from "./tfidf.json" with { type: "json" }; | ||
|
||
import { parse as parseCsv } from "jsr:@std/[email protected]/parse"; | ||
|
||
|
||
console.time("Time Elapsed"); | ||
|
||
console.log("\nImports loaded."); | ||
|
||
const file = Deno.readTextFileSync( | ||
"examples/sentiment-analysis/text_emotion.csv" | ||
); | ||
|
||
console.log("\nData file loaded."); | ||
console.timeLog("Time Elapsed"); | ||
|
||
const data = parseCsv(file, { skipFirstRow: true }) as { | ||
"examples/sentiment-analysis/text_emotion.csv", | ||
); | ||
|
||
const data = parseCsv(file, { skipFirstRow: true }) as { | ||
sentiment: string; | ||
content: string; | ||
}[]; | ||
const text = data.map((x) => x.content); | ||
}[]; | ||
const text = data.map((x) => x.content); | ||
const labels = data.map((x) => x.sentiment); | ||
|
||
console.log("\nCSV Parsed"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
const [[trainX, trainY], [testX, testY]] = useSplit( | ||
{ shuffle: true, ratio: [7, 3] }, | ||
text, | ||
labels | ||
const [[_trainX, _trainY], [testX, testY]] = useSplit( | ||
{ shuffle: true, ratio: [7, 3] }, | ||
text, | ||
labels, | ||
); | ||
|
||
console.log("Data Split"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
const vocab = new Map(); | ||
|
||
for (const entry of Vocab) { | ||
vocab.set(entry[0], entry[1]) | ||
vocab.set(entry[0], entry[1]); | ||
} | ||
|
||
const tokenizer = new SplitTokenizer({ | ||
skipWords: "english", | ||
vocabulary: vocab, | ||
standardize: { lowercase: true, stripNewlines: true }, | ||
skipWords: "english", | ||
vocabulary: vocab, | ||
standardize: { lowercase: true, stripNewlines: true }, | ||
}); | ||
|
||
const vectorizer = new CountVectorizer(tokenizer.vocabulary.size); | ||
|
||
console.log("\nX vectorized"); | ||
console.timeLog("Time Elapsed"); | ||
const transformer = new TfIdfTransformer({ idf: Float64Array.from(Idf) }); | ||
|
||
const encoder = new CategoricalEncoder<string>(); | ||
const mappings = new Map(); | ||
|
||
for (const entry of Mappings) { | ||
mappings.set(entry[0], entry[1]) | ||
mappings.set(entry[0], entry[1]); | ||
} | ||
|
||
encoder.mapping = mappings; | ||
|
||
console.log("\nCPU Backend Loading"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
await setupBackend(CPU); | ||
|
||
console.log("\nCPU Backend Loaded"); | ||
console.timeLog("Time Elapsed"); | ||
|
||
const net = Sequential.loadFile("examples/sentiment-analysis/sentiment.st") | ||
const net = Sequential.loadFile("examples/sentiment-analysis/sentiment.st"); | ||
|
||
const predYSoftmax = await net.predict( | ||
tensor(vectorizer.transform(tokenizer.transform(testX), "f32")) | ||
tensor( | ||
transformer.transform<"f32">( | ||
vectorizer.transform(tokenizer.transform(testX), "f32"), | ||
), | ||
), | ||
); | ||
|
||
CategoricalEncoder.fromSoftmax<"f32">(predYSoftmax as MatrixLike<"f32">); | ||
const predY = encoder.untransform(predYSoftmax as MatrixLike<"f32">); | ||
|
||
console.log(new ClassificationReport(testY, predY)); | ||
|
||
console.log(testY, predY) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.