Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
330f442d
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
330f442d
编写于
10月 03, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish code
上级
68f41a42
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
170 addition
and
17 deletion
+170
-17
01.fit_a_line/index.cn.html
01.fit_a_line/index.cn.html
+75
-2
01.fit_a_line/index.html
01.fit_a_line/index.html
+75
-2
03.image_classification/index.cn.html
03.image_classification/index.cn.html
+1
-1
03.image_classification/index.html
03.image_classification/index.html
+1
-1
04.word2vec/index.cn.html
04.word2vec/index.cn.html
+2
-1
04.word2vec/index.html
04.word2vec/index.html
+2
-1
06.understand_sentiment/index.cn.html
06.understand_sentiment/index.cn.html
+1
-1
06.understand_sentiment/index.html
06.understand_sentiment/index.html
+1
-1
06.understand_sentiment/train_conv.py
06.understand_sentiment/train_conv.py
+2
-1
06.understand_sentiment/train_dyn_rnn.py
06.understand_sentiment/train_dyn_rnn.py
+2
-1
06.understand_sentiment/train_stacked_lstm.py
06.understand_sentiment/train_stacked_lstm.py
+2
-1
07.label_semantic_roles/index.cn.html
07.label_semantic_roles/index.cn.html
+3
-2
07.label_semantic_roles/index.html
07.label_semantic_roles/index.html
+3
-2
未找到文件。
01.fit_a_line/index.cn.html
浏览文件 @
330f442d
...
...
@@ -219,6 +219,80 @@ PaddlePaddle提供了读取数据者发生器机制来读取训练数据。读
feed_order=['x', 'y']
```
以及一个绘画器来进行绘制:
```python
import six
import os
class PlotData(object):
def __init__(self):
self.step = []
self.value = []
def append(self, step, value):
self.step.append(step)
self.value.append(value)
def reset(self):
self.step = []
self.value = []
class Ploter(object):
def __init__(self, *args):
self.__args__ = args
self.__plot_data__ = {}
for title in args:
self.__plot_data__[title] = PlotData()
# demo in notebooks will use Ploter to plot figure, but when we convert
# the ipydb to py file for testing, the import of matplotlib will make the
# script crash. So we can use `export DISABLE_PLOT=True` to disable import
# these libs
self.__disable_plot__ = os.environ.get("DISABLE_PLOT")
if not self.__plot_is_disabled__():
import matplotlib.pyplot as plt
from IPython import display
self.plt = plt
self.display = display
def __plot_is_disabled__(self):
return self.__disable_plot__ == "True"
def append(self, title, step, value):
assert isinstance(title, six.string_types)
assert title in self.__plot_data__
data = self.__plot_data__[title]
assert isinstance(data, PlotData)
data.append(step, value)
def plot(self, path=None):
if self.__plot_is_disabled__():
return
titles = []
for title in self.__args__:
data = self.__plot_data__[title]
assert isinstance(data, PlotData)
if len(data.step) > 0:
titles.append(title)
self.plt.plot(data.step, data.value)
self.plt.legend(titles, loc='upper left')
if path is None:
self.display.clear_output(wait=True)
self.display.display(self.plt.gcf())
else:
self.plt.savefig(path)
self.plt.gcf().clear()
def reset(self):
for key in self.__plot_data__:
data = self.__plot_data__[key]
assert isinstance(data, PlotData)
data.reset()
```
除此之外,可以定义一个事件响应器来处理类似`打印训练进程`的事件:
```python
...
...
@@ -226,7 +300,6 @@ feed_order=['x', 'y']
params_dirname = "fit_a_line.inference.model"
# Plot data
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
...
...
@@ -301,7 +374,7 @@ inferencer = fluid.contrib.inferencer.Inferencer(
batch_size = 10
test_reader = paddle.batch(paddle.dataset.uci_housing.test(),batch_size=batch_size)
test_data =
test_reader().next(
)
test_data =
next(test_reader()
)
test_x = numpy.array([data[0] for data in test_data]).astype("float32")
test_y = numpy.array([data[1] for data in test_data]).astype("float32")
...
...
01.fit_a_line/index.html
浏览文件 @
330f442d
...
...
@@ -238,6 +238,80 @@ for loading the training data. A reader may return multiple columns, and we need
feed_order=['x', 'y']
```
And a ploter to plot metrics:
```python
import six
import os
class PlotData(object):
def __init__(self):
self.step = []
self.value = []
def append(self, step, value):
self.step.append(step)
self.value.append(value)
def reset(self):
self.step = []
self.value = []
class Ploter(object):
def __init__(self, *args):
self.__args__ = args
self.__plot_data__ = {}
for title in args:
self.__plot_data__[title] = PlotData()
# demo in notebooks will use Ploter to plot figure, but when we convert
# the ipydb to py file for testing, the import of matplotlib will make the
# script crash. So we can use `export DISABLE_PLOT=True` to disable import
# these libs
self.__disable_plot__ = os.environ.get("DISABLE_PLOT")
if not self.__plot_is_disabled__():
import matplotlib.pyplot as plt
from IPython import display
self.plt = plt
self.display = display
def __plot_is_disabled__(self):
return self.__disable_plot__ == "True"
def append(self, title, step, value):
assert isinstance(title, six.string_types)
assert title in self.__plot_data__
data = self.__plot_data__[title]
assert isinstance(data, PlotData)
data.append(step, value)
def plot(self, path=None):
if self.__plot_is_disabled__():
return
titles = []
for title in self.__args__:
data = self.__plot_data__[title]
assert isinstance(data, PlotData)
if len(data.step) > 0:
titles.append(title)
self.plt.plot(data.step, data.value)
self.plt.legend(titles, loc='upper left')
if path is None:
self.display.clear_output(wait=True)
self.display.display(self.plt.gcf())
else:
self.plt.savefig(path)
self.plt.gcf().clear()
def reset(self):
for key in self.__plot_data__:
data = self.__plot_data__[key]
assert isinstance(data, PlotData)
data.reset()
```
Moreover, an event handler is provided to print the training progress:
```python
...
...
@@ -245,7 +319,6 @@ Moreover, an event handler is provided to print the training progress:
params_dirname = "fit_a_line.inference.model"
# Plot data
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
...
...
@@ -323,7 +396,7 @@ inferencer = fluid.contrib.inferencer.Inferencer(
batch_size =
10
test_reader =
paddle.batch(paddle.dataset.uci_housing.test(),batch_size=batch_size)
test_data =
test_reader().next(
)
test_data =
next(test_reader()
)
test_x =
numpy.array([data[0]
for
data
in
test_data
]).
astype
("
float32
")
test_y =
numpy.array([data[1]
for
data
in
test_data
]).
astype
("
float32
")
...
...
03.image_classification/index.cn.html
浏览文件 @
330f442d
...
...
@@ -324,7 +324,7 @@ def layer_warp(block_func, input, ch_in, ch_out, count, stride):
def resnet_cifar10(ipt, depth=32):
# depth should be one of 20, 32, 44, 56, 110, 1202
assert (depth - 2) % 6 == 0
n = (depth - 2) / 6
n = (depth - 2) /
/
6
nStages = {16, 64, 128}
conv1 = conv_bn_layer(ipt, ch_out=16, filter_size=3, stride=1, padding=1)
res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
...
...
03.image_classification/index.html
浏览文件 @
330f442d
...
...
@@ -324,7 +324,7 @@ Note: besides the first convolutional layer and the last fully-connected layer,
def resnet_cifar10(ipt, depth=32):
# depth should be one of 20, 32, 44, 56, 110, 1202
assert (depth - 2) % 6 == 0
n = (depth - 2) / 6
n = (depth - 2) /
/
6
nStages = {16, 64, 128}
conv1 = conv_bn_layer(ipt, ch_out=16, filter_size=3, stride=1, padding=1)
res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
...
...
04.word2vec/index.cn.html
浏览文件 @
330f442d
...
...
@@ -250,6 +250,7 @@ import numpy
from functools import partial
import math
import os
import six
import sys
from __future__ import print_function
```
...
...
@@ -436,7 +437,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
most_possible_word_index = numpy.argmax(result[0])
print(most_possible_word_index)
print([
key for key, value in
word_dict.iteritems(
)
key for key, value in
six.iteritems(word_dict
)
if value == most_possible_word_index
][0])
```
...
...
04.word2vec/index.html
浏览文件 @
330f442d
...
...
@@ -263,6 +263,7 @@ import numpy
from functools import partial
import math
import os
import six
import sys
from __future__ import print_function
```
...
...
@@ -454,7 +455,7 @@ def infer(use_cuda, inference_program, params_dirname=None):
most_possible_word_index =
numpy.argmax(result[0])
print
(
most_possible_word_index
)
print
([
key
for
key
,
value
in
word_dict.iteritems
(
)
key
for
key
,
value
in
six.iteritems
(
word_dict
)
if
value =
=
most_possible_word_index
][0])
```
...
...
06.understand_sentiment/index.cn.html
浏览文件 @
330f442d
...
...
@@ -316,7 +316,7 @@ params_dirname = "understand_sentiment_conv.inference.model"
def event_handler(event):
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch,
map(np.array, event.metrics
)))
event.step, event.epoch,
list(map(np.array, event.metrics)
)))
if event.step == 10:
trainer.save_params(params_dirname)
...
...
06.understand_sentiment/index.html
浏览文件 @
330f442d
...
...
@@ -323,7 +323,7 @@ params_dirname = "understand_sentiment_conv.inference.model"
def event_handler(event):
if isinstance(event, fluid.contrib.trainer.EndStepEvent):
print("Step {0}, Epoch {1} Metrics {2}".format(
event.step, event.epoch,
map(np.array, event.metrics
)))
event.step, event.epoch,
list(map(np.array, event.metrics)
)))
if event.step == 10:
trainer.save_params(params_dirname)
...
...
06.understand_sentiment/train_conv.py
浏览文件 @
330f442d
...
...
@@ -111,7 +111,8 @@ def train(use_cuda, train_program, params_dirname):
event
.
step
,
avg_cost
,
acc
))
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
elif
isinstance
(
event
,
EndEpochEvent
):
trainer
.
save_params
(
params_dirname
)
...
...
06.understand_sentiment/train_dyn_rnn.py
浏览文件 @
330f442d
...
...
@@ -128,7 +128,8 @@ def train(use_cuda, train_program, params_dirname):
event
.
step
,
avg_cost
,
acc
))
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
elif
isinstance
(
event
,
EndEpochEvent
):
trainer
.
save_params
(
params_dirname
)
...
...
06.understand_sentiment/train_stacked_lstm.py
浏览文件 @
330f442d
...
...
@@ -119,7 +119,8 @@ def train(use_cuda, train_program, params_dirname):
event
.
step
,
avg_cost
,
acc
))
print
(
"Step {0}, Epoch {1} Metrics {2}"
.
format
(
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
event
.
step
,
event
.
epoch
,
list
(
map
(
np
.
array
,
event
.
metrics
))))
elif
isinstance
(
event
,
EndEpochEvent
):
trainer
.
save_params
(
params_dirname
)
...
...
07.label_semantic_roles/index.cn.html
浏览文件 @
330f442d
...
...
@@ -226,8 +226,9 @@ from __future__ import print_function
import math, os
import numpy as np
import paddle
import paddle.
v2.
dataset.conll05 as conll05
import paddle.dataset.conll05 as conll05
import paddle.fluid as fluid
import six
import time
with_gpu = os.getenv('WITH_GPU', '0') != '0'
...
...
@@ -459,7 +460,7 @@ def train(use_cuda, save_dirname=None, is_local=True):
start_time = time.time()
batch_id = 0
for pass_id in xrange(PASS_NUM):
for pass_id in
six.moves.
xrange(PASS_NUM):
for data in train_data():
cost = exe.run(main_program,
feed=feeder.feed(data),
...
...
07.label_semantic_roles/index.html
浏览文件 @
330f442d
...
...
@@ -249,8 +249,9 @@ from __future__ import print_function
import math, os
import numpy as np
import paddle
import paddle.
v2.
dataset.conll05 as conll05
import paddle.dataset.conll05 as conll05
import paddle.fluid as fluid
import six
import time
with_gpu = os.getenv('WITH_GPU', '0') != '0'
...
...
@@ -469,7 +470,7 @@ def train(use_cuda, save_dirname=None, is_local=True):
start_time = time.time()
batch_id = 0
for pass_id in xrange(PASS_NUM):
for pass_id in
six.moves.
xrange(PASS_NUM):
for data in train_data():
cost = exe.run(main_program,
feed=feeder.feed(data),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录