generator.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Generate neural network
  2. from lab import neural
  3. import numpy as np
  4. # Random generators
  5. def uniform(layer):
  6. """
  7. To generate network weights
  8. with a uniform distribution between -1.0 and 1.0
  9. """
  10. return np.random.uniform(low = -1.0, high = 1.0, size = layer.shape)
  11. def gaussUnitDev(layer):
  12. """
  13. To generate network weights
  14. with a gaussian distribution.
  15. """
  16. return np.random.normal(size = layer.shape)
  17. def gaussAdaptedDev(layer):
  18. """
  19. To generate network weights
  20. with a gaussian distribution
  21. where standard deviation is adpted 1 / sqrt(nl - 1)
  22. """
  23. nl, _ = layer.shape
  24. stdDev = 1 / np.sqrt(nl - 1)
  25. return np.random.normal(scale = stdDev, size = layer.shape)
  26. # Network weight initialization
  27. def generate(activation, derivative, hiddenLength = 30, weightGenerator = None):
  28. """
  29. activation : function used on network outputs
  30. derivative : the derivative of the activation function
  31. hiddenLength : number of neurons in the hidden layer
  32. weightGenerator : is one of
  33. None
  34. generator.uniform
  35. generator.gaussUnitDev
  36. generator.gaussAdaptedDev
  37. """
  38. net = neural.Network(activation, derivative, hiddenLength)
  39. if (weightGenerator is not None):
  40. net.layer1 = weightGenerator(net.layer1)
  41. net.layer2 = weightGenerator(net.layer2)
  42. return net