diff --git a/01.fit_a_line/README.cn.md b/01.fit_a_line/README.cn.md index bb6c1a43bfe7a171e7a7a1d82ae4f872bfbe342a..d4b1b9412fe95a50c756bbdfab5259df3a195004 100644 --- a/01.fit_a_line/README.cn.md +++ b/01.fit_a_line/README.cn.md @@ -177,6 +177,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 @@ -184,7 +258,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) @@ -259,7 +332,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") diff --git a/01.fit_a_line/README.md b/01.fit_a_line/README.md index 574283453dab0a6c1e7fdd9b0e544c42e0aa2425..dd2fc04728beb11edfa23cac224781abb969696a 100644 --- a/01.fit_a_line/README.md +++ b/01.fit_a_line/README.md @@ -196,6 +196,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 @@ -203,7 +277,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) @@ -281,7 +354,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") diff --git a/01.fit_a_line/image/ranges.png b/01.fit_a_line/image/ranges.png index 5325df4800985983e17476f007658d1cdb170b1c..916337f0720ef221851e89456c5c295e2e13445f 100644 Binary files a/01.fit_a_line/image/ranges.png and b/01.fit_a_line/image/ranges.png differ diff --git a/01.fit_a_line/plot.py b/01.fit_a_line/plot.py new file mode 100644 index 0000000000000000000000000000000000000000..847f7fa697a6811a8ecc6a0105864dc0e4d0e435 --- /dev/null +++ b/01.fit_a_line/plot.py @@ -0,0 +1,83 @@ +# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved +# +# 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 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() diff --git a/01.fit_a_line/train.py b/01.fit_a_line/train.py index 8019ff390681889c41d1423a317743860599751e..7953eeeedbedab1e4d6488bb6044ffe485fb4f19 100644 --- a/01.fit_a_line/train.py +++ b/01.fit_a_line/train.py @@ -70,7 +70,7 @@ feed_order = ['x', 'y'] params_dirname = "fit_a_line.inference.model" # Plot data -from paddle.v2.plot import Ploter +from plot import Ploter train_title = "Train cost" test_title = "Test cost" @@ -125,7 +125,7 @@ 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") diff --git a/03.image_classification/README.cn.md b/03.image_classification/README.cn.md index 53129f305fa71d58c30f3be18bf632378bd3d5b1..bb89a2889ccd6681bfec8600caf3703d1c9b2f58 100644 --- a/03.image_classification/README.cn.md +++ b/03.image_classification/README.cn.md @@ -282,7 +282,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) diff --git a/03.image_classification/README.md b/03.image_classification/README.md index 74d2c82df3c9f7f479fac400d7555abf8ff4e6c7..82da3218144f1e0fcc2fe5bc15d6051b36ae014d 100644 --- a/03.image_classification/README.md +++ b/03.image_classification/README.md @@ -282,7 +282,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) diff --git a/03.image_classification/resnet.py b/03.image_classification/resnet.py index f6b039a652e344f0349755c2bc924167538028cc..b7d2f62b33a7bbc3c6ea6c1f0938147148294421 100644 --- a/03.image_classification/resnet.py +++ b/03.image_classification/resnet.py @@ -70,7 +70,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) diff --git a/03.image_classification/train.py b/03.image_classification/train.py index 52394ed8d0aa26923677f8264bff8d970122c13b..cfe48322e78dbc8c76bb70bf54077bd4eabe3a84 100644 --- a/03.image_classification/train.py +++ b/03.image_classification/train.py @@ -102,7 +102,7 @@ def infer(use_cuda, inference_program, params_dirname=None): inferencer = Inferencer( infer_func=inference_program, param_path=params_dirname, place=place) - # Prepare testing data. + # Prepare testing data. from PIL import Image import numpy as np import os diff --git a/04.word2vec/README.cn.md b/04.word2vec/README.cn.md index 5741ab5ae205cdcd066f5f9fa5fc4b3acdc6efe8..dce7424bc896c9dc649aaec6b19095530b188e20 100644 --- a/04.word2vec/README.cn.md +++ b/04.word2vec/README.cn.md @@ -208,6 +208,7 @@ import numpy from functools import partial import math import os +import six import sys from __future__ import print_function ``` @@ -394,7 +395,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]) ``` diff --git a/04.word2vec/README.md b/04.word2vec/README.md index eeb9992ffc815b98a6a6abbeadc6b0705391584a..3151b73ea1aa9fb42914babd1eff993005f27a98 100644 --- a/04.word2vec/README.md +++ b/04.word2vec/README.md @@ -221,6 +221,7 @@ import numpy from functools import partial import math import os +import six import sys from __future__ import print_function ``` @@ -412,7 +413,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]) ``` diff --git a/04.word2vec/train.py b/04.word2vec/train.py index c07b622f91fbcfb69f7cc3a92c69b5d044fff9f7..229d018480805025cff72609b324690c34d2e28a 100644 --- a/04.word2vec/train.py +++ b/04.word2vec/train.py @@ -12,8 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function -import paddle.v2 as paddle +import paddle as paddle import paddle.fluid as fluid +import six import sys try: @@ -176,7 +177,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]) diff --git a/06.understand_sentiment/README.cn.md b/06.understand_sentiment/README.cn.md index f90390a7be2b434f97e8fbcd8bb512d4584a8ebc..5614abeeed78d79865fc1a9de90429e92a52531c 100644 --- a/06.understand_sentiment/README.cn.md +++ b/06.understand_sentiment/README.cn.md @@ -274,7 +274,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) diff --git a/06.understand_sentiment/README.md b/06.understand_sentiment/README.md index a658415fcee61521ee6144a49b00ed4c192874e6..6bad7494717cb88ec619f76108560735af3822a3 100644 --- a/06.understand_sentiment/README.md +++ b/06.understand_sentiment/README.md @@ -281,7 +281,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) diff --git a/06.understand_sentiment/train_conv.py b/06.understand_sentiment/train_conv.py index b4251276cc4297c3dba28167e04a746c2746545c..eeef644f0b6d84cc619827bf031f2b7ccdacaf33 100644 --- a/06.understand_sentiment/train_conv.py +++ b/06.understand_sentiment/train_conv.py @@ -111,7 +111,7 @@ 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, map(np.array, event.metrics))) + event.step, event.epoch, list(map(np.array, event.metrics)))) elif isinstance(event, EndEpochEvent): trainer.save_params(params_dirname) @@ -133,14 +133,14 @@ def infer(use_cuda, inference_program, params_dirname=None): place=place) # Setup input by creating LoDTensor to represent sequence of words. - # Here each word is the basic element of the LoDTensor and the shape of - # each word (base_shape) should be [1] since it is simply an index to + # Here each word is the basic element of the LoDTensor and the shape of + # each word (base_shape) should be [1] since it is simply an index to # look up for the corresponding word vector. # Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], - # which has only one lod level. Then the created LoDTensor will have only - # one higher level structure (sequence of words, or sentence) than the basic - # element (word). Hence the LoDTensor will hold data for three sentences of - # length 3, 4 and 2, respectively. + # which has only one lod level. Then the created LoDTensor will have only + # one higher level structure (sequence of words, or sentence) than the basic + # element (word). Hence the LoDTensor will hold data for three sentences of + # length 3, 4 and 2, respectively. # Note that lod info should be a list of lists. reviews_str = [ diff --git a/06.understand_sentiment/train_dyn_rnn.py b/06.understand_sentiment/train_dyn_rnn.py index bea431b0b1f56cbd8d7257e9924a2852c13dc56e..090720af5293995ecdeadf78dfe7dc37f569e019 100644 --- a/06.understand_sentiment/train_dyn_rnn.py +++ b/06.understand_sentiment/train_dyn_rnn.py @@ -128,7 +128,7 @@ 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, map(np.array, event.metrics))) + event.step, event.epoch, list(map(np.array, event.metrics)))) elif isinstance(event, EndEpochEvent): trainer.save_params(params_dirname) @@ -150,14 +150,14 @@ def infer(use_cuda, inference_program, params_dirname=None): place=place) # Setup input by creating LoDTensor to represent sequence of words. - # Here each word is the basic element of the LoDTensor and the shape of - # each word (base_shape) should be [1] since it is simply an index to + # Here each word is the basic element of the LoDTensor and the shape of + # each word (base_shape) should be [1] since it is simply an index to # look up for the corresponding word vector. # Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], - # which has only one lod level. Then the created LoDTensor will have only - # one higher level structure (sequence of words, or sentence) than the basic - # element (word). Hence the LoDTensor will hold data for three sentences of - # length 3, 4 and 2, respectively. + # which has only one lod level. Then the created LoDTensor will have only + # one higher level structure (sequence of words, or sentence) than the basic + # element (word). Hence the LoDTensor will hold data for three sentences of + # length 3, 4 and 2, respectively. # Note that lod info should be a list of lists. reviews_str = [ diff --git a/06.understand_sentiment/train_stacked_lstm.py b/06.understand_sentiment/train_stacked_lstm.py index 11f67fb1401ec07a465ef536e2a1521f60299b26..b665c6ecc3eb8be393a63a9998c46abbc8904d66 100644 --- a/06.understand_sentiment/train_stacked_lstm.py +++ b/06.understand_sentiment/train_stacked_lstm.py @@ -119,7 +119,7 @@ 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, map(np.array, event.metrics))) + event.step, event.epoch, list(map(np.array, event.metrics)))) elif isinstance(event, EndEpochEvent): trainer.save_params(params_dirname) @@ -141,14 +141,14 @@ def infer(use_cuda, inference_program, params_dirname=None): place=place) # Setup input by creating LoDTensor to represent sequence of words. - # Here each word is the basic element of the LoDTensor and the shape of - # each word (base_shape) should be [1] since it is simply an index to + # Here each word is the basic element of the LoDTensor and the shape of + # each word (base_shape) should be [1] since it is simply an index to # look up for the corresponding word vector. # Suppose the length_based level of detail (lod) info is set to [[3, 4, 2]], - # which has only one lod level. Then the created LoDTensor will have only - # one higher level structure (sequence of words, or sentence) than the basic - # element (word). Hence the LoDTensor will hold data for three sentences of - # length 3, 4 and 2, respectively. + # which has only one lod level. Then the created LoDTensor will have only + # one higher level structure (sequence of words, or sentence) than the basic + # element (word). Hence the LoDTensor will hold data for three sentences of + # length 3, 4 and 2, respectively. # Note that lod info should be a list of lists. reviews_str = [ diff --git a/07.label_semantic_roles/README.cn.md b/07.label_semantic_roles/README.cn.md index 0891f5b6b16a1b715b44db6c47ba079adfcad4c5..34baef5a91918bfe4b391143d6920dc025373a94 100644 --- a/07.label_semantic_roles/README.cn.md +++ b/07.label_semantic_roles/README.cn.md @@ -184,8 +184,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' @@ -417,7 +418,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), diff --git a/07.label_semantic_roles/README.md b/07.label_semantic_roles/README.md index 623d035da5ec71185f1269185f1dce8f366d2985..0a6ce1c88906a0515b529c380e161cc2aba83698 100644 --- a/07.label_semantic_roles/README.md +++ b/07.label_semantic_roles/README.md @@ -207,8 +207,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' @@ -427,7 +428,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), diff --git a/07.label_semantic_roles/train.py b/07.label_semantic_roles/train.py index 780167a2cfa7b25d2d064efb700cac324cde3f6b..87c7f3906cfeb6d415f4649e638d12ebd5e6fe3d 100644 --- a/07.label_semantic_roles/train.py +++ b/07.label_semantic_roles/train.py @@ -3,8 +3,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' @@ -167,7 +168,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), fetch_list=[avg_cost])