TensorFlow安装与配置教程(2022.12)

1. TensorFlow的安装

首先需要安装 Anaconda 环境,可以转至:Anaconda3安装与配置教程(2022.11)。

然后我们打开 Anaconda,创建一个 TensorFlow 环境:

conda create -n TensorFlow python=3.9

进入 TensorFlow 环境,安装 tensorflow:

conda activate TensorFlowconda install tensorflow # 安装CPU版本conda install tensorflow-gpu # 安装GPU版本

本文安装的为 GPU 版本,安装好后进入 Python,使用以下代码进行检测,没有报错即为安装成功:

>>> import tensorflow as tf>>> a = tf.constant(1.)>>> b = tf.constant(2.)>>> print(a+b)tf.Tensor(3.0, shape=(), dtype=float32)>>> print(tf.test.gpu_device_name())2022-12-19 11:37:47.885107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0 with 6007 MB memory: -> device: 0, name: NVIDIA GeForce RTX 2070, pci bus id: 0000:01:00.0, compute capability: 7.5/device:GPU:0>>> print(‘GPU:’,tf.config.list_physical_devices(device_type=’GPU’))GPU: [PhysicalDevice(name=’/physical_device:GPU:0′, device_type=’GPU’)]>>> print(‘CPU:’,tf.config.list_physical_devices(device_type=’CPU’))CPU: [PhysicalDevice(name=’/physical_device:CPU:0′, device_type=’CPU’)]>>> print(tf.test.is_gpu_available())2022-12-19 11:39:14.914081: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0 with 6007 MB memory: -> device: 0, name: NVIDIA GeForce RTX 2070, pci bus id: 0000:01:00.0, compute capability: 7.5True 2. PyCharm配置TensorFlow环境

在 PyCharm 中设置 Python 解释器,在 Conda 环境中选择现有环境,解释器选择:D:\Anaconda3_Environments\envs\TensorFlow\python.exe,Conda 可执行文件选择:D:\Anaconda3\Scripts\conda.exe。

设置好后即可在解释器选择菜单中找到 Python 3.9 (TensorFlow) 选项:

创建一个 Python 源文件,使用之前的代码进行测试:

import tensorflow as tfa = tf.constant(1.)b = tf.constant(2.)print(a+b)print(tf.test.gpu_device_name())print(‘GPU:’,tf.config.list_physical_devices(device_type=’GPU’))print(‘CPU:’,tf.config.list_physical_devices(device_type=’CPU’))print(tf.test.is_gpu_available())

终端的配置可以转至:PyTorch安装与配置教程(2022.11)。


比丘资源网 » TensorFlow安装与配置教程(2022.12)

发表回复

提供最优质的资源集合

立即查看 了解详情