1.背景

最近学习深度学习,需要安装TensorFlow的环境,这里总结记录一下.

2.安装过程:

(1)我这里介绍Anaconda下安装TensorFlow,所以前提是:你已经安装好了Anaconda.还没安装Anaconda的小伙伴也不要慌,先看看我这篇博客,先把Anaconda安装好.
链接:手把手教你win10下安装Anaconda
(2)win + R,然后输入cmd回车,进入Windows下的终端
在这里插入图片描述
(3)激活Anaconda
输入命令如下

activate

在这里插入图片描述
(4)创建新环境,这里我的环境名称为tf,Python版本为3.7
命令如下:

conda create -n tf python==3.7

在这里插入图片描述
(5)输入y,然后回车,继续创建
在这里插入图片描述
(6)新环境创建成功
在这里插入图片描述
(7)在base环境下激活新环境tf
命令如下:

conda activate tf

在这里插入图片描述
(8)使用pip命令安装TensorFlow,我这里安装的是使用GPU进行训练, 可以指定版本,这个大家可以按自己需求进行选择
命令如下:

pip install tensorflow_gpu==2.1.0

在这里插入图片描述
(9)将pycharm中解释器切换成新环境tf
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(10)在pycharm中输入以下代码,进行测试.验证TensorFlow是否安装成功.

import tensorflow as tf
import timeit


with tf.device('/cpu:0'):
	cpu_a = tf.random.normal([10000, 1000])
	cpu_b = tf.random.normal([1000, 2000])
	print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):
	gpu_a = tf.random.normal([10000, 1000])
	gpu_b = tf.random.normal([1000, 2000])
	print(gpu_a.device, gpu_b.device)

def cpu_run():
	with tf.device('/cpu:0'):
		c = tf.matmul(cpu_a, cpu_b)
	return c

def gpu_run():
	with tf.device('/gpu:0'):
		c = tf.matmul(gpu_a, gpu_b)
	return c


# warm up
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)


cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)

(11)小插曲,如果控制台报如下错误:
在这里插入图片描述
解决方法:
按照第一个报错提示,降低protobuf版本即可
在Windows终端里面输入如下命令:

pip install protobuf==3.19.0

在这里插入图片描述
(12)再次运行测试程序,控制台如果显示如下,则证明TensorFlow的GPU训练版安装成功!!!
在这里插入图片描述

附录:

参考来自B站up主:有幸遇见的上上

GitHub 加速计划 / te / tensorflow
184.54 K
74.12 K
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:1 个月前 )
a49e66f2 PiperOrigin-RevId: 663726708 2 个月前
91dac11a This test overrides disabled_backends, dropping the default value in the process. PiperOrigin-RevId: 663711155 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐