adjust default parameter; successful run recurrent-xor example
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import Callable, Sequence
|
||||
import numpy as np
|
||||
import jax
|
||||
from jax import vmap, numpy as jnp
|
||||
from .gene import BaseNodeGene, BaseConnGene
|
||||
from .gene import BaseNode, BaseConn
|
||||
from .operations import BaseMutation, BaseCrossover, BaseDistance
|
||||
from tensorneat.common import (
|
||||
State,
|
||||
@@ -22,8 +22,8 @@ class BaseGenome(StatefulBaseClass):
|
||||
num_outputs: int,
|
||||
max_nodes: int,
|
||||
max_conns: int,
|
||||
node_gene: BaseNodeGene,
|
||||
conn_gene: BaseConnGene,
|
||||
node_gene: BaseNode,
|
||||
conn_gene: BaseConn,
|
||||
mutation: BaseMutation,
|
||||
crossover: BaseCrossover,
|
||||
distance: BaseDistance,
|
||||
@@ -92,7 +92,6 @@ class BaseGenome(StatefulBaseClass):
|
||||
self.output_idx = np.array(layer_indices[-1])
|
||||
self.all_init_nodes = np.array(all_init_nodes)
|
||||
self.all_init_conns = np.c_[all_init_conns_in_idx, all_init_conns_out_idx]
|
||||
print(self.output_idx)
|
||||
|
||||
def setup(self, state=State()):
|
||||
state = self.node_gene.setup(state)
|
||||
|
||||
@@ -6,7 +6,7 @@ import numpy as np
|
||||
import sympy as sp
|
||||
|
||||
from .base import BaseGenome
|
||||
from .gene import DefaultNodeGene, DefaultConnGene
|
||||
from .gene import DefaultNode, DefaultConn
|
||||
from .operations import DefaultMutation, DefaultCrossover, DefaultDistance
|
||||
from .utils import unflatten_conns, extract_node_attrs, extract_conn_attrs
|
||||
|
||||
@@ -31,8 +31,8 @@ class DefaultGenome(BaseGenome):
|
||||
num_outputs: int,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(),
|
||||
conn_gene=DefaultConnGene(),
|
||||
node_gene=DefaultNode(),
|
||||
conn_gene=DefaultConn(),
|
||||
mutation=DefaultMutation(),
|
||||
crossover=DefaultCrossover(),
|
||||
distance=DefaultDistance(),
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
from .base import BaseConnGene
|
||||
from .default import DefaultConnGene
|
||||
from .base import BaseConn
|
||||
from .default import DefaultConn
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import jax
|
||||
from .. import BaseGene
|
||||
from ..base import BaseGene
|
||||
|
||||
|
||||
class BaseConnGene(BaseGene):
|
||||
class BaseConn(BaseGene):
|
||||
"Base class for connection genes."
|
||||
fixed_attrs = ["input_index", "output_index"]
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ import jax.numpy as jnp
|
||||
import jax.random
|
||||
import sympy as sp
|
||||
from tensorneat.common import mutate_float
|
||||
from .base import BaseConnGene
|
||||
from .base import BaseConn
|
||||
|
||||
|
||||
class DefaultConnGene(BaseConnGene):
|
||||
class DefaultConn(BaseConn):
|
||||
"Default connection gene, with the same behavior as in NEAT-python."
|
||||
|
||||
custom_attrs = ["weight"]
|
||||
@@ -14,9 +14,9 @@ class DefaultConnGene(BaseConnGene):
|
||||
self,
|
||||
weight_init_mean: float = 0.0,
|
||||
weight_init_std: float = 1.0,
|
||||
weight_mutate_power: float = 0.5,
|
||||
weight_mutate_rate: float = 0.8,
|
||||
weight_replace_rate: float = 0.1,
|
||||
weight_mutate_power: float = 0.15,
|
||||
weight_mutate_rate: float = 0.2,
|
||||
weight_replace_rate: float = 0.015,
|
||||
):
|
||||
super().__init__()
|
||||
self.weight_init_mean = weight_init_mean
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from .base import BaseNodeGene
|
||||
from .default import DefaultNodeGene
|
||||
from .base import BaseNode
|
||||
from .default import DefaultNode
|
||||
from .bias import BiasNode
|
||||
|
||||
@@ -2,7 +2,7 @@ import jax, jax.numpy as jnp
|
||||
from .. import BaseGene
|
||||
|
||||
|
||||
class BaseNodeGene(BaseGene):
|
||||
class BaseNode(BaseGene):
|
||||
"Base class for node genes."
|
||||
fixed_attrs = ["index"]
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from typing import Tuple
|
||||
from typing import Union, Sequence, Callable, Optional
|
||||
|
||||
import numpy as np
|
||||
import jax, jax.numpy as jnp
|
||||
import sympy as sp
|
||||
from tensorneat.common import (
|
||||
@@ -12,10 +13,10 @@ from tensorneat.common import (
|
||||
convert_to_sympy,
|
||||
)
|
||||
|
||||
from . import BaseNodeGene
|
||||
from . import BaseNode
|
||||
|
||||
|
||||
class BiasNode(BaseNodeGene):
|
||||
class BiasNode(BaseNode):
|
||||
"""
|
||||
Default node gene, with the same behavior as in NEAT-python.
|
||||
The attribute response is removed.
|
||||
@@ -27,31 +28,46 @@ class BiasNode(BaseNodeGene):
|
||||
self,
|
||||
bias_init_mean: float = 0.0,
|
||||
bias_init_std: float = 1.0,
|
||||
bias_mutate_power: float = 0.5,
|
||||
bias_mutate_rate: float = 0.7,
|
||||
bias_replace_rate: float = 0.1,
|
||||
aggregation_default: callable = Agg.sum,
|
||||
aggregation_options: Tuple = (Agg.sum,),
|
||||
bias_mutate_power: float = 0.15,
|
||||
bias_mutate_rate: float = 0.2,
|
||||
bias_replace_rate: float = 0.015,
|
||||
bias_lower_bound: float = -5,
|
||||
bias_upper_bound: float = 5,
|
||||
aggregation_default: Optional[Callable] = None,
|
||||
aggregation_options: Union[Callable, Sequence[Callable]] = Agg.sum,
|
||||
aggregation_replace_rate: float = 0.1,
|
||||
activation_default: callable = Act.sigmoid,
|
||||
activation_options: Tuple = (Act.sigmoid,),
|
||||
activation_default: Optional[Callable] = None,
|
||||
activation_options: Union[Callable, Sequence[Callable]] = Act.sigmoid,
|
||||
activation_replace_rate: float = 0.1,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
if isinstance(aggregation_options, Callable):
|
||||
aggregation_options = [aggregation_options]
|
||||
if isinstance(activation_options, Callable):
|
||||
activation_options = [activation_options]
|
||||
|
||||
if len(aggregation_options) == 1 and aggregation_default is None:
|
||||
aggregation_default = aggregation_options[0]
|
||||
if len(activation_options) == 1 and activation_default is None:
|
||||
activation_default = activation_options[0]
|
||||
|
||||
self.bias_init_mean = bias_init_mean
|
||||
self.bias_init_std = bias_init_std
|
||||
self.bias_mutate_power = bias_mutate_power
|
||||
self.bias_mutate_rate = bias_mutate_rate
|
||||
self.bias_replace_rate = bias_replace_rate
|
||||
self.bias_lower_bound = bias_lower_bound
|
||||
self.bias_upper_bound = bias_upper_bound
|
||||
|
||||
self.aggregation_default = aggregation_options.index(aggregation_default)
|
||||
self.aggregation_options = aggregation_options
|
||||
self.aggregation_indices = jnp.arange(len(aggregation_options))
|
||||
self.aggregation_indices = np.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_indices = np.arange(len(activation_options))
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
def new_identity_attrs(self, state):
|
||||
@@ -62,6 +78,7 @@ class BiasNode(BaseNodeGene):
|
||||
def new_random_attrs(self, state, randkey):
|
||||
k1, k2, k3 = jax.random.split(randkey, num=3)
|
||||
bias = jax.random.normal(k1, ()) * self.bias_init_std + self.bias_init_mean
|
||||
bias = jnp.clip(bias, self.bias_lower_bound, self.bias_upper_bound)
|
||||
agg = jax.random.choice(k2, self.aggregation_indices)
|
||||
act = jax.random.choice(k3, self.activation_indices)
|
||||
|
||||
@@ -80,7 +97,7 @@ class BiasNode(BaseNodeGene):
|
||||
self.bias_mutate_rate,
|
||||
self.bias_replace_rate,
|
||||
)
|
||||
|
||||
bias = jnp.clip(bias, self.bias_lower_bound, self.bias_upper_bound)
|
||||
agg = mutate_int(
|
||||
k2, agg, self.aggregation_indices, self.aggregation_replace_rate
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Tuple, Union, Sequence, Callable
|
||||
from typing import Optional, Union, Sequence, Callable
|
||||
|
||||
import numpy as np
|
||||
import jax, jax.numpy as jnp
|
||||
@@ -14,10 +14,10 @@ from tensorneat.common import (
|
||||
convert_to_sympy,
|
||||
)
|
||||
|
||||
from . import BaseNodeGene
|
||||
from .base import BaseNode
|
||||
|
||||
|
||||
class DefaultNodeGene(BaseNodeGene):
|
||||
class DefaultNode(BaseNode):
|
||||
"Default node gene, with the same behavior as in NEAT-python."
|
||||
|
||||
custom_attrs = ["bias", "response", "aggregation", "activation"]
|
||||
@@ -26,18 +26,22 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self,
|
||||
bias_init_mean: float = 0.0,
|
||||
bias_init_std: float = 1.0,
|
||||
bias_mutate_power: float = 0.5,
|
||||
bias_mutate_rate: float = 0.7,
|
||||
bias_replace_rate: float = 0.1,
|
||||
bias_mutate_power: float = 0.15,
|
||||
bias_mutate_rate: float = 0.2,
|
||||
bias_replace_rate: float = 0.015,
|
||||
bias_lower_bound: float = -5,
|
||||
bias_upper_bound: float = 5,
|
||||
response_init_mean: float = 1.0,
|
||||
response_init_std: float = 0.0,
|
||||
response_mutate_power: float = 0.5,
|
||||
response_mutate_rate: float = 0.7,
|
||||
response_replace_rate: float = 0.1,
|
||||
aggregation_default: Callable = Agg.sum,
|
||||
response_mutate_power: float = 0.15,
|
||||
response_mutate_rate: float = 0.2,
|
||||
response_replace_rate: float = 0.015,
|
||||
response_lower_bound: float = -5,
|
||||
response_upper_bound: float = 5,
|
||||
aggregation_default: Optional[Callable] = None,
|
||||
aggregation_options: Union[Callable, Sequence[Callable]] = Agg.sum,
|
||||
aggregation_replace_rate: float = 0.1,
|
||||
activation_default: Callable = Act.sigmoid,
|
||||
activation_default: Optional[Callable] = None,
|
||||
activation_options: Union[Callable, Sequence[Callable]] = Act.sigmoid,
|
||||
activation_replace_rate: float = 0.1,
|
||||
):
|
||||
@@ -48,17 +52,26 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
if isinstance(activation_options, Callable):
|
||||
activation_options = [activation_options]
|
||||
|
||||
if len(aggregation_options) == 1 and aggregation_default is None:
|
||||
aggregation_default = aggregation_options[0]
|
||||
if len(activation_options) == 1 and activation_default is None:
|
||||
activation_default = activation_options[0]
|
||||
|
||||
self.bias_init_mean = bias_init_mean
|
||||
self.bias_init_std = bias_init_std
|
||||
self.bias_mutate_power = bias_mutate_power
|
||||
self.bias_mutate_rate = bias_mutate_rate
|
||||
self.bias_replace_rate = bias_replace_rate
|
||||
self.bias_lower_bound = bias_lower_bound
|
||||
self.bias_upper_bound = bias_upper_bound
|
||||
|
||||
self.response_init_mean = response_init_mean
|
||||
self.response_init_std = response_init_std
|
||||
self.response_mutate_power = response_mutate_power
|
||||
self.response_mutate_rate = response_mutate_rate
|
||||
self.response_replace_rate = response_replace_rate
|
||||
self.reponse_lower_bound = response_lower_bound
|
||||
self.response_upper_bound = response_upper_bound
|
||||
|
||||
self.aggregation_default = aggregation_options.index(aggregation_default)
|
||||
self.aggregation_options = aggregation_options
|
||||
@@ -71,16 +84,21 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self.activation_replace_rate = activation_replace_rate
|
||||
|
||||
def new_identity_attrs(self, state):
|
||||
return jnp.array(
|
||||
[0, 1, self.aggregation_default, -1]
|
||||
) # activation=-1 means Act.identity
|
||||
bias = 0
|
||||
res = 1
|
||||
agg = self.aggregation_default
|
||||
act = self.activation_default
|
||||
|
||||
return jnp.array([bias, res, agg, act]) # activation=-1 means Act.identity
|
||||
|
||||
def new_random_attrs(self, state, randkey):
|
||||
k1, k2, k3, k4 = jax.random.split(randkey, num=4)
|
||||
bias = jax.random.normal(k1, ()) * self.bias_init_std + self.bias_init_mean
|
||||
bias = jnp.clip(bias, self.bias_lower_bound, self.bias_upper_bound)
|
||||
res = (
|
||||
jax.random.normal(k2, ()) * self.response_init_std + self.response_init_mean
|
||||
)
|
||||
res = jnp.clip(res, self.reponse_lower_bound, self.response_upper_bound)
|
||||
agg = jax.random.choice(k3, self.aggregation_indices)
|
||||
act = jax.random.choice(k4, self.activation_indices)
|
||||
|
||||
@@ -98,7 +116,7 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self.bias_mutate_rate,
|
||||
self.bias_replace_rate,
|
||||
)
|
||||
|
||||
bias = jnp.clip(bias, self.bias_lower_bound, self.bias_upper_bound)
|
||||
res = mutate_float(
|
||||
k2,
|
||||
res,
|
||||
@@ -108,7 +126,7 @@ class DefaultNodeGene(BaseNodeGene):
|
||||
self.response_mutate_rate,
|
||||
self.response_replace_rate,
|
||||
)
|
||||
|
||||
res = jnp.clip(res, self.reponse_lower_bound, self.response_upper_bound)
|
||||
agg = mutate_int(
|
||||
k4, agg, self.aggregation_indices, self.aggregation_replace_rate
|
||||
)
|
||||
|
||||
@@ -23,9 +23,9 @@ from ...utils import (
|
||||
class DefaultMutation(BaseMutation):
|
||||
def __init__(
|
||||
self,
|
||||
conn_add: float = 0.2,
|
||||
conn_add: float = 0.1,
|
||||
conn_delete: float = 0,
|
||||
node_add: float = 0.2,
|
||||
node_add: float = 0.1,
|
||||
node_delete: float = 0,
|
||||
):
|
||||
self.conn_add = conn_add
|
||||
|
||||
@@ -3,7 +3,7 @@ from jax import vmap, numpy as jnp
|
||||
from .utils import unflatten_conns
|
||||
|
||||
from .base import BaseGenome
|
||||
from .gene import DefaultNodeGene, DefaultConnGene
|
||||
from .gene import DefaultNode, DefaultConn
|
||||
from .operations import DefaultMutation, DefaultCrossover, DefaultDistance
|
||||
from .utils import unflatten_conns, extract_node_attrs, extract_conn_attrs
|
||||
|
||||
@@ -20,8 +20,8 @@ class RecurrentGenome(BaseGenome):
|
||||
num_outputs: int,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(),
|
||||
conn_gene=DefaultConnGene(),
|
||||
node_gene=DefaultNode(),
|
||||
conn_gene=DefaultConn(),
|
||||
mutation=DefaultMutation(),
|
||||
crossover=DefaultCrossover(),
|
||||
distance=DefaultDistance(),
|
||||
|
||||
Reference in New Issue
Block a user