Public
Edited
Apr 1, 2023
1 fork
Insert cell
Insert cell
tf = require("@tensorflow/tfjs")
Insert cell
IRIS.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
numOfFeatures = 4
Insert cell
labelMap = ({
"Iris-setosa": 0,
"Iris-versicolor": 1,
"Iris-virginica": 2
})
Insert cell
batchSize = 8
Insert cell
output = []
Insert cell
output
Insert cell
xs = tf.tidy(() => {
return tf.tensor(
iris.map((obj) => [
obj.sepal_length,
obj.sepal_width,
obj.petal_length,
obj.petal_width
])
);
})
Insert cell
xs.toString()
Insert cell
ys = tf.tidy(() => {
return tf.oneHot(
iris.map((obj) => labelMap[obj.species]),
3
);
})
Insert cell
ys.toString()
Insert cell
labels = ["Iris-setosa", "Iris-versicolor", "Iris-virginica"]
Insert cell
mutable accuracy = ""
Insert cell
mutable loss = ""
Insert cell
model = {
const model = tf.sequential({
layers: [
tf.layers.dense({
inputShape: [numOfFeatures],
units: 6,
activation: "relu"
}),
tf.layers.dense({ units: 3, activation: "softmax" })
]
});

await model.compile({
optimizer: "sgd",
loss: "categoricalCrossentropy",
metrics: ["accuracy"]
});

return model;
}
Insert cell
model
.fit(xs, ys, {
epochs: 50,
batchSize: 8,
shuffle: true
})
.then((info) => {
mutable accuracy = "Final accuracy " + info.history.acc[49];
mutable loss = "Final loss " + info.history.loss[49];
})
Insert cell
accuracy
Insert cell
loss
Insert cell
prediction1 = model.predict(tf.tensor([[6, 3, 4.5, 1.5]])).data()
Insert cell
bytesAllocated = {
return tf.memory();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more