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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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