2017-02-01 15 views
2
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)) 

このTensorflowの例はセル内のJupyter Notebookで実行しています。 初めてコードを実行したときに正常に動作します。しかし、私は再実行セル、それは次のエラーを与えるとき、私は多くのことを試してみましたが、エラーを見つけ出すように見えることはできません。Jupiter Notebookで動作しない単純なTensorflowの例

--------------------------------------------------------------------------- 
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'> 

答えて

2

最適化の結果はあなたので「none」ですそれを印刷することはできません。最適化ステップの後に損失を印刷する場合は、

loss,_ = sess.run([loss,grads_and_vars]); 
print(loss) 
関連する問題