modify the behavior for mutate_add_node and mutate_add_conn. Currently, this two mutation will just change the structure of the network, but not influence the output for the network.
This commit is contained in:
@@ -9,13 +9,6 @@ class BaseNodeGene(BaseGene):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def crossover(self, state, randkey, gene1, gene2):
|
||||
return jnp.where(
|
||||
jax.random.normal(randkey, gene1.shape) > 0,
|
||||
gene1,
|
||||
gene2,
|
||||
)
|
||||
|
||||
def forward(self, state, attrs, inputs, is_output_node=False):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import Tuple
|
||||
|
||||
import jax, jax.numpy as jnp
|
||||
|
||||
from utils import Act, Agg, act, agg, mutate_int, mutate_float
|
||||
from utils import Act, Agg, act_func, agg_func, mutate_int, mutate_float
|
||||
from . import BaseNodeGene
|
||||
|
||||
|
||||
@@ -23,12 +23,12 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
response_mutate_power: float = 0.5,
|
||||
response_mutate_rate: float = 0.7,
|
||||
response_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
aggregation_default: callable = Agg.sum,
|
||||
aggregation_options: Tuple = (Agg.sum,),
|
||||
aggregation_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
):
|
||||
super().__init__()
|
||||
self.bias_init_mean = bias_init_mean
|
||||
@@ -43,25 +43,20 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self.response_mutate_rate = response_mutate_rate
|
||||
self.response_replace_rate = response_replace_rate
|
||||
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
self.aggregation_default = aggregation_options.index(aggregation_default)
|
||||
self.aggregation_options = aggregation_options
|
||||
self.aggregation_indices = jnp.arange(len(aggregation_options))
|
||||
self.aggregation_replace_rate = aggregation_replace_rate
|
||||
|
||||
def new_custom_attrs(self, state):
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
def new_identity_attrs(self, state):
|
||||
return jnp.array(
|
||||
[
|
||||
self.bias_init_mean,
|
||||
self.response_init_mean,
|
||||
self.activation_default,
|
||||
self.aggregation_default,
|
||||
]
|
||||
)
|
||||
[0, 1, self.aggregation_default, -1]
|
||||
) # activation=-1 means Act.identity
|
||||
|
||||
def new_random_attrs(self, state, randkey):
|
||||
k1, k2, k3, k4 = jax.random.split(randkey, num=4)
|
||||
@@ -69,17 +64,17 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
res = (
|
||||
jax.random.normal(k2, ()) * self.response_init_std + self.response_init_mean
|
||||
)
|
||||
act = jax.random.randint(k3, (), 0, len(self.activation_options))
|
||||
agg = jax.random.randint(k4, (), 0, len(self.aggregation_options))
|
||||
return jnp.array([bias, res, act, agg])
|
||||
agg = jax.random.choice(k3, self.aggregation_indices)
|
||||
act = jax.random.choice(k4, self.activation_indices)
|
||||
|
||||
def mutate(self, state, randkey, node):
|
||||
return jnp.array([bias, res, agg, act])
|
||||
|
||||
def mutate(self, state, randkey, attrs):
|
||||
k1, k2, k3, k4 = jax.random.split(randkey, num=4)
|
||||
index = node[0]
|
||||
|
||||
bias, res, agg, act = attrs
|
||||
bias = mutate_float(
|
||||
k1,
|
||||
node[1],
|
||||
bias,
|
||||
self.bias_init_mean,
|
||||
self.bias_init_std,
|
||||
self.bias_mutate_power,
|
||||
@@ -89,7 +84,7 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
|
||||
res = mutate_float(
|
||||
k2,
|
||||
node[2],
|
||||
res,
|
||||
self.response_init_mean,
|
||||
self.response_init_std,
|
||||
self.response_mutate_power,
|
||||
@@ -97,33 +92,33 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self.response_replace_rate,
|
||||
)
|
||||
|
||||
act = mutate_int(
|
||||
k3, node[3], self.activation_indices, self.activation_replace_rate
|
||||
)
|
||||
|
||||
agg = mutate_int(
|
||||
k4, node[4], self.aggregation_indices, self.aggregation_replace_rate
|
||||
k4, agg, self.aggregation_indices, self.aggregation_replace_rate
|
||||
)
|
||||
|
||||
return jnp.array([index, bias, res, act, agg])
|
||||
act = mutate_int(k3, act, self.activation_indices, self.activation_replace_rate)
|
||||
|
||||
def distance(self, state, node1, node2):
|
||||
return jnp.array([bias, res, agg, act])
|
||||
|
||||
def distance(self, state, attrs1, attrs2):
|
||||
bias1, res1, agg1, act1 = attrs1
|
||||
bias2, res2, agg2, act2 = attrs2
|
||||
return (
|
||||
jnp.abs(node1[1] - node2[1]) # bias
|
||||
+ jnp.abs(node1[2] - node2[2]) # response
|
||||
+ (node1[3] != node2[3]) # activation
|
||||
+ (node1[4] != node2[4]) # aggregation
|
||||
jnp.abs(bias1 - bias2) # bias
|
||||
+ jnp.abs(res1 - res2) # response
|
||||
+ (agg1 != agg2) # aggregation
|
||||
+ (act1 != act2) # activation
|
||||
)
|
||||
|
||||
def forward(self, state, attrs, inputs, is_output_node=False):
|
||||
bias, res, act_idx, agg_idx = attrs
|
||||
bias, res, agg, act = attrs
|
||||
|
||||
z = agg(agg_idx, inputs, self.aggregation_options)
|
||||
z = agg_func(agg, inputs, self.aggregation_options)
|
||||
z = bias + res * z
|
||||
|
||||
# the last output node should not be activated
|
||||
z = jax.lax.cond(
|
||||
is_output_node, lambda: z, lambda: act(act_idx, z, self.activation_options)
|
||||
is_output_node, lambda: z, lambda: act_func(act, z, self.activation_options)
|
||||
)
|
||||
|
||||
return z
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import Tuple
|
||||
|
||||
import jax, jax.numpy as jnp
|
||||
|
||||
from utils import Act, Agg, act, agg, mutate_int, mutate_float
|
||||
from utils import Act, Agg, act_func, agg_func, mutate_int, mutate_float
|
||||
from . import BaseNodeGene
|
||||
|
||||
|
||||
@@ -21,12 +21,12 @@ class NodeGeneWithoutResponse(BaseNodeGene):
|
||||
bias_mutate_power: float = 0.5,
|
||||
bias_mutate_rate: float = 0.7,
|
||||
bias_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
aggregation_default: callable = Agg.sum,
|
||||
aggregation_options: Tuple = (Agg.sum,),
|
||||
aggregation_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
):
|
||||
super().__init__()
|
||||
self.bias_init_mean = bias_init_mean
|
||||
@@ -35,39 +35,36 @@ class NodeGeneWithoutResponse(BaseNodeGene):
|
||||
self.bias_mutate_rate = bias_mutate_rate
|
||||
self.bias_replace_rate = bias_replace_rate
|
||||
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
self.aggregation_default = aggregation_options.index(aggregation_default)
|
||||
self.aggregation_options = aggregation_options
|
||||
self.aggregation_indices = jnp.arange(len(aggregation_options))
|
||||
self.aggregation_replace_rate = aggregation_replace_rate
|
||||
|
||||
def new_custom_attrs(self, state):
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
def new_identity_attrs(self, state):
|
||||
return jnp.array(
|
||||
[
|
||||
self.bias_init_mean,
|
||||
self.activation_default,
|
||||
self.aggregation_default,
|
||||
]
|
||||
)
|
||||
[0, self.aggregation_default, -1]
|
||||
) # activation=-1 means Act.identity
|
||||
|
||||
def new_random_attrs(self, state, randkey):
|
||||
k1, k2, k3, k4 = jax.random.split(randkey, num=4)
|
||||
k1, k2, k3 = jax.random.split(randkey, num=3)
|
||||
bias = jax.random.normal(k1, ()) * self.bias_init_std + self.bias_init_mean
|
||||
act = jax.random.randint(k3, (), 0, len(self.activation_options))
|
||||
agg = jax.random.randint(k4, (), 0, len(self.aggregation_options))
|
||||
return jnp.array([bias, act, agg])
|
||||
agg = jax.random.choice(k2, self.aggregation_indices)
|
||||
act = jax.random.choice(k3, self.activation_indices)
|
||||
|
||||
def mutate(self, state, randkey, node):
|
||||
k1, k2, k3, k4 = jax.random.split(randkey, num=4)
|
||||
index = node[0]
|
||||
return jnp.array([bias, agg, act])
|
||||
|
||||
def mutate(self, state, randkey, attrs):
|
||||
k1, k2, k3 = jax.random.split(randkey, num=3)
|
||||
bias, agg, act = attrs
|
||||
|
||||
bias = mutate_float(
|
||||
k1,
|
||||
node[1],
|
||||
bias,
|
||||
self.bias_init_mean,
|
||||
self.bias_init_std,
|
||||
self.bias_mutate_power,
|
||||
@@ -75,32 +72,29 @@ class NodeGeneWithoutResponse(BaseNodeGene):
|
||||
self.bias_replace_rate,
|
||||
)
|
||||
|
||||
act = mutate_int(
|
||||
k3, node[2], self.activation_indices, self.activation_replace_rate
|
||||
)
|
||||
|
||||
agg = mutate_int(
|
||||
k4, node[3], self.aggregation_indices, self.aggregation_replace_rate
|
||||
k2, agg, self.aggregation_indices, self.aggregation_replace_rate
|
||||
)
|
||||
|
||||
return jnp.array([index, bias, act, agg])
|
||||
act = mutate_int(k3, act, self.activation_indices, self.activation_replace_rate)
|
||||
|
||||
def distance(self, state, node1, node2):
|
||||
return (
|
||||
jnp.abs(node1[1] - node2[1]) # bias
|
||||
+ (node1[2] != node2[2]) # activation
|
||||
+ (node1[3] != node2[3]) # aggregation
|
||||
)
|
||||
return jnp.array([bias, agg, act])
|
||||
|
||||
def distance(self, state, attrs1, attrs2):
|
||||
bias1, agg1, act1 = attrs1
|
||||
bias2, agg2, act2 = attrs2
|
||||
|
||||
return jnp.abs(bias1 - bias2) + (agg1 != agg2) + (act1 != act2)
|
||||
|
||||
def forward(self, state, attrs, inputs, is_output_node=False):
|
||||
bias, act_idx, agg_idx = attrs
|
||||
bias, agg, act = attrs
|
||||
|
||||
z = agg(agg_idx, inputs, self.aggregation_options)
|
||||
z = agg_func(agg, inputs, self.aggregation_options)
|
||||
z = bias + z
|
||||
|
||||
# the last output node should not be activated
|
||||
z = jax.lax.cond(
|
||||
is_output_node, lambda: z, lambda: act(act_idx, z, self.activation_options)
|
||||
is_output_node, lambda: z, lambda: act_func(act, z, self.activation_options)
|
||||
)
|
||||
|
||||
return z
|
||||
|
||||
@@ -2,7 +2,7 @@ from typing import Tuple
|
||||
|
||||
import jax, jax.numpy as jnp
|
||||
|
||||
from utils import Act, Agg, act, agg, mutate_int, mutate_float
|
||||
from utils import Act, Agg, act_func, agg_func, mutate_int, mutate_float
|
||||
from . import BaseNodeGene
|
||||
|
||||
|
||||
@@ -23,12 +23,12 @@ class NormalizedNode(BaseNodeGene):
|
||||
bias_mutate_power: float = 0.5,
|
||||
bias_mutate_rate: float = 0.7,
|
||||
bias_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
aggregation_default: callable = Agg.sum,
|
||||
aggregation_options: Tuple = (Agg.sum,),
|
||||
aggregation_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_replace_rate: float = 0.1,
|
||||
alpha_init_mean: float = 1.0,
|
||||
alpha_init_std: float = 1.0,
|
||||
alpha_mutate_power: float = 0.5,
|
||||
@@ -47,16 +47,16 @@ class NormalizedNode(BaseNodeGene):
|
||||
self.bias_mutate_rate = bias_mutate_rate
|
||||
self.bias_replace_rate = bias_replace_rate
|
||||
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
self.aggregation_default = aggregation_options.index(aggregation_default)
|
||||
self.aggregation_options = aggregation_options
|
||||
self.aggregation_indices = jnp.arange(len(aggregation_options))
|
||||
self.aggregation_replace_rate = aggregation_replace_rate
|
||||
|
||||
self.activation_default = activation_options.index(activation_default)
|
||||
self.activation_options = activation_options
|
||||
self.activation_indices = jnp.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
self.alpha_init_mean = alpha_init_mean
|
||||
self.alpha_init_std = alpha_init_std
|
||||
self.alpha_mutate_power = alpha_mutate_power
|
||||
@@ -69,38 +69,31 @@ class NormalizedNode(BaseNodeGene):
|
||||
self.beta_mutate_rate = beta_mutate_rate
|
||||
self.beta_replace_rate = beta_replace_rate
|
||||
|
||||
def new_custom_attrs(self, state):
|
||||
def new_identity_attrs(self, state):
|
||||
return jnp.array(
|
||||
[
|
||||
self.bias_init_mean,
|
||||
self.activation_default,
|
||||
self.aggregation_default,
|
||||
0, # mean
|
||||
1, # std
|
||||
self.alpha_init_mean, # alpha
|
||||
self.beta_init_mean, # beta
|
||||
]
|
||||
)
|
||||
[0, self.aggregation_default, -1, 0, 1, 1, 0]
|
||||
) # activation=-1 means Act.identity
|
||||
|
||||
def new_random_attrs(self, state, randkey):
|
||||
k1, k2, k3, k4, k5, k6 = jax.random.split(randkey, num=6)
|
||||
k1, k2, k3, k4, k5 = jax.random.split(randkey, num=5)
|
||||
bias = jax.random.normal(k1, ()) * self.bias_init_std + self.bias_init_mean
|
||||
agg = jax.random.randint(k2, (), 0, len(self.aggregation_options))
|
||||
act = jax.random.randint(k3, (), 0, len(self.activation_options))
|
||||
agg = jax.random.randint(k4, (), 0, len(self.aggregation_options))
|
||||
|
||||
mean = 0
|
||||
std = 1
|
||||
alpha = jax.random.normal(k5, ()) * self.alpha_init_std + self.alpha_init_mean
|
||||
beta = jax.random.normal(k6, ()) * self.beta_init_std + self.beta_init_mean
|
||||
alpha = jax.random.normal(k4, ()) * self.alpha_init_std + self.alpha_init_mean
|
||||
beta = jax.random.normal(k5, ()) * self.beta_init_std + self.beta_init_mean
|
||||
|
||||
return jnp.array([bias, act, agg, mean, std, alpha, beta])
|
||||
return jnp.array([bias, agg, act, mean, std, alpha, beta])
|
||||
|
||||
def mutate(self, state, randkey, node):
|
||||
k1, k2, k3, k4, k5, k6 = jax.random.split(randkey, num=6)
|
||||
index = node[0]
|
||||
def mutate(self, state, randkey, attrs):
|
||||
k1, k2, k3, k4, k5 = jax.random.split(randkey, num=5)
|
||||
bias, act, agg, mean, std, alpha, beta = attrs
|
||||
|
||||
bias = mutate_float(
|
||||
k1,
|
||||
node[1],
|
||||
bias,
|
||||
self.bias_init_mean,
|
||||
self.bias_init_std,
|
||||
self.bias_mutate_power,
|
||||
@@ -108,20 +101,15 @@ class NormalizedNode(BaseNodeGene):
|
||||
self.bias_replace_rate,
|
||||
)
|
||||
|
||||
act = mutate_int(
|
||||
k3, node[2], self.activation_indices, self.activation_replace_rate
|
||||
)
|
||||
|
||||
agg = mutate_int(
|
||||
k4, node[3], self.aggregation_indices, self.aggregation_replace_rate
|
||||
k2, agg, self.aggregation_indices, self.aggregation_replace_rate
|
||||
)
|
||||
|
||||
mean = node[4]
|
||||
std = node[5]
|
||||
act = mutate_int(k3, act, self.activation_indices, self.activation_replace_rate)
|
||||
|
||||
alpha = mutate_float(
|
||||
k5,
|
||||
node[6],
|
||||
k4,
|
||||
alpha,
|
||||
self.alpha_init_mean,
|
||||
self.alpha_init_std,
|
||||
self.alpha_mutate_power,
|
||||
@@ -130,8 +118,8 @@ class NormalizedNode(BaseNodeGene):
|
||||
)
|
||||
|
||||
beta = mutate_float(
|
||||
k6,
|
||||
node[7],
|
||||
k5,
|
||||
beta,
|
||||
self.beta_init_mean,
|
||||
self.beta_init_std,
|
||||
self.beta_mutate_power,
|
||||
@@ -139,40 +127,42 @@ class NormalizedNode(BaseNodeGene):
|
||||
self.beta_replace_rate,
|
||||
)
|
||||
|
||||
return jnp.array([index, bias, act, agg, mean, std, alpha, beta])
|
||||
return jnp.array([bias, agg, act, mean, std, alpha, beta])
|
||||
|
||||
def distance(self, state, node1, node2):
|
||||
def distance(self, state, attrs1, attrs2):
|
||||
bias1, agg1, act1, mean1, std1, alpha1, beta1 = attrs1
|
||||
bias2, agg2, act2, mean2, std2, alpha2, beta2 = attrs2
|
||||
return (
|
||||
jnp.abs(node1[1] - node2[1]) # bias
|
||||
+ (node1[2] != node2[2]) # activation
|
||||
+ (node1[3] != node2[3]) # aggregation
|
||||
+ (node1[6] - node2[6]) # alpha
|
||||
+ (node1[7] - node2[7]) # beta
|
||||
jnp.abs(bias1 - bias2) # bias
|
||||
+ (agg1 != agg2) # aggregation
|
||||
+ (act1 != act2) # activation
|
||||
+ jnp.abs(alpha1 - alpha2) # alpha
|
||||
+ jnp.abs(beta1 - beta2) # beta
|
||||
)
|
||||
|
||||
def forward(self, state, attrs, inputs, is_output_node=False):
|
||||
"""
|
||||
post_act = (agg(inputs) + bias - mean) / std * alpha + beta
|
||||
"""
|
||||
bias, act_idx, agg_idx, mean, std, alpha, beta = attrs
|
||||
bias, agg, act, mean, std, alpha, beta = attrs
|
||||
|
||||
z = agg(agg_idx, inputs, self.aggregation_options)
|
||||
z = agg_func(agg, inputs, self.aggregation_options)
|
||||
z = bias + z
|
||||
z = (z - mean) / (std + self.eps) * alpha + beta # normalization
|
||||
|
||||
# the last output node should not be activated
|
||||
z = jax.lax.cond(
|
||||
is_output_node, lambda: z, lambda: act(act_idx, z, self.activation_options)
|
||||
is_output_node, lambda: z, lambda: act_func(act, z, self.activation_options)
|
||||
)
|
||||
|
||||
return z
|
||||
|
||||
def update_by_batch(self, state, attrs, batch_inputs, is_output_node=False):
|
||||
|
||||
bias, act_idx, agg_idx, mean, std, alpha, beta = attrs
|
||||
bias, agg, act, mean, std, alpha, beta = attrs
|
||||
|
||||
batch_z = jax.vmap(agg, in_axes=(None, 0, None))(
|
||||
agg_idx, batch_inputs, self.aggregation_options
|
||||
batch_z = jax.vmap(agg_func, in_axes=(None, 0, None))(
|
||||
agg, batch_inputs, self.aggregation_options
|
||||
)
|
||||
|
||||
batch_z = bias + batch_z
|
||||
@@ -192,8 +182,8 @@ class NormalizedNode(BaseNodeGene):
|
||||
batch_z = jax.lax.cond(
|
||||
is_output_node,
|
||||
lambda: batch_z,
|
||||
lambda: jax.vmap(act, in_axes=(None, 0, None))(
|
||||
act_idx, batch_z, self.activation_options
|
||||
lambda: jax.vmap(act_func, in_axes=(None, 0, None))(
|
||||
act, batch_z, self.activation_options
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user