San*_*Ipk 1 machine-learning neural-network python-3.x deep-learning tensorflow
我对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)
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:
回溯(最近一次调用最后一次):文件"C:/ Users/Sandeep IPK/PycharmProjects/tests/samples2.py",第10行,在sess.run(grad)文件"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)文件"C:\ Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py",第408行,在init self._fetch_mapper = _FetchMapper.for_fetch(提取)文件"C:\ Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py",第230行,在for_fetch中返回_ListFetchMapper(fetch)文件"C:\ Users\Sandeep IPK\AppData\Local\Programs \在init中的 Python\Python35\lib\site-packages\tensorflow\python\client\session.py",第337行 self._mappers = [_FetchMapper.for_fetch(fetch)for fetches in 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)中获取提取内容]文件"C:\ Users\Sandeep IPK\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py",第227行,in_fetch(fetch,type(fetch)))TypeError:Fetch参数None无效类型
进程以退出代码1结束
TensorFlow使用基于链规则的反向累积来计算点处的梯度值.为了计算相对于变量的函数梯度,您必须同时定义它们.您还必须指定要计算渐变的值.在这个例子中,你计算的梯度y=x**2+x+1就x在2:
#!/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]
Run Code Online (Sandbox Code Playgroud)
如果变量是矩阵,也可以计算梯度.在这种情况下,梯度也是矩阵.这里我们使用一个简单的例子,当函数取决于所有矩阵元素的总和:
#!/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)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5262 次 |
| 最近记录: |