I have the following code to compute an example of gradients using 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()
But the line-
res_grad = sess.run(grad)
Gives the following error-
TypeError Traceback (most recent call last) in ----> 1 res = sess.run(grad)
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 948 try: 949 result = self._run(None, fetches, feed_dict, options_ptr, --> 950 run_metadata_ptr) 951 if run_metadata: 952 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1156 # Create a fetch handler to take care of the structure of fetches. 1157 fetch_handler = _FetchHandler( -> 1158 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) 1159 1160 # Run request and get response.~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in init(self, graph, fetches, feeds, feed_handles) 472 """ 473 with graph.as_default(): --> 474 self._fetch_mapper = _FetchMapper.for_fetch(fetches) 475 self._fetches = [] 476 self._targets = []
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 262 elif isinstance(fetch, (list, tuple)): 263 # NOTE(touts): This is also the code path for namedtuples. --> 264 return _ListFetchMapper(fetch) 265 elif isinstance(fetch, collections.Mapping): 266 return _DictFetchMapper(fetch)
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in init(self, fetches) 371 """ 372 self._fetch_type = type(fetches) --> 373 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 374 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 375
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in (.0) 371 """ 372 self._fetch_type = type(fetches) --> 373 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 374 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 375
~/.local/lib/python3.7/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 259 if fetch is None: 260 raise TypeError('Fetch argument %r has invalid type %r' % (fetch, --> 261 type(fetch))) 262 elif isinstance(fetch, (list, tuple)): 263 # NOTE(touts): This is also the code path for namedtuples.
TypeError: Fetch argument None has invalid type
What's going wrong?
Thanks!