简体   繁体   English

tensorflow教程代码失败并出现AttributeError:“ Tensor”对象没有属性“ numpy”

[英]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新手)

  1. Regarding the RuntimeError in the Google Notebook. 关于Google笔记本中的RuntimeError You're looking at the version of the notebook in the master branch in GitHub. 您正在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. 如果要查找与TensorFlow特定发行版相对应的笔记本,则应查看相应发行分支中的版本。 For example, https://colab.research.google.com/github/tensorflow/tensorflow/blob/r1.10/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb It seems www.tensorflow.org/tutorials is incorrectly linking to the master branch version, so that is certainly a bug on the website. 例如, https: //colab.research.google.com/github/tensorflow/tensorflow/blob/r1.10/tensorflow/contrib/eager/python/examples/notebooks/eager_basics.ipynb似乎是www.tensorflow.org/教程错误地链接到master分支版本,因此肯定是网站上的错误。

Update : The website has been fixed, links should point to the appropriate release branch. 更新 :网站已修复,链接应指向相应的发行分支。

  1. The second error suggests that there is either a missing call to 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.

相关问题 Tensorflow 2.3: AttributeError: 'Tensor' 对象没有属性 'numpy' - Tensorflow 2.3: AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError:“Tensor”对象在 Tensorflow 2.1 中没有属性“numpy” - AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1 tensor.numpy() 在 tensorflow.data.Dataset 中不起作用。 抛出错误:AttributeError: 'Tensor' object has no attribute 'numpy' - tensor.numpy() not working in tensorflow.data.Dataset. Throws the error: AttributeError: 'Tensor' object has no attribute 'numpy' TensorFlow:AttributeError:'Tensor'对象没有属性'shape' - TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' AttributeError: 'Tensor' 对象没有属性 'numpy' - AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError: 'Tensor' 对象没有属性 'numpy - AttributeError: 'Tensor' object has no attribute 'numpy AttributeError: 'Tensor' object 在自定义损失 function (Tensorflow 2.1.0) 中没有属性 'numpy' - AttributeError: 'Tensor' object has no attribute 'numpy' in custom loss function (Tensorflow 2.1.0) TensorFlow:AttributeError:'Tensor'对象没有属性'log10' - TensorFlow: AttributeError: 'Tensor' object has no attribute 'log10' AttributeError:“张量”object 在自定义 model 中没有属性“numpy” - AttributeError: 'Tensor' object has no attribute 'numpy' in a custom model Tensorflow 2.0.0: AttributeError: 'TensorSliceDataset' 对象没有属性 'as_numpy_iterator' - Tensorflow 2.0.0: AttributeError: 'TensorSliceDataset' object has no attribute 'as_numpy_iterator'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM