首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'str‘对象没有属性'numpy’

AttributeError:'str‘对象没有属性'numpy’
EN

Stack Overflow用户
提问于 2022-12-01 13:11:18
回答 2查看 60关注 0票数 -2

我的命令

代码语言:javascript
复制
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())
代码语言:javascript
复制
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.
代码语言:javascript
复制
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
代码语言:javascript
复制
>>>     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'
>>>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-12-01 13:46:10

您正试图在python中直接运行powershell。但是powershell解释器只会说powershell,不能对python代码进行本机解释。

您必须将python代码放在python文件中,例如my_code.py,并在powershell中使用python my_code.py调用/执行它。现在使用python解释器来运行脚本。详情请参见如何运行python代码

票数 1
EN

Stack Overflow用户

发布于 2022-12-02 13:57:26

我已经将您问题中的代码块分成三个不同的部分,每个错误一个。

当然,您应该将Python代码放入实际的脚本中,REPL也会工作,但pip install不是Python代码。Windows 11 Powershell这样的纯文本也不是。如果您正在从某个博客/文档中复制代码,那么只执行实际的Python代码..。

Python关心缩进,所以reviews not defined是因为您的for循环在定义变量的地方没有缩进。

'str' object has no attribute 'numpy'在这一行print(review.numpy().decode("utf-8")),因为字符串没有numpy函数..。它也应该被解码了。所以直接打印评论文本就行了。

我看到你链接到的视频/图片是2019年的.从那以后,Tensorflow发布了几个新版本,所以API可能已经改变了。您需要查阅官方文档以获得已安装的版本

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74642430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档