出错代码图
出错提示
runfile('E:/TensorFlow/线性回归.py', wdir='E:/TensorFlow')
Traceback (most recent call last):
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:305 in __init__
self._unique_fetches.append(ops.get_default_graph().as_graph_element(
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:3577 in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py:3673 in _as_graph_element_locked
raise TypeError("Can not convert a %s into a %s." %
TypeError: Can not convert a method into a Tensor or Operation.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File e:\ProgramData\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)
File e:\tensorflow\线性回归.py:52
sess.run(optimizer,feed_dict={X:x,Y:y})
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:969 in run
result = self._run(None, fetches, feed_dict, options_ptr,
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:1177 in _run
fetch_handler = _FetchHandler(
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:486 in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:277 in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File e:\ProgramData\anaconda3\lib\site-packages\tensorflow\python\client\session.py:308 in __init__
raise TypeError(f'Argument `fetch` = {fetch} has invalid type '
TypeError: Argument `fetch` = <bound method Optimizer.minimize of <tensorflow.python.training.gradient_descent.GradientDescentOptimizer object at 0x00000153A1309150>> has invalid type "method" must be a string or Tensor. (Can not convert a method into a Tensor or Operation.)
Spyder的这行代码optimizer=tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize
(cost)
在minimize后面不能回车键,回车就提示52行的sess.run(optimizer,feed_dict={X:x,Y:y})的optimizer不存在了。
正确的带码是这样