Skip to content

Instantly share code, notes, and snippets.

@RileyLazarou
Last active November 10, 2019 22:01
Show Gist options
  • Save RileyLazarou/ad739762b1da5098efbf02157ed4eec7 to your computer and use it in GitHub Desktop.
Save RileyLazarou/ad739762b1da5098efbf02157ed4eec7 to your computer and use it in GitHub Desktop.
Neural Network Organism Reproduction
class Organism():
# [Some code removed here]
def mate(self, other, mutate=True):
if self.use_bias != other.use_bias:
raise ValueError('Both parents must use bias or not use bias')
if not len(self.layers) == len(other.layers):
raise ValueError('Both parents must have same number of layers')
if not all(self.layers[x].shape == other.layers[x].shape for x in range(len(self.layers))):
raise ValueError('Both parents must have same shape')
child = copy.deepcopy(self)
for i in range(len(child.layers)):
pass_on = np.random.rand(1, child.layers[i].shape[1]) < 0.5
child.layers[i] = pass_on * self.layers[i] + ~pass_on * other.layers[i]
child.biases[i] = pass_on * self.biases[i] + ~pass_on * other.biases[i]
if mutate:
child.mutate()
return child
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment