即使在导入 Numpy 后也无法识别

Even after importing Numpy not recognised

我的代码:

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 时获得所有问题的解决方案

按照以下link:

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.

感谢@Akella Niranjan 的参考。为了社区的利益,请在此处提供由@Sihyung Park.

撰写的解决方案

为 Apple Silicon M1 安装 TensorFlow-macOS

用系统python安装很容易,安装脚本是Apple给的。但是,我的目标是将它与其他 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