求一個張量的梯度_張量流中離散策略梯度的最小工作示例2 0

求一個張量的梯度

Training discrete actor networks with TensorFlow 2.0 is easy once you know how to do it, but also rather different from implementations in TensorFlow 1.0. As the 2.0 version was only released in September 2019, most examples that circulate on the web are still designed for TensorFlow 1.0. In a related article — in which we also discuss the mathematics in more detail — we already treated the continuous case. Here, we use a simple multi-armed bandit problem to show how we can implement and update an actor network the discrete setting [1].

一旦您知道如何使用TensorFlow 2.0訓練離散的actor網絡就很容易了,而且與TensorFlow 1.0的實現也有很大不同。 由于2.0版本僅在2019年9月發布,因此大多數在網絡上傳播的示例仍是針對TensorFlow 1.0設計的。 在相關文章中(我們還將更詳細地討論數學),我們已經處理了連續的情況。 在這里,我們使用一個簡單的多臂匪問題來說明如何實現離散設置[1]并更新演員網絡。

一點數學 (A bit of mathematics)

We use the classical policy gradient algorithm REINFORCE in which the actor is represented by a neural network known as the actor network. In the discrete case, the network output is simply the probability of selecting each of the actions. So, if the set of actions is defined by A and the action by a ∈ A, then the network output are the probabilities p(a), ?a ∈ A. The input layer contains the state s or a feature array ?(s), followed by one or more hidden layers that transform the input, with the output being the probabilities for each action that might be selected.

我們使用經典的策略梯度算法REINFORCE,其中角色由稱為角色網絡的神經網絡表示。 在離散情況下,網絡輸出僅僅是選擇每個動作的概率。 因此,如果一組動作由A定義,而動作由a∈A定義 ,則網絡輸出為概率p(a)?a∈A 。 輸入層包含狀態s或要素數組?(s) ,后跟一個或多個隱藏層來轉換輸入,輸出是每個可能選擇的動作的概率。

The policy π is parameterized by θ, which in deep reinforcement learning represents the neural network weights. After each action we take, we observe a reward v. Computing the gradients for θ and using learning rate α, the update rule typically encountered in textbooks looks as follows [2,3]:

策略π由θ參數化在深度強化學習中它表示神經網絡權重。 在我們執行每個動作之后,我們都會觀察到獎勵v 。 計算θ的梯度并使用學習率α ,教科書中通常會遇到的更新規則如下[2,3]:

Image for post

When applying backpropagation updates to neural networks we must slightly modify this update rule, but the procedure follows the same lines. Although we might update the network weights manually, we typically prefer to let TensorFlow (or whatever library you use) handle the update. We only need to provide a loss function; the computer handles the calculation of gradients and other fancy tricks such as customized learning rates. In fact, the sole thing we have to do is add a minus sign, as we perform gradient descent rather than ascent. Thus, the loss function — which is known as the log loss function or cross-entropy loss function[4] — looks like this:

將反向傳播更新應用于神經網絡時,我們必須稍微修改此更新規則,但是該過程遵循相同的原則。 盡管我們可能會手動更新網絡權重,但我們通常更喜歡讓TensorFlow(或您使用的任何庫)來處理更新。 我們只需要提供一個損失函數; 計算機可以處理梯度和其他花式技巧(例如自定義學習率)的計算。 實際上,我們唯一要做的就是添加一個減號,因為我們執行梯度下降而不是上升 。 因此,損失函數(稱為對數損失函數交叉熵損失函數 [4])如下所示:

Image for post

TensorFlow 2.0實施 (TensorFlow 2.0 implementation)

Now let’s move on to the actual implementation. If you have some experience with TensorFlow, you likely first compile your network withmodel.compileand then perform model.fitormodel.train_on_batchto fit the network to your data. As TensorFlow 2.0 requires a loss function to have exactly two arguments, (y_true and y_predicted) we cannot use these methods though, since we need the action, state and reward as input arguments. The GradientTapefunctionality — which did not exist in TensorFlow 1.0 [5] — conveniently solves this problem. After storing a forward pass through the actor network on a `tape' , it is able to perform automatic differentiation in a backward pass later on.

現在讓我們繼續實際的實現。 如果您有使用TensorFlow的經驗,則可能首先使用model.compile編譯網絡,然后執行model.fitmodel.train_on_batch使網絡適合您的數據。 由于TensorFlow 2.0需要一個損失函數來具有正好兩個參數( y_truey_predicted ),因此我們無法使用這些方法,因為我們需要將操作,狀態和獎勵作為輸入參數。 TensorFlow 1.0 [5]中不存在的GradientTape功能可以方便地解決此問題。 在通過actor網絡將前向通行證存儲在“ 磁帶 ”上之后,稍后可以在后向通行證中執行自動區分。

We start by defining our cross entropy loss function:

我們首先定義交叉熵損失函數:

In the next step, we use the function .trainable_variables to retrieve the network weights. Subsequently, tape.gradient calculates all the gradients for you by simply plugging in the loss value and the trainable variables. With optimizer.apply_gradients we update the network weights using a selected optimizer. As mentioned earlier, it is crucial that the forward pass (in which we obtain the action probabilities from the network) is included in the GradientTape. The code to update the weights is as follows:

在下一步中,我們使用函數.trainable_variables檢索網絡權重。 隨后, tape.gradient只需插入損失值和可訓練變量,即可為您計算所有梯度。 通過optimizer.apply_gradients我們使用選定的優化器來更新網絡權重。 如前所述,至關重要的是將正向傳遞(我們從網絡中獲得動作概率)包括在GradientTape中。 更新權重的代碼如下:

多臂匪 (Multi-armed bandit)

In the multi-armed bandit problem, we are able to play several slot machines with unique pay-off properties [6]. Each machine i has a mean payoff μ_i and a standard deviation σ_i, which are unknown to the player. At every decision moment you play one of the machines and observe the reward. After sufficient iterations and exploration, you should be able to fairly accurately estimate the mean reward of each machine. Naturally, the optimal policy is to always play the slot machine with the highest expected payoff.

在多武裝匪徒問題中,我們能夠玩幾臺具有獨特回報特性的老虎機[6]。 每臺機器i均具有玩家不知道的平均收益μ_i和標準偏差σ_i 。 在每個決策時刻,您都玩一臺機器并觀察獎勵。 經過足夠的迭代和探索,您應該能夠相當準確地估計每臺機器的平均回報。 自然,最佳策略是始終使用預期收益最高的老虎機。

Using Keras, we define a dense actor network. It takes a fixed state (a tensor with value 1) as input. We have two hidden layers that use five ReLUs per layer as activation functions. The network outputs the probabilities of playing each slot machine. The bias weights are initialized in such a way that each machine has equal probability at the beginning. Finally, the chosen optimizer is Adam with its default learning rate of 0.001.

使用Keras,我們定義了一個密集的actor網絡。 它采用固定狀態(值為1的張量)作為輸入。 我們有兩個隱藏層,每個層使用五個ReLU作為激活函數。 網絡輸出玩每個老虎機的概率。 偏置權重的初始化方式是,每臺機器在開始時都有相同的概率。 最后,選擇的優化器是Adam,默認學習率為0.001。

We test four settings with differing mean payoffs. For simplicity we set all standard deviations equal. The figures below show the learned probabilities for each slot machine, testing with four machines. As expected, the policy learns to play the machine(s) with the highest expected payoff. Some exploration naturally persists, especially when payoffs are close together. A bit of fine-tuning and you surely will do a lot better during your next Vegas trip.

我們測試了四種具有不同平均收益的設置。 為簡單起見,我們將所有標準偏差設置為相等。 下圖顯示了在四臺老虎機上進行測試后,每臺老虎機的學習概率。 正如預期的那樣,該策略將學習播放具有最高預期收益的機器。 自然會持續進行一些探索,尤其是當收益接近時。 進行一些微調,在您下一次維加斯之旅中,您肯定會做得更好。

Image for post

關鍵點 (Key points)

  • We define a pseudo-loss to update actor networks. For discrete control, the pseudo-loss function is simply the negative log probability multiplied with the reward signal, also known as the log loss- or cross-entropy loss function.

    我們定義了偽損失來更新參與者網絡。 對于離散控制,偽損失函數僅是負對數概率乘以獎勵信號,也稱為對數損失或交叉熵損失函數。
  • Common TensorFlow 2.0 functions only accept loss functions with exactly two arguments. The GradientTape does not have this restriction.

    常見的TensorFlow 2.0函數僅接受具有兩個參數的損失函數。 GradientTape沒有此限制。

  • Actor networks are updated using three steps: (i) define a custom loss function, (ii) compute the gradients for the trainable variables and (iii) apply the gradients to update the weights of the actor network.

    使用三個步驟來更新角色網絡:(i)定義自定義損失函數;(ii)計算可訓練變量的梯度;(iii)應用梯度來更新角色網絡的權重。

This article is partially based on my method paper: ‘Implementing Actor Networks for Discrete Control in TensorFlow 2.0’ [1]

本文部分基于我的方法論文:“在Actors Networks中實現TensorFlow 2.0中的離散控制” [1]

The GitHub code (implemented using Python 3.8 and TensorFlow 2.3) can be found at: www.github.com/woutervanheeswijk/example_discrete_control

GitHub代碼(使用Python 3.8和TensorFlow 2.3實現)可以在以下位置找到: www.github.com/woutervanheeswijk/example_discrete_control

翻譯自: https://towardsdatascience.com/a-minimal-working-example-for-discrete-policy-gradients-in-tensorflow-2-0-d6a0d6b1a6d7

求一個張量的梯度

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389961.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389961.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389961.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

docker環境 快速使用elasticsearch-head插件

docker環境 快速使用elasticsearch-head插件 #elasticsearch配置 #進入elk容器 docker exec -it elk /bin/bash #head插件訪問配置 echo #head插件訪問# http.cors.enabled: true http.cors.allow-origin: "*" >>/etc/elasticsearch/elasticsearch.yml#重啟el…

476. 數字的補數

476. 數字的補數 給你一個 正 整數 num ,輸出它的補數。補數是對該數的二進制表示取反。 例 1:輸入:num 5 輸出:2 解釋:5 的二進制表示為 101(沒有前導零位),其補數為 010。所以你…

zabbix網絡發現主機

1 功能介紹 默認情況下,當我在主機上安裝agent,然后要在server上手動添加主機并連接到模板,加入一個主機組。 如果有很多主機,并且經常變動,手動操作就很麻煩。 網絡發現就是主機上安裝了agent,然后server自…

python股市_如何使用python和破折號創建儀表板來主導股市

python股市始終關注大局 (Keep Your Eyes on the Big Picture) I’ve been fascinated with the stock market since I was a little kid. There is certainly no shortage of data to analyze, and if you find an edge you can make some easy money. To stay on top of the …

阿里巴巴開源 Sentinel,進一步完善 Dubbo 生態

為什么80%的碼農都做不了架構師?>>> 阿里巴巴開源 Sentinel,進一步完善 Dubbo 生態 Sentinel 開源地址:https://github.com/alibaba/Sentinel 轉載于:https://my.oschina.net/dyyweb/blog/1925839

數據結構與算法 —— 鏈表linked list(01)

鏈表(維基百科) 鏈表(Linked list)是一種常見的基礎數據結構,是一種線性表,但是并不會按線性的順序存儲數據,而是在每一個節點里存到下一個節點的指針(Pointer)。由于不必須按順序存儲,鏈表在插入的時候可以…

離群值如何處理_有理處理離群值的局限性

離群值如何處理ARIMA models can be quite adept when it comes to modelling the overall trend of a series along with seasonal patterns.ARIMA模型可以很好地建模一系列總體趨勢以及季節性模式。 In a previous article titled SARIMA: Forecasting Seasonal Data with P…

網絡爬蟲基礎練習

0.可以新建一個用于練習的html文件,在瀏覽器中打開。 1.利用requests.get(url)獲取網頁頁面的html文件 import requests newsurlhttp://news.gzcc.cn/html/xiaoyuanxinwen/ res requests.get(newsurl) #返回response對象 res.encodingutf-8 2.利用BeautifulSoup的H…

10生活便捷:購物、美食、看病時這樣搜,至少能省一半心

本次課程介紹實實在在能夠救命、省錢的網站,解決了眼前這些需求后,還有“詩和遠方”——不花錢也能點亮自己的生活,獲得美的享受! 1、健康醫療這么搜,安全又便捷 現在的醫療市場確實有些混亂,由于醫療的專業…

ppt圖表圖表類型起始_梅科圖表

ppt圖表圖表類型起始There are different types of variable width bar charts but two are the most popular: 1) Bar Mekko chart; 2) Marimekko chart.可變寬度條形圖有不同類型,但最受歡迎的有兩種:1)Mekko條形圖; 2)Marimekko圖表。 Th…

Tomcat日志亂碼了怎么處理?

【前言】 tomacat日志有三個地方,分別是Output(控制臺)、Tomcat Localhost Log(tomcat本地日志)、Tomcat Catalina Log。 啟動日志和大部分報錯日志、普通日志都在output打印;有些錯誤日志,在Tomcat Localhost Log。 三個日志顯示區,都可能…

python 編碼規范

縮進 用4個空格來縮進代碼 分號 不要在行尾加分號, 也不要用分號將兩條命令放在同一行。 行長度 每行不超過80個字符 以下情況除外: l 長的導入模塊語句 l 注釋里的URL 不要使用反斜杠連接行。 Python會將 圓括號, 中括號和花括號中的行隱式的連接起來 , 你可以利用…

5888. 網絡空閑的時刻

5888. 網絡空閑的時刻 給你一個有 n 個服務器的計算機網絡,服務器編號為 0 到 n - 1 。同時給你一個二維整數數組 edges ,其中 edges[i] [ui, vi] 表示服務器 ui 和 vi 之間有一條信息線路,在 一秒 內它們之間可以傳輸 任意 數目的信息。再…

django框架預備知識

內容: 1.web預備知識 2.django介紹 3.web框架的本質及分類 4.django安裝與基本設置 1.web預備知識 HTTP協議:https://www.cnblogs.com/wyb666/p/9383077.html 關于web的本質:http://www.cnblogs.com/wyb666/p/9034042.html 如何自定義web框架…

現實世界 機器學習_公司溝通分析簡介現實世界的機器學習方法

現實世界 機器學習In my previous posts I covered analytical subjects from a scientific point of view, rather than an applied real world problem. For this reason, this article aims at approaching an analytical idea from a managerial point of view, rather tha…

拷貝構造函數和賦值函數

1、拷貝構造函數:用一個已經有的對象構造一個新的對象。 CA(const CA & c )函數的名稱必須和類名稱相一致,它的唯一的一個參數是本類型的一個引用變量,該參數是const 類型,不可變。 拷貝構造函數什么時…

[bzoj3036]綠豆蛙的歸宿

題目大意:給定 $DAG$ 帶邊權連通圖,保證所有點都能到達終點 $n$,每個點等概率沿邊走,求起點 $1$ 到終點 $n$ 的期望長度。 題解:拓撲,然后倒著$DP$就可以了 卡點:無 C Code: #includ…

5902. 檢查句子中的數字是否遞增

5902. 檢查句子中的數字是否遞增 句子是由若干 token 組成的一個列表,token 間用 單個 空格分隔,句子沒有前導或尾隨空格。每個 token 要么是一個由數字 0-9 組成的不含前導零的 正整數 ,要么是一個由小寫英文字母組成的 單詞 。 示例&…

蒜頭君吃桃

蒜頭君買了一堆桃子不知道個數,第一天吃了一半的桃子,還不過癮,有多吃了一個。以后他每天吃剩下的桃子的一半還多一個,到 nn 天只剩下一個桃子了。蒜頭君想知道一開始買了多少桃子。 輸入格式 輸入一個整數 n(2≤n≤60)&#xff0…

Chrome keyboard shortcuts

2019獨角獸企業重金招聘Python工程師標準>>> Chrome keyboard shortcuts https://support.google.com/chrome/answer/157179?hlen 轉載于:https://my.oschina.net/qwfys200/blog/1927456