Keras 简明教程
Keras - Installation
本章介绍了如何在机器上安装 Keras。在进入安装之前,让我们了解 Keras 的基本要求。
This chapter explains about how to install Keras on your machine. Before moving to installation, let us go through the basic requirements of Keras.
Prerequisites
您必须满足以下要求 −
You must satisfy the following requirements −
-
Any kind of OS (Windows, Linux or Mac)
-
Python version 3.5 or higher.
Python
Keras 是基于 Python 的神经网络库,因此必须在您的机器上安装 Python。如果已在您的机器上正确安装了 Python,请打开终端并输入 python,您会看到类似于下面指定的响应,
Keras is python based neural network library so python must be installed on your machine. If python is properly installed on your machine, then open your terminal and type python, you could see the response similar as specified below,
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18)
[MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
截至目前,最新版本是“3.7.2”。如果未安装 Python,请访问官方 Python 链接 - www.python.org 根据您的操作系统下载最新版本并立即在您的系统上安装。
As of now the latest version is ‘3.7.2’. If Python is not installed, then visit the official python link - www.python.org and download the latest version based on your OS and install it immediately on your system.
Keras Installation Steps
Keras 安装非常容易。按照以下步骤,可以正确安装系统中的 Keras。
Keras installation is quite easy. Follow below steps to properly install Keras on your system.
Step 1: Create virtual environment
Virtualenv 用于管理不同项目的 Python 包。这有助于避免破坏在其他环境中安装的包。因此,在开发 Python 应用程序时,始终建议使用虚拟环境。
Virtualenv is used to manage Python packages for different projects. This will be helpful to avoid breaking the packages installed in the other environments. So, it is always recommended to use a virtual environment while developing Python applications.
Linux/Mac OS
Linux/Mac OS
对于 Linux 或 mac OS 用户,转到项目根目录,然后键入以下命令以创建虚拟环境,
Linux or mac OS users, go to your project root directory and type the below command to create virtual environment,
python3 -m venv kerasenv
执行上述命令后,将在安装位置中使用 bin,lib and include folders 创建“kerasenv”目录。
After executing the above command, “kerasenv” directory is created with bin,lib and include folders in your installation location.
Windows
Windows
Windows 用户可以使用以下命令,
Windows user can use the below command,
py -m venv keras
Step 2: Activate the environment
此步骤将在 shell 路径中配置 python 和 pip 可执行文件。
This step will configure python and pip executables in your shell path.
Linux/Mac OS
Linux/Mac OS
现在,我们已经创建了一个名为“kerasvenv”的虚拟环境。进入该文件夹,然后键入以下命令,
Now we have created a virtual environment named “kerasvenv”. Move to the folder and type the below command,
$ cd kerasvenv kerasvenv $ source bin/activate
Windows
Windows
Windows 用户进入“kerasenv”文件夹并键入以下命令,
Windows users move inside the “kerasenv” folder and type the below command,
.\env\Scripts\activate
Step 3: Python libraries
Keras 依赖于以下 python 库。
Keras depends on the following python libraries.
-
Numpy
-
Pandas
-
Scikit-learn
-
Matplotlib
-
Scipy
-
Seaborn
希望您已在系统中安装了以上所有库。如果未安装这些库,请使用以下命令逐个安装。
Hopefully, you have installed all the above libraries on your system. If these libraries are not installed, then use the below command to install one by one.
numpy
numpy
pip install numpy
您可能会看到以下响应,
you could see the following response,
Collecting numpy
Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
numpy-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
|████████████████████████████████| 14.4MB 2.8MB/s
pandas
pandas
pip install pandas
我们可以看到以下响应,
We could see the following response,
Collecting pandas
Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
pandas-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
|████████████████████████████████| 14.4MB 2.8MB/s
matplotlib
matplotlib
pip install matplotlib
我们可以看到以下响应,
We could see the following response,
Collecting matplotlib
Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/
matplotlib-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
|████████████████████████████████| 14.4MB 2.8MB/s
scipy
scipy
pip install scipy
我们可以看到以下响应,
We could see the following response,
Collecting scipy
Downloading
https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8
/scipy-3.1.1-cp36-cp36m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64.
macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB)
|████████████████████████████████| 14.4MB 2.8MB/s
scikit-learn
scikit-learn
它是一个开源机器学习库。它用于分类、回归和聚类算法。在进行安装之前,它需要以下内容 -
It is an open source machine learning library. It is used for classification, regression and clustering algorithms. Before moving to the installation, it requires the following −
-
Python version 3.5 or higher
-
NumPy version 1.11.0 or higher
-
SciPy version 0.17.0 or higher
-
joblib 0.11 or higher.
现在,我们使用以下命令安装 scikit-learn -
Now, we install scikit-learn using the below command −
pip install -U scikit-learn
Seaborn
Seaborn
Seaborn 是一个神奇的库,它允许你轻松地可视化你的数据。使用以下命令进行安装 -
Seaborn is an amazing library that allows you to easily visualize your data. Use the below command to install −
pip install seaborn
你可能会看到如下所示的类似信息 -
You could see the message similar as specified below −
Collecting seaborn
Downloading
https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc
/seaborn-0.9.0-py3-none-any.whl (208kB) 100%
|████████████████████████████████| 215kB 4.0MB/s
Requirement already satisfied: numpy> = 1.9.3 in
./lib/python3.7/site-packages (from seaborn) (1.17.0)
Collecting pandas> = 0.15.2 (from seaborn)
Downloading
https://files.pythonhosted.org/packages/39/b7/441375a152f3f9929ff8bc2915218ff1a063a59d7137ae0546db616749f9/
pandas-0.25.0-cp37-cp37m-macosx_10_9_x86_64.
macosx_10_10_x86_64.whl (10.1MB) 100%
|████████████████████████████████| 10.1MB 1.8MB/s
Requirement already satisfied: scipy>=0.14.0 in
./lib/python3.7/site-packages (from seaborn) (1.3.0)
Collecting matplotlib> = 1.4.3 (from seaborn)
Downloading
https://files.pythonhosted.org/packages/c3/8b/af9e0984f
5c0df06d3fab0bf396eb09cbf05f8452de4e9502b182f59c33b/
matplotlib-3.1.1-cp37-cp37m-macosx_10_6_intel.
macosx_10_9_intel.macosx_10_9_x86_64
.macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) 100%
|████████████████████████████████| 14.4MB 1.4MB/s
......................................
......................................
Successfully installed cycler-0.10.0 kiwisolver-1.1.0
matplotlib-3.1.1 pandas-0.25.0 pyparsing-2.4.2
python-dateutil-2.8.0 pytz-2019.2 seaborn-0.9.0
Keras Installation Using Python
截至目前,我们已经完成了安装 Kera 的基本要求。现在,使用如下所示的相同过程安装 Keras -
As of now, we have completed basic requirements for the installtion of Kera. Now, install the Keras using same procedure as specified below −
pip install keras
Anaconda Cloud
我们相信你已经在你的机器上安装了 anaconda cloud。如果未安装 anaconda,请访问官方链接, https://www.anaconda.com/download 并根据你的操作系统选择下载。
We believe that you have installed anaconda cloud on your machine. If anaconda is not installed, then visit the official link, https://www.anaconda.com/download and choose download based on your OS.
Create a new conda environment
启动 anaconda 提示,这将打开基础 Anaconda 环境。让我们创建一个新的 conda 环境。此过程类似于 virtualenv。在你的 conda 终端中键入以下命令 -
Launch anaconda prompt, this will open base Anaconda environment. Let us create a new conda environment. This process is similar to virtualenv. Type the below command in your conda terminal −
conda create --name PythonCPU
如果你愿意,你还可以使用 GPU 创建和安装模块。在本教程中,我们遵循 CPU 说明。
If you want, you can create and install modules using GPU also. In this tutorial, we follow CPU instructions.
Activate conda environment
要激活环境,请使用以下命令 -
To activate the environment, use the below command −
activate PythonCPU
Install spyder
Spyder 是用于执行 python 应用程序的 IDE。让我们使用以下命令在我们的 conda 环境中安装这个 IDE -
Spyder is an IDE for executing python applications. Let us install this IDE in our conda environment using the below command −
conda install spyder
Install python libraries
我们已经知道 keras 所需的 python 库 numpy、pandas 等。你可以使用以下语法安装所有模块 -
We have already known the python libraries numpy, pandas, etc., needed for keras. You can install all the modules by using the below syntax −
Syntax
Syntax
conda install -c anaconda <module-name>
例如,你想要安装 pandas -
For example, you want to install pandas −
conda install -c anaconda pandas
与相同的方法一样,尝试自己安装剩余的模块。
Like the same method, try it yourself to install the remaining modules.
Install Keras
现在,一切都看起来不错,所以你可以使用以下命令开始 keras 安装 -
Now, everything looks good so you can start keras installation using the below command −
conda install -c anaconda keras
Launch spyder
最后,使用以下命令在您的 conda 终端中启动 spyder −
Finally, launch spyder in your conda terminal using the below command −
spyder
为了确保一切都正确安装,导入所有模块,它会添加所有内容,如果出现任何问题,您将收到 module not found 错误消息。
To ensure everything was installed correctly, import all the modules, it will add everything and if anything went wrong, you will get module not found error message.