Index
バージョン
ml-agents 0.6
単純なミスかな・・・?
まだエラーがある・・・
1 2 3 4 | UnityAgentsException: The Communicator was unable to connect. Please make sure the External process is ready to accept communication with Unity. MLAgents.Batcher.SendAcademyParameters (MLAgents.CommunicatorObjects.UnityRLInitializationOutput academyParameters) (at Assets/ML-Agents/Scripts/Batcher.cs:91) MLAgents.Academy.InitializeEnvironment () (at Assets/ML-Agents/Scripts/Academy.cs:321) MLAgents.Academy.Awake () (at Assets/ML-Agents/Scripts/Academy.cs:230) |
とりあえずエラーの翻訳
UnityAgentsException: The Communicator was unable to connect. Please make sure the External process is ready to accept communication with Unity.
コミュニケータが接続できませんでした。外部プロセスがUnityとの通信を受け入れる準備ができていることを確認してください。
一体どういう意味なんだ・・・
とりあえずGoogle検索
参考 GoogleThe Communicator was unable to connect. Please make sure the External process is ready to accept communication with Unity.とりあえずコマンドプロンプト起動すれば良さそう
別のエラーが出た・・・。
1 2 3 4 5 | UnityAgentsException: Vector Observation size mismatch between continuous agent RollerAgent and brain RoolerBallBrain. Was Expecting 1 but received 8. MLAgents.Agent.SendInfoToBrain () (at Assets/ML-Agents/Scripts/Agent.cs:580) MLAgents.Agent.SendInfo () (at Assets/ML-Agents/Scripts/Agent.cs:984) MLAgents.Academy.EnvironmentStep () (at Assets/ML-Agents/Scripts/Academy.cs:583) MLAgents.Academy.FixedUpdate () (at Assets/ML-Agents/Scripts/Academy.cs:611) |
とりあえずエラーの翻訳
Vector Observation size mismatch between continuous agent RollerAgent and brain RoolerBallBrain. Was Expecting 1 but received 8.
連続エージェントRollerAgentとブレインRoolerBallBrainの間のベクトル観測サイズの不一致。 1を期待していたが8を受け取った。
つまり・・・?
とりあえず検索
参考 Vector Observation size mismatch between continuous agent RollerAgent and brain RoolerBallBrain. Was Expecting 1 but received 8.Google
DiscreteをContiniousに変えても変わらず・・・
そもそもDiscreteとContinousの違いってなんだっけ
参考 discrete continuous ml agentGoogle
Discreteの場合、配列には1つの要素しか含まれません。つまりact[0]に値が格納され、Action Sizeの内のいずれかの値が来ます。
Continuousは複数の要素を受け取れます。配列の長さはAction Sizeと同じで、複数の支持を同時に受け取れます。
参考 【Unity】Unityで機械学習する「ML-Agent」を色々と試して得た知見とか - テラシュールブログテラシュールブログ
なるほど?
もう一回コードを見直してみよう
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | using System.Collections.Generic; using UnityEngine; using MLAgents; public class RollerAgent : Agent { Rigidbody rBody; float previousDistance; void Start () { rBody = GetComponent(); previousDistance = 0; } public Transform Target; public override void AgentReset() { if (this.transform.position.y < 0) { // If the Agent fell, zero its momentum this.rBody.angularVelocity = Vector3.zero; this.rBody.velocity = Vector3.zero; this.transform.position = new Vector3( 0, 0.5f, 0); } // Move the target to a new spot Target.position = new Vector3(Random.value * 8 - 4, 0.5f, Random.value * 8 - 4); } public override void CollectObservations() { // 相対座標を計算(Calculate relative position) Vector3 relativePosition = Target.position - this.transform.position; // 相対座標を正規化して設定(Relative position) AddVectorObs(relativePosition.x/5); AddVectorObs(relativePosition.z/5); // 床の隅からの距離を正規化した値を設定(Distance to edges of platform) AddVectorObs((this.transform.position.x + 5)/5); AddVectorObs((this.transform.position.x - 5)/5); AddVectorObs((this.transform.position.z + 5)/5); AddVectorObs((this.transform.position.z - 5)/5); // エージェントの速度(Agent velocity) AddVectorObs(rBody.velocity.x/5); AddVectorObs(rBody.velocity.z/5); } public float speed = 10; public override void AgentAction(float[] vectorAction, string textAction) { // Targetとの距離 float distanceToTarget = Vector3.Distance( this.transform.position, Target.position); // Targetに接触したか if (distanceToTarget < 1.42f) { AddReward(1); Done(); } // 近づいたサブリワード if (distanceToTarget < previousDistance) { previousDistance = distanceToTarget; AddReward(0.03f); } // 近づかなかったペナルティ AddReward(-0.05f); // プラットフォームから転落 if (this.transform.position.y < -1.0) { AddReward(-1); Done(); } // 動作の設定。sizeが2なのでvectorActionは2つ Vector3 controlSignal = Vector3.zero; controlSignal.x = vectorAction[0]; controlSignal.z = vectorAction[1]; rBody.AddForce(controlSignal * speed); } } |
ActionSize辺りをいじればいい・・・?
sizeが2なのでvectorActionは2つって書いてある・・・ということは?
変わらん・・・でもキューブは移動したな・・・?初めて動いた気がする
エラーメッセージは変わってないな・・・
UnityAgentsException: Vector Observation size mismatch between continuous agent RollerAgent and brain RoolerBallBrain. Was Expecting 1 but received 8.
あ・・・Vector Observationって書いてある・・・
う・・・動いた!!!こいつ動くぞ!!!
でもすげー頭悪いな・・・50000回は練習したぞ
はあ・・・とりあえずStackedVectorsってなんだろ
参考 stacked vectors ml agentsGoogle
(speaking under correction) Let’s say your observation space is 3 big e.g. an x,y and z value. if you set stacked vectors to 2, it means that your observation space is now actually 6 vectors: the current x,y,z as well as the previous update step’s x,y,z. In this example, it might allow the agent to make inferences based on the velocity of what is being observed.
参考 What is the Brain stacked vectors value? · Issue #566 · Unity-Technologies/ml-agents · GitHubGitHub
(補正下で話す)あなたの観測空間は3であるとしましょう。 x、y、zの値積み上げベクトルを2に設定すると、観測空間は実際には6つのベクトルになります。つまり、現在のx、y、zと、前の更新ステップのx、y、zです。この例では、エージェントは観測されているものの速度に基づいて推論を行うことができます。
うーん・・・つまり・・・?よくわからん
StackedVectorsは2でいいのか?
・・・違うな・・・
設定書いてあるじゃん・・・
参考 UnityのML-Agentsで、新しい学習環境を作成する - tanaka's Programming Memotanaka's Programming Memo
うーん・・・でもうまく学習しないな・・・
5万回じゃ足りんかったか・・・
学習の設定をします。デフォルトの5万ステップだと、あまり学習の成果が見られません。3D Ballのサンプルは12個のエージェントを同時にトレーニングしていたので2万ステップ程度で効果が表れましたが、こちらは同時に1つのエージェントしかトレーニングしないので、その10倍の20万ステップは最低でも学習させたいところです。余裕をもって、50万ステップに設定します。 参考 UnityのML-Agentsで、新しい学習環境を作成する - tanaka's Programming Memotanaka's Programming Memo
- ML-Agentsのリポジトリーからダウンロードした
ml-agents-master
フォルダー内のconifg/trainer_config.yaml
を何らかのテキストエディターで開きます- 22行目の
curiosity_enc_size: 128
という行と、24行目のBananaBrain:
の間に、以下を追加します
- RollerBallBrain:
- normalize: true
- max_steps: 5.0e5
- 上書き保存します
これで、Unityの
RollerBallBrain
という名前のブレインをトレーニングする場合、以下が有効になります(このためにBrainの名前をRollerBallBrain
に変更しました)。参考 UnityのML-Agentsで、新しい学習環境を作成する - tanaka's Programming Memotanaka's Programming Memo
- 入力データを正規化(normalize)する
- ステップ数を50万回(
5.0e5
は、5x10^5
の意味)
ふむふむ・・・なるほど・・・あれ・・・50万回にならんぞ・・・?
インデントを揃えてなかったから・・・?いや違うな・・・
名前か・・・?ああ綴り間違いか・・・_| ̄|○
(俺もだいぶしつこくなったなあ・・・前はすぐにエラーばっかだと諦めてたのに)
(そういやhttpsじゃないリンクって貼っていいのかな・・・)
こいつ直線ばっか動くな・・・(10万回ぐらい)
ほんとにうまくいくんかな・・・変わってないぞ・・・(20万回ぐらい)
変わらん・・・(30万回ぐらい)
・・・
止まったし・・・直線ばっか動くAIになったぞ・・・
設定が悪いかもしれんけどもう一回やろう・・・
・・・1万回超えた辺りから直線しか進まなくなるなあ
近づかなかったらペナルティと近づいたリワードを2倍にしてみよう・・・
うーん・・・なんかすぐ死ぬなあ・・・死んだバツを2倍にしてみよう・・・
だめだ・・・すぐ自殺する・・・ひどくなった・・・
接触したリワードを2倍にするか・・・違うなあ・・・
転落したバツも2倍にするか・・・うまくいかんなあ・・・
(神様もこんなノリで世界作ってんのかな・・・)
コードが違ってた?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | using System.Collections.Generic; using UnityEngine; using MLAgents; public class RollerAgent : Agent { Rigidbody rBody; private void Start() { rBody = GetComponent(); } public Transform Target; public Transform Floor; private float previousDistance = float.MaxValue; public override void AgentReset() { // 前回の距離をリセット previousDistance = float.MaxValue; if (this.transform.position.y < -1.0) { // エージェントが落ちた this.transform.position = Floor.transform.position + Vector3.zero; this.rBody.angularVelocity = Vector3.zero; this.rBody.velocity = Vector3.zero; } else { // ターゲットを新しい場所へ移動させる Target.position = new Vector3( Floor.transform.position.x + Random.value*8-4, Floor.transform.position.y + 0.5f, Floor.transform.position.z +Random.value*8-4 ); } } public override void CollectObservations() { // 相対座標を計算(Calculate relative position) Vector3 relativePosition = Target.position - this.transform.position; // 相対座標を正規化して設定(Relative position) AddVectorObs(relativePosition.x/5); AddVectorObs(relativePosition.z/5); // 床の隅からの距離を正規化した値を設定(Distance to edges of platform) AddVectorObs((this.transform.position.x + 5)/5); AddVectorObs((this.transform.position.x - 5)/5); AddVectorObs((this.transform.position.z + 5)/5); AddVectorObs((this.transform.position.z - 5)/5); // エージェントの速度(Agent velocity) AddVectorObs(rBody.velocity.x/5); AddVectorObs(rBody.velocity.z/5); } public float speed = 10; public override void AgentAction(float[] vectorAction, string textAction) { // Targetとの距離 float distanceToTarget = Vector3.Distance( this.transform.position, Target.position); // Targetに接触したか if (distanceToTarget < 1.42f) { AddReward(1); Done(); } // 近づいたサブリワード if (distanceToTarget < previousDistance) { previousDistance = distanceToTarget; AddReward(0.1f); } // 近づかなかったペナルティ AddReward(-0.05f); // プラットフォームから転落 if (this.transform.position.y < -1.0) { AddReward(-1); Done(); } // 動作の設定。sizeが2なのでvectorActionは2つ Vector3 controlSignal = Vector3.zero; controlSignal.x = vectorAction[0]; controlSignal.z = vectorAction[1]; rBody.AddForce(controlSignal * speed); } } |
学習するエージェントも複製した。
割といい感じ。
できた!
よかったよかった
さて次は何をしようか・・・
コメントを残す