tf.gradients()是如何工作的?

我对TensorFlow还比较陌生,虽然看了一些教程,但我不明白tf.gradients()是如何工作的。如果我输入两个二维矩阵,它会如何计算偏导数?我真的很困惑,如果有人能帮我解答,将会非常有帮助。

import tensorflow as tfimport numpy as npX = 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 (most recent call last): File “C:/Users/Sandeep IPK/PycharmProjects/tests/samples2.py”, line 10, in sess.run(grad) File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 767, in run run_metadata_ptr) File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 952, in _run fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 408, in init self._fetch_mapper = _FetchMapper.for_fetch(fetches) File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 230, in for_fetch return _ListFetchMapper(fetch) File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 337, in init self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 337, in self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] File “C:\Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 227, in for_fetch (fetch, type(fetch)))TypeError: Fetch argument None has invalid type

进程以退出码1结束


回答:

TensorFlow使用基于链式法则的反向累积来计算某点的梯度值。为了计算函数相对于某个变量的梯度,你必须定义这两个变量。同时,你还需要指定你想要计算梯度的值。在这个例子中,你计算y=x**2+x+1相对于x2处的梯度:

#!/usr/bin/env python3import tensorflow as tfx = tf.Variable(2.0)y = x**2 + x - 1grad = 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 python3import tensorflow as tfX = tf.Variable(tf.random_normal([3, 3]))X_sum = tf.reduce_sum(X)y = X_sum**2 + X_sum - 1grad = 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)]

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注