【tensorflow2.0】自动微分机制

前端之家收集整理的这篇文章主要介绍了【tensorflow2.0】自动微分机制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

神经网络通常依赖反向传播求梯度来更新网络参数,求梯度过程通常是一件非常复杂而容易出错的事情。

而深度学习框架可以帮助我们自动地完成这种求梯度运算。

Tensorflow一般使用梯度磁带tf.GradientTape来记录正向运算过程,然后反播磁带自动得到梯度值。

这种利用tf.GradientTape求微分的方法叫做Tensorflow的自动微分机制。

一,利用梯度磁带求导数

  1. import tensorflow as tf
  2. numpy as np
  3. # f(x) = a*x**2 + b*x + c的导数
  4. x = tf.Variable(0.0,name = "x",dtype = tf.float32)
  5. a = tf.constant(1.0)
  6. b = tf.constant(-2.0)
  7. c = tf.constant(1.0)
  8. with tf.GradientTape() as tape:
  9. y = a*tf.pow(x,2) + b*x + c
  10. dy_dx = tape.gradient(y,x)
  11. print(dy_dx)

tf.Tensor(-2.0,shape=(),dtype=float32)

  1. 对常量张量也可以求导,需要增加watch
  2. with tf.GradientTape() as tape:
  3. tape.watch([a,b,c])
  4. y = a*tf.pow(x,1)"> c
  5. dy_dx,dy_da,dy_db,dy_dc =print(dy_da)
  6. print(dy_dc)

tf.Tensor(0.0, shape=(), dtype=float32)
tf.Tensor(1.0, dtype=float32)

  1. 可以求二阶导数
  2. with tf.GradientTape() as tape2:
  3. with tf.GradientTape() as tape1:
  4. y = a*tf.pow(x,1)"> c
  5. dy_dx = tape1.gradient(y,x)
  6. dy2_dx2 = tape2.gradient(dy_dx,x)
  1. tf.Tensor(2.0, dtype=float32)
  1. 可以在autograph中使用
  2. @tf.function
  3. def f(x):
  4. a = tf.constant(1.0)
  5. b = tf.constant(-2.0)
  6. c = tf.constant(1.0)
  7. 自变量转换成tf.float32
  8. x = tf.cast(x,tf.float32)
  9. with tf.GradientTape() as tape:
  10. tape.watch(x)
  11. y = a*tf.pow(x,2)+b*x+c
  12. dy_dx =return((dy_dx,y))
  13. tf.print(f(tf.constant(0.0)))
  14. tf.print(f(tf.constant(1.0)))
  1. (-2, 1)
    (0, 0)

二,利用梯度磁带和优化器求最小值

  1. f(x) = a*x**2 + b*x + c的最小值
  2. # 使用optimizer.apply_gradients
  3. )
  4. optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
  5. for _ in range(1000):
  6. with tf.GradientTape() as tape:
  7. y = a*tf.pow(x,x)
  8. optimizer.apply_gradients(grads_and_vars=[(dy_dx,x)])
  9. tf.print(y =; x =
  10.  使用optimizer.minimize optimizer.minimize相当于先用tapegradient,再apply_gradient
  11.  tf.float32)
  12.  
  13.  注意f()无参数
  14.  f():   
  15.     a = tf.constant(1.0)
  16.     y = a*tf.pow(x,1)">c
  17.     (y)
  18.  
  19. optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)   
  20. ):
  21.     optimizer.minimize(f,[x])   
  22.  
  23. tf.y = 0 ; x = 0.999998569
  24.  在autograph中完成最小值求解 tf.float32)
  25. optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
  26.  
  27. @tf.function
  28.  minimizef():
  29.     a = tf.constant(1.0in tf.range(1000): 注意autograph时使用tf.range(1000)而不是range(1000)
  30.         with tf.GradientTape() as tape:
  31.             y = a*tf.pow(x,1)"> c
  32.         dy_dx == a*tf.pow(x,1)"> c
  33.      y
  34.  
  35. tf.(minimizef())
  36. tf.print(x)
  37. 0
    0.999998569
  38.  使用optimizer.minimize
  39. )   
  40.  
  41. @tf.function
  42. (y)
  43.  
  44. @tf.function
  45.  train(epoch):  
  46.     in tf.range(epoch):  
  47.         optimizer.minimize(f,[x])
  48.     (f())
  49.  
  50.  
  51. tf.print(train(1000))
  52. tf.print(x)
  53. 0
    0.999998569
  54.  

  55. 参考:

  56. 开源电子书地址:https://lyhue1991.github.io/eat_tensorflow2_in_30_days/

  57. GitHub 项目地址:https://github.com/lyhue1991/eat_tensorflow2_in_30_days

  58.  

猜你在找的Tensorflow相关文章