📜  如何安装 tensorflow - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:09:05.082000             🧑  作者: Mango

如何安装 TensorFlow - Shell/Bash

TensorFlow是一个功能强大的开源机器学习框架,它可以帮助您构建和训练深度学习模型。在本篇文章中,我们将介绍如何在Linux/Mac上通过Shell/Bash命令行安装TensorFlow。

步骤 1 - 安装 pip

在安装TensorFlow之前,需要先安装pip,它是Python的软件包管理器。如果您已经安装了pip,则可以跳过此步骤。

在Ubuntu 18.04上,您可以使用以下命令安装pip:

sudo apt update
sudo apt install python3-pip
步骤 2 - 安装 TensorFlow

通过以下命令安装 TensorFlow:

pip3 install tensorflow

这将会安装 TensorFlow 的最新版本。

如果您需要安装一个特定的版本,请使用以下命令:

pip3 install tensorflow==<version>

例如,要安装 TensorFlow 2.0.0 版本,请使用以下命令:

pip3 install tensorflow==2.0.0
步骤 3 - 验证 TensorFlow 安装

要验证TensorFlow是否成功安装,请在命令行中运行以下Python代码:

python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

如果您看到类似以下输出,则说明TensorFlow已成功安装:

2021-06-04 20:47:19.994468: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
tf.Tensor(-1694.3104, shape=(), dtype=float32)
总结

通过这些简单的步骤,您就可以在Linux/Mac上安装TensorFlow。要通过Python代码运行TensorFlow,只需导入"tensorflow"模块即可。如果需要更多的帮助,请查阅TensorFlow官方文档。