📜  Python – tensorflow.gradients()

📅  最后修改于: 2022-05-13 01:55:26.011000             🧑  作者: Mango

Python – tensorflow.gradients()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

gradients()用于获取 xs 中 ys wrt x 之和的符号导数。启用急切执行时它不起作用。

示例 1:

Python3
# Importing the library
import tensorflow as tf
 
# Defining function
@tf.function
def gfg():
  a = tf.ones([1, 2])
  b = 5*a
 
  # Calculating gradient
  g1 = tf.gradients([b+a], [a])
 
  # Printing result
  print("res: ",g1)
 
# Calling the  function
gfg()


Python3
# Importing the library
import tensorflow as tf
 
# Defining function
@tf.function
def gfg():
  a = tf.ones([1, 2])
  b = 5*a
 
  # Calculating gradient
  g1 = tf.gradients([b], [a])
 
  # Printing result
  print("res: ",g1)
 
# Calling the  function
gfg()


输出:

res:  []

示例 2:

Python3

# Importing the library
import tensorflow as tf
 
# Defining function
@tf.function
def gfg():
  a = tf.ones([1, 2])
  b = 5*a
 
  # Calculating gradient
  g1 = tf.gradients([b], [a])
 
  # Printing result
  print("res: ",g1)
 
# Calling the  function
gfg()

输出:

res:  []