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

python人工智能自定義求導(dǎo)tf_diffs詳解

 更新時(shí)間:2022年07月29日 15:11:35   作者:plum_blossom  
這篇文章主要為大家介紹了python人工智能自定義求導(dǎo)tf_diffs詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

自定義求導(dǎo):(近似求導(dǎo)數(shù)的方法)

讓x向左移動(dòng)eps得到一個(gè)點(diǎn),向右移動(dòng)eps得到一個(gè)點(diǎn),這兩個(gè)點(diǎn)形成一條直線,這個(gè)點(diǎn)的斜率就是x這個(gè)位置的近似導(dǎo)數(shù)。

eps足夠小,導(dǎo)數(shù)就足夠真。

def f(x):
    return 3. * x ** 2 + 2. * x - 1
def approximate_derivative(f, x, eps=1e-3):
    return (f(x + eps) - f(x - eps)) / (2. * eps)
print(approximate_derivative(f, 1.))

運(yùn)行結(jié)果:

7.999999999999119

多元函數(shù)的求導(dǎo)

def g(x1, x2):
    return (x1 + 5) * (x2 ** 2)
def approximate_gradient(g, x1, x2, eps=1e-3):
    dg_x1 = approximate_derivative(lambda x: g(x, x2), x1, eps)
    dg_x2 = approximate_derivative(lambda x: g(x1, x), x2, eps)
    return dg_x1, dg_x2
print(approximate_gradient(g, 2., 3.))

運(yùn)行結(jié)果:

(8.999999999993236, 41.999999999994486)

在tensorflow中的求導(dǎo)

x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape() as tape:
    z = g(x1, x2)
dz_x1 = tape.gradient(z, x1)
print(dz_x1)

運(yùn)行結(jié)果:

tf.Tensor(9.0, shape=(), dtype=float32)

但是tf.GradientTape()只能使用一次,使用一次之后就會(huì)被消解

try:
    dz_x2 = tape.gradient(z, x2)
except RuntimeError as ex:
    print(ex)

運(yùn)行結(jié)果:

A non-persistent GradientTape can only be used to compute one set of gradients (or jacobians)

解決辦法:設(shè)置persistent = True,記住最后要把tape刪除掉

x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape(persistent = True) as tape:
    z = g(x1, x2)
dz_x1 = tape.gradient(z, x1)
dz_x2 = tape.gradient(z, x2)
print(dz_x1, dz_x2)
del tape

運(yùn)行結(jié)果:

tf.Tensor(9.0, shape=(), dtype=float32) tf.Tensor(42.0, shape=(), dtype=float32)

使用tf.GradientTape()

同時(shí)求x1,x2的偏導(dǎo)

x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape() as tape:
    z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)

運(yùn)行結(jié)果:

[<tf.Tensor: shape=(), dtype=float32, numpy=9.0>, <tf.Tensor: shape=(), dtype=float32, numpy=42.0>]

對(duì)常量求偏導(dǎo)

x1 = tf.constant(2.0)
x2 = tf.constant(3.0)
with tf.GradientTape() as tape:
    z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)

運(yùn)行結(jié)果:

[None, None]

可以使用watch函數(shù)關(guān)注常量上的導(dǎo)數(shù)

x1 = tf.constant(2.0)
x2 = tf.constant(3.0)
with tf.GradientTape() as tape:
    tape.watch(x1)
    tape.watch(x2)
    z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)

運(yùn)行結(jié)果:

[<tf.Tensor: shape=(), dtype=float32, numpy=9.0>, <tf.Tensor: shape=(), dtype=float32, numpy=42.0>]

也可以使用兩個(gè)目標(biāo)函數(shù)對(duì)一個(gè)變量求導(dǎo):

x = tf.Variable(5.0)
with tf.GradientTape() as tape:
    z1 = 3 * x
    z2 = x ** 2
tape.gradient([z1, z2], x)

運(yùn)行結(jié)果:

<tf.Tensor: shape=(), dtype=float32, numpy=13.0>

結(jié)果13是z1對(duì)x的導(dǎo)數(shù)加上z2對(duì)于x的導(dǎo)數(shù)

求二階導(dǎo)數(shù)的方法

x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape(persistent=True) as outer_tape:
    with tf.GradientTape(persistent=True) as inner_tape:
        z = g(x1, x2)
    inner_grads = inner_tape.gradient(z, [x1, x2])
outer_grads = [outer_tape.gradient(inner_grad, [x1, x2])
               for inner_grad in inner_grads]
print(outer_grads)
del inner_tape
del outer_tape

運(yùn)行結(jié)果:

[[None, <tf.Tensor: shape=(), dtype=float32, numpy=6.0>], [<tf.Tensor: shape=(), dtype=float32, numpy=6.0>, <tf.Tensor: shape=(), dtype=float32, numpy=14.0>]]

結(jié)果是一個(gè)2x2的矩陣,左上角是z對(duì)x1的二階導(dǎo)數(shù),右上角是z先對(duì)x1求導(dǎo),在對(duì)x2求導(dǎo)

左下角是z先對(duì)x2求導(dǎo),在對(duì)x1求導(dǎo),右下角是z對(duì)x2的二階導(dǎo)數(shù)

學(xué)會(huì)自定義求導(dǎo)就可以模擬梯度下降法了,梯度下降就是求導(dǎo),再在導(dǎo)數(shù)的位置前進(jìn)一點(diǎn)點(diǎn) 模擬梯度下降法:

learning_rate = 0.1
x = tf.Variable(0.0)
for _ in range(100):
    with tf.GradientTape() as tape:
        z = f(x)
    dz_dx = tape.gradient(z, x)
    x.assign_sub(learning_rate * dz_dx)
print(x)

運(yùn)行結(jié)果:

<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>

結(jié)合optimizers進(jìn)行梯度下降法

learning_rate = 0.1
x = tf.Variable(0.0)
optimizer = keras.optimizers.SGD(lr = learning_rate)
for _ in range(100):
    with tf.GradientTape() as tape:
        z = f(x)
    dz_dx = tape.gradient(z, x)
    optimizer.apply_gradients([(dz_dx, x)])
print(x)

運(yùn)行結(jié)果:

<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>

以上就是python人工智能自定義求導(dǎo)tf_diffs詳解的詳細(xì)內(nèi)容,更多關(guān)于python自定義求導(dǎo)tf_diffs的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • AI與Python人工智能遺傳算法

    AI與Python人工智能遺傳算法

    這篇文章主要為大家介紹了AI與Python人工智能遺傳算法的詳解教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解

    Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解

    這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • Django使用模板后無(wú)法找到靜態(tài)資源文件問題解決

    Django使用模板后無(wú)法找到靜態(tài)資源文件問題解決

    這篇文章主要介紹了Django使用模板后無(wú)法找到靜態(tài)資源文件問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • flask框架url與重定向操作實(shí)例詳解

    flask框架url與重定向操作實(shí)例詳解

    這篇文章主要介紹了flask框架url與重定向操作,結(jié)合實(shí)例形式詳細(xì)分析了flask框架URL映射、傳參、重定向等相關(guān)概念、原理與操作注意事項(xiàng),需要的朋友可以參考下
    2020-01-01
  • 使用Python的PIL如何將數(shù)組輸出圖像

    使用Python的PIL如何將數(shù)組輸出圖像

    這篇文章主要介紹了使用Python的PIL如何將數(shù)組輸出圖像問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python callable內(nèi)置函數(shù)原理解析

    Python callable內(nèi)置函數(shù)原理解析

    這篇文章主要介紹了Python callable內(nèi)置函數(shù)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Python concurrent.futures模塊使用實(shí)例

    Python concurrent.futures模塊使用實(shí)例

    這篇文章主要介紹了Python concurrent.futures模塊使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python集合add()函數(shù)使用詳解

    Python集合add()函數(shù)使用詳解

    python中add()方法用于給集合添加元素,如果添加的元素在集合中已存在,則不執(zhí)行任何操作,本文將給大家介紹一下Python集合add()函數(shù)使用方法,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2023-07-07
  • 簡(jiǎn)述:我為什么選擇Python而不是Matlab和R語(yǔ)言

    簡(jiǎn)述:我為什么選擇Python而不是Matlab和R語(yǔ)言

    這篇文章主要介紹了簡(jiǎn)述:我為什么選擇Python而不是Matlab和R語(yǔ)言,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • OpenCV-Python實(shí)現(xiàn)圖像平滑處理操作

    OpenCV-Python實(shí)現(xiàn)圖像平滑處理操作

    圖像平滑處理的噪聲取值主要有6種方法,本文主要介紹了這6種方法的具體使用并配置實(shí)例方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-06-06

最新評(píng)論

正镶白旗| 新沂市| 皋兰县| 阳信县| 上蔡县| 陇南市| 祁东县| 比如县| 黔西县| 比如县| 皮山县| 长春市| 疏勒县| 绥芬河市| 舞阳县| 石狮市| 房产| 漳平市| 西贡区| 济南市| 汽车| 海城市| 怀化市| 灵川县| 区。| 蒲城县| 敦煌市| 封开县| 房产| 交口县| 林西县| 平泉县| 隆尧县| 鄂尔多斯市| 九龙县| 隆子县| 沅陵县| 卢氏县| 靖远县| 广宗县| 射阳县|