SE 2811, Lab 5: Decorated Networks

In this lab you will develop a tool using the Decorator Pattern to display feed-forward neural networks. A neural network takes data from some source such as pixels from an image or samples from an audio stream and generates a number specifying a classification of that input. A feed-forward neural network is one in which the signals enters at one end, flows through the network, and exits without looping backwards on itself. For example, the network

assumes the input is on the left and the output on the right. If this is a classification network, the rightmost, top circle could indicate the probability a picture is an image is of a cat and the rightmost, bottom circle could indicate the probability a picture is an image of a dog. This particular network can be divided into four distinct layers as shown in the following picture. Note the middle two layers are known as "hidden layers" because they do not interact with anything outside of the network. A deep neural network contains many hidden layers.

You will be given a starting point for this lab; your instructor will give directions on this. You will extend this into a program that draws the structure of a specified neural network. Note that you are not computing the output of the network, just drawing the network on the screen. You will use the Decorator Pattern as part of your solution.

This is an individual lab.

Approach

A starting point for drawing a feed-forward neural network is to draw the first layer. In this case, just a series of five circles:

5 circles, one above the other

The second layer is drawn by _decorating_ the first layer by adding three circles and the associated connectors. This is illustrated in the following pair of images. These show extending three layers into four:

In the image on the left, the program has drawn the initial layer (five circles) and applied the decorator twice to construct the sub-network boxed in gold and labeled "Existing network". The purple shows extending the network by adding another decoration (that is, another layer). The result will be the network shown on the right. (Note that your solution will not show the dotted boxes or the notes; these are just to help see how the network is structured.)

Using decorations allows different types of network layers. For this lab, we have just two types, but the model clearly extends to others:

Good OO means that a decorated network can draw itself, including any networks it contains. Drawing a network should be recursive. You should not need a for loop drawing each layer. If you have an arraylist or other list of all layers anywhere in your solution, you are probably applying the pattern incorrectly.

Drawing the network

You are to complete a JavaFX program drawing these type of neural networks using the Decorator Pattern. Note the domain of this assignment is drawing neural networks: code to place the nodes (circles) and draw edges between them must be in the domain classes, not the JavaFX controller class. (Putting everything in a JavaFX controller is a great example of writing a “doit” or “system” class!) The following is an ordered list for developing a solution:

  1. Start by spending about 15 minutes drawing a class diagram capturing the domain (as described in the previous section). Be sure to include key classes, behaviors, and relationships. Do not worry at this point about marking some classes as abstract or how to apply the pattern.

  2. Next, create a “minimal solution diagram” - one that adds classes for the pattern and removes unneeded domain classes. Although you will eventually include JavaFX components, do not include them in this diagram so that you can focus your efforts on the domain. Likewise, do not include driver code that draws a particular type of network.
    As you write your minimal solution diagram, recall that a decorator both IS-A and HAS-A decorated object, so the decorator pattern classes are not domain objects in the classical sense. The decorator’s responsibilities are to extend the core object while ensuring the decorated object has the same (external) behavior as that core object. In this problem, the decorator is responsible for adding a layer of nodes with appropriate connectors. But it is also responsible for representing the entire network that is decorated.

  3. Once you have a minimal solution diagram sketch, you are probably ready to start your JavaFX implementation. Some notes on this implementation:

    • A reasonably sized network (say, one less than 10 layers with no more than 10 nodes in a layer) should fit on a 700 by 900 pixel screen. This may seem like a small area, but remember that when we are grading we must have room to view your output and write comments.
    • Ensure you use named constants to determine the relative placement of network nodes and connectors! You should be able to adjust all of the sizes with just a few changes.
    • Each layer must be vertically symmetrical about the center of the window. That is, you should be able to draw a horizontal line and have the lower half mirror the upper half.
    • It should not be horizontally symmetrical. The distance of each layer from the left-hand side of the window will be determined by the number of other layers to that layer’s left.

  4. In addition to the classes used to draw the network, implement the createInception() and createAlexNet() methods within the controller and add your own create…() method to the controller. The createInception() and createAlexNet() methods should remain in the provided Controller class. These three methods construct three different networks as client code and should not be placed within any of the classes shown in your minimal solution diagram. You will likely want to start by drawing just a simple, one-layer network, extend this to a two-layer network, and continue extending this as you implement the rest of the system. The required types of networks are

    • AlexNet: a network, implemented in createAlexNet(), with the following layers:

      1. An identity layer with four nodes
      2. A 1x1 convolutional layer
      3. A 1x1 convolutional layer
      4. A fully-connected layer with four nodes
      5. A fully-connected layer with three nodes; this is the output layer

    • Inception: a network, implemented in createInception(), with the following layers:

      1. An identity layer with three nodes
      2. A 1x1 convolutional layer
      3. A 1x1 convolutional layer
      4. A 1x1 convolutional layer; this is the output layer

    • An additional network of your own design. It must be a non-trivial network that illustrates your solution can generate other, interesting networks. Explore some of the boundaries - include a number of nodes in various layers, mix convolutional layers with fully-connected layers, show what happens if you create layers with a single node, etc. Be sure to also show what happens if you create layers with more than 5 node in them, both with an even number and with an odd number of nodes in any particular layer.

Note that the provided code gives examples of how to draw edges and nodes on a JavaFX canvas. During your design stage, please decide where you will move the helper methods and any associated constants. They should not be a responsibility of the controller.

Replace this same example code with your decorator client code. Reduce the burden on this code as much as possible. It should simply nest decorators as discussed in class and should not pass information to the decorators beyond what the client absolutely must provide. Details about the layout should be determined by the decorator pattern classes instead.

For full credit, you must show arrows at the right ends of the neural network connectors (as shown in the first sample neural network at the top of this writeup). However, the penalty for not adding these arrows will be very small.

Directions for the sections taught by Drs. Thomas and Hasker

Your instructor will give submission directions. For the sections for Dr. Hasker and Dr. Thomas, start with the code se2811-decorator-lab.zip. For these sections, you will submit

See Canvas for additional directions.

Just for fun

If you are interested, you might consider implementing additional layers: