This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| env = gym.make('CartPole-v0') | |
| # 準備 Q table | |
| ## Environment 中各個 feature 的 bucket 分配數量 | |
| ## 1 代表任何值皆表同一 state,也就是這個 feature 其實不重要 | |
| n_buckets = (1, 1, 6, 3) | |
| ## Action 數量 | |
| n_actions = env.action_space.n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_state(observation, n_buckets, state_bounds): | |
| state = [0] * len(observation) | |
| for i, s in enumerate(observation): # 每個 feature 有不同的分配 | |
| l, u = state_bounds[i][0], state_bounds[i][1] # 每個 feature 值的範圍上下限 | |
| if s <= l: # 低於下限,分配為 0 | |
| state[i] = 0 | |
| elif s >= u: # 高於上限,分配為最大值 | |
| state[i] = n_buckets[i] - 1 | |
| else: # 範圍內,依比例分配 | |
| state[i] = int(((s - l) / (u - l)) * n_buckets[i]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def choose_action(state, q_table, action_space, epsilon): | |
| if np.random.random_sample() < epsilon: # 有 ε 的機率會選擇隨機 action | |
| return action_space.sample() | |
| else: # 其他時間根據現有 policy 選擇 action,也就是在 Q table 裡目前 state 中,選擇擁有最大 Q value 的 action | |
| return np.argmax(q_table[state]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| env = gym.make('CartPole-v0') | |
| for i_episode in range(200): | |
| observation = env.reset() | |
| rewards = 0 | |
| for t in range(250): | |
| env.render() | |
| action = choose_action(observation) | |
| observation, reward, done, info = env.step(action) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 定義 policy | |
| def choose_action(observation): | |
| pos, v, ang, rot = observation | |
| return 0 if ang < 0 else 1 # 柱子左傾則小車左移,否則右移 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| env = gym.make('CartPole-v0') | |
| # 跑 200 個 episode,每個 episode 都是一次任務嘗試 | |
| for i_episode in range(200): | |
| observation = env.reset() # 讓 environment 重回初始狀態 | |
| rewards = 0 # 累計各 episode 的 reward | |
| for t in range(250): # 設個時限,每個 episode 最多跑 250 個 action | |
| env.render() # 呈現 environment | |
| # Key section |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Usage: python3 clean_chinese_corpus.py <corpus-to-clean> > <output> | |
| # | |
| import argparse | |
| import re | |
| # Parse args |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pip3 uninstall $(basename $(find . -name '*.egg-info') .egg-info) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Tested on AWS EC2 Linux | |
| # tree -I '<pattern-to-ignore>' -> display tree | |
| # grep -o '^[^#]*' .gitignore -> remove comments in .gitignore | |
| # | sed 's/\/$//g' -> replace trailing '/' after directories | |
| # | tr '\n' '|' -> join strings with '|' | |
| tree -I $(grep -o '^[^#]*' .gitignore | sed 's/\/$//g' | tr '\n' '|') | |
| # Tested on MacOSX |
NewerOlder