[英]tensorflow tutorial code fails with AttributeError: 'Tensor' object has no attribute 'numpy'
Just installed tensorflow-gpu 1.10 on Win10 using Python 3.6.6 and CUDA 9.0 Trying the sample code at https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python/examples/notebooks/custom_training.ipynb#scrollTo=_WRu7Pze7wk8 刚刚使用Python 3.6.6和CUDA 9.0在Win10上安装了tensorflow-gpu 1.10,在https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/contrib/eager/python中尝试了示例代码/examples/notebooks/custom_training.ipynb#scrollTo=_WRu7Pze7wk8
Problems right at the top: 最上方的问题:
class Model(object): def __init__(self):
# Initialize variable to (5.0, 0.0)
# In practice, these should be initialized to random values.
self.W = tf.Variable(5.0)
self.b = tf.Variable(0.0)
def __call__(self, x):
return self.W * x + self.b model = Model()
assert model(3.0).numpy() == 15.0
When run on Google Notebook it fails with 在Google笔记本上运行时,失败并显示
RuntimeError: tf.Variable not supported when eager execution is enabled.
RuntimeError:启用急切执行时不支持tf.Variable。 Please use tf.contrib.eager.Variable instead
请改用tf.contrib.eager.Variable
You should fix that. 你应该解决这个问题。 With it fixed, the code runs without error on Notebook.
修复后,代码在Notebook上运行不会出现错误。
However when I copy it to a local .py file and run that, I get this really unexpected error: 但是,当我将其复制到本地.py文件并运行该文件时,我得到了这个确实意外的错误:
Traceback (most recent call last): File "linear.py", line 15, in assert model(3.0).numpy() == 15.0 AttributeError: 'Tensor' object has no attribute 'numpy'
追溯(最近一次调用最近):在断言模型(3.0)中的文件“ linear.py”,第15行,.numpy()== 15.0 AttributeError:“ Tensor”对象没有属性“ numpy”
However in Python's interactive mode... 但是在Python的交互模式下...
>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> v = tf.contrib.eager.Variable(4.7)
>>> print( v.numpy() )
4.7
>>>
What gives?? 是什么赋予了?? (please bear in mind that I am a total Python and tensorflow noob)
(请记住我是一个Python和tensorflow新手)
RuntimeError
in the Google Notebook. RuntimeError
。 You're looking at the version of the notebook in the master
branch in GitHub. master
分支中查看笔记本的版本。 That version of the notebook is meant to be in sync with the master
branch of the codebase, where tf.Variable
works just fine. master
分支同步,其中tf.Variable
可以正常工作。 If you're looking for the notebook corresponding to a particular release version of TensorFlow, you should look at the version in the corresponding release branch. Update : The website has been fixed, links should point to the appropriate release branch. 更新 :网站已修复,链接应指向相应的发行分支。
tf.enable_eager_execution()
or the code is being run inside a with tf.Graph().as_default()
block. tf.enable_eager_execution()
调用,或者代码正在with tf.Graph().as_default()
块在内部运行。 Is that what might be happening? Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.