1、命令式编程
import tensorflow as tf
# 1.创建输入张量
a = tf.constant(2.)
b = tf.constant(4.)
# 2.直接计算并打印
print('a+b=',a+b)
2、利用GPU加速计算
注:指定运行在CPU上还是GPU上。tensorflow中不同的GPU使用/gpu:0和/gpu:1区分,而CPU不区分设备号,统一使用 /cpu:0
tf.matmul()函数的使用(知识点:创建数组,数组相乘):
import tensorflow as tf
import numpy as np
a = [1,2,3]
b = np.array([a])
c = np.transpose(b)
d = tf.matmul(b,c)
print(a,b,c,d)
在/gpu:0上进行数组乘法计算:
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt
import os
import numpy as np
os.environ['TF.CPP.MIN.LOG.LEVEL'] = '2'
n = 100
with tf.device('/gpu:0'):
cpu_a = tf.random.normal([1,n])
cpu_b = tf.random.normal([n,1])
cpu_c = tf.matmul(cpu_a,cpu_b)
x = [i for i in range(9)] # for 循环产生数组
print(x,cpu_a,cpu_b,cpu_c)
cpu_a = np.transpose(cpu_a)
plt.plot(cpu_a,label='cpu_a')
plt.plot(cpu_b,label='cpu_b')
plt.legend()
plt.show()
cpu_b如下图所示:
测试一:
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt
import timeit
cpu_data = []
gpu_data = []
for n in range(9):
n = 9**n
# 创建在 CPU 上运算的 2 个矩阵
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([1, n]) # 符合正态分布、随机选取
cpu_b = tf.random.normal([n, 1])
print(cpu_a.device, cpu_b.device)
# 创建使用 GPU 运算的 2 个矩阵
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([1, n])
gpu_b = tf.random.normal([n, 1])
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
# 第一次计算需要热身,避免将初始化阶段时间结算在内
cpu_time = timeit.timeit(cpu_run, number=10) # 测量运算时间 number为测试次数
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)
# 正式计算 10 次,取平均时间
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)
cpu_data.append(cpu_time/10) # 在数组后加上相应的元素
gpu_data.append(gpu_time/10)
del cpu_a,cpu_b,gpu_a,gpu_b # 删除对应的变量而不是值
print(cpu_data)
print(gpu_data)
x = [9**i for i in range(9)]
cpu_data = [1000*i for i in cpu_data]
gpu_data = [1000*i for i in gpu_data]
plt.plot(x, cpu_data, 'C1')
plt.plot(x, cpu_data, color='C1', marker='s', label='CPU')
plt.plot(x, gpu_data,'C0')
plt.plot(x, gpu_data, color='C0', marker='^', label='GPU')
plt.gca().set_xscale('log')
plt.gca().set_yscale('log')
#plt.ylim([0,100])
plt.xlabel('矩阵大小n:(1xn)@(nx1)')
plt.ylabel('运算时间(ms)')
plt.legend()
plt.show()
对测试一进行一定的修改:
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt
import timeit
cpu_data = []
gpu_data = []
for n in range(9):
n = 9**n
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([1, n]) # 符合正态分布、随机选取
cpu_b = tf.random.normal([n, 1])
c = tf.matmul(cpu_a, cpu_b) # 矩阵相乘
return c
def gpu_run():
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([1, n])
gpu_b = tf.random.normal([n, 1])
c = tf.matmul(gpu_a, gpu_b)
return c
# 第一次计算需要热身,避免将初始化阶段时间结算在内
cpu_time = timeit.timeit(cpu_run, number=10) # 测量运算时间 number为测试次数
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)
# 正式计算 10 次,取平均时间
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)
cpu_data.append(cpu_time/10) # 在数组后加上相应的元素
gpu_data.append(gpu_time/10)
#del cpu_a,cpu_b,gpu_a,gpu_b # 删除对应的变量而不是值
print(cpu_data)
print(gpu_data)
x = [9**i for i in range(9)]
cpu_data = [1000*i for i in cpu_data]
gpu_data = [1000*i for i in gpu_data]
plt.plot(x, cpu_data, 'C1')
plt.plot(x, cpu_data, color='C1', marker='s', label='CPU')
plt.plot(x, gpu_data,'C0')
plt.plot(x, gpu_data, color='C0', marker='^', label='GPU')
plt.gca().set_xscale('log')
plt.gca().set_yscale('log')
#plt.ylim([0,100])
plt.xlabel('矩阵大小n:(1xn)@(nx1)')
plt.ylabel('运算时间(ms)')
plt.legend()
plt.show()
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt
import os
os.environ['TF.CPP.MIN.LOG.LEVEL'] = '2'
n = 100
with tf.device('/gpu:0'):
cpu_a = tf.random.normal([1,n])
cpu_b = tf.random.normal([n,1])
cpu_c = tf.matmul(cpu_a,cpu_b)
x = [i for i in range(9)] # for 循环产生数组
print(x,cpu_a,cpu_b,cpu_c)
# plt.plot(cpu_a,label='cpu_a')
plt.plot(cpu_b,label='cpu_a')
plt.show()
import tensorflow as tf
# 创建 4 个张量
a = tf.constant(1.)
b = tf.constant(2.)
c = tf.constant(3.)
w = tf.constant(4.)
with tf.GradientTape() as tape:# 构建梯度环境
tape.watch([w]) # 将 w 加入梯度跟踪列表
# 构建计算过程
y = a * w**2 + b * w + c
# 求导
[dy_dw] = tape.gradient(y, [w])
print(dy_dw) # 打印出导数
print()aprint(a)
输出结果: