-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathC1_W1_Assignment.html
More file actions
executable file
·101 lines (89 loc) · 4.85 KB
/
C1_W1_Assignment.html
File metadata and controls
executable file
·101 lines (89 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<html>
<head></head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
<script lang="js">
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, {
columnConfigs: {
species: {
isLabel: true
}
}
// YOUR CODE HERE
});
// 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 = trainingData.map(({xs,ys}) => {
const labels = [
ys.species == "malignant" ? 1:0,
ys.species == "benign" ? 1:0
]
return{ xs:Object.values(xs), ys:Object.values(labels)};
}).batch(10);
// YOUR CODE HERE
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, {
columnConfigs: {
species: {
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 = testingData.map(({xs,ys}) => {
const labels = [
ys.species == "malignant" ? 1:0,
ys.species == "benign" ? 1:0
]
return { xs:Obhect.values(xs), ys:Object.values(labels)};
}).batch(10);// 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 = (await trainingData.columnNames()).length - 1; // 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.
const model = tf.sequential();
model.add(tf.layers.dense({inputShape:[numofFeatures],activation="relu", units:10}))
model.add(tf.layers.dense({activation:"sigmoid", units : 2}));
// YOUR CODE HERE
// Compile the model using the binaryCrossentropy loss,
// the rmsprop optimizer, and accuracy for your metrics.
model.compile(loss:"binarycrossentropy", optimizer:tf.train.adam(0.0612)});// YOUR CODE HERE);
await model.fitDataset(convertedTrainingData,
{epochs:100,
validationData: convertedTestingData,
callbacks:{
onEpochEnd: async(epoch, logs) =>{
console.log("Epoch: " + epoch + " Loss: " + logs.loss + " Accuracy: " + logs.acc);
}
}});
await model.save('downloads://my_model');
}
run();
</script>
<body>
</body>
</html>