Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Deep-Learning-with-TensorFlow-book
提交
1a6f594a
D
Deep-Learning-with-TensorFlow-book
项目概览
OpenDocCN
/
Deep-Learning-with-TensorFlow-book
通知
4
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Deep-Learning-with-TensorFlow-book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1a6f594a
编写于
11月 08, 2019
作者:
John(°_°)…
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add ch1
上级
1fb6bef8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
120 addition
and
0 deletion
+120
-0
ch1/autograd.py
ch1/autograd.py
+17
-0
ch1/gpu_accelerate.py
ch1/gpu_accelerate.py
+72
-0
ch1/tf1.py
ch1/tf1.py
+20
-0
ch1/tf2.py
ch1/tf2.py
+11
-0
未找到文件。
ch1/autograd.py
0 → 100644
浏览文件 @
1a6f594a
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
)
ch1/gpu_accelerate.py
0 → 100644
浏览文件 @
1a6f594a
import
numpy
as
np
import
matplotlib
from
matplotlib
import
pyplot
as
plt
# Default parameters for plots
matplotlib
.
rcParams
[
'font.size'
]
=
20
matplotlib
.
rcParams
[
'figure.titlesize'
]
=
20
matplotlib
.
rcParams
[
'figure.figsize'
]
=
[
9
,
7
]
matplotlib
.
rcParams
[
'font.family'
]
=
[
'STKaiti'
]
matplotlib
.
rcParams
[
'axes.unicode_minus'
]
=
False
import
tensorflow
as
tf
import
timeit
cpu_data
=
[]
gpu_data
=
[]
for
n
in
range
(
9
):
n
=
10
**
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
)
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
x
=
[
10
**
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
.
savefig
(
'gpu-time.svg'
)
\ No newline at end of file
ch1/tf1.py
0 → 100644
浏览文件 @
1a6f594a
import
tensorflow
as
tf
assert
tf
.
__version__
.
startswith
(
'1.'
)
# 1.创建计算图阶段
# 创建2个输入端子,指定类型和名字
a_ph
=
tf
.
placeholder
(
tf
.
float32
,
name
=
'variable_a'
)
b_ph
=
tf
.
placeholder
(
tf
.
float32
,
name
=
'variable_b'
)
# 创建输出端子的运算操作,并命名
c_op
=
tf
.
add
(
a_ph
,
b_ph
,
name
=
'variable_c'
)
# 2.运行计算图阶段
# 创建运行环境
sess
=
tf
.
InteractiveSession
()
# 初始化操作也需要作为操作运行
init
=
tf
.
global_variables_initializer
()
sess
.
run
(
init
)
# 运行初始化操作,完成初始化
# 运行输出端子,需要给输入端子赋值
c_numpy
=
sess
.
run
(
c_op
,
feed_dict
=
{
a_ph
:
2.
,
b_ph
:
4.
})
# 运算完输出端子才能得到数值类型的c_numpy
print
(
'a+b='
,
c_numpy
)
\ No newline at end of file
ch1/tf2.py
0 → 100644
浏览文件 @
1a6f594a
#%%
import
tensorflow
as
tf
assert
tf
.
__version__
.
startswith
(
'2.'
)
# 1.创建输入张量
a
=
tf
.
constant
(
2.
)
b
=
tf
.
constant
(
4.
)
# 2.直接计算并打印
print
(
'a+b='
,
a
+
b
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录