EnsekiTT Blog

EnsekiTTが書くブログです。

一人っ子だからUnityで強化学習して一緒にゲームする友達を作る話

こんにちは、えんせきです。
皆さん天皇誕生日をいかがお過ごしですか?
クリスマスを前にして寂しく一人っ子なのも辛いので友達がほしいから作ることにしました。
一緒にゲームで遊んだらもう友達だよね。

つまりなにしたの?

UnityのMachine Learning Agentsで遊んだ。

f:id:ensekitt:20171225002015j:plain

Unity Machine Learning Agentsってなに?

ゲームを自動で何万回も遊んで上手になってくれる子を作る一人っ子の強い味方。
ちょっと適当すぎましたね。

Unityはゲームとかを作る開発環境とかで、物理エンジンとかも入ってるゲームエンジニアの強い味方。
ゲームはプレイすると、スコアが出てきて、そのスコアが良いほうが褒められる娯楽。ただし今回は環境として扱われる。
強化学習は、褒められた方式を覚えてどんどんスコアを良くすることができる機械学習の一つ。エージェントになる。

Unity Machine Learning Agentsは環境とエージェントを繋いでくれる。
エージェントは環境に対してアクションを起こす。これはゲームをプレイすることと同じ。
環境はエージェントに対して今のアクションのスコアを返す。これはゲームのスコアが表示されるのと同じ。
で、エージェントは何万回もゲームをプレイしてスコアが良かった方法をひたすら学習してうまくなる。

環境の準備

UnityのインストールとPython(ぼくはpyenvで用意した)をインストールする。
unity3d.com

環境の準備は主にここのドキュメントを読みながら進めた
github.com

とりあえずForkしてCloneした

git clone https://github.com/YourAccount/ml-agents
cd ml-agents

必要な依存関係

pyenv virtualenv 3.6.3 unityml
pyenv local unityml

cd ml-agents
cd python
pip install .

まずはExamplesをやる

板の上にボールを乗っけてバランスを取るゲームを上手くやる。

Unityを起動して今回実行するExampleを読み込む

f:id:ensekitt:20171225001307j:plain

開いてAssetsからExamples/3DBall/Sceneを開く

f:id:ensekitt:20171225001320j:plain

ビルド前の設定を行う

f:id:ensekitt:20171225001332j:plain

ビルドする

f:id:ensekitt:20171225001343j:plain

Jupyter notebookから学習を実行する

$ jupyter notebook

を起動してハイパーパラメータを設定する

### General parameters
max_steps = 5e5 # Set maximum number of steps to run environment.
run_path = "ppo" # The sub-directory name for model and summary statistics
load_model = False # Whether to load a saved model.
train_model = True # Whether to train the model.
summary_freq = 10000 # Frequency at which to save training statistics.
save_freq = 50000 # Frequency at which to save model.
env_name = "3dball.app" # Name of the training environment file.
curriculum_file = None

### Algorithm-specific parameters for tuning
gamma = 0.99 # Reward discount rate.
lambd = 0.95 # Lambda parameter for GAE.
time_horizon = 2048 # How many steps to collect per agent before adding to buffer.
beta = 1e-3 # Strength of entropy regularization
num_epoch = 5 # Number of gradient descent steps per batch of experiences.
num_layers = 2 # Number of hidden layers between state/observation encoding and value/policy layers.
epsilon = 0.2 # Acceptable threshold around ratio of old and new policy probabilities.
buffer_size = 2048 # How large the experience buffer should be before gradient descent.
learning_rate = 3e-4 # Model learning rate.
hidden_units = 64 # Number of units in hidden layer.
batch_size = 64 # How many experiences per gradient descent update step.
normalize = False

### Logging dictionary for hyperparameters
hyperparameter_dict = {'max_steps':max_steps, 'run_path':run_path, 'env_name':env_name,
    'curriculum_file':curriculum_file, 'gamma':gamma, 'lambd':lambd, 'time_horizon':time_horizon,
    'beta':beta, 'num_epoch':num_epoch, 'epsilon':epsilon, 'buffe_size':buffer_size,
    'leaning_rate':learning_rate, 'hidden_units':hidden_units, 'batch_size':batch_size}

env_nameをさっきの3dball.appに設定して実行する
f:id:ensekitt:20171225001406j:plain

PPOってアルゴリズムで学習している様子。
[1707.06347] Proximal Policy Optimization Algorithms

TensorBoardで進捗確認

f:id:ensekitt:20171225001445j:plain

学習を待っているので続きは次回

クリエイティブ・コモンズ・ライセンス
この 作品 は クリエイティブ・コモンズ 表示 4.0 国際 ライセンスの下に提供されています。