Skip to content

Commit

Permalink
demo/add: Start methods to load models #82
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Harris committed Mar 6, 2020
1 parent c3efc5a commit f8f3c1d
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 22 deletions.
3 changes: 2 additions & 1 deletion demo/client/src/components/addModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class AddModel extends React.Component {
<Typography component="p">
If you want to use a model that is already deployed, then you can add its information <Link href='/addDeployedModel'>here</Link>.
</Typography>
{/* TODO Recommend using a test net for the first time if the network is not a test net. */}
<form className={this.classes.container} noValidate autoComplete="off">
<div className={this.classes.form} >
<TextField
Expand Down Expand Up @@ -613,7 +614,7 @@ class AddModel extends React.Component {
from: account,
})

const pleaseAcceptKey = this.notify("Please accept to deploy the incentive mechanism contract");
const pleaseAcceptKey = this.notify("Please accept the prompt to deploy the incentive mechanism contract")
const result = imContract.deploy({
data: contractInfo.bytecode,
arguments: args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const NearestCentroidClassifier = artifacts.require("./classification/NearestCen
const SparseNearestCentroidClassifier = artifacts.require("./classification/SparseNearestCentroidClassifier")
const SparsePerceptron = artifacts.require("./classification/SparsePerceptron")

const { convertData, convertNum } = require('../../src/float-utils-node')
const { convertData, convertNum } = require('../float-utils-node')

const _toFloat = 1E9

async function loadDensePerceptron(model, web3, toFloat) {
async function deployDensePerceptron(model, web3, toFloat) {
let gasUsed = 0
const weightChunkSize = 450
const { classifications } = model
Expand All @@ -36,7 +36,7 @@ async function loadDensePerceptron(model, web3, toFloat) {
}
}

async function loadSparsePerceptron(model, web3, toFloat) {
async function deploySparsePerceptron(model, web3, toFloat) {
const weightChunkSize = 300
const { classifications } = model
const weights = convertData(model.weights, web3, toFloat)
Expand All @@ -62,7 +62,7 @@ async function loadSparsePerceptron(model, web3, toFloat) {
}
}

async function loadNearestCentroidClassifier(model, web3, toFloat) {
async function deployNearestCentroidClassifier(model, web3, toFloat) {
let gasUsed = 0
const classifications = []
const centroids = []
Expand Down Expand Up @@ -105,7 +105,7 @@ async function loadNearestCentroidClassifier(model, web3, toFloat) {
})
}

exports.loadSparseNearestCentroidClassifier = async function (model, web3, toFloat) {
exports.deploySparseNearestCentroidClassifier = async function (model, web3, toFloat) {
let gasUsed = 0
const initialChunkSize = 500
const chunkSize = 500
Expand Down Expand Up @@ -167,7 +167,7 @@ exports.loadSparseNearestCentroidClassifier = async function (model, web3, toFlo
})
}

async function loadNaiveBayes(model, web3, toFloat) {
async function deployNaiveBayes(model, web3, toFloat) {
let gasUsed = 0
const initialFeatureChunkSize = 150
const featureChunkSize = 350
Expand Down Expand Up @@ -213,20 +213,20 @@ async function loadNaiveBayes(model, web3, toFloat) {
* @returns The contract for the model, an instance of `Classifier64`
* along with the the total amount of gas used to deploy the model.
*/
exports.loadModel = async function (path, web3, toFloat = _toFloat) {
exports.deployModel = async function (path, web3, toFloat = _toFloat) {
const model = JSON.parse(fs.readFileSync(path, 'utf8'))
switch (model.type) {
case 'dense perceptron':
return loadDensePerceptron(model, web3, toFloat)
return deployDensePerceptron(model, web3, toFloat)
case 'naive bayes':
return loadNaiveBayes(model, web3, toFloat)
return deployNaiveBayes(model, web3, toFloat)
case 'dense nearest centroid classifier':
case 'nearest centroid classifier':
return loadNearestCentroidClassifier(model, web3, toFloat)
return deployNearestCentroidClassifier(model, web3, toFloat)
case 'sparse nearest centroid classifier':
return exports.loadSparseNearestCentroidClassifier(model, web3, toFloat)
return exports.deploySparseNearestCentroidClassifier(model, web3, toFloat)
case 'sparse perceptron':
return loadSparsePerceptron(model, web3, toFloat)
return deploySparsePerceptron(model, web3, toFloat)
default:
// Should not happen.
throw new Error(`Unrecognized model type: "${model.type}"`)
Expand Down

0 comments on commit f8f3c1d

Please sign in to comment.