0


TensorFlow安装与配置教程(2022.12)

1. TensorFlow的安装

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

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

conda create -n TensorFlow python=3.9

进入 TensorFlow 环境,安装

tensorflow

conda activate TensorFlow

conda 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-1911:37:47.885107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0with6007 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-1911:39:14.914081: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /device:GPU:0with6007 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 tf

a = 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)。


本文转载自: https://blog.csdn.net/m0_51755720/article/details/128369492
版权归原作者 柃歌 所有, 如有侵权,请联系我们删除。

“TensorFlow安装与配置教程(2022.12)”的评论:

还没有评论