Public
Edited
Apr 14, 2021
2 forks
Comments locked
8 stars
Insert cell
Insert cell
Insert cell
autoencoder = {
const model = tf.sequential();
// To simulate PCA we use 1 hidden layer with a linear (relu) activation
const encoder = tf.layers.dense({
units: 3,
batchInputShape:[null,4], //We will input N samples X 4 columns
activation: 'relu',
kernelInitializer:"randomNormal", //Randomize to avoid degenerate cases
biasInitializer:"ones"});
const decoder = tf.layers.dense({units: 4, activation: 'relu'});

model.add(encoder);
model.add(decoder);
await model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
return {
'model':model,
'encoder':encoder,
'decoder':decoder
}
}
Insert cell
Insert cell
Insert cell
Insert cell
autoencoderTrain = {
const xs = tf.tensor2d(irisDataSliced);
let h = await autoencoder.model.fit(xs, xs, {epochs: 5,batchSize:15,shuffle:true,validationSpit:0.1});
xs.dispose();
return h;
}
Insert cell
Insert cell

predictions = {
autoencoderTrain;
const tidyWrapper = tf.tidy(() => {
const predictor = tf.sequential();
predictor.add(autoencoder.encoder);
let xs = tf.tensor2d(irisDataSliced);
let ret = predictor.predict(xs);
xs.dispose();
return ret;
});

yield tidyWrapper.data();
try {
yield invalidation;
} finally {
tidyWrapper.dispose();
}
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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