Home > Net >  AttributeError: 'str' object has no attribute 'numpy'
AttributeError: 'str' object has no attribute 'numpy'

Time:12-01

My command

Windows 11 PowerShell.

!pip install tensorflow-datasets
pip install tensorflow-datasets

# pip install tfds-nightly

import tensorflow_datasets as tfds
datasets = tfds.load("imdb_reviews")

train_set = tfds.load("imdb_reviews") # 25.000 reviews.
test_set = datasets["test"]           # 25.000 reviews.

train_set, test_set = tfds.load("imdb_reviews", split=["train", "test"])
train_set, test_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test"])
train_set, test_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]"])
train_set, test_set, valid_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]", "test[60%:]"])
train_set, test_set, valid_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]", "test[60%:]"], as_supervised = True)


for review, label in train_set.take(2):
    print(review.numpy().decode("utf-8"))
    print(label.numpy())
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\donhu> pip install tensorflow-datasets
Collecting tensorflow-datasets
  Downloading tensorflow_datasets-4.7.0-py3-none-any.whl (4.7 MB)
     |████████████████████████████████| 4.7 MB 2.2 MB/s
Requirement already satisfied: protobuf>=3.12.2 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (3.19.6)
Collecting tensorflow-metadata
  Downloading tensorflow_metadata-1.11.0-py3-none-any.whl (52 kB)
     |████████████████████████████████| 52 kB ...
Requirement already satisfied: numpy in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (1.23.4)
Requirement already satisfied: requests>=2.19.0 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (2.28.1)
Requirement already satisfied: six in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (1.16.0)
Requirement already satisfied: termcolor in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (2.1.0)
Collecting etils[epath]
  Downloading etils-0.9.0-py3-none-any.whl (140 kB)
     |████████████████████████████████| 140 kB ...
Collecting promise
  Downloading promise-2.3.tar.gz (19 kB)
Requirement already satisfied: tqdm in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (4.64.1)
Collecting toml
  Downloading toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting dill
  Downloading dill-0.3.6-py3-none-any.whl (110 kB)
     |████████████████████████████████| 110 kB 6.4 MB/s
Requirement already satisfied: absl-py in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tensorflow-datasets) (1.3.0)
Collecting googleapis-common-protos<2,>=1.52.0
  Downloading googleapis_common_protos-1.57.0-py2.py3-none-any.whl (217 kB)
     |████████████████████████████████| 217 kB 6.4 MB/s
Requirement already satisfied: certifi>=2017.4.17 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from requests>=2.19.0->tensorflow-datasets) (2022.9.24)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from requests>=2.19.0->tensorflow-datasets) (2.1.1)
Requirement already satisfied: idna<4,>=2.5 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from requests>=2.19.0->tensorflow-datasets) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from requests>=2.19.0->tensorflow-datasets) (1.26.12)Requirement already satisfied: typing_extensions; extra == "epath" in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from etils[epath]->tensorflow-datasets) (4.4.0)
Collecting importlib_resources; extra == "epath"
  Downloading importlib_resources-5.10.0-py3-none-any.whl (34 kB)
Requirement already satisfied: zipp; extra == "epath" in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from etils[epath]->tensorflow-datasets) (3.10.0)
Requirement already satisfied: colorama; platform_system == "Windows" in c:\users\donhu\appdata\local\programs\python\python39\lib\site-packages (from tqdm->tensorflow-datasets) (0.4.6)
Building wheels for collected packages: promise
  Building wheel for promise (setup.py) ... done
  Created wheel for promise: filename=promise-2.3-py3-none-any.whl size=21554 sha256=8d6db1312d74403cffbe332a56a0caeb292ff65701f5c958a2c836715275b299
  Stored in directory: c:\users\donhu\appdata\local\pip\cache\wheels\e1\e8\83\ddea66100678d139b14bc87692ece57c6a2a937956d2532608
Successfully built promise
Installing collected packages: googleapis-common-protos, tensorflow-metadata, importlib-resources, etils, promise, toml, dill, tensorflow-datasets
Successfully installed dill-0.3.6 etils-0.9.0 googleapis-common-protos-1.57.0 importlib-resources-5.10.0 promise-2.3 tensorflow-datasets-4.7.0 tensorflow-metadata-1.11.0 toml-0.10.2
WARNING: You are using pip version 20.2.3; however, version 22.3.1 is available.
You should consider upgrading via the 'c:\users\donhu\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip' command.
PS C:\Users\donhu> python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow_datasets as tfds
>>> datasets = tfds.load("imdb_reviews")
Downloading and preparing dataset Unknown size (download: Unknown size, generated: Unknown size, total: Unknown size) to C:\Users\donhu\tensorflow_datasets\imdb_reviews\plain_text\1.0.0...
Dl Completed...:   0%|                                                                                                              | 0/1 [00:11<?, ? url/s]
Dl Size...:  26%|███████████████████████████▌                                                                             | 21/80 [00:11<00:25,  2.27 MiB/s]
Dl Completed...:   0%|                                                                                                              | 0/1 [00:12<?, ? url/s]
Dl Size...:  28%|████████████████████████████▉
Dl Completed...:   0%|                                                                                                              | 0/1 [00:12<?, ? url/s]
Dl Size...:  29%|██████████████████████████████▏
Dl Completed...:   0%|                                                                                                              | 0/1 [00:13<?, ? url/s]
Dl Size...:  30%|███████████████████████████████▌
Dl Completed...:   0%|                                                                                                              | 0/1 [00:13<?, ? url/s]
Dl Size...: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 80/80 [00:51<00:00,  1.55 MiB/s]
Dl Completed...: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:51<00:00, 51.53s/ url]
Generating splits...:   0%|                                                                                                      | 0/3 [00:00<?, ? splits/s]
Generating train examples...: 7479 examples [00:02, 6153.86 examples/s]
Dataset imdb_reviews downloaded and prepared to C:\Users\donhu\tensorflow_datasets\imdb_reviews\plain_text\1.0.0. Subsequent calls will reuse this data.
2022-12-01 19:48:16.580270: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-12-01 19:48:17.465275: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 3994 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1660 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5
>>> train_set = tfds.load("imdb_reviews")
>>>
>>> test_set = datasets["test"]
>>> train_set, test_set = tfds.load("imdb_reviews", split=["train", "test"])
>>> for review, label in train_set.take(2):
... print(review.numpy().decode("utf-8"))
  File "<stdin>", line 2
    print(review.numpy().decode("utf-8"))
    ^
IndentationError: expected an indented block
>>> print(review.numpy().decode("utf-8"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'review' is not defined
>>>     Windows 11 PowerShell.
  File "<stdin>", line 1
    Windows 11 PowerShell.
IndentationError: unexpected indent
>>>
>>> !pip install tensorflow-datasets
  File "<stdin>", line 1
    !pip install tensorflow-datasets
    ^
SyntaxError: invalid syntax
>>> pip install tensorflow-datasets
  File "<stdin>", line 1
    pip install tensorflow-datasets
        ^
SyntaxError: invalid syntax
>>>
>>> # pip install tfds-nightly
>>>
>>> import tensorflow_datasets as tfds
>>> datasets = tfds.load("imdb_reviews")
>>>
>>> train_set = tfds.load("imdb_reviews") # 25.000 reviews.
>>> test_set = datasets["test"]           # 25.000 reviews.
>>>
>>> train_set, test_set = tfds.load("imdb_reviews", split=["train", "test"])
>>> train_set, test_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test"])
>>> train_set, test_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]"])
>>> train_set, test_set, valid_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]"], "test[60%:]")
  File "<stdin>", line 1
    train_set, test_set, valid_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]"], "test[60%:]")
                                                                                                                ^
SyntaxError: positional argument follows keyword argument
>>> train_set, test_set, valid_set = tfds.load("imdb_reviews:1.0.0", split=["train", "test[:60%]", "test[60%:]"])
>>> for review, label in train_set.take(2):
...     print(review.numpy().decode("utf-8"))
...     print(label.numpy())
...
2022-12-01 20:07:51.639683: W tensorflow/core/kernels/data/cache_dataset_ops.cc:856] The calling iterator did not fully read the dataset being cached. In order to avoid unexpected truncation of the dataset, the partially cached contents of the dataset  will be discarded. This can happen if you have an input pipeline similar to `dataset.cache().take(k).repeat()`. You should use `dataset.take(k).cache().repeat()` instead.
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'str' object has no attribute 'numpy'
>>>

enter image description here

How to fix it?

CodePudding user response:

You are trying to run python directly within powershell. But the powershell-interpreter speaks only powershell and cannot natively interprete python code.

You have to put the python code in a python file, e.g. my_code.py and call/execute it with python my_code.py from within powershell. Now the python interpreter is used to run the script. See How to run python code for details.

  • Related