-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[demo] Add ability to deploy any of the supported model types (#85)
Resolves #82 Improve smoothness. Dismiss some notifications. Add warning if deploying to main.
- Loading branch information
Showing
16 changed files
with
675 additions
and
198 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
178 changes: 178 additions & 0 deletions
178
demo/client/src/ml-models/__tests__/deploy-model.test.ts
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 |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import assert from 'assert' | ||
import Web3 from 'web3' | ||
import { convertNum } from '../../float-utils' | ||
import { CentroidInfo, ModelDeployer, NearestCentroidModel } from '../deploy-model' | ||
|
||
declare const web3: Web3 | ||
|
||
function assertEqualNumbers(actual: any, expected: any, message?: string | Error): void { | ||
if (web3.utils.isBN(actual)) { | ||
if (web3.utils.isBN(expected)) { | ||
if (message === undefined) { | ||
message = `actual: ${actual} (${typeof actual})\nexpected: ${expected} (${typeof expected})` | ||
} | ||
return assert(actual.eq(expected), message) | ||
} else { | ||
const expectedBN = web3.utils.toBN(expected) | ||
if (message === undefined) { | ||
message = `actual: ${actual} (${typeof actual})\nexpected: ${expected} (${typeof expected}) => BN: ${expectedBN}` | ||
} | ||
return assert(actual.eq(expectedBN), message) | ||
} | ||
} else if (web3.utils.isBN(expected)) { | ||
const actualBN = web3.utils.toBN(actual) | ||
if (message === undefined) { | ||
message = `actual: ${actual} (${typeof actual}) => BN: ${actualBN}\nexpected: ${expected} (${typeof expected})` | ||
} | ||
return assert(actualBN.eq(expected), message) | ||
} else { | ||
if (typeof actual === 'string') { | ||
actual = parseInt(actual) | ||
} | ||
return assert.equal(actual, expected, message) | ||
} | ||
} | ||
|
||
describe("ModelDeployer", () => { | ||
let account: string | ||
const deployer = new ModelDeployer(web3) | ||
|
||
beforeAll(async () => { | ||
const accounts = await web3.eth.getAccounts() | ||
// Pick a random account between 2 and 9 since 0 and 1 are usually used in the browser. | ||
account = accounts[2 + Math.min(Math.floor(Math.random() * 8), 7)] | ||
}) | ||
|
||
it("should deploy Naive Bayes", async () => { | ||
const model = { | ||
classifications: [ | ||
"A", | ||
"B" | ||
], | ||
classCounts: [ | ||
2, | ||
3 | ||
], | ||
featureCounts: [ | ||
[[0, 2], [1, 1]], | ||
[[1, 3], [2, 2]], | ||
], | ||
totalNumFeatures: 9, | ||
smoothingFactor: 1.0, | ||
type: "naive bayes" | ||
} | ||
const m = await deployer.deployModel( | ||
model, | ||
{ | ||
account, | ||
}) | ||
|
||
for (let i = 0; i < model.classifications.length; ++i) { | ||
assert.equal(await m.methods.classifications(i).call(), model.classifications[i]) | ||
assertEqualNumbers(await m.methods.getNumSamples(i).call(), model.classCounts[i]) | ||
for (const [featureIndex, count] of model.featureCounts[i]) { | ||
assertEqualNumbers(await m.methods.getFeatureCount(i, featureIndex).call(), count) | ||
} | ||
} | ||
assertEqualNumbers(await m.methods.getClassTotalFeatureCount(0).call(), 3) | ||
assertEqualNumbers(await m.methods.getClassTotalFeatureCount(1).call(), 5) | ||
}) | ||
|
||
it("should deploy dense Nearest Centroid", async () => { | ||
const model = new NearestCentroidModel( | ||
'dense nearest centroid classifier', | ||
{ | ||
"AA": new CentroidInfo([-1, -1], 2), | ||
"BB": new CentroidInfo([+1, +1], 2), | ||
} | ||
) | ||
const m = await deployer.deployModel( | ||
model, | ||
{ | ||
account, | ||
}) | ||
|
||
let i = -1 | ||
for (let [classification, centroidInfo] of Object.entries(model.intents)) { | ||
++i | ||
assert.equal(await m.methods.classifications(i).call(), classification) | ||
assertEqualNumbers(await m.methods.getNumSamples(i).call(), centroidInfo.dataCount) | ||
for (let j = 0; j < centroidInfo.centroid.length; ++j) { | ||
assertEqualNumbers(await m.methods.getCentroidValue(i, j).call(), convertNum(centroidInfo.centroid[j], web3)) | ||
} | ||
} | ||
}) | ||
|
||
it("should deploy sparse Nearest Centroid", async () => { | ||
const model = new NearestCentroidModel( | ||
'sparse nearest centroid classifier', | ||
{ | ||
"AA": new CentroidInfo([0, +1], 2), | ||
"BB": new CentroidInfo([+1, 0], 2), | ||
} | ||
) | ||
const m = await deployer.deployModel( | ||
model, | ||
{ | ||
account, | ||
}) | ||
|
||
let i = -1 | ||
for (let [classification, centroidInfo] of Object.entries(model.intents)) { | ||
++i | ||
assert.equal(await m.methods.classifications(i).call(), classification) | ||
assertEqualNumbers(await m.methods.getNumSamples(i).call(), centroidInfo.dataCount) | ||
for (let j = 0; j < centroidInfo.centroid.length; ++j) { | ||
assertEqualNumbers(await m.methods.getCentroidValue(i, j).call(), convertNum(centroidInfo.centroid[j], web3)) | ||
} | ||
} | ||
}) | ||
|
||
it("should deploy dense Perceptron", async () => { | ||
const classifications = ["A", "B"] | ||
const weights = [1, -1] | ||
const intercept = 0 | ||
const m = await deployer.deployModel( | ||
{ | ||
type: 'dense perceptron', | ||
classifications, | ||
weights, | ||
intercept, | ||
}, | ||
{ | ||
account, | ||
}) | ||
|
||
for (let i = 0; i < classifications.length; ++i) { | ||
assert.equal(await m.methods.classifications(i).call(), classifications[i]) | ||
} | ||
for (let i = 0; i < weights.length; ++i) { | ||
assertEqualNumbers(await m.methods.weights(i).call(), convertNum(weights[i], web3)) | ||
} | ||
assertEqualNumbers(await m.methods.intercept().call(), convertNum(intercept, web3)) | ||
}) | ||
|
||
it("should deploy sparse Perceptron", async () => { | ||
const classifications = ["AA", "BB"] | ||
const weights = [2, -2] | ||
const intercept = 3 | ||
const m = await deployer.deployModel( | ||
{ | ||
type: 'sparse perceptron', | ||
classifications, | ||
weights, | ||
intercept, | ||
}, | ||
{ | ||
account, | ||
}) | ||
|
||
for (let i = 0; i < classifications.length; ++i) { | ||
assert.equal(await m.methods.classifications(i).call(), classifications[i]) | ||
} | ||
for (let i = 0; i < weights.length; ++i) { | ||
assertEqualNumbers(await m.methods.weights(i).call(), convertNum(weights[i], web3)) | ||
} | ||
assertEqualNumbers(await m.methods.intercept().call(), convertNum(intercept, web3)) | ||
}) | ||
}) |
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
Oops, something went wrong.