看到一篇跟之前想過的想法很像的論文,先記錄一下:
Category: AI/ML
深入淺出 End-to-End Learning on Robotics
前言
隨著機器人不斷地發展,機器人已經越來越聰明,可以漸漸學習執行更高複雜度的任務。在實作如此複雜的機器人時,參考人類的學習能力是很自然的一件事情。人類學習是一大黑盒子,從五官收到 sensory input,對應到所有肌肉的 control output。學習藉著 raw sensory input ,產生最後的 control output,就稱為 end-to-end learning。
相較來說,傳統方法常傾向把過程中的每一個功能都拆分成一個個 module,再將這些 module 串成一個 pipeline,無法評論哪個好,只是呈現給大家看。
接下來我們會帶大家一起來讀 End-to-End Training of Deep Visuomotor Policies,對這類方法有簡單的認識。雖然這篇論文提出的架構並沒有辦法讓機器人像人類般聰明地做事情(光是接收到的 sensor input 就還比人類少很多,起碼缺少了聽覺、觸覺、味覺、嗅覺),但是這篇論文仍然提出了一個很好的架構,有初步的廣泛應用能力(e.g. 學會怎麼掛上衣架後,就算衣架上有毛巾還是可以掛上)、並且可以處理用 hand crafted 程式很難做到的複雜任務(像是旋緊瓶蓋、把方塊放進大小接近的洞裡),相當值得一讀。
可以做到的成果
他們用來實驗的 task 包含了下圖中的幾種任務,我們可以先看看下面的影片
由此影片應該看得出這個架構有它厲害的地方,接著就讓我們來看看它是怎麼做到的!
黑盒子架構簡介
這篇論文的目標,是希望可以讓機器人學習從 raw image input 擷取出重要的資訊,來決定要怎麼產生 action。所以可以預期,我們應該要有一個黑盒子,接收 raw image input,直接產生機器人的 motor control output(就像我們控制各種肌肉一般),而這個黑盒子,就由 DNN 來實作。
先上個黑盒子的架構圖;
架構圖跟我們一開始認為的目標是相合的,最左邊的 input 接收的是 240 x 240 的 raw image input,經過了中間的 7 層 layer 之後,產生最後的 motor torque(就是馬達要做什麼 action)。前面的 4 層 layer 都是在處理影像、抽取特徵,後面的 3 個 layer 則是將特徵對應到 motor torque。
這個黑盒子就是這篇論文提到的 policy,policy 的定義如下圖,基本上就是會根據 observation o,產生 action u 的機率分布(採取各種不同 action 的機率):
如果您是對 DNN 有些了解的讀者,您一定會想問,這看起來不就只是個 supervised learning 嗎?好像也沒什麼特別的啊?
其實,只要牽扯到機器人,要收集海量的 training data 就是一件很困難的事情,不過答案沒有這麼簡單!要可以做到影片中的結果,就算你真的認真地收集了海量的 data,透過 supervised learning 來訓練上面這個 DNN 也還是會失敗。換句話說,這篇論文完全不是直接使用大量的 training data 來訓練上面這個黑盒子。
之所以不這樣訓練是因為,就算用海量的資料訓練出了一個 fitting error 很小的黑盒子,在實際執行的過程中,肯定還是會因為觀察到的 observation 跟 training data 不同、採取的 action 有誤差等等,逐漸產生在 training 過程中沒看過的 state,最後就很容易導致在 testing 過程中發生各種錯誤。(我們把這個問題簡稱為 training data 不是從 policy 來的問題)舉例來說,如果我們要訓練一個自動駕駛的系統,我們會提供很多人類開車的 training data,但是人類開車通常都是處在很正常的狀態,轉彎的時候也通常會離牆壁很遠,但如果自動駕駛系統在遇到彎道時,有一些控制誤差,使車子很靠近牆壁,這種狀況就是 training data 中不太會有的 state,這時候就有點恐怖了,因為你沒 train 過它這種狀況,你就不知道它會採取什麼行為。
要避免這個問題,就是要在 training 時,讓上面的黑盒子自己產生 training data,這樣在訓練時,就真的是看到自己採取的各個 action 是好還是不好。這就很像是我們在學習的過程中,自己動手下去做,才會真正知道自己有什麼問題。譬如我們可以看很多諸葛亮的故事,覺得他好聰明,但是知道他在那些狀況下會採取的行動,對我們的幫助通常不大,基本上就只是在看故事而已,因為我們不太可能碰到司馬懿帶兵來打、自己兵又很少,可以使用空城計的狀況,重點應該是我們在日常生活中碰到狀況,要能用諸葛亮的智慧來採取行動。
這就是避免 training data 不是從 policy 來的問題 的核心精神,必須用我們自己的 policy 去採取行動,並透過這些行動造成的結果好壞,來改進我們的 policy,這個概念可以簡化如下圖:
既然講到這邊,就讓我延伸一下,做完行動,該怎麼評判自己做得好不好(estimate return)就是眼界問題了。同樣做一件事,不同格局的人所下的評價可能天差地別,例如有一個學生 A 想要全力發展自己的強項,有的老師可能覺得將一項專長磨練到頂尖很好、有的老師可能認為應該先補足弱項才好。假設 A 只有兩種 action (發展強項 or 發展弱項)可以採取,當他的 observation 是看到自己的強項有好的表現 & 有些弱項表現得不好,他得把發展強項帶來的好處的 return 估計得更高,把弱項減損的 return 下修,他才會去發展強項。若是走發展強項之路,他最後很可能會成為某領域的頂尖專家,而非各項都平平的人。這也是為什麼有好的老師(or mentor)很重要,因為他們可以用更高的眼界來 estimate return(用 Reinforcement Learning 的名詞來說,就是他們的 reward function 更好)。好了,題外話講完,讓我們繼續往下看。
Training 方法簡介
看到這邊大家應該很好奇,上面的概念講起來很簡單,在這篇論文是怎麼實際 train 那個 7 層的黑盒子呢?
讓我們先看這篇論文的 training 方法概念圖:
核心的概念是這樣,首先我們先有一個 RL 的演算法,幫助我們產生一個可以拿來當作指導者的 guiding distribution – Pi(就是上圖最左下角的 optimize local controller Pi),這個 guiding distribution 就可以產生一系列的 action 與 state pair(也就是上圖中的 collect samples from Pi),然後,我們再將這一些由指導者產生的 sample,拿來訓練我們的 policy(上圖右下角的 train global policy to match local controller,也就是單純的 supervised learning),使 policy 無限逼近指導者。
原本要直接訓練上面的黑盒子很困難,因為要產生大量的 training data 非常困難,但是因為有了 Guided Policy Search(GPS)的架構,我們就可以同時滿足:
- Training data 是來自於 policy 本身(雖然實際上是來自於指導者的 guiding distribution,但是經過 GPS 訓練,policy 跟指導者會有相同的行為,所以沒問題)
- 有了可以拿來產生 supervised sample 的指導者,解決不知道哪來 data 可以 train 的問題
我們可以看一下這一小段論文加強理解:
一開始的 avoid this issue 指的是要避免 training data 不是從 policy 來的問題,所以他們交互使用 RL 跟 supervised learning,加上又已經有理論(Wang and Banerjee, 2014)證實這種訓練方法可以讓 policy 跟指導者的 guiding distribution 有相同行為,所以訓練可行。
接下來您可能會想問,我們憑什麼相信指導者的 guiding distribution 可以產生 supervised learning 的 training data?
Guided Policy Search 方法簡介
上一段最後的問題已經逼近核心了,最後就讓我們再回答這個問題。我們之所以可以相信指導者的 guiding distribution,是因為 guiding distribution 是透過 system dynamics 來優化的。而 system dynamics 就是真實世界的運行法則(接下來,guiding distribution == controller,為了符合論文的語言,方便待會閱讀)。
system dynamics 可以告訴我們, given 某個 state 跟 action,下一個 state 的機率分布會是什麼。換句話說,state dynamics 跟你說若你種了這些因,你會得到什麼果。而 controller 是為了達到某個 goal state,而採取 action,也就是可以告訴你為了達到某個果,你要種什麼因。所以 state dynamics 可以用來修正 controller 對因果關係的了解。
舉個生活化的例子,假設今天是星期天,你想吃飽,你就走去附近的小吃店 A 買食物,但是你走到那邊發現,那間小吃店 A 星期天公休。這個過程就是:
- 一開始,你處在星期天的飢餓 state,所以你要採取 action
- 你的 controller 跟你說,星期天飢餓時,走去附近某小吃店 A 就對啦!讚讚
- 走到某小吃店 A,才由 system dynamics 無情地跟你說,你在星期天飢餓時,走去小吃店,你會看到的下一個 state 是,小吃店 A 沒開
- 於是你就可以修正自己的 controller,知道下次星期天飢餓時,不要走去小吃店 A 了
也就是說,只要您實際嘗試,您一定可以透過這個世界的真實狀態(system dynamics),修正自己的認知(controller),不再天真地認為走去小吃店 A 就有東西吃。
這就是為什麼我們可以相信 guiding distribution 的直觀原因。上述概念完全可以在下圖中重新印證,您可以看看下圖中的論文原文再強化一下觀念。
詳細為何可以 optimize linear-Gaussian controllers 就不提了,有點太過理論,若有興趣可以自行去看論文中 Section 4.2 的數學說明!
總結
今天我們跟大家簡單介紹了 End-to-End learning 的概念,並用一篇論文來詳細地介紹這種概念怎麼在機器人上運用(利用 guided policy search)。
此外,在過程中我順便 demo 了一下可以比較快速地了解一篇論文的閱讀法(先看動機 -> 看結果可以做到什麼 -> 問他們憑什麼可以做到 -> 直接找到關鍵答案,所以像 related work 就會在此閱讀方法中被忽略),希望對各位有幫助!
延伸閱讀
如果您對機器人或是 ROS 相關文章有興趣,歡迎到 我的首頁 看更多文章!
Basic categorization of RL
We can gain cool insights by knowing types of RL algorithm:
Policy gradient: Learn by directly adjusting policy to maximize the reward. E.g. Shoot the basketball. If you did not make it (low reward), then you’ll adjust how you shoot the ball.
Value-based: Learn by core values. E.g. we know it is good to be optimistic. So we can still trust ourselves in difficult situations because we have this value function to help us know what to do.
Actor-Critic: Learn by having teachers to teach us how to do well. E.g. when we grow up, our parents teach us to do some things (e.g. to treat others well) and not to do some things (e.g. to bully others). We are actors. Parents are our critics. When being criticized, we might adjust our policy/value function/model.
Model-based: Learn the model of our world. E.g. We know if we release our cup in the air, it will fall to the ground due to gravity on Earth.
It is not difficult to know we actually use all these methods to learn.
The pictures above come from Deep RL course in UCB.
Why RL can learn from discrete reward?
One of the reasons is the expectation is reward (r) x probability (p) . Because p is continuous, it can smooth r.
For example, r = +1/-1 in the driving scheme below, but p might be 0.899 (if you drive 1000 times, you will fall 101 times). As a result, you get a smoothed expectation.
The picture above comes from Deep RL course taught by Sergey Levine.
Dive into TensorFlow (3) – Init the variable
The tf.Variable
class creates a tensor with an initial value that can be modified, much like a normal Python variable. This tensor stores its state in the session, so you must initialize the state of the tensor manually.
We use the tf.global_variables_initializer()
function to initialize the state of all the Variable tensors.
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
The tf.global_variables_initializer()
call returns an operation that will initialize all TensorFlow variables from the graph. You call the operation using a session to initialize all the variables as shown above.
Dive into TensorFlow (2) – Feeding dataset to session
x = tf.placeholder(tf.string)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Hello World'})
The feed_dict
parameter in tf.session.run()
help us set the placeholder tensor. The above example shows the tensor x
being set to the string "Hello, world"
.
It’s also possible to set more than one tensor using feed_dict
as shown below.
x = tf.placeholder(tf.string)
y = tf.placeholder(tf.int32)
z = tf.placeholder(tf.float32)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'Test String', y: 123, z: 45.67
Deep RL Assignment 1: Imitation Learning
To do this hw, I created a conda environment. Let me record the commands:
source activate rlcourse # Section 2 python3.4 run_expert.py experts/Hopper-v1.pkl Hopper-v1 --render
How to install and setup MuJoCo 1.31?
OK, first you want to make sure you have this kind of setting by following older installation guide in MuJoCo repo:
Next, you can run the test by going to the bin
and run./test ../model/humanoid.xml xs 10
:
ros@ros-K401UB:~/.mujoco/mjpro131/bin$ ./test ../model/humanoid.xml xs 10 Comparison of original and saved model Max difference : 2.61e-06 Field name : body_mass Simulation .......... Simulation time : 0.64 s Realtime factor : 15.59 x Time per step : 0.128 ms Contacts per step : 9 Constraints per step : 39 Degrees of freedom : 27
In my case, I have to copy my mjkey.txt to the ~/.mujoco/mjpro131/bin
in order to run successfully:
Dive into TensorFlow (1) – Tensor and Session
Tensor
In TensorFlow, data isn’t stored as integers, floats, or strings. These values are encapsulated in an object called a tensor. Tensors come in a variety of sizes as shown below:
# A is a 0-dimensional int32 tensor
A = tf.constant(1234)
# B is a 1-dimensional int32 tensor
B = tf.constant([123,456,789])
# C is a 2-dimensional int32 tensor
C = tf.constant([ [123,456,789], [222,333,444] ])
tf.constant()
is one of many TensorFlow operations. The tensor returned by tf.constant()
is called a constant tensor, because the value of the tensor never changes.
Session
TensorFlow’s API is built around the idea of a computational graph, a way of visualizing a mathematical process. A “TensorFlow Session” is an environment for running a graph. The session is in charge of allocating the operations to GPU(s) and/or CPU(s), including remote machines.
Hello World
import tensorflow as tf
# Create TensorFlow object called hello_constant
hello_constant = tf.constant('Hello World!')
with tf.Session() as sess:
# Run the tf.constant operation in the session
output = sess.run(hello_constant)
print(output)
The code first creates the tensor, hello_constant
, from the previous lines. The next step is to evaluate the tensor in a session.
The code creates a session instance, sess
, using tf.Session
. The sess.run()
function then evaluates the tensor and returns the results.
What do policy and policy search mean?
Policy – The agent’s action selection is modeled as a map called “policy”. The policy gives the probability of taking action a when in state s.
Policy Search – A method to learn by searching directly in (some subset of) the policy space.
Some methods of policy search are presented in the picture below, and the material is from policy search tutorial in ICML 2015.