Spyder运行出错TypeError: Can not convert a method into a Tensor or Operation.

出错代码图

出错提示

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不存在了。

正确的带码是这样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值