Google News
logo
ml5.js - Interview Questions
Defining Custom Layers in ml5.NeuralNetwork.
Defaults : By default the ml5.neuralNetwork has simple default architectures for the classification, regression and imageClassificaiton tasks.
 
default classification layers :
layers:[
  {
    type: 'dense',
    units: this.options.hiddenUnits,
    activation: 'relu',
  },
  {
    type: 'dense',
    activation: 'softmax',
  },
];
 
default regression layers :
layers: [
  {
    type: 'dense',
    units: this.options.hiddenUnits,
    activation: 'relu',
  },
  {
    type: 'dense',
    activation: 'sigmoid',
  },
];
 
default imageClassification layers :
layers = [
  {
    type: 'conv2d',
    filters: 8,
    kernelSize: 5,
    strides: 1,
    activation: 'relu',
    kernelInitializer: 'varianceScaling',
  },
  {
    type: 'maxPooling2d',
    poolSize: [2, 2],
    strides: [2, 2],
  },
  {
    type: 'conv2d',
    filters: 16,
    kernelSize: 5,
    strides: 1,
    activation: 'relu',
    kernelInitializer: 'varianceScaling',
  },
  {
    type: 'maxPooling2d',
    poolSize: [2, 2],
    strides: [2, 2],
  },
  {
    type: 'flatten',
  },
  {
    type: 'dense',
    kernelInitializer: 'varianceScaling',
    activation: 'softmax',
  },
];
 
Defining Custom Layers : You can define custom neural network architecture by defining your layers in the options that are passed to the ml5.neuralNetwork on initialization.
 
A neural network with 3 layers :
const options = {
  debug: true,
  task: 'classification',
  layers: [
    {
      type: 'dense',
      units: 16,
      activation: 'relu'
    },
    {
      type: 'dense',
      units: 16,
      activation: 'sigmoid'
    },
    {
      type: 'dense',
      activation: 'sigmoid'
    }
  ]
};
const nn = ml5.neuralNetwork(options);
Advertisement