当前位置:   article > 正文

TensorFlow安装与配置教程(2022.12)_tensorflow配置

tensorflow配置

1. TensorFlow的安装

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

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

conda create -n TensorFlow python=3.9
  • 1

进入 TensorFlow 环境,安装 tensorflow

conda activate TensorFlow

conda install tensorflow  # 安装CPU版本
conda install tensorflow-gpu  # 安装GPU版本
  • 1
  • 2
  • 3
  • 4

本文安装的为 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.5
True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

3. TensorFlow v1的安装

上面安装的TensorFlow为最新版本(目前是v2),很多时候可能因为兼容问题需要用到v1版本,v1版本支持的Python版本为3.5、3.6、3.7,更高版本无法安装v1版的TensorFlow。首先创建环境:

conda create -n TensorFlow-v1 python=3.6
  • 1

安装v1版的TensorFlow:

conda activate TensorFlow-v1
conda install tensorflow==1.14.0
  • 1
  • 2
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/775299
推荐阅读
相关标签
  

闽ICP备14008679号