Comments (4)
forはコンパイル時に全て展開されてかなり時間を食うので、固定長であっても使わない方が良い。
jax.lax.fori_loop
やjax.lax.map
に変える。
- before
# 遅い
for xy in range(BOARD_SIZE * BOARD_SIZE):
_, _, is_illegal = step(state, xy)
legal_action = legal_action.at[xy].set(is_illegal)
- after
# 速い
jax.lax.map(lambda xy: step(state, xy), jnp.arange(BOARD_SIZE * BOARD_SIZE))[2]
どうしてもfor
ですべての要素を調べないといけない場合が生じる場合、アルゴリズムを見直した方が良い。
(stateに新たに変数を追加するくらいで遅くなったりはしない)
例:囲碁で石を取った時に、隣接する連に呼吸点を追加する部分
- before
for _xy in range(BOARD_SIZE * BOARD_SIZE):
for _around in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
# 全ての点の周囲四方を調べ、取られた石と一致したら呼吸点に追加
- after
# 隣接しているかどうかをstateに記録(0:なし 1:呼吸点 2:石)
jax.lax.map(
lambda l: jnp.where((l > 0) & surrounded_stones, 1, l), # 呼吸点に追加
_state.liberty,
)
whileは条件が合えば使える。
jaxの制限を意識しすぎて非効率的なアルゴリズムを組むくらいならjax.lax.while_loop
を使った方が良い。
from pgx.
関連がありそうなリンク: https://medium.com/@ngoodger_7766/writing-an-rl-environment-in-jax-9f74338898ba
from pgx.
関数の途中でのリターンやbreakはできる?どうする?
from pgx.
- 途中でのリターンはcondで分けてreturn以降を別関数へ分ける
- fori_loop, map, scanを使い分ける
- while_loop
- どうすてもifがネストするときには条件に名前を付けてネストを浅くする
from pgx.
Related Issues (20)
- Specify rng explicitly in step function HOT 3
- [BridgeBidding] Use cuckoo hashing for LUT
- [Mahjong] Determine the next player after Meld
- [Mahjong] Segmantation fault
- Implement `import pgx.v1 as pgx`
- [Baseline, examples] Migrate to Flax (or other NN libraries) HOT 3
- [Test] shorten test runtime
- ValueError: mutable default <class 'jaxlib.xla_extension.ArrayImpl'> for field current_player is not allowed: use default_factory HOT 6
- underpromotion moves for black and whiter should be different HOT 4
- [Mahjong] Reduce compilation/run time HOT 2
- [Kuhn poker] Fix visualization
- [Chess] Reconsider zobrist hash
- Fix codecov CI
- Haiku errors in Py3.8
- Royal Game of Ur Environment HOT 4
- [Internal change] Decompose the game logic and API HOT 2
- alphazero one game search in mult devices HOT 1
- Remove `player_id` argument in `Env.observe`
- Add `jaxlib` to `requirements.txt`
- AlphaZero training on standard chess HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgx.