diff --git a/C1_Browser-based-TF-JS/W1/assignment/C1_W1_Assignment.html b/C1_Browser-based-TF-JS/W1/assignment/C1_W1_Assignment.html index 023c6143..96125d4d 100755 --- a/C1_Browser-based-TF-JS/W1/assignment/C1_W1_Assignment.html +++ b/C1_Browser-based-TF-JS/W1/assignment/C1_W1_Assignment.html @@ -6,80 +6,68 @@ async function run(){ const trainingUrl = '/data/wdbc-train.csv'; - // Take a look at the 'wdbc-train.csv' file and specify the column - // that should be treated as the label in the space below. - // HINT: Remember that you are trying to build a classifier that - // can predict from the data whether the diagnosis is malignant or benign. + // Specify the column that should be treated as the label const trainingData = tf.data.csv(trainingUrl, { - - // YOUR CODE HERE - + columnConfigs: { + diagnosis: { isLabel: true } + } }); - // Convert the training data into arrays in the space below. - // Note: In this case, the labels are integers, not strings. - // Therefore, there is no need to convert string labels into - // a one-hot encoded array of label values like we did in the - // Iris dataset example. - const convertedTrainingData = // YOUR CODE HERE - + // Convert the training data into arrays + const convertedTrainingData = trainingData + .map(({ xs, ys }) => { + return { xs: Object.values(xs), ys: Object.values(ys)[0] }; + }) + .batch(32); + const testingUrl = '/data/wdbc-test.csv'; - // Take a look at the 'wdbc-test.csv' file and specify the column - // that should be treated as the label in the space below.. - // HINT: Remember that you are trying to build a classifier that - // can predict from the data whether the diagnosis is malignant or benign. + // Specify the column that should be treated as the label for testing data const testingData = tf.data.csv(testingUrl, { - - // YOUR CODE HERE - + columnConfigs: { + diagnosis: { isLabel: true } + } }); - // Convert the testing data into arrays in the space below. - // Note: In this case, the labels are integers, not strings. - // Therefore, there is no need to convert string labels into - // a one-hot encoded array of label values like we did in the - // Iris dataset example. - const convertedTestingData = // YOUR CODE HERE - + // Convert the testing data into arrays + const convertedTestingData = testingData + .map(({ xs, ys }) => { + return { xs: Object.values(xs), ys: Object.values(ys)[0] }; + }) + .batch(32); - // Specify the number of features in the space below. - // HINT: You can get the number of features from the number of columns - // and the number of labels in the training data. - const numOfFeatures = // YOUR CODE HERE - - - // In the space below create a neural network that predicts 1 if the diagnosis is malignant - // and 0 if the diagnosis is benign. Your neural network should only use dense - // layers and the output layer should only have a single output unit with a - // sigmoid activation function. You are free to use as many hidden layers and - // neurons as you like. - // HINT: Make sure your input layer has the correct input shape. We also suggest - // using ReLu activation functions where applicable. For this dataset only a few - // hidden layers should be enough to get a high accuracy. + // Specify the number of features (columns minus the label) + const numOfFeatures = 30; + + // Create a neural network const model = tf.sequential(); - - // YOUR CODE HERE + model.add(tf.layers.dense({ inputShape: [numOfFeatures], units: 16, activation: 'relu' })); + model.add(tf.layers.dense({ units: 8, activation: 'relu' })); + model.add(tf.layers.dense({ units: 4, activation: 'relu' })); + model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' })); + // Compile the model + model.compile({ + loss: 'binaryCrossentropy', + optimizer: 'rmsprop', + metrics: ['accuracy'] + }); - - // Compile the model using the binaryCrossentropy loss, - // the rmsprop optimizer, and accuracy for your metrics. - model.compile(// YOUR CODE HERE); - - + // Train the model await model.fitDataset(convertedTrainingData, - {epochs:100, - validationData: convertedTestingData, - callbacks:{ - onEpochEnd: async(epoch, logs) =>{ - console.log("Epoch: " + epoch + " Loss: " + logs.loss + " Accuracy: " + logs.acc); - } - }}); + {epochs: 100, + validationData: convertedTestingData, + callbacks:{ + onEpochEnd: async(epoch, logs) =>{ + console.log("Epoch: " + epoch + " Loss: " + logs.loss + " Accuracy: " + logs.acc); + } + }}); + + // Save the model await model.save('downloads://my_model'); } run(); - \ No newline at end of file +