赞
踩












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)


import tensorflow as tf
x = tf.constant(1.)
a = tf.constant(2.)
b = tf.constant(3.)
c = tf.constant(4.)
with tf.GradientTape() as tape:
tape.watch([a, b, c])
y = a ** 2 * x + b * x + c
[dy_da, dy_db, dy_dc] = tape.gradient(y, [a, b, c])
print(dy_da, dy_db, dy_dc)








选择custom(自定义)

这里特别注意一下几点:1. 就是电脑本身的显卡驱动号;2. 就是自己的电脑有没有安装visual stuio;首先把Nvidia GeForce应用程序关掉,这个没什么用。

把cuda中的visual studio integration去掉,这里因为我们自己的电脑没有安装visual stuio如果安装了。如果我们自己的电脑装了visual stuio如果安装了可以不用去掉。

这里也要注意左边一列新的驱动版本和当前驱动版本;需要保证左边一列(display driver)的驱动版本号,大于等于当前版本驱动号,否则有可能出错。要是满足了,这一列可以去掉勾,不用安装了




通过anaconda创建虚拟环境 conda create -n tensorflow-2.0_python-3.7 python=3.7
然后进入刚刚创建好的环境 conda activate tensorflow-2.0_python-3.7

安装tensorflow

查看conda中Python的安装路径where ipython

TensorFlow测试

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
a = tf.constant(1.)
b = tf.constant(2.)
print(a + b)
print("GPU:", tf.test.is_gpu_available())
CSDN私信我!QQ邮箱:594042358@qq.com或者直接加我QQ: 594042358!Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。