Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x TensorFlow梯度示例_Python 3.x_Tensorflow - Fatal编程技术网

Python 3.x TensorFlow梯度示例

Python 3.x TensorFlow梯度示例,python-3.x,tensorflow,Python 3.x,Tensorflow,我有下面的代码来计算使用TensorFlow的渐变示例- # z = f(x, y) = 2*x - y # Partial derivative of dz/dx = 2; dz/dy = -1 # Initialize x and y variables- x = tf.Variable([1], dtype = tf.int32) y = tf.Variable([2], dtype = tf.int32)

我有下面的代码来计算使用TensorFlow的渐变示例-

# z = f(x, y) = 2*x - y
# Partial derivative of dz/dx = 2; dz/dy = -1
# Initialize x and y variables-                                         
x = tf.Variable([1], dtype = tf.int32)
y = tf.Variable([2], dtype = tf.int32)
z = tf.subtract(2 * x, y)

# Define gradient operation-
grad = tf.gradients(z, [x, y])


# Initialize TensorFlow session-
sess = tf.Session()

# Initialize all variables-
sess.run(tf.global_variables_initializer())

# Compute gradient defined above-
res_grad = sess.run(grad)

# Close the session-
sess.close()
但是这条线-

res_grad = sess.run(grad)
给出以下错误-

TypeError回溯(最近的调用 最后)在 ---->1 res=sess.run(梯度)

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 运行中(自我、获取、馈送、选项、运行元数据) 948尝试: 949结果=self.\u运行(无、获取、馈送、选项、, -->950运行(元数据(ptr) 951如果运行\u元数据: 952 proto_data=tf_session.tf_GetBuffer(运行元数据\u ptr)

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 在运行中(self、handle、fetches、feed\u dict、options、run\u metadata)
1156#创建一个获取处理程序来处理 吸引。1157 fetch_handler=_FetchHandler( ->1158 self._graph,fetches,feed_dict_tensor,feed_handles=feed_handles)1159 1160#运行请求并获取 答复

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 在init(self、graph、fetches、feed、feed\u句柄) 472 """ 473与graph.as_default()一样: -->474 self.\u fetch\u mapper=\u FetchMapper.for\u fetch(fetches) 475 self._fetches=[] 476自我目标=[]

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 在for_fetch中(fetch) 262 elif isinstance(获取,(列表,元组)): 263注意(touts):这也是namedtuples的代码路径。 -->264返回\u ListFetchMapper(获取) 265 elif isinstance(获取、集合、映射): 266返回_DictFetchMapper(fetch)

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 在初始化中(自取) 371 """ 372自我获取类型=类型(获取) -->373 self.\u mappers=[\u FetchMapper.for\u fetch(fetch)for fetch in fetches] 374 self.\u unique\u fetches,self.\u value\u index=\u uniquify\u fetches(self.\u映射器) 375

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 英寸(.0) 371 """ 372自我获取类型=类型(获取) -->373 self.\u mappers=[\u FetchMapper.for\u fetch(fetch)for fetch in fetches] 374 self.\u unique\u fetches,self.\u value\u index=\u uniquify\u fetches(self.\u映射器) 375

~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py 在for_fetch中(fetch) 259如果fetch为None: 260 raise TypeError('提取参数%r具有无效的类型%r'(提取, -->261类型(提取))) 262 elif isinstance(获取,(列表,元组)): 263注意(touts):这也是namedtuples的代码路径

TypeError:获取参数None的类型无效

怎么了


谢谢!

您遇到此错误是因为您正在传递整数张量。请使用浮点,它应该会起作用

x = tf.Variable([1.0])
y = tf.Variable([2.0])
z = tf.subtract(2 * x, y)

# Define gradient operation-
grad = tf.gradients(z, [x, y])

# Initialize TensorFlow session-
sess = tf.Session()

# Initialize all variables-
sess.run(tf.global_variables_initializer())

# Compute gradient defined above-
res_grad = sess.run(grad)

print (res_grad) # Output = [array([ 2.], dtype=float32), array([-1.], dtype=float32)]

# Close the session-
sess.close()
这是在中更改的。允许在整数张量上使用梯度会导致tf.while_循环不正确,如果不进行此更改,则无法令人满意地解决这些问题