Skip to content

Instantly share code, notes, and snippets.

@gognjanovski
Created February 18, 2019 16:04
Show Gist options
  • Save gognjanovski/321405b557af9bbab92bbaeacc5c1be7 to your computer and use it in GitHub Desktop.
Save gognjanovski/321405b557af9bbab92bbaeacc5c1be7 to your computer and use it in GitHub Desktop.
Building CNN (Convolutional Neural Network) with Tensorflow.JS
const buildCnn = function (data) {
return new Promise(function (resolve, reject) {
// Linear (sequential) stack of layers
const model = tf.sequential();
// Define input layer
model.add(tf.layers.inputLayer({
inputShape: [7, 1],
}));
// Add the first convolutional layer
model.add(tf.layers.conv1d({
kernelSize: 2,
filters: 128,
strides: 1,
use_bias: true,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
}));
// Add the Average Pooling layer
model.add(tf.layers.averagePooling1d({
poolSize: [2],
strides: [1]
}));
// Add the second convolutional layer
model.add(tf.layers.conv1d({
kernelSize: 2,
filters: 64,
strides: 1,
use_bias: true,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
}));
// Add the Average Pooling layer
model.add(tf.layers.averagePooling1d({
poolSize: [2],
strides: [1]
}));
// Add Flatten layer, reshape input to (number of samples, number of features)
model.add(tf.layers.flatten({
}));
// Add Dense layer,
model.add(tf.layers.dense({
units: 1,
kernelInitializer: 'VarianceScaling',
activation: 'linear'
}));
return resolve({
'model': model,
'data': data
});
});
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment