Skip to content

How to: Create your own layers on Carrot

Nicholas Szerman edited this page Jul 1, 2019 · 2 revisions

Creating your own Liquid Carrot Layer

So you want to create your own Liquid Carrot Layer? That's awesome ❤️❤️ (and easy 😉)! Let's get started✈️ :)

Key components

Every Liquid Carrot layer requires just four components:

  • Create the class
  • Add functionality
  • Setting input_nodes and output_nodes
  • Adding internal nodes and setting the order of activation

You will see that these are super simple!

Create the class

Every LC layer needs to support everything that a Group supports. The way we do that is by extending it:

class AmazingLayer extends Group {
  constructor() {
    super(...arguments);

    // input_nodes and output_nodes are required as well
    this.input_nodes = [];
    this.output_nodes = [];
  }
}

Add functionality

Then you want to add the functionality you want⚙️🛠️. We like to do that by chaining Groups:

      const input_group = new Group(size);
      const internal_group = new Group(size);
      const output_group = new Group(size);

      input_group.connect(internal_group)
      internal_group.connect(output_group)

Setting input_nodes and output_nodes

Now that you have added some functionality, you want to tell the layer what are the inputs and the outputs😮:

      ...add the functionality here (by chaining groups or node by node)...

      this.input_nodes.push(input_group.nodes);
      this.output_nodes.push(output_group.nodes);
      // In this case we could also have used new_amazing_layer.input_nodes = input_group.nodes;

Adding internal nodes and setting the order of activation

Now that you have added all the functionality you want👍🏼, you let the layer know what nodes does it have and the order of activation. You do that by simply adding the nodes sequentially:

      this.addNodes(input_group);
      this.addNodes(internal_group);
      this.addNodes(output_group);

Conclusion

That's it 😀!

Here's the full example:

const Group = carrot.Group; // Or = require('./src/Group')

class AmazingLayer extends Group {
  constructor() {
    super(...arguments);

    // input_nodes and output_nodes are required as well
    this.input_nodes = [];
    this.output_nodes = [];

    const size = 10;
    const input_group = new Group(size);
    const internal_group = new Group(size);
    const output_group = new Group(size);

    input_group.connect(internal_group);
    internal_group.connect(output_group);

    this.addNodes(input_group);
    this.addNodes(internal_group);
    this.addNodes(output_group);
  }
}