ansh.gandhi
ansh.gandhi

Reputation: 82

Simple Tensorflow example not working in Jupyter Notebook

import tensorflow as tf

x = tf.constant(1.0)
w = tf.Variable(0.8)
y = w * x
y_ = tf.constant(0.0)
loss = (y - y_)**2
optim = tf.train.GradientDescentOptimizer(learning_rate=0.025)
grads_and_vars = optim.compute_gradients(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(grads_and_vars))

I am running this Tensorflow example in Jupyter Notebook in a cell. When I run the code the first time it works fine. But, when I re-run the cell, it gives the following error, I have tried a lot but can't seem to figure out the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-6883a1591d9c> in <module>()
     13 with tf.Session() as sess:
     14     sess.run(tf.global_variables_initializer())
---> 15     print(sess.run(grads_and_vars))

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    764     try:
    765       result = self._run(None, fetches, feed_dict, options_ptr,
--> 766                          run_metadata_ptr)
    767       if run_metadata:
    768         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    949 
    950     # Create a fetch handler to take care of the structure of fetches.
--> 951     fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
    952 
    953     # Run request and get response.

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds)
    405     """
    406     with graph.as_default():
--> 407       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    408     self._fetches = []
    409     self._targets = []

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.
--> 230       return _ListFetchMapper(fetch)
    231     elif isinstance(fetch, dict):
    232       return _DictFetchMapper(fetch)

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339 

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339 

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.
--> 230       return _ListFetchMapper(fetch)
    231     elif isinstance(fetch, dict):
    232       return _DictFetchMapper(fetch)

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in __init__(self, fetches)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339 

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in <listcomp>(.0)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339 

/Users/ansh/Installations/anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
    225     if fetch is None:
    226       raise TypeError('Fetch argument %r has invalid type %r' %
--> 227                       (fetch, type(fetch)))
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>

Upvotes: 2

Views: 300

Answers (1)

Xale
Xale

Reputation: 359

The result from an optimization is 'None' so you can't print that. If you want to print the loss after an optimization step you can do

loss,_ = sess.run([loss,grads_and_vars]);
print(loss)

Upvotes: 2

Related Questions