Last active
December 15, 2019 20:32
-
-
Save shobhitchittora/bd883457bf0f3f71629e26944e59cf1b to your computer and use it in GitHub Desktop.
Classification-Caner
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
const tf = require('@tensorflow/tfjs-node'); | |
const TRAINING_DATA_URL = 'https://raw.githubusercontent.com/lmoroney/dlaicourse/master/TensorFlow%20Deployment/Course%201%20-%20TensorFlow-JS/Week%201/Exercise/wdbc-train.csv'; | |
const TESTING_DATA_URL = 'https://raw.githubusercontent.com/lmoroney/dlaicourse/master/TensorFlow%20Deployment/Course%201%20-%20TensorFlow-JS/Week%201/Exercise/wdbc-test.csv'; | |
async function run() { | |
// diagnosis column in the labelled csv | |
// has all the decisioning data | |
const trainingData = tf.data.csv( | |
TRAINING_DATA_URL, { | |
columnConfigs: { | |
diagnosis: { | |
isLabel: true | |
} | |
} | |
}); | |
const testingData = tf.data.csv( | |
TESTING_DATA_URL, { | |
columnConfigs: { | |
diagnosis: { | |
isLabel: true | |
} | |
} | |
}); | |
const numOfFeatures = (await trainingData.columnNames()).length - 1; | |
const convertedTrainingData = | |
trainingData.map(({ xs, ys }) => { | |
const labels = [ys.diagnosis] | |
return { xs: Object.values(xs), ys: Object.values(labels) }; | |
}).batch(100); | |
const convertedTestingData = | |
testingData.map(({ xs, ys }) => { | |
const labels = [ys.diagnosis] | |
return { xs: Object.values(xs), ys: Object.values(labels) }; | |
}).batch(100); | |
const model = tf.sequential(); | |
model.add(tf.layers.dense({ inputShape: [numOfFeatures], activation: "sigmoid", units: numOfFeatures / 2 })) | |
model.add(tf.layers.dense({ activation: "softmax", units: 2 })); | |
model.compile({ loss: 'categoricalCrossentropy', optimizer: tf.train.adam(0.01) }); | |
await model.fitDataset(convertedTrainingData, | |
{ | |
epochs: 200, | |
validationData: convertedTestingData | |
} | |
); | |
// prediction should be 1 | |
const testValue1 = tf.tensor2d( | |
[0.03044074238, 0.9549225569, 0.1050681432, -0.1227571612, 0.8043023999, 2.735854561, 1.42332992, 0.4536366295, 2.099581594, 1.872171652, -0.4102104418, 1.662683772, -0.3504014478, -0.179655777, 0.3402900665, 6.308376767, 2.739713598, 0.8511109458, 3.737990259, 3.072596581, -0.1056454613, 1.910304965, -0.01866302817, -0.2027993271, 0.9221218488, 4.451638642, 2.915026087, 0.9617363037, 3.594656981, 3.434156634], | |
[1, numOfFeatures] | |
); | |
// prediction should be 0 | |
const testValue2 = tf.tensor2d( | |
[-1.150364822, -0.390641961, -1.128550208, -0.9587635786, 0.3109837048, -0.5959945009, -0.8025961223, -0.8024900198, 0.2945390619, 0.09425149978, -0.4950522998, 1.487201532, -0.5144878222, -0.4915400491, 0.2814983704, -0.6045120643, -0.4690070058, -0.6117000235, 0.05798237404, -0.3576370159, -1.043175598, 0.2135328249, -1.036044595, -0.8488077145, 0.3424985112, -0.7300974341, -0.8123205317, -0.757983665, -0.01614760641, -0.3850340195], | |
[1, numOfFeatures] | |
); | |
const prediction1 = model.predict(testValue1); | |
const prediction2 = model.predict(testValue2); | |
tf.print(prediction1); | |
tf.print(prediction2); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment