Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Tensor' object has no attribute 'numpy' #36595

Closed
TheUnixDude opened this issue Feb 9, 2020 · 17 comments
Closed

'Tensor' object has no attribute 'numpy' #36595

TheUnixDude opened this issue Feb 9, 2020 · 17 comments

Comments

@TheUnixDude
Copy link

@TheUnixDude TheUnixDude commented Feb 9, 2020

URL(s) with the issue:

https://www.tensorflow.org/tutorials/quickstart/beginner

Description of issue (what needs changing):

The instructions state, in the fourth block of sample code, to use a command which returns a fatal error.

predictions = model(x_train[:1]).numpy()

Clear description

When running the example, as written, on Debian Stable (python 3.7.3)

>>> predictions = model(x_train[:1]).numpy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Tensor' object has no attribute 'numpy'

@oanush

This comment has been minimized.

Copy link

@oanush oanush commented Feb 10, 2020

@TheUnixDude ,
import tensorflow has attribute numpy, hence when tried running the tutorials, I was able to execute without any issues. Please try running the code again and let us know your feedback.Thank you!

@TheUnixDude

This comment has been minimized.

Copy link
Author

@TheUnixDude TheUnixDude commented Feb 10, 2020

Just a straight-up copy and paste of the commands given does not work with Debian Stable; this is after a install of tensorflow via pip3. Here's the full output.

$ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import absolute_import, division, print_function, unicode_literals
>>> import tensorflow as tf
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/me/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
>>> mnist = tf.keras.datasets.mnist
>>> (x_train, y_train), (x_test, y_test) = mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 6s 1us/step
>>> x_train, x_test = x_train / 255.0, x_test / 255.0
>>> model = tf.keras.models.Sequential([
...   tf.keras.layers.Flatten(input_shape=(28, 28)),
...   tf.keras.layers.Dense(128, activation='relu'),
...   tf.keras.layers.Dropout(0.2),
...   tf.keras.layers.Dense(10)
... ])
WARNING:tensorflow:From /home/me/.local/lib/python3.7/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
>>> predictions = model(x_train[:1]).numpy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Tensor' object has no attribute 'numpy'
>>> 

@TheUnixDude

This comment has been minimized.

Copy link
Author

@TheUnixDude TheUnixDude commented Feb 10, 2020

Ok, I figured out the issue.

I'm hesitant to close this, however, since there a documentation issue in that the commands given in the tutorial are not the same as what is opened in Colab.

The Colab code contains a Colab-specific version check on the tensorflow module. And, by default, pip3 will not install tensorflow version 2 - you get 1.14 as of today.

Since the new module is not called "tensorflow2", anyone looking at the Beginner's Tutorial might assume that we need to "pip3 install tensorflow", which is a good and valid assumption normally for python when trying to figure out modules you need, but that assumption is wrong in this case.

@MarkDaoust

This comment has been minimized.

Copy link
Contributor

@MarkDaoust MarkDaoust commented Feb 13, 2020

Hi @rushabh-v

tensorflow version 2 - you get 1.14 as of today.

I don't think that's correct.
You probably have tensorflow 1.14 installed, and "pip install tensorflow" says "tensorflow is already installed".

Try "pip install -U tensorflow".

@rushabh-v

This comment has been minimized.

Copy link
Contributor

@rushabh-v rushabh-v commented Feb 13, 2020

Okay then, should I close the PR?

@MarkDaoust

This comment has been minimized.

Copy link
Contributor

@MarkDaoust MarkDaoust commented Feb 13, 2020

I think so.

@MarkDaoust MarkDaoust closed this Feb 13, 2020
@TheUnixDude

This comment has been minimized.

Copy link
Author

@TheUnixDude TheUnixDude commented Feb 13, 2020

Just to note, I did not have Tensorflow installed when I began this. Mark's suggestion is incorrect.

Trying to update via "pip install -U tensorflow" does not install the new version.

Example of how broken this is There's clearly a newer version, and pip refuses to update when told to. Note these versions have moved forward since I opened this BR.

$ pip3 search tensorflow
tensorflow (2.1.0)                                - TensorFlow is an open source machine learning framework for everyone.
  INSTALLED: 2.0.0b1
  LATEST:    2.1.0

$ pip3 install -U tensorflow
Requirement already up-to-date: tensorflow in ./.local/lib/python3.7/site-packages (2.0.0b1)
@MarkDaoust

This comment has been minimized.

Copy link
Contributor

@MarkDaoust MarkDaoust commented Feb 13, 2020

Hmmm...

Other than TensorFlow already being installed, the only way I can think of for pip install to get you an old version is if you're running a system or python version that we no longer support, pip might still find all the old bersions that are supported on that system.

could that be it?
Any other ideas?

@MarkDaoust MarkDaoust reopened this Feb 13, 2020
@lamberta lamberta assigned mihaimaruseac, gunan and angerson and unassigned oanush Feb 13, 2020
@lamberta

This comment has been minimized.

Copy link
Member

@lamberta lamberta commented Feb 13, 2020

Thanks, @TheUnixDude. Confirmed Debian stable is installing 1.14
Ignoring that we say in the docs that we officially support "Ubuntu 16.04 or later (64-bit)", this is odd.

Repro in Docker:

$ docker run -it --rm debian:stable bash

# apt update
# apt install python3-dev python3-pip

# python3 --version
Python 3.7.3

# pip3 install tensorflow

Collecting tensorflow
  Downloading https://files.pythonhosted.org/packages/f4/28/96efba1a516cdacc2e2d6d081f699c001d414cc8ca3250e6d59ae657eb2b/tensorflow-1.14.0-cp37-cp37m-manylinux1_x86_64.whl (109.3MB)
...

## Trying known bad version to see available packages:
# pip3 install tensorflow==7.8.7
Collecting tensorflow==7.8.7
  Could not find a version that satisfies the requirement tensorflow==7.8.7 (from versions: 1.13.0rc1, 1.13.0rc2, 1.13.1, 1.13.2, 1.14.0rc0, 1.14.0rc1, 1.14.0, 2.0.0a0, 2.0.0b0, 2.0.0b1)
No matching distribution found for tensorflow==7.8.7

So it doesn't seem a stable version of 2.x is available.

@gunan

This comment has been minimized.

Copy link
Member

@gunan gunan commented Feb 13, 2020

It is probably because the pip version is just too old.
could you check the pip version?
Our minimum required pip version is listed on our installation page

@mihaimaruseac

This comment has been minimized.

Copy link
Collaborator

@mihaimaruseac mihaimaruseac commented Feb 13, 2020

Yes, pip install --upgrade pip setuptools is needed

@lamberta

This comment has been minimized.

Copy link
Member

@lamberta lamberta commented Feb 13, 2020

Ah, @gunan, you are right . ANd we do mention this throughout the docs.

# pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

# pip3 install -U pip
...
# pip3 --version
pip 20.0.2 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)

# pip3 install tensorflow
Downloading tensorflow-2.1.0-cp37-cp37m-manylinux2010_x86_64.whl (421.8 MB)
@lamberta lamberta closed this Feb 13, 2020
@TheUnixDude

This comment has been minimized.

Copy link
Author

@TheUnixDude TheUnixDude commented Feb 13, 2020

$ apt search python3-pip
Sorting... Done
Full Text Search... Done
python3-pip/stable,now 18.1-5 all [installed]
  Python package installer

So Debian Stable is not a supported release for Tensorflow version 2. In fact, pip3 version 20 isn't even available in sid. But, yes, upgrading pip3 outside of the package manager does solve the problem even though pip3 now throws a warning when used.

Maybe call this out in the Beginner's page? I would not expect a beginner to know the multiple layers needed to navigate this failure.

$ apt search python3-pip
Sorting... Done
Full Text Search... Done
python3-pip/stable,now 18.1-5 all [installed]
  Python package installer


$ pip3 install -U pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 770kB/s 
Installing collected packages: pip
Successfully installed pip-20.0.2

$ pip3 install -U tensorflow
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow
  Downloading tensorflow-2.1.0-cp37-cp37m-manylinux2010_x86_64.whl (421.8 MB)

<snip remainder of upgrade output>
@mihaimaruseac

This comment has been minimized.

Copy link
Collaborator

@mihaimaruseac mihaimaruseac commented Feb 13, 2020

Debian's package manager installs very old versions for a lot of packages. pip, cabal and others all need to be updated outside of it.

I don't know if we can document all the quirks of all the operating systems, but letting @lamberta decide what can be done.

@lamberta

This comment has been minimized.

Copy link
Member

@lamberta lamberta commented Feb 13, 2020

Maybe call this out in the Beginner's page?

I think it's worth an additional note in the first quickstart guides because it is confusing.

My ideal solution is a patch to 1.14 with a warning for folks to upgrade their pip to install the latest version. Typically these kinds of patches are reserved for security fixes, but this is a major usability flaw, IMHO

@TheUnixDude

This comment has been minimized.

Copy link
Author

@TheUnixDude TheUnixDude commented Feb 13, 2020

I made a patch & pull request to the doc adding a link to the install page with a note to follow it closely - but I cannot agree to the Google CLA so I closed the PR. Sorry I can't be of more assistance there.

@lamberta

This comment has been minimized.

Copy link
Member

@lamberta lamberta commented Feb 13, 2020

No worries. I'll make a separate update to the docs. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

9 participants
You can’t perform that action at this time.