Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
VisualDL
提交
20045033
V
VisualDL
项目概览
PaddlePaddle
/
VisualDL
接近 2 年 前同步成功
通知
89
Star
4655
Fork
642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
10
列表
看板
标记
里程碑
合并请求
2
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
V
VisualDL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
10
Issue
10
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
20045033
编写于
12月 17, 2018
作者:
W
wuzewu
提交者:
GitHub
12月 17, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add paddlefluid demo about using visualdl (#519)
上级
51f16e2e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
118 addition
and
0 deletion
+118
-0
demo/paddle/paddlefluid_mnist.py
demo/paddle/paddlefluid_mnist.py
+118
-0
未找到文件。
demo/paddle/paddlefluid_mnist.py
0 → 100644
浏览文件 @
20045033
# Copyright (c) 2017 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
import
paddle
import
paddle.fluid
as
fluid
from
visualdl
import
LogWriter
# define a LeNet-5 nn
def
lenet_5
(
img
,
label
):
conv1
=
fluid
.
nets
.
simple_img_conv_pool
(
input
=
img
,
filter_size
=
5
,
num_filters
=
20
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
"relu"
)
conv1_bn
=
fluid
.
layers
.
batch_norm
(
input
=
conv1
)
conv2
=
fluid
.
nets
.
simple_img_conv_pool
(
input
=
conv1_bn
,
filter_size
=
5
,
num_filters
=
50
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
"relu"
)
predition
=
fluid
.
layers
.
fc
(
input
=
conv2
,
size
=
10
,
act
=
"softmax"
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predition
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
acc
=
fluid
.
layers
.
accuracy
(
input
=
predition
,
label
=
label
)
return
avg_cost
,
acc
# train the nn
def
train
():
img
=
fluid
.
layers
.
data
(
name
=
"img"
,
shape
=
[
1
,
28
,
28
],
dtype
=
"float32"
)
label
=
fluid
.
layers
.
data
(
name
=
"label"
,
shape
=
[
1
],
dtype
=
"int64"
)
avg_cost
,
acc
=
lenet_5
(
img
,
label
)
# get the mnist dataset
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
64
)
# define the loss
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
)
optimizer
.
minimize
(
avg_cost
)
# running on cpu
place
=
fluid
.
CPUPlace
()
feeder
=
fluid
.
DataFeeder
(
feed_list
=
[
img
,
label
],
place
=
place
)
exe
=
fluid
.
Executor
(
place
)
log_writter
=
LogWriter
(
"./vdl_log"
,
sync_cycle
=
10
)
with
log_writter
.
mode
(
"train"
)
as
logger
:
scalar_loss
=
logger
.
scalar
(
tag
=
"loss"
)
scalar_accuracy
=
logger
.
scalar
(
tag
=
"accuracy"
)
num_samples
=
10
image_input
=
logger
.
image
(
tag
=
"input"
,
num_samples
=
num_samples
)
histogram
=
logger
.
histogram
(
tag
=
"histogram"
,
num_buckets
=
50
)
# init all param
exe
.
run
(
fluid
.
default_startup_program
())
step
=
0
sample_num
=
0
epochs
=
5
param_name
=
fluid
.
default_startup_program
().
global_block
().
all_parameters
(
)[
0
].
name
# start to train
for
i
in
range
(
epochs
):
for
batch
in
train_reader
():
cost
,
accuracy
,
input
,
param
=
exe
.
run
(
feed
=
feeder
.
feed
(
batch
),
fetch_list
=
[
avg_cost
.
name
,
acc
.
name
,
img
.
name
,
param_name
])
step
+=
1
# record the loss and accuracy
scalar_loss
.
add_record
(
step
,
cost
)
scalar_accuracy
.
add_record
(
step
,
accuracy
)
if
sample_num
%
num_samples
==
0
:
image_input
.
start_sampling
()
idx
=
image_input
.
is_sample_taken
()
if
idx
!=
-
1
:
# the first image in the batch data
image_data
=
input
[
0
]
# the image shape recrod in VDL is H * W * C
image_data
=
image_data
.
reshape
([
28
,
28
,
1
])
image_input
.
set_sample
(
idx
,
image_data
.
shape
,
100
*
image_data
.
flatten
())
sample_num
+=
1
if
sample_num
%
num_samples
==
0
:
image_input
.
finish_sampling
()
sample_num
=
0
# record the parameter trend
histogram
.
add_record
(
step
,
param
.
flatten
())
if
__name__
==
"__main__"
:
train
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录