modifying

This commit is contained in:
wls2002
2023-06-27 18:47:47 +08:00
parent ba369db0b2
commit 114ff2b0cc
28 changed files with 451 additions and 123 deletions

View File

@@ -1,55 +0,0 @@
import numpy as np
import jax.numpy as jnp
import jax
a = {1:2, 2:3, 4:5}
print(a.values())
a = jnp.array([1, 0, 1, 0, np.nan])
b = jnp.array([1, 1, 1, 1, 1])
c = jnp.array([1, 1, 1, 1, 1])
full = jnp.array([
[1, 1, 1],
[0, 1, 1],
[1, 1, 1],
[0, 1, 1],
])
print(jnp.column_stack([a[:, None], b[:, None], c[:, None]]))
aux0 = full[:, 0, None]
aux1 = full[:, 1, None]
print(aux0, aux0.shape)
print(jnp.concatenate([aux0, aux1], axis=1))
f_a = jnp.array([False, False, True, True])
f_b = jnp.array([True, False, False, False])
print(jnp.logical_and(f_a, f_b))
print(f_a & f_b)
print(f_a + jnp.nan * 0.0)
print(f_a + 1 * 0.0)
@jax.jit
def main():
return func('happy') + func('sad')
def func(x):
if x == 'happy':
return 1
else:
return 2
a = jnp.zeros((3, 3))
print(a.dtype)
c = None
b = 1 or c
print(b)

26
examples/evox_test.py Normal file
View File

@@ -0,0 +1,26 @@
import jax
from jax import numpy as jnp
from evox import algorithms, problems, pipelines
from evox.monitors import StdSOMonitor
monitor = StdSOMonitor()
pso = algorithms.PSO(
lb=jnp.full(shape=(2,), fill_value=-32),
ub=jnp.full(shape=(2,), fill_value=32),
pop_size=100,
)
ackley = problems.classic.Ackley()
pipeline = pipelines.StdPipeline(pso, ackley, fitness_transform=monitor.record_fit)
key = jax.random.PRNGKey(42)
state = pipeline.init(key)
# run the pipeline for 100 steps
for i in range(100):
state = pipeline.step(state)
print(monitor.get_min_fitness())

View File

@@ -1,27 +1,18 @@
import numpy as np
from jax import jit
from functools import partial
from configs import Configer
from neat.pipeline import Pipeline
import jax
from jax import numpy as jnp, jit
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
xor_outputs = np.array([[0], [1], [1], [0]], dtype=np.float32)
def main():
config = Configer.load_config("xor.ini")
print(config)
pipeline = Pipeline(config)
forward_func = pipeline.ask()
# inputs = np.tile(xor_inputs, (150, 1, 1))
outputs = forward_func(xor_inputs)
print(outputs)
@partial(jit, static_argnames=['reverse'])
def rank_element(array, reverse=False):
"""
rank the element in the array.
if reverse is True, the rank is from large to small.
"""
if reverse:
array = -array
return jnp.argsort(jnp.argsort(array))
@jit
def f(x, jit_config):
return x + jit_config["bias_mutate_rate"]
if __name__ == '__main__':
main()
a = jnp.array([1 ,5, 3, 5, 2, 1, 0])
print(rank_element(a, reverse=True))

28
examples/jit_xor.py Normal file
View File

@@ -0,0 +1,28 @@
import numpy as np
from configs import Configer
from jit_pipeline import Pipeline
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
xor_outputs = np.array([[0], [1], [1], [0]], dtype=np.float32)
def evaluate(forward_func):
"""
:param forward_func: (4: batch, 2: input size) -> (pop_size, 4: batch, 1: output size)
:return:
"""
outs = forward_func(xor_inputs)
fitnesses = 4 - np.sum((outs - xor_outputs) ** 2, axis=(1, 2))
return np.array(fitnesses) # returns a list
def main():
config = Configer.load_config("xor.ini")
pipeline = Pipeline(config, seed=6)
nodes, cons = pipeline.auto_run(evaluate)
print(nodes, cons)
if __name__ == '__main__':
main()

View File

@@ -1,7 +1,7 @@
import numpy as np
from configs import Configer
from neat.pipeline import Pipeline
from pipeline import Pipeline
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
xor_outputs = np.array([[0], [1], [1], [0]], dtype=np.float32)
@@ -21,6 +21,8 @@ def main():
config = Configer.load_config("xor.ini")
pipeline = Pipeline(config, seed=6)
nodes, cons = pipeline.auto_run(evaluate)
print(nodes, cons)
if __name__ == '__main__':
main()