Skip to content

Instantly share code, notes, and snippets.

@pyliaorachel
pyliaorachel / treeignore.sh
Last active January 21, 2019 00:22
Command to display directory tree structure ignoring everything in .gitignore
# 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
@pyliaorachel
pyliaorachel / uninstall-editable.sh
Created August 5, 2017 17:13
Uninstall editable packages
pip3 uninstall $(basename $(find . -name '*.egg-info') .egg-info)
@pyliaorachel
pyliaorachel / clean_chinese_corpus.py
Created October 22, 2017 14:24
Remove non Chinese or non space characters
#
# Usage: python3 clean_chinese_corpus.py <corpus-to-clean> > <output>
#
import argparse
import re
# Parse args
@pyliaorachel
pyliaorachel / random_action.py
Created June 3, 2018 09:00
OpenAI Gym CartPole - Random Action
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
@pyliaorachel
pyliaorachel / hand_made_policy_choose_action.py
Created June 3, 2018 09:01
OpenAI Gym CartPole - Hand-made Policy (choose action)
# 定義 policy
def choose_action(observation):
pos, v, ang, rot = observation
return 0 if ang < 0 else 1 # 柱子左傾則小車左移,否則右移
@pyliaorachel
pyliaorachel / hand_made_policy_learn.py
Created June 3, 2018 09:02
OpenAI Gym CartPole - Hand-made Policy (learn)
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)
@pyliaorachel
pyliaorachel / q_table_choose_action.py
Created June 3, 2018 09:03
OpenAI Gym CartPole - Q table (choose action)
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])
@pyliaorachel
pyliaorachel / q_table_get_state.py
Created June 3, 2018 09:03
OpenAI Gym CartPole - Q table (get state)
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])
@pyliaorachel
pyliaorachel / q_table_learn.py
Created June 3, 2018 09:03
OpenAI Gym CartPole - Q table (get state)
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