From 1b6201bf516407945ddea7009e428aeeb6facb90 Mon Sep 17 00:00:00 2001 From: Chlorophytums <118323027+Chlorophytums@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:17:48 +0700 Subject: [PATCH] Update C1_W1_Assignment.html --- .../W1/assignment/C1_W1_Assignment.html | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 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..90f54911 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 @@ -12,7 +12,10 @@ // can predict from the data whether the diagnosis is malignant or benign. const trainingData = tf.data.csv(trainingUrl, { - // YOUR CODE HERE + columnNames: ['feature1', 'feature2', ..., 'featureN', 'diagnosis'], + columnConfigs: { + diagnosis: {isLabel: true} + } // YOUR CODE HERE }); @@ -21,7 +24,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}) => { + const labels = ys.diagnosis === 'M' ? 1 : 0; + return { xs: Object.values(xs), ys: labels }; + }).batch(32); // YOUR CODE HERE const testingUrl = '/data/wdbc-test.csv'; @@ -30,8 +36,10 @@ // 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 + columnNames: ['feature1', 'feature2', ..., 'featureN', 'diagnosis'], + columnConfigs: { + diagnosis: {isLabel: true} + } // YOUR CODE HERE }); @@ -40,13 +48,16 @@ // 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 = const convertedTestingData = testingData.map(({xs, ys}) => { + const labels = ys.diagnosis === 'M' ? 1 : 0; + return { xs: Object.values(xs), ys: labels }; + }).batch(32); // YOUR CODE HERE // 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(convertedTrainingData).length - 1; // YOUR CODE HERE // In the space below create a neural network that predicts 1 if the diagnosis is malignant @@ -59,13 +70,19 @@ // hidden layers should be enough to get a high accuracy. const model = tf.sequential(); - // YOUR CODE HERE + model.add(tf.layers.dense({units: 64, activation: 'relu', inputShape: [numOfFeatures]})); + model.add(tf.layers.dense({units: 32, activation: 'relu'})); + model.add(tf.layers.dense({units: 1, activation: 'sigmoid'})); // YOUR CODE HERE // 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'] + }); // YOUR CODE HERE await model.fitDataset(convertedTrainingData, @@ -82,4 +99,4 @@ - \ No newline at end of file +