Dyllan M
Dyllan M

Reputation: 301

Neural Network only producing values of 1 when I add more hidden layers

So, I'm building a machine learning arena simulation where I can swap different algorithms to show the advantages and disadvantages of different models.

I tried using ReLU activation, however this is not ideal as SoftMax produces a probability distribution, which means pretty much only 1 action can be done at a time.

I'm thinking sigmoid is the best choice, however when I calculate the output layers, it progressively gets larger and larger going through each layer, so when I add 2 hidden layers: all output nodes result in 1.

Here's a demonstration: https://i.gyazo.com/b12d4efdd1b0af518751762cb2f000f9.mp4

Here's some code snippets:

class NeuralNetwork:

    layer_weights: list
    neuron_weights: list = None  # Stored here for verbose
    neuron_screen_locations: list = None

    def __init__(
        self,
        dimensions: Tuple[int] = None,
        layer_weights: list = None
    ):

        if dimensions:
            self.layer_weights = []
            for i in range(len(dimensions)-1):
                self.layer_weights.append(
                    np.random.uniform(
                    size=(dimensions[i], dimensions[i+1])
                    )
                )
            return

        self.layer_weights = list(layer_weights)

    def activate_layer(self, layer: list):
        for x in np.nditer(layer, op_flags=['readwrite']):
            x[...] = self.sigmoid(x)

    def output(self, inputs: list):
        self.neuron_weights = []
        self.neuron_weights.append(np.array((inputs)))
        output = inputs

        for weight_layer in self.layer_weights:
            output = np.matmul(output, weight_layer)
            self.activate_layer(output)
            self.neuron_weights.append(output)

        return output

    def sigmoid(self, x, derivative=False):
        ...

    def ReLU(self, x):
        ...

    def softmax(self, x):
        ...

    def draw_neurons(self): # Draws neurons to screen
        ...

    def draw_weights(self): # Draws synaptic connections between neurons to screen
        ...

EDIT:

I also tried using Tanh which yielded similar results... here's a demonstration (with even more layers): https://i.gyazo.com/d779dce5cd974bc644d0f1ffa267c062.mp4

Here is the code for my input features (maybe the problem could be here?):

def look(self, match_up: MatchUp):
    """Set up Neural Network inputs."""
    p: Pawn = self.pawn

    imminent: Laser = match_up.get_most_imminent_laser(p)
    enemy: Pawn = match_up.get_closest_opponent(p)

    max_angle = math.pi * 2

    self.inputs = [
        1/math.sqrt(p.dist_squared(actor=imminent)
                    ) if imminent != None else 1,
        p.angle_to(actor=imminent)/max_angle if imminent != None else 1,

        1/math.sqrt(p.dist_squared(actor=enemy)) if enemy != None else 1,
        p.angle_to(actor=enemy)/max_angle if enemy != None else 1,

        p.get_direc()/max_angle,
        p.health/p.stat_bias.max_health
    ]

Upvotes: 1

Views: 153

Answers (1)

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

Your problem is the weight initialization. Because you use uniform weight initialization your network explodes in values and therefore only produces ones and suffers from vanishing gradients. In a sense you should strive for an initialization which produces normally distributed outputs after every layers.

For sigmoid/TanH this would be glorot initialization, stddev = sqrt(2 / (Nr. input nodes + Nr. output nodes)).

For ReLU it would be he initialization stddev = sqrt(2 / (Nr. input nodes)).

For your program you just have to replace the initialization from np.random.uniform(0,1, size=(dimensions[i], dimensions[i+1])) to np.random.normal(0, np.sqrt(2 / (dimensions[i] + dimensions[i+1])), size=(dimensions[i], dimensions[i+1])) and it should work as intended.

Citations: glorot Init. [http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf?hc_location=ufi], He Init. [https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf]

Upvotes: 1

Related Questions