Skip to content

Instantly share code, notes, and snippets.

View fedden's full-sized avatar
🤖
Training deep audio models

Leon Fedden fedden

🤖
Training deep audio models
View GitHub Profile
def mutate(dna, mutation_rate):
for i in range(len(dna)):
if np.random.random_sample() < mutation_rate:
ri = np.random.randint(0, 10)
while ri == i:
ri = np.random.randint(0, 10)
dna[i], dna[ri] = dna[ri], dna[i]
return dna
def get_fitness(dna):
difference_one = abs(36 - np.sum(dna[:5]))
difference_two = abs(360 - np.product(dna[-5:]))
return difference_one + difference_two
def create_random_population(size):
pop = []
for _ in range(size):
dna = np.arange(1, 11)
np.random.shuffle(dna)
pop.append(dna)
return np.array(pop)
@fedden
fedden / pub.md
Created December 5, 2017 00:41
publications
  • Web Audio 2017 -  Write once run anywhere revisited: machine learning and audio tools in the browser with C++ and emscripten - http://eecs.qmul.ac.uk/~keno/18.pdf
  • IEEE Transactions on Emerging Topics in Computational Intelligence - Automatic Programming of VST Sound Synthesizers using Deep Networks and Other Techniques
# Imports
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
# A way of plotting each step of the optimisation process.
def plot(surface, positions, best_fitness, best_position, iteration):
plt.cla()
# Construct the class
ga = GA(dna_size=2, # Each dna will have size elements
dna_bounds=(-5.11, 5.11), # DNA cannot optimise outside of these bounds
dna_start_position=[3.1, 3.1], # DNA begins randomly around this position
elitism=0.5, # We keep the 50% fittest individuals
population_size=500, # We have 500 genes in the gene pool
mutation_rate=0.01, # Roguhly every 100 dice rolls we mutate
mutation_sigma=0.1, # The gene will mutated at a bound of (-0.1, 0.1)
mutation_decay=0.999, # The mutation size will decay each step
mutation_limit=0.01) # The mutation size will never get smaller than this
class GA():
def __init__(self,
dna_size,
dna_bounds,
dna_start_position=None,
elitism=0.01,
population_size=200,
mutation_rate=0.01,
mutation_sigma=0.1,
class Rastrigin():
def __get_error_surface_volume(self, *X, **kwargs):
A = kwargs.get('A', 10)
return A + sum([(np.square(x) - A * np.cos(2 * np.pi * x)) for x in X])
def evaluate(self, position):
A = 10
return A + np.sum([(np.square(x) - A * np.cos(2 * np.pi * x)) for x in position])
def mutate(dna, mutation_sigma, mutation_rate):
# If random dice roll (between zero and one) is less than mutation
# rate (between zero and one) then inject noise into the dna.
if np.random.random_sample() < mutation_rate:
# The noise is scaled by the mutation_sigma.
dna += np.random.standard_normal(size=dna.shape) * mutation_sigma
return dna
def crossover(dna1, dna2):
# Sanity check.
assert len(dna1) == len(dna2)
# The child is now a copy of dna1.
child = np.copy(dna1)
# A random list of zeros and ones, signifying which
# of the child's indices will become dna2 values (one)