Add CustomFuncFit into problem; Add related examples
This commit is contained in:
56
examples/func_fit/custom_func_fit.py
Normal file
56
examples/func_fit/custom_func_fit.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from tensorneat.pipeline import Pipeline
|
||||
from tensorneat.algorithm.neat import NEAT
|
||||
from tensorneat.genome import DefaultGenome, DefaultNode, DefaultMutation, BiasNode
|
||||
from tensorneat.problem.func_fit import CustomFuncFit
|
||||
from tensorneat.common import Act, Agg
|
||||
|
||||
|
||||
def pagie_polynomial(inputs):
|
||||
x, y = inputs
|
||||
res = 1 / (1 + jnp.pow(x, -4)) + 1 / (1 + jnp.pow(y, -4))
|
||||
|
||||
# important! returns an array, NOT a scalar
|
||||
return jnp.array([res])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
custom_problem = CustomFuncFit(
|
||||
func=pagie_polynomial,
|
||||
low_bounds=[-1, -1],
|
||||
upper_bounds=[1, 1],
|
||||
method="sample",
|
||||
num_samples=1000,
|
||||
)
|
||||
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
pop_size=10000,
|
||||
species_size=20,
|
||||
survival_threshold=0.01,
|
||||
genome=DefaultGenome(
|
||||
num_inputs=2,
|
||||
num_outputs=1,
|
||||
init_hidden_layers=(),
|
||||
node_gene=BiasNode(
|
||||
activation_options=[Act.identity, Act.inv, Act.square],
|
||||
aggregation_options=[Agg.sum, Agg.product],
|
||||
),
|
||||
output_transform=Act.identity,
|
||||
),
|
||||
),
|
||||
problem=custom_problem,
|
||||
generation_limit=100,
|
||||
fitness_target=-1e-4,
|
||||
seed=42,
|
||||
)
|
||||
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
# show result
|
||||
# pipeline.show(state, best)
|
||||
print(pipeline.algorithm.genome.repr(state, *best))
|
||||
@@ -1,16 +1,39 @@
|
||||
import jax, jax.numpy as jnp
|
||||
|
||||
arr = jnp.ones((10, 10))
|
||||
a = jnp.array([
|
||||
[1, 2, 3],
|
||||
[4, 5, 6]
|
||||
])
|
||||
from tensorneat.pipeline import Pipeline
|
||||
from tensorneat.algorithm.neat import NEAT
|
||||
from tensorneat.genome import DefaultGenome, DefaultNode, DefaultMutation, BiasNode
|
||||
from tensorneat.problem.func_fit import CustomFuncFit
|
||||
from tensorneat.common import Act, Agg
|
||||
|
||||
def attach_with_inf(arr, idx):
|
||||
target_dim = arr.ndim + idx.ndim - 1
|
||||
expand_idx = jnp.expand_dims(idx, axis=tuple(range(idx.ndim, target_dim)))
|
||||
|
||||
return jnp.where(expand_idx == 1, jnp.nan, arr[idx])
|
||||
def pagie_polynomial(inputs):
|
||||
x, y = inputs
|
||||
return x + y
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
genome=DefaultGenome(
|
||||
num_inputs=2,
|
||||
num_outputs=1,
|
||||
max_nodes=3,
|
||||
max_conns=2,
|
||||
init_hidden_layers=(),
|
||||
node_gene=BiasNode(
|
||||
activation_options=[Act.identity],
|
||||
aggregation_options=[Agg.sum],
|
||||
),
|
||||
output_transform=Act.identity,
|
||||
mutation=DefaultMutation(
|
||||
node_add=0,
|
||||
node_delete=0,
|
||||
conn_add=0.0,
|
||||
conn_delete=0.0,
|
||||
)
|
||||
)
|
||||
randkey = jax.random.PRNGKey(42)
|
||||
state = genome.setup()
|
||||
nodes, conns = genome.initialize(state, randkey)
|
||||
print(genome)
|
||||
|
||||
|
||||
b = attach_with_inf(arr, a)
|
||||
print(b)
|
||||
Reference in New Issue
Block a user