Skip to content

Instantly share code, notes, and snippets.

View RileyLazarou's full-sized avatar

Riley Lazarou RileyLazarou

  • Flatland Data Solutions
  • Saskatoon, Canada
View GitHub Profile
@RileyLazarou
RileyLazarou / connga_organism_1.py
Last active November 10, 2019 17:47
Neural Network Organism
class Organism():
def __init__(self, dimensions, use_bias=True, output='softmax'):
self.layers = []
self.biases = []
self.use_bias = use_bias
self.output = self._activation(output)
for i in range(len(dimensions)-1):
shape = (dimensions[i], dimensions[i+1])
std = np.sqrt(2 / sum(shape))
layer = np.random.normal(0, std, shape)
@RileyLazarou
RileyLazarou / connga_organism_2.py
Last active November 10, 2019 22:01
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')
@RileyLazarou
RileyLazarou / connga_organism_3.py
Created November 10, 2019 22:02
Neural Network Organism Mutation
class Organism():
# [Some code removed here]
def mutate(self, stdev=0.03):
for i in range(len(self.layers)):
self.layers[i] += np.random.normal(0, stdev, self.layers[i].shape)
if self.use_bias:
self.biases[i] += np.random.normal(0, stdev, self.biases[i].shape)
# Load the data
df = pd.read_csv('iris.csv')
# Enumerate the classes
unique_classes = sorted(list(set(df['variety'])))
class_number = {y : x for x,y in enumerate(unique_classes)}
df['variety'] = [class_number[x] for x in df['variety']]
# Convert to numpy array and standardize the features
data_X = df[['sepal.length', 'sepal.width', 'petal.length', 'petal.width']].values
data_Y = df[['variety']].values
data_X = data_X - np.min(data_X, axis=0)
# Set up the environment and collect the observation space and action space sizes
env = gym.make("CartPole-v1")
observation_space = env.observation_space.shape[0]
action_space = env.action_space.n
# The function for creating the initial population
organism_creator = lambda : Organism([observation_space, 16, 16, 16, action_space], output='softmax')
def simulate_and_evaluate(organism, trials=1):
"""
# The function to create the initial population
organism_creator = lambda : Organism([1, 16, 16, 16, 1], output='linear')
# The function we are trying to learn. numpy doesn't have tau...
true_function = lambda x : np.sin(2 * np.pi * x) #
# The loss function, mean squared error, will serve as the negative fitness
loss_function = lambda y_true, y_estimate : np.mean((y_true - y_estimate)**2)
def simulate_and_evaluate(organism, replicates=1):
"""
Randomly generate `replicates` samples in [0,1],
@RileyLazarou
RileyLazarou / connga_full.py
Last active November 17, 2019 21:20
Neural Network Evolutionary Algorithm
import copy
import numpy as np
class Organism():
def __init__(self, dimensions, use_bias=True, output='softmax'):
self.layers = []
self.biases = []
self.use_bias = use_bias
self.output = self._activation(output)
@RileyLazarou
RileyLazarou / connga_ecosystem_1.py
Last active November 17, 2019 21:20
Neural Network Organism Selection
class Ecosystem():
# [Some code removed here]
def generation(self, repeats=1, keep_best=True):
rewards = rewards = [np.mean([self.scoring_function(x) for _ in range(repeats)]) for x in self.population]
self.population = [self.population[x] for x in np.argsort(rewards)[::-1]]
new_population = []
for i in range(self.population_size):
parent_1_idx = i % self.holdout
if self.mating:
@RileyLazarou
RileyLazarou / batchnorm_bug.py
Last active March 17, 2020 02:23
tf.keras BatchNormalization Anomaly
import numpy as np
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Activation, LeakyReLU
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import matplotlib.pyplot as plt
def build_G(batchnorm_momentum=0.9):
input_layer = Input(shape=(2,))
for i in range(4):
@RileyLazarou
RileyLazarou / vanilla_gan_main.py
Created June 21, 2020 22:01
vanilla gan main
def main():
from time import time
epochs = 600
batches = 10
generator = Generator(1)
discriminator = Discriminator(1, [64, 32, 1])
noise_fn = lambda x: torch.rand((x, 1), device='cpu')
data_fn = lambda x: torch.randn((x, 1), device='cpu')
gan = VanillaGAN(generator, discriminator, noise_fn, data_fn, device='cpu')
loss_g, loss_d_real, loss_d_fake = [], [], []