介绍 (Introduction)
An open-source machine learning software library, TensorFlow is used to train neural networks. Expressed in the form of stateful dataflow graphs, each node in the graph represents the operations performed by neural networks on multi-dimensional arrays. These multi-dimensional arrays are commonly known as “tensors,” hence the name TensorFlow.
TensorFlow是开放源代码的机器学习软件库,用于训练神经网络。 以状态数据流图的形式表示, 图中的每个节点表示神经网络在多维数组上执行的操作。 这些多维数组通常称为“张量”,因此名称为TensorFlow。
In this tutorial, you’ll install TensorFlow in a Python virtual environment with virtualenv
. This approach isolates the TensorFlow installation and gets things up and running quickly. Once you complete the installation, you’ll validate your installation by importing Tensorflow to ensure you have no errors.
在本教程中,您将使用virtualenv
在Python虚拟环境中安装TensorFlow。 这种方法可以隔离TensorFlow安装并快速启动和运行。 完成安装后,您将通过导入Tensorflow来验证安装,以确保没有错误。
先决条件 (Prerequisites)
Before you begin this tutorial, you’ll need the following:
在开始本教程之前,您需要满足以下条件:
One Ubuntu 20.04 server with at least 4GB of RAM set up by following the Ubuntu 20.04 initial server setup guide, including a sudo non-root user and a firewall.
按照Ubuntu 20.04初始服务器设置指南 (包括sudo非root用户和防火墙)设置一台至少具有4GB RAM 的Ubuntu 20.04服务器 。
Python 3.8 or higher and
virtualenv
installed. Follow How To Install Python 3 on Ubuntu 20.04 to configure Python andvirtualenv
.Python 3.8或更高版本并安装了
virtualenv
。 遵循如何在Ubuntu 20.04上安装Python 3来配置Python和virtualenv
。
第1步-创建编程环境 (Step 1 — Creating a Programming Environment)
In this step, we’ll create a virtual environment in order to install TensorFlow into it without compromising our other programming projects. If you already have a clean programming environment set up, feel free to skip this step.
在此步骤中,我们将创建一个虚拟环境,以便将TensorFlow安装到其中,而不会影响其他编程项目。 如果您已经设置了干净的编程环境,请随时跳过此步骤。
First, create a project directory. We’ll call it tf-demo
for demonstration purposes, but choose a directory name that is meaningful to you:
首先,创建一个项目目录。 出于演示目的,我们将其称为tf-demo
,但选择一个对您有意义的目录名称:
mkdir ~/tf-demo
mkdir〜/ tf-demo
Navigate to your newly created tf-demo
directory:
导航到新创建的tf-demo
目录:
cd ~/tf-demo
cd〜/ tf-demo
Then create a new virtual environment called tensorflow-dev
, for instance. Run the following command to create the environment:
然后创建一个新的虚拟环境,例如tensorflow-dev
。 运行以下命令来创建环境:
python3 -m venv tensorflow-dev
python3 -m venv tensorflow-dev
This creates a new tensorflow-dev
directory which will contain all of the packages that you install while this environment is activated. It also includes pip
and a standalone version of Python.
这将创建一个新的tensorflow-dev
目录,其中将包含激活此环境时安装的所有软件包。 它还包括pip
和Python的独立版本。
Now activate your virtual environment:
现在激活您的虚拟环境:
source tensorflow-dev/bin/activate
源tensorflow-dev / bin / activate
Once activated, your terminal prompt will reflect that you are in the virtual environment:
激活后,您的终端提示将反映您处于虚拟环境中:
(tensorflow-dev)username@hostname:~/tf-demo $
At this point you can install TensorFlow in your virtual environment.
此时,您可以在虚拟环境中安装TensorFlow。
第2步—安装TensorFlow (Step 2 — Installing TensorFlow)
When installing TensorFlow, we want to make sure we are installing and upgrading to the newest version available in PyPi.
在安装TensorFlow时,我们要确保要安装并升级到PyPi中可用的最新版本。
Therefore, we’ll be using the following command syntax with pip:
因此,我们将对pip使用以下命令语法:
- pip install --upgrade tensorflow pip install --upgrade张量流
Once you press ENTER
, TensorFlow will install, and you should receive output that indicates that the install along with any dependent packages was successful.
一旦按ENTER
,TensorFlow将安装,并且您应该收到输出,指示安装以及所有相关软件包均已成功。
Output
...
Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.0.1 mock-2.0.0 numpy-1.16.2 pbr-5.1.3 protobuf-3.7.0 setuptools-40.8.0 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0 termcolor-1.1.0 werkzeug-0.15.0 wheel-0.33.1
...
Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
You can deactivate your virtual environment at any time by using the following command:
您可以随时使用以下命令停用虚拟环境:
- deactivate 停用
To reactivate the environment later, navigate to your project directory and run source tensorflow-dev/bin/activate
.
要稍后重新激活环境,请导航到您的项目目录并运行source tensorflow-dev /bin/activate
。
Now that you have installed TensorFlow, let’s make sure the TensorFlow installation works.
现在您已经安装了TensorFlow,让我们确保TensorFlow安装正常。
步骤3 —验证安装 (Step 3 — Validating Installation)
To validate the installation of TensorFlow, we are going to ensure that we can import the TensorFlow package.
为了验证TensorFlow的安装,我们将确保我们可以导入TensorFlow软件包。
- python Python
The following prompt will appear on your terminal:
以下提示将出现在您的终端上:
>>>
This is the prompt for the Python interpreter, and it indicates that it’s ready for you to start entering some Python statements.
这是Python解释器的提示,它表明您已经准备好开始输入一些Python语句。
First, type this line to import the TensorFlow package and make it available as the local variable tf
. Press ENTER
after typing in the line of code:
首先,输入以下行以导入TensorFlow软件包,并将其用作局部变量tf
。 在代码行中ENTER
后按ENTER
:
- import tensorflow as tf 将tensorflow作为tf导入
As long as you have received no errors, you have installed TensorFlow successfully. If you have received an error, you should ensure that your server is powerful enough to handle TensorFlow. You may need to resize your server, making sure it has at least 4GB of memory.
只要没有收到任何错误,便表示您已成功安装TensorFlow。 如果收到错误,则应确保服务器足够强大以处理TensorFlow。 您可能需要调整服务器的大小,以确保它至少有4GB的内存。
结论 (Conclusion)
In this tutorial, you have installed TensorFlow in a Python virtual environment and validated that TensorFlow works by importing it.
在本教程中,您已在Python虚拟环境中安装了TensorFlow,并通过导入TensorFlow验证了该功能是否正常。
TensorFlow’s programmer’s guide provides a useful resource and reference for TensorFlow development. You can also explore Kaggle, a competitive environment for practical application of machine learning concepts that pit you against other machine learning, data science, and statistics enthusiasts.
TensorFlow的程序员指南为TensorFlow开发提供了有用的资源和参考。 您还可以探索Kaggle ,这是一个实际应用机器学习概念的竞争环境,可让您与其他机器学习,数据科学和统计爱好者一道。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-tensorflow-on-ubuntu-20-04