From a7fd8e80d6cb7322b7580a0137c689f0e9bbef57 Mon Sep 17 00:00:00 2001 From: wildansibil Date: Sat, 16 Nov 2024 21:34:59 +0700 Subject: [PATCH] Update C1_W1_Assignment.html --- .../W1/assignment/C1_W1_Assignment.html | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) 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..1993169c 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 @@ -11,8 +11,9 @@ // HINT: Remember that you are trying to build a classifier that // can predict from the data whether the diagnosis is malignant or benign. const trainingData = tf.data.csv(trainingUrl, { - - // YOUR CODE HERE + columnConfigs: { + diagnosis: {isLabel: true} // Treat 'diagnosis' column as the label + } }); @@ -21,8 +22,10 @@ // 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 - + 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 @@ -30,9 +33,9 @@ // HINT: Remember that you are trying to build a classifier that // can predict from the data whether the diagnosis is malignant or benign. const testingData = tf.data.csv(testingUrl, { - - // YOUR CODE HERE - + columnConfigs: { + diagnosis: {isLabel: true} // Treat 'diagnosis' column as the label + } }); // Convert the testing data into arrays in the space below. @@ -40,13 +43,19 @@ // 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 + const convertedTestingData = testingData.map(({xs, ys}) => { + // Normalize the feature values and return + return {xs: Object.values(xs), ys: Object.values(ys)[0]}; + }).batch(32); + + // Specify the number of features + const sample = await trainingData.take(1).toArray(); // 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 + const numOfFeatures = Object.keys(sample[0].xs).length; // In the space below create a neural network that predicts 1 if the diagnosis is malignant @@ -59,13 +68,32 @@ // hidden layers should be enough to get a high accuracy. const model = tf.sequential(); - // YOUR CODE HERE + // Input layer + model.add(tf.layers.dense({ + inputShape: [numOfFeatures], + units: 32, + activation: 'relu' + })); - + // Hidden layers + model.add(tf.layers.dense({ + units: 16, + activation: 'relu' + })); + + // Output layer + model.add(tf.layers.dense({ + units: 1, + activation: 'sigmoid' // For binary classification + })); // Compile the model using the binaryCrossentropy loss, // the rmsprop optimizer, and accuracy for your metrics. - model.compile(// YOUR CODE HERE); + model.compile({ + optimizer: 'rmsprop', + loss: 'binaryCrossentropy', + metrics: ['accuracy'] + }); await model.fitDataset(convertedTrainingData, @@ -82,4 +110,4 @@ - \ No newline at end of file +