12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # Generate neural network
- from lab import neural
- import numpy as np
- # Random generators
- def uniform(layer):
- """
- To generate network weights
- with a uniform distribution between -1.0 and 1.0
- """
- return np.random.uniform(low = -1.0, high = 1.0, size = layer.shape)
- def gaussUnitDev(layer):
- """
- To generate network weights
- with a gaussian distribution.
- """
- return np.random.normal(size = layer.shape)
- def gaussAdaptedDev(layer):
- """
- To generate network weights
- with a gaussian distribution
- where standard deviation is adpted 1 / sqrt(nl - 1)
- """
- nl, _ = layer.shape
- stdDev = 1 / np.sqrt(nl - 1)
- return np.random.normal(scale = stdDev, size = layer.shape)
- # Network weight initialization
- def generate(activation, derivative, hiddenLength = 30, weightGenerator = None):
- """
- activation : function used on network outputs
- derivative : the derivative of the activation function
- hiddenLength : number of neurons in the hidden layer
- weightGenerator : is one of
- None
- generator.uniform
- generator.gaussUnitDev
- generator.gaussAdaptedDev
- """
- net = neural.Network(activation, derivative, hiddenLength)
- if (weightGenerator is not None):
- net.layer1 = weightGenerator(net.layer1)
- net.layer2 = weightGenerator(net.layer2)
- return net
|