Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use categorical encoder in multiclass example #50

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/classification/binary_iris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
ClassificationReport,
// Split the dataset
useSplit,
} from "https://deno.land/x/vectorizer@v0.2.3/mod.ts";
} from "https://deno.land/x/vectorizer@v0.3.6/mod.ts";

// Define classes
const classes = ["Setosa", "Versicolor"];
Expand Down
35 changes: 19 additions & 16 deletions examples/classification/iris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Sequential,
setupBackend,
SoftmaxLayer,
tensor,
tensor1D,
tensor2D,
} from "../../mod.ts";
Expand All @@ -21,37 +22,39 @@ import {
ClassificationReport,
// Split the dataset
useSplit,
} from "https://deno.land/x/[email protected]/mod.ts";

// Define classes
const classes = ["setosa", "versicolor", "virginica"];
// One-hot encoding of targets
CategoricalEncoder,
} from "https://deno.land/x/[email protected]/mod.ts";

// Read the training dataset
const _data = Deno.readTextFileSync("examples/classification/iris.csv");
const data = parse(_data);

// Get the predictors (x) and targets (y)
const x = data.map((fl) => fl.slice(0, 2).map(Number));
const y = data.map((fl) => {
const yi = new Array(classes.length).fill(0);
yi[classes.indexOf(fl[4])] = 1;
return yi;
});
const x = data.map((fl) => fl.slice(0, 4).map(Number));
const y_pre = data.map((fl) => fl[4]);

const encoder = new CategoricalEncoder()

const y = encoder.fit(y_pre).transform(y_pre, "f32")

// Split the dataset for training and testing
// @ts-ignore Matrices can be split
const [train, test] = useSplit({ ratio: [7, 3], shuffle: true }, x, y) as [
[typeof x, typeof y],
[typeof x, typeof y],
[typeof x, typeof y]
];

// Setup the CPU backend for Netsaur
await setupBackend(CPU);

console.log(train[1])

// Create a sequential neural network
const net = new Sequential({
// Set number of minibatches to 4
// Set size of output to 4
size: [4, 2],
size: [4, 4],

// Disable logging during training
silent: false,
Expand Down Expand Up @@ -80,11 +83,11 @@ net.train(
[
{
inputs: tensor2D(train[0]),
outputs: tensor2D(train[1]),
outputs: tensor(train[1].data as Float32Array, train[1].shape),
},
],
// Train for 300 epochs
300,
400,
1,
0.02,
);
Expand All @@ -95,8 +98,8 @@ console.log(`training time: ${performance.now() - time}ms`);
const res = await Promise.all(
test[0].map((input) => net.predict(tensor1D(input))),
);
const y1 = res.map((x) => x.data.indexOf(Math.max(...x.data)));
const y0 = test[1].map((x) => x.indexOf(1));
const y1 = res.map((x) => encoder.getOg(x.data.indexOf(Math.max(...x.data))));
const y0 = encoder.untransform(test[1]);

console.log(y1.map((x, i) => [y0[i], x]));
const cMatrix = new ClassificationReport(y0, y1);
Expand Down
2 changes: 1 addition & 1 deletion examples/classification/spam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
TextVectorizer,
// Split the dataset
useSplit,
} from "https://deno.land/x/vectorizer@v0.2.3/mod.ts";
} from "https://deno.land/x/vectorizer@v0.3.6/mod.ts";

// Define classes
const ymap = ["spam", "ham"];
Expand Down
Loading