Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update C1_W1_Assignment.html #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions C1_Browser-based-TF-JS/W1/assignment/C1_W1_Assignment.html
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
<!DOCTYPE html>
<html>
<head></head>
<head>
<title>Diagnosis Classifier</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
<script lang="js">

<script lang="javascript">
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.
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

const convertedTrainingData = trainingData.map(({xs, ys}) => {
return [Object.values(xs), ys.diagnosis === 'M' ? 1 : 0];
});

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.
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

const convertedTestingData = testingData.map(({xs, ys}) => {
return [Object.values(xs), ys.diagnosis === 'M' ? 1 : 0];
});

// 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 = (await trainingData.columnNames()).length - 1;

// 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
Expand All @@ -58,16 +65,19 @@
// using ReLu activation functions where applicable. For this dataset only a few
// hidden layers should be enough to get a high accuracy.
const model = tf.sequential();

// YOUR CODE HERE



model.add(tf.layers.dense({units: 32, activation: 'relu', inputShape: [numOfFeatures]}));
model.add(tf.layers.dense({units: 16, activation: 'relu'}));
model.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));

// Compile the model using the binaryCrossentropy loss,
// the rmsprop optimizer, and accuracy for your metrics.
model.compile(// YOUR CODE HERE);


model.compile({
loss: 'binaryCrossentropy',
optimizer: 'rmsprop',
metrics: ['accuracy']
});

await model.fitDataset(convertedTrainingData,
{epochs:100,
validationData: convertedTestingData,
Expand All @@ -76,10 +86,12 @@
console.log("Epoch: " + epoch + " Loss: " + logs.loss + " Accuracy: " + logs.acc);
}
}});

await model.save('downloads://my_model');
}
run();
</script>
</head>
<body>
</body>
</html>
</html>