【问题标题】:How do tf.gradients() work?tf.gradients() 是如何工作的?
【发布时间】:2017-10-11 13:35:55
【问题描述】:

我是 tensorflow 的新手,我看过一些教程,但我不知道 tf.gradients() 是如何工作的。如果我给它两个二维矩阵的输入,它将如何计算偏导数?我真的很困惑,如果有人可以请帮助我,这将是一个很大的帮助。

import tensorflow as tf
import numpy as np

X = np.random.rand(3,3)
y = np.random.rand(2,2)

grad = tf.gradients(X,y)

with tf.Session() as sess:
    sess.run(grad)
    print(grad)

这给出了一个错误:

Traceback(最近一次调用最后一次): 文件“C:/Users/Sandeep IPK/PycharmProjects/tests/samples2.py”,第 10 行,在 sess.run(研究生) 运行中的文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 767 行 run_metadata_ptr) 文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 952 行,在 _run fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) init 中的文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 408 行 self._fetch_mapper = _FetchMapper.for_fetch(fetches) 文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 230 行,在 for_fetch 返回_ListFetchMapper(获取) init 中的文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 337 行 self._mappers = [_FetchMapper.for_fetch(fetch) for fetches] 文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 337 行,在 self._mappers = [_FetchMapper.for_fetch(fetch) for fetches] 文件“C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”,第 227 行,在 for_fetch (获取,类型(获取))) TypeError: Fetch 参数 None 的类型无效

进程以退出代码 1 结束

【问题讨论】:

  • 能否请您以等宽字体输入错误,仅用于略读目的

标签: python-3.x machine-learning tensorflow neural-network deep-learning


【解决方案1】:

TensorFlow 使用基于链式法则的reverse accumulation 来计算点处的梯度值。为了计算函数相对于变量的梯度,您必须同时定义两者。您还必须指定要计算梯度的值。在此示例中,您计算​​ y=x**2+x+1 相对于 x2 处的梯度:

#!/usr/bin/env python3
import tensorflow as tf

x = tf.Variable(2.0)
y = x**2 + x - 1

grad = tf.gradients(y, x)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    grad_value = sess.run(grad)
    print(grad_value)

# output: [5.0]

如果您的变量是矩阵,也可以计算梯度。在这种情况下,梯度也将是一个矩阵。这里我们使用一个简单的例子,当函数依赖于所有矩阵元素的总和时:

#!/usr/bin/env python3
import tensorflow as tf

X = tf.Variable(tf.random_normal([3, 3]))
X_sum = tf.reduce_sum(X)
y = X_sum**2 + X_sum - 1

grad = tf.gradients(y, X)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    grad_value = sess.run(grad)
    print(grad_value)

# output: [array([[ 9.6220665,  9.6220665,  9.6220665],
#   [ 9.6220665,  9.6220665,  9.6220665],
#   [ 9.6220665,  9.6220665,  9.6220665]], dtype=float32)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2023-01-13
    • 2019-08-18
    • 1970-01-01
    • 2017-07-24
    • 2016-11-13
    • 2021-10-13
    • 2011-02-24
    相关资源
    最近更新 更多