【问题标题】:Even after importing Numpy not recognised即使在导入 Numpy 后也无法识别
【发布时间】:2021-06-26 10:46:47
【问题描述】:

我的代码:

import numpy as np

import tensorflow

输出:

NameError                                 Traceback (most recent call last)
< ipython-input-1-87dbf2ff9b36 > in < module >
----> 1 x = np.random.randint(0,100)

NameError: name 'np' is not defined

我不明白的是,即使我已经导入了库,它也没有被检测到并给我一个 NameError,很多库也是如此。

我目前通过 Anaconda 使用 Jupyter Lab。

==================================>

M1 Macbook

上安装 TensorFlow 时获得了所有问题的解决方案

点击以下链接:

https://naturale0.github.io/machine%20learning/setting-up-m1-mac-for-both-tensorflow-and-pytorch#:~:text=Macs%20with%20ARM64%2Dbased%20M1,both%20from%20consumers%20and%20developers.

【问题讨论】:

  • 如果只导入 numpy 而不重命名会发生什么?
  • @Seppeke 问题依然存在。
  • @Seppeke 我尝试在终端上运行相同的程序,但看起来有问题:我收到此错误zsh: Illegal hardware instruction python3
  • 您使用的是什么设备? stackoverflow.com/questions/65383338/… 也许这有帮助
  • @Seppeke 我正在使用 Macbook M1 Air

标签: python tensorflow jupyter-notebook anaconda jupyter


【解决方案1】:

感谢@Akella Niranjan 的参考。为了社区提供解决方案的利益,这里由@Sihyung Park 撰写。

为 Apple Silicon M1 安装 TensorFlow-macOS

安装脚本由苹果公司提供,使用系统python很容易安装。但是,我的目标是将它与其他 python 一起安装,这样我就可以毫无困难地安装额外的数据科学包。

我主要是按照here的说明安装tensorflow-macos,然后创建了它的ipykernel,这样我就可以通过切换jupyter notebook里面的内核随时运行这个环境了。

  1. 首先install miniforge, which natively supports M1。安装默认到目录~/miniforge3/.

  2. 使用 conda 创建名为 tf_macos 的虚拟环境并安装 Python 3.8。

    conda create -n tf_macos
    conda activate tf_macos
    conda install -y python=3.8
    

我指定了 3.8 版本,使其符合 tensorflow-macos 的要求。

  1. Apple Github下载并安装tensorflow-macos
git clone https://github.com/apple/tensorflow_macos.git
cd tensorflow_macos/arm64
pip install --force pip==20.2.4 wheel setuptools cached-property six
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl tensorflow_addons-0.11.2+mlcompute-cp38-cp38-macosx_11_0_arm64.whl
pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
pip install --upgrade --force --no-dependencies tensorflow_macos-0.1a1-cp38-cp38-macosx_11_0_arm64.whl
  1. 将环境添加为 jupyter 内核。
pip install jupyter
python -m ipykernel install --name=tf_macos

然后只需将 ipython 内核切换到 tf_macos 就可以让我们轻松使用 mlcompute-backend TensorFlow

  1. 安装附加包(可选)
conda install scipy pandas matplotlib

更多信息可以参考here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 2018-07-22
    • 2018-04-15
    • 2019-07-03
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多