最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow中k.gradients()和tf.stop_gradient()用法說明

 更新時間:2020年06月10日 09:47:25   作者:書上猴爵  
這篇文章主要介紹了Tensorflow中k.gradients()和tf.stop_gradient()用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

上周在實驗室開荒某個代碼,看到中間這么一段,對Tensorflow中的stop_gradient()還不熟悉,特此周末進行重新并總結。

y = xx + K.stop_gradient(rounded - xx)

這代碼最終調(diào)用位置在tensoflow.python.ops.gen_array_ops.stop_gradient(input, name=None),關于這段代碼為什么這樣寫的意義在文末給出。

【stop_gradient()意義】

用stop_gradient生成損失函數(shù)w.r.t.的梯度。

【tf.gradients()理解】

tf中我們只需要設計我們自己的函數(shù),tf提供提供強大的自動計算函數(shù)梯度方法,tf.gradients()。

tf.gradients(
 ys,
 xs,
 grad_ys=None,
 name='gradients',
 colocate_gradients_with_ops=False,
 gate_gradients=False,
 aggregation_method=None,
 stop_gradients=None,
 unconnected_gradients=tf.UnconnectedGradients.NONE
)

gradients() adds ops to the graph to output the derivatives of ys with respect to xs. It returns a list of Tensor of length len(xs) where each tensor is the sum(dy/dx) for y in ys.

1、tf.gradients()實現(xiàn)ys對xs的求導

2、ys和xs可以是Tensor或者list包含的Tensor

3、求導返回值是一個list,list的長度等于len(xs)

eg.假設返回值是[grad1, grad2, grad3],ys=[y1, y2],xs=[x1, x2, x3]。則計算過程為:

import numpy as np
import tensorflow as tf
 
#構造數(shù)據(jù)集
x_pure = np.random.randint(-10, 100, 32)
x_train = x_pure + np.random.randn(32) / 32
y_train = 3 * x_pure + 2 + np.random.randn(32) / 32
 
x_input = tf.placeholder(tf.float32, name='x_input')
y_input = tf.placeholder(tf.float32, name='y_input')
w = tf.Variable(2.0, name='weight')
b = tf.Variable(1.0, name='biases')
y = tf.add(tf.multiply(x_input, w), b)
 
loss_op = tf.reduce_sum(tf.pow(y_input - y, 2)) / (2 * 32)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss_op)
gradients_node = tf.gradients(loss_op, w)
 
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
 
for i in range(20):
 _, gradients, loss = sess.run([train_op, gradients_node, loss_op], feed_dict={x_input: x_train[i], y_input: y_train[i]})
 print("epoch: {} \t loss: {} \t gradients: {}".format(i, loss, gradients))
sess.close()

自定義梯度和更新函數(shù)

import numpy as np
import tensorflow as tf
 
#構造數(shù)據(jù)集
x_pure = np.random.randint(-10, 100, 32)
x_train = x_pure + np.random.randn(32) / 32
y_train = 3 * x_pure + 2 + np.random.randn(32) / 32
 
x_input = tf.placeholder(tf.float32, name='x_input')
y_input = tf.placeholder(tf.float32, name='y_input')
w = tf.Variable(2.0, name='weight')
b = tf.Variable(1.0, name='biases')
y = tf.add(tf.multiply(x_input, w), b)
 
loss_op = tf.reduce_sum(tf.pow(y_input - y, 2)) / (2 * 32)
# train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss_op)
 
#自定義權重更新
grad_w, grad_b = tf.gradients(loss_op, [w, b])
new_w = w.assign(w - 0.01 * grad_w)
new_b = b.assign(b - 0.01 * grad_b)
 
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
 
for i in range(20):
 _, gradients, loss = sess.run([new_w, new_b, loss_op], feed_dict={x_input: x_train[i], y_input: y_train[i]})
 print("epoch: {} \t loss: {} \t gradients: {}".format(i, loss, gradients))
sess.close()

【tf.stop_gradient()理解】

在tf.gradients()參數(shù)中存在stop_gradients,這是一個List,list中的元素是tensorflow graph中的op,一旦進入這個list,將不會被計算梯度,更重要的是,在該op之后的BP計算都不會運行。

import numpy as np
import tensorflow as tf
 
a = tf.constant(0.)
b = 2 * a
c = a + b
g = tf.gradients(c, [a, b])
 
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print(sess.run(g))
 
#輸出[3.0, 1.0]

在用一個stop_gradient()的例子

import tensorflow as tf
 
#實驗一
w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)
a = tf.multiply(w1, 3.0)
a_stoped = tf.stop_gradient(a)
 
# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)
gradients = tf.gradients(b, xs=[w1, w2])
print(gradients)
#輸出[None, <tf.Tensor 'gradients/Mul_1_grad/Reshape_1:0' shape=() dtype=float32>]
 
#實驗二
a = tf.Variable(1.0)
b = tf.Variable(1.0)
c = tf.add(a, b)
c_stoped = tf.stop_gradient(c)
d = tf.add(a, b)
e = tf.add(c_stoped, d)
gradients = tf.gradients(e, xs=[a, b])
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print(sess.run(gradients))
 
#因為梯度從另外地方傳回,所以輸出 [1.0, 1.0]

【答案】

開始提出的問題,為什么存在那段代碼:

t = g(x)

y = t + tf.stop_gradient(f(x) - t)

這里,我們本來的前向傳遞函數(shù)是XX,但是想要在反向時傳遞的函數(shù)是g(x),因為在前向過程中,tf.stop_gradient()不起作用,因此+t和-t抵消掉了,只剩下f(x)前向傳遞;而在反向過程中,因為tf.stop_gradient()的作用,使得f(x)-t的梯度變?yōu)榱?,從而只剩下g(x)在反向傳遞。

以上這篇Tensorflow中k.gradients()和tf.stop_gradient()用法說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Linux上Miniconda的安裝的實現(xiàn)步驟

    Linux上Miniconda的安裝的實現(xiàn)步驟

    Miniconda是一個輕量級、免費且開源的跨平臺軟件包管理系統(tǒng),本文主要介紹了Linux上Miniconda的安裝的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • 如何通過python的fabric包完成代碼上傳部署

    如何通過python的fabric包完成代碼上傳部署

    這篇文章主要介紹了如何通過python的fabric包完成代碼上傳部署,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Jupyter notebook中如何添加Pytorch運行環(huán)境

    Jupyter notebook中如何添加Pytorch運行環(huán)境

    這篇文章主要介紹了Jupyter notebook中如何添加Pytorch運行環(huán)境,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • pandas中read_csv、rolling、expanding用法詳解

    pandas中read_csv、rolling、expanding用法詳解

    這篇文章主要介紹了pandas中read_csv、rolling、expanding用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python最基本的數(shù)據(jù)類型以及對元組的介紹

    Python最基本的數(shù)據(jù)類型以及對元組的介紹

    這篇文章主要介紹了Python最基本的數(shù)據(jù)類型以及對元組的介紹,來自于IBM官方網(wǎng)站技術文檔,需要的朋友可以參考下
    2015-04-04
  • Python Requests庫及用法詳解

    Python Requests庫及用法詳解

    Requests庫作為Python中最受歡迎的HTTP庫之一,為開發(fā)人員提供了簡單而強大的方式來發(fā)送HTTP請求和處理響應,本文將帶領您深入探索Python Requests庫的世界,我們將從基礎知識開始,逐步深入,覆蓋各種高級用法和技巧,感興趣的朋友一起看看吧
    2024-06-06
  • Python3中對json格式數(shù)據(jù)的分析處理

    Python3中對json格式數(shù)據(jù)的分析處理

    這篇文章主要介紹了Python3中對json格式數(shù)據(jù)的分析處理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 基于PyQt5實現(xiàn)的Windows定時關機工具

    基于PyQt5實現(xiàn)的Windows定時關機工具

    在日常使用電腦的過程中,我們經(jīng)常會遇到需要定時關機的場景,雖然 Windows 自帶 shutdown 命令可以定時關機,但操作方式較為繁瑣,缺乏可視化界面,因此,本篇文章將帶大家實現(xiàn)一個基于 PyQt5 的 Windows 定時關機工具,需要的朋友可以參考下
    2025-04-04
  • Tesserocr庫的正確安裝方式

    Tesserocr庫的正確安裝方式

    今天小編就為大家分享一篇關于Tesserocr庫的正確安裝方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 使用python進行量化交易的完整指南

    使用python進行量化交易的完整指南

    量化交易,作為現(xiàn)代金融市場中的一種先進交易方式,通過運用數(shù)學模型、統(tǒng)計方法和計算機算法來指導交易決策,旨在提高交易效率和決策的準確性,本文將詳細介紹如何使用Python進行量化交易,包括策略開發(fā)、數(shù)據(jù)處理、回測、風險管理和實盤交易等關鍵步驟
    2024-09-09

最新評論

屏东市| 新绛县| 鹰潭市| 兴海县| 景宁| 垫江县| 红原县| 庄浪县| 和龙市| 西林县| 炎陵县| 同德县| 新宁县| 磐石市| 甘德县| 四川省| 通江县| 孟津县| 新津县| 泸水县| 信丰县| 灵石县| 吉木乃县| 贵南县| 札达县| 淮阳县| 湖北省| 张家口市| 高淳县| 林周县| 神池县| 綦江县| 榆社县| 周宁县| 旬阳县| 淳安县| 石屏县| 元江| 秦安县| 长宁县| 裕民县|