提交 b571a414 编写于 作者: Q qijun

Merge remote-tracking branch 'baidu/develop' into feature/add_v2_api_doc

...@@ -126,51 +126,57 @@ def seqToseq_net(source_dict_dim, target_dict_dim, is_generating=False): ...@@ -126,51 +126,57 @@ def seqToseq_net(source_dict_dim, target_dict_dim, is_generating=False):
def main(): def main():
paddle.init(use_gpu=False, trainer_count=1) paddle.init(use_gpu=False, trainer_count=1)
is_generating = True
# source and target dict dim. # source and target dict dim.
dict_size = 30000 dict_size = 30000
source_dict_dim = target_dict_dim = dict_size source_dict_dim = target_dict_dim = dict_size
# define network topology # train the network
cost = seqToseq_net(source_dict_dim, target_dict_dim) if not is_generating:
parameters = paddle.parameters.create(cost) cost = seqToseq_net(source_dict_dim, target_dict_dim)
parameters = paddle.parameters.create(cost)
# define optimize method and trainer
optimizer = paddle.optimizer.Adam( # define optimize method and trainer
learning_rate=5e-5, optimizer = paddle.optimizer.Adam(
regularization=paddle.optimizer.L2Regularization(rate=1e-3)) learning_rate=5e-5,
trainer = paddle.trainer.SGD(cost=cost, regularization=paddle.optimizer.L2Regularization(rate=8e-4))
parameters=parameters, trainer = paddle.trainer.SGD(cost=cost,
update_equation=optimizer) parameters=parameters,
update_equation=optimizer)
# define data reader # define data reader
feeding = { wmt14_reader = paddle.batch(
'source_language_word': 0, paddle.reader.shuffle(
'target_language_word': 1, paddle.dataset.wmt14.train(dict_size), buf_size=8192),
'target_language_next_word': 2 batch_size=5)
}
# define event_handler callback
wmt14_reader = paddle.batch( def event_handler(event):
paddle.reader.shuffle( if isinstance(event, paddle.event.EndIteration):
paddle.dataset.wmt14.train(dict_size=dict_size), buf_size=8192), if event.batch_id % 10 == 0:
batch_size=5) print "\nPass %d, Batch %d, Cost %f, %s" % (
event.pass_id, event.batch_id, event.cost,
# define event_handler callback event.metrics)
def event_handler(event): else:
if isinstance(event, paddle.event.EndIteration): sys.stdout.write('.')
if event.batch_id % 10 == 0: sys.stdout.flush()
print "\nPass %d, Batch %d, Cost %f, %s" % (
event.pass_id, event.batch_id, event.cost, event.metrics) # start to train
else: trainer.train(
sys.stdout.write('.') reader=wmt14_reader, event_handler=event_handler, num_passes=2)
sys.stdout.flush()
# generate a english sequence to french
# start to train else:
trainer.train( gen_creator = paddle.dataset.wmt14.test(dict_size)
reader=wmt14_reader, gen_data = []
event_handler=event_handler, for item in gen_creator():
num_passes=10000, gen_data.append((item[0], ))
feeding=feeding) if len(gen_data) == 3:
break
beam_gen = seqToseq_net(source_dict_dim, target_dict_dim, is_generating)
parameters = paddle.dataset.wmt14.model()
trg_dict = paddle.dataset.wmt14.trg_dict(dict_size)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -25,6 +25,11 @@ namespace paddle { ...@@ -25,6 +25,11 @@ namespace paddle {
* Input: a sequence * Input: a sequence
* If SequenceLevel = kNonseq: * If SequenceLevel = kNonseq:
* Output: a sequence containing only the last instance of the input sequence * Output: a sequence containing only the last instance of the input sequence
* If stride_ > 0:
* Output: a shorten sequence. The operation of getting last instance of a
* sequence is independently performed on every slice of the input
* sequence, which is obtained by sliding a window with the window
* size set to stride_.
* If SequenceLevel = kSeq: * If SequenceLevel = kSeq:
* Check input sequence must has sub-sequence * Check input sequence must has sub-sequence
* Output: a sequence containing only the last instance of each sub-sequence * Output: a sequence containing only the last instance of each sub-sequence
...@@ -37,6 +42,7 @@ class SequenceLastInstanceLayer : public SequencePoolLayer { ...@@ -37,6 +42,7 @@ class SequenceLastInstanceLayer : public SequencePoolLayer {
protected: protected:
MatrixPtr tmpSrc_; MatrixPtr tmpSrc_;
MatrixPtr tmpDest_; MatrixPtr tmpDest_;
std::vector<int> instanceIds_;
public: public:
explicit SequenceLastInstanceLayer(const LayerConfig& config) explicit SequenceLastInstanceLayer(const LayerConfig& config)
...@@ -54,6 +60,7 @@ REGISTER_LAYER(seqlastins, SequenceLastInstanceLayer); ...@@ -54,6 +60,7 @@ REGISTER_LAYER(seqlastins, SequenceLastInstanceLayer);
bool SequenceLastInstanceLayer::init(const LayerMap& layerMap, bool SequenceLastInstanceLayer::init(const LayerMap& layerMap,
const ParameterMap& parameterMap) { const ParameterMap& parameterMap) {
SequencePoolLayer::init(layerMap, parameterMap); SequencePoolLayer::init(layerMap, parameterMap);
reversed_ = config_.select_first();
tmpSrc_ = tmpSrc_ =
Matrix::create(nullptr, /* height= */ 1, 1, /* trans= */ false, useGpu_); Matrix::create(nullptr, /* height= */ 1, 1, /* trans= */ false, useGpu_);
...@@ -66,7 +73,8 @@ bool SequenceLastInstanceLayer::init(const LayerMap& layerMap, ...@@ -66,7 +73,8 @@ bool SequenceLastInstanceLayer::init(const LayerMap& layerMap,
void SequenceLastInstanceLayer::forward(PassType passType) { void SequenceLastInstanceLayer::forward(PassType passType) {
SequencePoolLayer::forward(passType); SequencePoolLayer::forward(passType);
const int* starts = startPositions_->getData(false); auto starts = (stride_ > 0) ? stridePositions_->getData()
: startPositions_->getData(false);
MatrixPtr inputValue = getInputValue(0); MatrixPtr inputValue = getInputValue(0);
MatrixPtr outputValue = getOutputValue(); MatrixPtr outputValue = getOutputValue();
...@@ -74,9 +82,10 @@ void SequenceLastInstanceLayer::forward(PassType passType) { ...@@ -74,9 +82,10 @@ void SequenceLastInstanceLayer::forward(PassType passType) {
AsyncGpuBlock asyncGpuBlock; AsyncGpuBlock asyncGpuBlock;
REGISTER_TIMER_INFO("SequenceLastInstanceLayerForward", getName().c_str()); REGISTER_TIMER_INFO("SequenceLastInstanceLayerForward", getName().c_str());
instanceIds_.clear();
for (size_t seqId = 0; seqId < newBatchSize_; ++seqId) { for (size_t seqId = 0; seqId < newBatchSize_; ++seqId) {
int insId = int insId = reversed_ ? starts[seqId] : starts[seqId + 1] - 1;
config_.select_first() ? starts[seqId] : starts[seqId + 1] - 1; instanceIds_.push_back(insId);
outputValue->subMatrix(seqId, 1, tmpDest_) outputValue->subMatrix(seqId, 1, tmpDest_)
->assign(*(inputValue->subMatrix(insId, 1, tmpSrc_))); ->assign(*(inputValue->subMatrix(insId, 1, tmpSrc_)));
...@@ -96,18 +105,13 @@ void SequenceLastInstanceLayer::backward(const UpdateCallback& callback) { ...@@ -96,18 +105,13 @@ void SequenceLastInstanceLayer::backward(const UpdateCallback& callback) {
MatrixPtr inputGrad = getInputGrad(0); MatrixPtr inputGrad = getInputGrad(0);
MatrixPtr outputGrad = getOutputGrad(); MatrixPtr outputGrad = getOutputGrad();
const int* starts = startPositions_->getData(false);
size_t numSequences = startPositions_->getSize() - 1;
if (inputGrad) { if (inputGrad) {
AsyncGpuBlock asyncGpuBlock; AsyncGpuBlock asyncGpuBlock;
REGISTER_TIMER_INFO("SequenceLastInstanceLayerBackward", getName().c_str()); REGISTER_TIMER_INFO("SequenceLastInstanceLayerBackward", getName().c_str());
for (size_t seqId = 0; seqId < numSequences; ++seqId) { for (size_t seqId = 0; seqId < newBatchSize_; ++seqId) {
int insId = inputGrad->subMatrix(instanceIds_[seqId], 1, tmpDest_)
config_.select_first() ? starts[seqId] : starts[seqId + 1] - 1;
inputGrad->subMatrix(insId, 1, tmpDest_)
->add(*(outputGrad->subMatrix(seqId, 1, tmpSrc_))); ->add(*(outputGrad->subMatrix(seqId, 1, tmpSrc_)));
} }
} }
......
...@@ -37,6 +37,7 @@ bool SequencePoolLayer::init(const LayerMap& layerMap, ...@@ -37,6 +37,7 @@ bool SequencePoolLayer::init(const LayerMap& layerMap,
} else { } else {
LOG(FATAL) << "Unknown trans_type: " << config_.trans_type(); LOG(FATAL) << "Unknown trans_type: " << config_.trans_type();
} }
stride_ = config_.seq_pool_stride();
setNeedSequenceInfo(false); setNeedSequenceInfo(false);
return true; return true;
} }
...@@ -55,8 +56,6 @@ void SequencePoolLayer::forward(PassType passType) { ...@@ -55,8 +56,6 @@ void SequencePoolLayer::forward(PassType passType) {
CHECK_EQ(starts->getData()[newBatchSize_], input.getBatchSize()); CHECK_EQ(starts->getData()[newBatchSize_], input.getBatchSize());
CHECK_EQ(newBatchSize_, starts->getSize() - 1); CHECK_EQ(newBatchSize_, starts->getSize() - 1);
resetOutput(newBatchSize_, dim);
/* If type_ = kNonSeq, both seq has or not has sub-seq degrade to a non-seq, /* If type_ = kNonSeq, both seq has or not has sub-seq degrade to a non-seq,
* thus, in this case, output_ has no sequenceStartPositions. * thus, in this case, output_ has no sequenceStartPositions.
* If type_ = kSeq, seq has sub-seq degrades to a seq, thus, only in this * If type_ = kSeq, seq has sub-seq degrades to a seq, thus, only in this
...@@ -67,6 +66,15 @@ void SequencePoolLayer::forward(PassType passType) { ...@@ -67,6 +66,15 @@ void SequencePoolLayer::forward(PassType passType) {
<< "when trans_type = seq, input must hasSubseq"; << "when trans_type = seq, input must hasSubseq";
output_.degradeSequence(input); output_.degradeSequence(input);
} }
if (stride_ > 0) {
CHECK_EQ(input.hasSubseq(), 0UL)
<< "sequence stride pooling is invalid for hasSubseq now";
output_.poolSequenceWithStride(
input, stride_, &stridePositions_, reversed_);
newBatchSize_ = stridePositions_->getSize() - 1;
}
resetOutput(newBatchSize_, dim);
} }
void SequencePoolLayer::backward(const UpdateCallback& callback) { void SequencePoolLayer::backward(const UpdateCallback& callback) {
......
...@@ -26,6 +26,10 @@ namespace paddle { ...@@ -26,6 +26,10 @@ namespace paddle {
* Output: output size is the number of input sequences (NOT input instances) * Output: output size is the number of input sequences (NOT input instances)
* output[i] = seqlastin/average/max_{for each instance in this * output[i] = seqlastin/average/max_{for each instance in this
* sequence}{input[i]} * sequence}{input[i]}
* If stride_ > 0:
* Check input sequence must not have sub-sequence
* Output: a shorten sequence, pooling is performed upon a small local
* area
* If SequenceLevel = kSeq: * If SequenceLevel = kSeq:
* Check input sequence must has sub-sequence * Check input sequence must has sub-sequence
* Output: output size is the number of input sub-sequences * Output: output size is the number of input sub-sequences
...@@ -42,6 +46,11 @@ protected: ...@@ -42,6 +46,11 @@ protected:
enum SequenceLevel { kNonSeq = 0, kSeq = 1 }; enum SequenceLevel { kNonSeq = 0, kSeq = 1 };
size_t newBatchSize_; size_t newBatchSize_;
ICpuGpuVectorPtr startPositions_; ICpuGpuVectorPtr startPositions_;
int stride_;
// Store the start position of each window.
IVectorPtr stridePositions_;
// Whether the input sequence is reversed or not.
bool reversed_ = false;
public: public:
explicit SequencePoolLayer(const LayerConfig& config) : Layer(config) {} explicit SequencePoolLayer(const LayerConfig& config) : Layer(config) {}
......
...@@ -804,10 +804,14 @@ TEST(Layer, ExpandLayer) { ...@@ -804,10 +804,14 @@ TEST(Layer, ExpandLayer) {
testExpandLayer("seq", true); // seq expand to hasSubseq testExpandLayer("seq", true); // seq expand to hasSubseq
} }
void testDegradeLayer(bool hasSubseq, string layer_type, string trans_type) { void testDegradeLayer(bool hasSubseq,
string layer_type,
string trans_type,
int stride) {
TestConfig config; TestConfig config;
config.layerConfig.set_type(layer_type); config.layerConfig.set_type(layer_type);
config.layerConfig.set_size(10); config.layerConfig.set_size(10);
config.layerConfig.set_seq_pool_stride(stride);
config.biasSize = 0; config.biasSize = 0;
config.inputDefs.push_back( config.inputDefs.push_back(
...@@ -827,36 +831,46 @@ void testDegradeLayer(bool hasSubseq, string layer_type, string trans_type) { ...@@ -827,36 +831,46 @@ void testDegradeLayer(bool hasSubseq, string layer_type, string trans_type) {
if (layer_type == "average") { if (layer_type == "average") {
for (auto strategy : {"average", "sum", "squarerootn"}) { for (auto strategy : {"average", "sum", "squarerootn"}) {
LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
<< " average_strategy=" << strategy; << " average_strategy=" << strategy
<< " seq_pool_stride=" << stride;
config.layerConfig.set_average_strategy(strategy); config.layerConfig.set_average_strategy(strategy);
testDegradeLayerGrad(config, layer_type); testDegradeLayerGrad(config, layer_type);
} }
} else { } else {
LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type; LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
<< " seq_pool_stride=" << stride;
testDegradeLayerGrad(config, layer_type); testDegradeLayerGrad(config, layer_type);
} }
} }
TEST(Layer, MaxLayer) { TEST(Layer, MaxLayer) {
testDegradeLayer(false, "max", "non-seq"); // seq max to non-seq testDegradeLayer(false, "max", "non-seq", -1); // seq max to non-seq
testDegradeLayer(true, "max", "non-seq"); // hasSubseq max to non-seq testDegradeLayer(true, "max", "non-seq", -1); // hasSubseq max to non-seq
testDegradeLayer(true, "max", "seq"); // hasSubseq max to seq testDegradeLayer(true, "max", "seq", -1); // hasSubseq max to seq
} }
TEST(Layer, SequenceLastInstanceLayer) { TEST(Layer, SequenceLastInstanceLayer) {
testDegradeLayer(false, testDegradeLayer(false,
"seqlastins", "seqlastins",
"non-seq"); // seq seqlastins to non-seq "non-seq",
-1); // seq seqlastins to non-seq
testDegradeLayer(false,
"seqlastins",
"non-seq",
5); // seq seqlastins to a shorten seq, stride window = 5
testDegradeLayer(true, testDegradeLayer(true,
"seqlastins", "seqlastins",
"non-seq"); // hasSubseq seqlastins to non-seq "non-seq",
testDegradeLayer(true, "seqlastins", "seq"); // hasSubseq seqlastins to seq -1); // hasSubseq seqlastins to non-seq
testDegradeLayer(
true, "seqlastins", "seq", -1); // hasSubseq seqlastins to seq
} }
TEST(Layer, AverageLayer) { TEST(Layer, AverageLayer) {
testDegradeLayer(false, "average", "non-seq"); // seq average to non-seq testDegradeLayer(false, "average", "non-seq", -1); // seq average to non-seq
testDegradeLayer(true, "average", "non-seq"); // hasSubseq average to non-seq testDegradeLayer(
testDegradeLayer(true, "average", "seq"); // hasSubseq average to seq true, "average", "non-seq", -1); // hasSubseq average to non-seq
testDegradeLayer(true, "average", "seq", -1); // hasSubseq average to seq
} }
TEST(Layer, SequenceConcatLayer) { TEST(Layer, SequenceConcatLayer) {
......
...@@ -559,6 +559,49 @@ void Argument::degradeSequence(const Argument& input) { ...@@ -559,6 +559,49 @@ void Argument::degradeSequence(const Argument& input) {
tgtBuf[numSequences] = numSubSequences; tgtBuf[numSequences] = numSubSequences;
} }
void Argument::poolSequenceWithStride(const Argument& input,
size_t stride,
IVectorPtr* stridePostions,
bool reversed) {
// If input.sequenceStartPositions = [0, 9, 14, 17, 30] and stride = 5,
// then sequenceStartPositions = [0, 2, 3, 4, 7].
// If reversed = false, stridePostions = [0, 5, 9, 14, 17, 22, 27, 30];
// else reversed = true, stridePostions = [0, 4, 9, 14, 17, 20, 25, 30]
CHECK(input.sequenceStartPositions);
CHECK_EQ(input.hasSubseq(), 0UL);
CHECK_GT(stride, 0) << "stride must larger than 0";
size_t numSequences = input.getNumSequences();
ICpuGpuVector::resizeOrCreate(
sequenceStartPositions, numSequences + 1, false);
const int* starts = input.sequenceStartPositions->getData(false);
int* tgtBuf = sequenceStartPositions->getMutableData(false);
// first index of target sequence and stride positions are both 0
tgtBuf[0] = 0;
std::vector<int> stridePos;
for (size_t seqId = 0; seqId < numSequences; ++seqId) {
size_t seqLength = starts[seqId + 1] - starts[seqId];
stridePos.emplace_back(starts[seqId]);
if (seqLength == 0) {
// empty sequence
tgtBuf[seqId + 1] = tgtBuf[seqId];
} else {
int size = ceil((float)seqLength / stride);
tgtBuf[seqId + 1] = tgtBuf[seqId] + size;
for (int i = 0; i < size - 1; ++i) {
int cur = reversed ? starts[seqId + 1] - (size - 1 - i) * stride
: stridePos.back() + stride;
stridePos.emplace_back(cur);
}
}
}
stridePos.emplace_back(starts[numSequences]);
int size = stridePos.size();
CHECK_EQ(size - 1, tgtBuf[numSequences]);
IVector::resizeOrCreate(*stridePostions, size, false);
(*stridePostions)->copyFrom(stridePos.data(), size);
}
void Argument::getValueString( void Argument::getValueString(
std::unordered_map<std::string, std::string>* out) const { std::unordered_map<std::string, std::string>* out) const {
if (value) { if (value) {
......
...@@ -291,6 +291,15 @@ struct Argument { ...@@ -291,6 +291,15 @@ struct Argument {
*/ */
void degradeSequence(const Argument& input); void degradeSequence(const Argument& input);
/*
After pooling with stride n (n is smaller than sequence length),
a long sequence will be shorten.
This function is invalid for sequence having sub-sequence.
*/
void poolSequenceWithStride(const Argument& input,
size_t stride,
IVectorPtr* stridePositions,
bool reversed = false);
/** /**
* @brief getValueString will return the argument's output in string. There * @brief getValueString will return the argument's output in string. There
* are several kinds of output. The keys of output dictionary are 'value', * are several kinds of output. The keys of output dictionary are 'value',
......
add_simple_unittest(test_common) add_simple_unittest(test_common)
add_simple_unittest(test_argument)
/* Copyright (c) 2016 PaddlePaddle 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. */
#include <gtest/gtest.h>
#include <paddle/parameter/Argument.h>
using namespace paddle; // NOLINT
TEST(Argument, poolSequenceWithStride) {
Argument input, output;
ICpuGpuVector::resizeOrCreate(input.sequenceStartPositions, 5, false);
int* inStart = input.sequenceStartPositions->getMutableData(false);
inStart[0] = 0;
inStart[1] = 9;
inStart[2] = 14;
inStart[3] = 17;
inStart[4] = 30;
int strideResult[] = {0, 5, 9, 14, 17, 22, 27, 30};
int strideResultReversed[] = {0, 4, 9, 14, 17, 20, 25, 30};
for (auto reversed : {false, true}) {
IVectorPtr stridePositions;
output.poolSequenceWithStride(
input, 5 /* stride */, &stridePositions, reversed);
const int* outStart = output.sequenceStartPositions->getData(false);
CHECK_EQ(outStart[0], 0);
CHECK_EQ(outStart[1], 2);
CHECK_EQ(outStart[2], 3);
CHECK_EQ(outStart[3], 4);
CHECK_EQ(outStart[4], 7);
CHECK_EQ(stridePositions->getSize(), 8);
auto result = reversed ? strideResultReversed : strideResult;
for (int i = 0; i < 8; i++) {
CHECK_EQ(stridePositions->getData()[i], result[i]);
}
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
initMain(argc, argv);
return RUN_ALL_TESTS();
}
...@@ -187,6 +187,13 @@ class SequenceScanner(IScanner): ...@@ -187,6 +187,13 @@ class SequenceScanner(IScanner):
self.__inner_scanner__ = inner_scanner self.__inner_scanner__ = inner_scanner
self.__setter__ = setter self.__setter__ = setter
def pre_scan(self, dat):
for each in dat:
self.__inner_scanner__.pre_scan(each)
def finish_pre_scan(self, argument):
self.__inner_scanner__.finish_pre_scan(argument)
def scan(self, dat): def scan(self, dat):
self.__seq__.append(self.__seq__[-1] + self.get_size(dat)) self.__seq__.append(self.__seq__[-1] + self.get_size(dat))
for each in dat: for each in dat:
......
...@@ -83,13 +83,17 @@ def __arguments_to_numpy__(i, arg): ...@@ -83,13 +83,17 @@ def __arguments_to_numpy__(i, arg):
assert isinstance(arg, swig_paddle.Arguments) assert isinstance(arg, swig_paddle.Arguments)
value = arg.getSlotValue(i) value = arg.getSlotValue(i)
ids = arg.getSlotIds(i) ids = arg.getSlotIds(i)
prob = arg.getSlotIn(i)
if value is not None: if value is not None:
assert isinstance(value, swig_paddle.Matrix) assert isinstance(value, swig_paddle.Matrix)
value = value.copyToNumpyMat() value = value.copyToNumpyMat()
if ids is not None: if ids is not None:
assert isinstance(ids, swig_paddle.IVector) assert isinstance(ids, swig_paddle.IVector)
ids = ids.copyToNumpyArray() ids = ids.copyToNumpyArray()
return {"value": value, "id": ids} if prob is not None:
assert isinstance(prob, swig_paddle.Matrix)
prob = prob.copyToNumpyMat()
return {"value": value, "id": ids, "prob": prob}
def __monkeypatch_gradient_machine__(): def __monkeypatch_gradient_machine__():
......
...@@ -441,6 +441,11 @@ message LayerConfig { ...@@ -441,6 +441,11 @@ message LayerConfig {
// blank label used in ctc loss // blank label used in ctc loss
optional uint32 blank = 52 [default = 0]; optional uint32 blank = 52 [default = 0];
// stride parameter for seqlastins layer, AverageLayer, MaxLayer, which
// controls the scope of pooling operation. can be set > 0.
// leave empty or set to -1 to disable this stride pooling.
optional int32 seq_pool_stride = 53 [default = -1];
} }
message EvaluatorConfig { message EvaluatorConfig {
......
...@@ -2485,6 +2485,7 @@ class SequenceLastInstanceLayer(LayerBase): ...@@ -2485,6 +2485,7 @@ class SequenceLastInstanceLayer(LayerBase):
active_type='linear', active_type='linear',
trans_type='non-seq', trans_type='non-seq',
bias=False, bias=False,
stride=-1,
**xargs): **xargs):
super(SequenceLastInstanceLayer, self).__init__( super(SequenceLastInstanceLayer, self).__init__(
name, name,
...@@ -2495,10 +2496,11 @@ class SequenceLastInstanceLayer(LayerBase): ...@@ -2495,10 +2496,11 @@ class SequenceLastInstanceLayer(LayerBase):
**xargs) **xargs)
config_assert( config_assert(
len(inputs) == 1, 'SequenceLastInstanceLayer must have 1 input') len(inputs) == 1, 'SequenceLastInstanceLayer must have 1 input')
if trans_type == 'seq':
config_assert(stride == -1, 'subseq does not support stride window')
self.config.trans_type = trans_type self.config.trans_type = trans_type
for input_index in xrange(len(self.inputs)): self.config.seq_pool_stride = stride
input_layer = self.get_input_layer(input_index) self.set_layer_size(self.get_input_layer(0).size)
self.set_layer_size(input_layer.size)
self.create_bias_parameter(bias, self.config.size) self.create_bias_parameter(bias, self.config.size)
...@@ -2510,10 +2512,16 @@ class SequenceFirstInstanceLayer(SequenceLastInstanceLayer): ...@@ -2510,10 +2512,16 @@ class SequenceFirstInstanceLayer(SequenceLastInstanceLayer):
active_type='linear', active_type='linear',
trans_type='non-seq', trans_type='non-seq',
bias=False, bias=False,
stride=-1,
**xargs): **xargs):
super(SequenceFirstInstanceLayer, self).__init__( super(SequenceFirstInstanceLayer, self).__init__(
name, inputs=inputs, active_type=active_type, bias=bias, **xargs) name,
self.config.trans_type = trans_type inputs=inputs,
active_type=active_type,
trans_type=trans_type,
bias=bias,
stride=stride,
**xargs)
self.config.select_first = True self.config.select_first = True
......
...@@ -1342,10 +1342,16 @@ def grumemory(input, ...@@ -1342,10 +1342,16 @@ def grumemory(input,
def last_seq(input, def last_seq(input,
name=None, name=None,
agg_level=AggregateLevel.EACH_TIMESTEP, agg_level=AggregateLevel.EACH_TIMESTEP,
stride=-1,
layer_attr=None): layer_attr=None):
""" """
Get Last Timestamp Activation of a sequence. Get Last Timestamp Activation of a sequence.
If stride > 0, this layer slides a window whose size is determined by stride,
and return the last value of the window as the output. Thus, a long sequence
will be shorten. Note that for sequence with sub-sequence, the default value
of stride is -1.
The simple usage is: The simple usage is:
.. code-block:: python .. code-block:: python
...@@ -1357,6 +1363,8 @@ def last_seq(input, ...@@ -1357,6 +1363,8 @@ def last_seq(input,
:type name: basestring :type name: basestring
:param input: Input layer name. :param input: Input layer name.
:type input: LayerOutput :type input: LayerOutput
:param stride: window size.
:type stride: Int
:param layer_attr: extra layer attributes. :param layer_attr: extra layer attributes.
:type layer_attr: ExtraLayerAttribute. :type layer_attr: ExtraLayerAttribute.
:return: LayerOutput object. :return: LayerOutput object.
...@@ -1368,11 +1376,15 @@ def last_seq(input, ...@@ -1368,11 +1376,15 @@ def last_seq(input,
" series information at all. Maybe you want to use" " series information at all. Maybe you want to use"
" first_seq instead.") " first_seq instead.")
if agg_level == AggregateLevel.EACH_SEQUENCE:
assert stride == -1
Layer( Layer(
name=name, name=name,
type=LayerType.SEQUENCE_LAST_INSTANCE, type=LayerType.SEQUENCE_LAST_INSTANCE,
inputs=[input.name], inputs=[input.name],
trans_type=agg_level, trans_type=agg_level,
stride=stride,
**ExtraLayerAttribute.to_kwargs(layer_attr)) **ExtraLayerAttribute.to_kwargs(layer_attr))
return LayerOutput( return LayerOutput(
name, name,
...@@ -1386,10 +1398,16 @@ def last_seq(input, ...@@ -1386,10 +1398,16 @@ def last_seq(input,
def first_seq(input, def first_seq(input,
name=None, name=None,
agg_level=AggregateLevel.EACH_TIMESTEP, agg_level=AggregateLevel.EACH_TIMESTEP,
stride=-1,
layer_attr=None): layer_attr=None):
""" """
Get First Timestamp Activation of a sequence. Get First Timestamp Activation of a sequence.
If stride > 0, this layer slides a window whose size is determined by stride,
and return the first value of the window as the output. Thus, a long sequence
will be shorten. Note that for sequence with sub-sequence, the default value
of stride is -1.
The simple usage is: The simple usage is:
.. code-block:: python .. code-block:: python
...@@ -1401,6 +1419,8 @@ def first_seq(input, ...@@ -1401,6 +1419,8 @@ def first_seq(input,
:type name: basestring :type name: basestring
:param input: Input layer name. :param input: Input layer name.
:type input: LayerOutput :type input: LayerOutput
:param stride: window size.
:type stride: Int
:param layer_attr: extra layer attributes. :param layer_attr: extra layer attributes.
:type layer_attr: ExtraLayerAttribute. :type layer_attr: ExtraLayerAttribute.
:return: LayerOutput object. :return: LayerOutput object.
...@@ -1413,11 +1433,15 @@ def first_seq(input, ...@@ -1413,11 +1433,15 @@ def first_seq(input,
' time series information at all. Maybe you want to use' ' time series information at all. Maybe you want to use'
' last_seq instead.') ' last_seq instead.')
if agg_level == AggregateLevel.EACH_SEQUENCE:
assert stride == -1
Layer( Layer(
name=name, name=name,
type=LayerType.SEQUENCE_FIRST_INSTANCE, type=LayerType.SEQUENCE_FIRST_INSTANCE,
inputs=[input.name], inputs=[input.name],
trans_type=agg_level, trans_type=agg_level,
stride=stride,
**ExtraLayerAttribute.to_kwargs(layer_attr)) **ExtraLayerAttribute.to_kwargs(layer_attr))
return LayerOutput( return LayerOutput(
name, name,
...@@ -4873,7 +4897,7 @@ def nce_layer(input, ...@@ -4873,7 +4897,7 @@ def nce_layer(input,
if neg_distribution is not None: if neg_distribution is not None:
assert isinstance(neg_distribution, collections.Sequence) assert isinstance(neg_distribution, collections.Sequence)
assert len(neg_distribution) == num_classes assert len(neg_distribution) == num_classes
assert sum(neg_distribution) == 1 assert abs(sum(neg_distribution) - 1.0) < 1e-5
if not isinstance(act, BaseActivation): if not isinstance(act, BaseActivation):
raise TypeError() raise TypeError()
......
...@@ -14,4 +14,7 @@ for op in seq_op: ...@@ -14,4 +14,7 @@ for op in seq_op:
for al in agg_level: for al in agg_level:
opts.append(op(input=din, agg_level=al)) opts.append(op(input=din, agg_level=al))
for op in seq_op:
opts.append(op(input=din, agg_level=AggregateLevel.EACH_TIMESTEP, stride=5))
outputs(opts) outputs(opts)
...@@ -15,6 +15,7 @@ layers { ...@@ -15,6 +15,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "seq" trans_type: "seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__first_seq_1__" name: "__first_seq_1__"
...@@ -26,6 +27,7 @@ layers { ...@@ -26,6 +27,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_0__" name: "__last_seq_0__"
...@@ -36,6 +38,7 @@ layers { ...@@ -36,6 +38,7 @@ layers {
input_layer_name: "data" input_layer_name: "data"
} }
trans_type: "seq" trans_type: "seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_1__" name: "__last_seq_1__"
...@@ -46,12 +49,38 @@ layers { ...@@ -46,12 +49,38 @@ layers {
input_layer_name: "data" input_layer_name: "data"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
}
layers {
name: "__first_seq_2__"
type: "seqlastins"
size: 30
active_type: "linear"
inputs {
input_layer_name: "data"
}
select_first: true
trans_type: "non-seq"
seq_pool_stride: 5
}
layers {
name: "__last_seq_2__"
type: "seqlastins"
size: 30
active_type: "linear"
inputs {
input_layer_name: "data"
}
trans_type: "non-seq"
seq_pool_stride: 5
} }
input_layer_names: "data" input_layer_names: "data"
output_layer_names: "__first_seq_0__" output_layer_names: "__first_seq_0__"
output_layer_names: "__first_seq_1__" output_layer_names: "__first_seq_1__"
output_layer_names: "__last_seq_0__" output_layer_names: "__last_seq_0__"
output_layer_names: "__last_seq_1__" output_layer_names: "__last_seq_1__"
output_layer_names: "__first_seq_2__"
output_layer_names: "__last_seq_2__"
sub_models { sub_models {
name: "root" name: "root"
layer_names: "data" layer_names: "data"
...@@ -59,11 +88,15 @@ sub_models { ...@@ -59,11 +88,15 @@ sub_models {
layer_names: "__first_seq_1__" layer_names: "__first_seq_1__"
layer_names: "__last_seq_0__" layer_names: "__last_seq_0__"
layer_names: "__last_seq_1__" layer_names: "__last_seq_1__"
layer_names: "__first_seq_2__"
layer_names: "__last_seq_2__"
input_layer_names: "data" input_layer_names: "data"
output_layer_names: "__first_seq_0__" output_layer_names: "__first_seq_0__"
output_layer_names: "__first_seq_1__" output_layer_names: "__first_seq_1__"
output_layer_names: "__last_seq_0__" output_layer_names: "__last_seq_0__"
output_layer_names: "__last_seq_1__" output_layer_names: "__last_seq_1__"
output_layer_names: "__first_seq_2__"
output_layer_names: "__last_seq_2__"
is_recurrent_layer_group: false is_recurrent_layer_group: false
} }
...@@ -128,6 +128,7 @@ layers { ...@@ -128,6 +128,7 @@ layers {
input_layer_name: "__simple_gru_0__" input_layer_name: "__simple_gru_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_1__" name: "__last_seq_1__"
...@@ -138,6 +139,7 @@ layers { ...@@ -138,6 +139,7 @@ layers {
input_layer_name: "__simple_gru_1__" input_layer_name: "__simple_gru_1__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__fc_layer_0__" name: "__fc_layer_0__"
......
...@@ -210,6 +210,7 @@ layers { ...@@ -210,6 +210,7 @@ layers {
input_layer_name: "__lstm_group_0__" input_layer_name: "__lstm_group_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_1__" name: "__last_seq_1__"
...@@ -220,6 +221,7 @@ layers { ...@@ -220,6 +221,7 @@ layers {
input_layer_name: "__lstm_group_1__" input_layer_name: "__lstm_group_1__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__fc_layer_0__" name: "__fc_layer_0__"
......
...@@ -143,6 +143,7 @@ layers { ...@@ -143,6 +143,7 @@ layers {
input_layer_name: "__recurrent_layer_0__" input_layer_name: "__recurrent_layer_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__first_seq_0__" name: "__first_seq_0__"
...@@ -154,6 +155,7 @@ layers { ...@@ -154,6 +155,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_1__" name: "__last_seq_1__"
...@@ -164,6 +166,7 @@ layers { ...@@ -164,6 +166,7 @@ layers {
input_layer_name: "__lstmemory_0__" input_layer_name: "__lstmemory_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__first_seq_1__" name: "__first_seq_1__"
...@@ -175,6 +178,7 @@ layers { ...@@ -175,6 +178,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__last_seq_2__" name: "__last_seq_2__"
...@@ -185,6 +189,7 @@ layers { ...@@ -185,6 +189,7 @@ layers {
input_layer_name: "__gru_0__" input_layer_name: "__gru_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__first_seq_2__" name: "__first_seq_2__"
...@@ -196,6 +201,7 @@ layers { ...@@ -196,6 +201,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
parameters { parameters {
name: "___fc_layer_0__.w0" name: "___fc_layer_0__.w0"
......
...@@ -96,6 +96,7 @@ layers { ...@@ -96,6 +96,7 @@ layers {
input_layer_name: "rnn_forward" input_layer_name: "rnn_forward"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__recurrent_group_1__" name: "__recurrent_group_1__"
...@@ -145,6 +146,7 @@ layers { ...@@ -145,6 +146,7 @@ layers {
} }
select_first: true select_first: true
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__recurrent_group_2__" name: "__recurrent_group_2__"
...@@ -193,6 +195,7 @@ layers { ...@@ -193,6 +195,7 @@ layers {
input_layer_name: "rnn_subseq_forward" input_layer_name: "rnn_subseq_forward"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__lstm_group_0___recurrent_group" name: "__lstm_group_0___recurrent_group"
...@@ -282,6 +285,7 @@ layers { ...@@ -282,6 +285,7 @@ layers {
input_layer_name: "__lstm_group_0__" input_layer_name: "__lstm_group_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__gru_group_0___recurrent_group" name: "__gru_group_0___recurrent_group"
...@@ -330,6 +334,7 @@ layers { ...@@ -330,6 +334,7 @@ layers {
input_layer_name: "__gru_group_0__" input_layer_name: "__gru_group_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
layers { layers {
name: "__recurrent_group_3__" name: "__recurrent_group_3__"
...@@ -378,6 +383,7 @@ layers { ...@@ -378,6 +383,7 @@ layers {
input_layer_name: "__fc_layer_0__" input_layer_name: "__fc_layer_0__"
} }
trans_type: "non-seq" trans_type: "non-seq"
seq_pool_stride: -1
} }
parameters { parameters {
name: "___mixed_0__.w0" name: "___mixed_0__.w0"
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
from py_paddle import DataProviderConverter from py_paddle import DataProviderConverter
import collections
import paddle.trainer.PyDataProvider2 as pydp2 import paddle.trainer.PyDataProvider2 as pydp2
__all__ = ['DataFeeder'] __all__ = ['DataFeeder']
...@@ -35,15 +35,30 @@ class DataFeeder(DataProviderConverter): ...@@ -35,15 +35,30 @@ class DataFeeder(DataProviderConverter):
DataFeeder converts this mini-batch data entries into Arguments in order DataFeeder converts this mini-batch data entries into Arguments in order
to feed it to C++ interface. to feed it to C++ interface.
The example usage: The simple usage shows below
.. code-block:: python
feeding = ['image', 'label']
data_types = enumerate_data_types_of_data_layers(topology)
feeder = DataFeeder(data_types=data_types, feeding=feeding)
minibatch_data = [([1.0, 2.0, 3.0, ...], 5)]
arg = feeder(minibatch_data)
If mini-batch data and data layers are not one to one mapping, we
could pass a dictionary to feeding parameter to represent the mapping
relationship.
.. code-block:: python .. code-block:: python
data_types = [('image', paddle.data_type.dense_vector(784)), data_types = [('image', paddle.data_type.dense_vector(784)),
('label', paddle.data_type.integer_value(10))] ('label', paddle.data_type.integer_value(10))]
reader_dict = {'image':0, 'label':1} feeding = {'image':0, 'label':1}
feeder = DataFeeder(data_types=data_types, reader_dict=reader_dict) feeder = DataFeeder(data_types=data_types, feeding=feeding)
minibatch_data = [ minibatch_data = [
( [1.0,2.0,3.0,4.0], 5, [6,7,8] ), # first sample ( [1.0,2.0,3.0,4.0], 5, [6,7,8] ), # first sample
( [1.0,2.0,3.0,4.0], 5, [6,7,8] ) # second sample ( [1.0,2.0,3.0,4.0], 5, [6,7,8] ) # second sample
...@@ -65,9 +80,9 @@ class DataFeeder(DataProviderConverter): ...@@ -65,9 +80,9 @@ class DataFeeder(DataProviderConverter):
a tuple of (data_name, data_type). a tuple of (data_name, data_type).
:type data_types: list :type data_types: list
:param reader_dict: A dictionary to specify the position of each data :param feeding: A dictionary or a sequence to specify the position of each
in the input data. data in the input data.
:type feeding: dict :type feeding: dict|collections.Sequence|None
""" """
def __init__(self, data_types, feeding=None): def __init__(self, data_types, feeding=None):
...@@ -75,6 +90,13 @@ class DataFeeder(DataProviderConverter): ...@@ -75,6 +90,13 @@ class DataFeeder(DataProviderConverter):
input_types = [] input_types = []
if feeding is None: if feeding is None:
feeding = default_feeding_map(data_types) feeding = default_feeding_map(data_types)
elif isinstance(feeding, collections.Sequence):
feed_list = feeding
feeding = dict()
for i, name in enumerate(feed_list):
feeding[name] = i
elif not isinstance(feeding, dict):
raise TypeError("Feeding should be dict or sequence or None.")
self.feeding = feeding self.feeding = feeding
for each in data_types: for each in data_types:
......
...@@ -34,7 +34,7 @@ URL_TRAIN = 'http://paddlepaddle.cdn.bcebos.com/demo/wmt_shrinked_data/wmt14.tgz ...@@ -34,7 +34,7 @@ URL_TRAIN = 'http://paddlepaddle.cdn.bcebos.com/demo/wmt_shrinked_data/wmt14.tgz
MD5_TRAIN = 'a755315dd01c2c35bde29a744ede23a6' MD5_TRAIN = 'a755315dd01c2c35bde29a744ede23a6'
# this is the pretrained model, whose bleu = 26.92 # this is the pretrained model, whose bleu = 26.92
URL_MODEL = 'http://paddlepaddle.bj.bcebos.com/demo/wmt_14/wmt14_model.tar.gz' URL_MODEL = 'http://paddlepaddle.bj.bcebos.com/demo/wmt_14/wmt14_model.tar.gz'
MD5_MODEL = '6b097d23e15654608c6f74923e975535' MD5_MODEL = '4ce14a26607fb8a1cc23bcdedb1895e4'
START = "<s>" START = "<s>"
END = "<e>" END = "<e>"
...@@ -140,6 +140,12 @@ def model(): ...@@ -140,6 +140,12 @@ def model():
return parameters return parameters
def trg_dict(dict_size):
tar_file = download(URL_TRAIN, 'wmt14', MD5_TRAIN)
src_dict, trg_dict = __read_to_dict__(tar_file, dict_size)
return trg_dict
def fetch(): def fetch():
download(URL_TRAIN, 'wmt14', MD5_TRAIN) download(URL_TRAIN, 'wmt14', MD5_TRAIN)
download(URL_MODEL, 'wmt14', MD5_MODEL) download(URL_MODEL, 'wmt14', MD5_MODEL)
...@@ -48,8 +48,13 @@ class Inference(object): ...@@ -48,8 +48,13 @@ class Inference(object):
self.__gradient_machine__.finish() self.__gradient_machine__.finish()
def iter_infer_field(self, field, **kwargs): def iter_infer_field(self, field, **kwargs):
if not isinstance(field, list) and not isinstance(field, tuple):
field = [field]
for result in self.iter_infer(**kwargs): for result in self.iter_infer(**kwargs):
yield [each_result[field] for each_result in result] for each_result in result:
item = [each_result[each_field] for each_field in field]
yield item
def infer(self, field='value', **kwargs): def infer(self, field='value', **kwargs):
retv = None retv = None
...@@ -87,9 +92,11 @@ def infer(output_layer, parameters, input, feeding=None, field='value'): ...@@ -87,9 +92,11 @@ def infer(output_layer, parameters, input, feeding=None, field='value'):
:type input: collections.Iterable :type input: collections.Iterable
:param feeding: Reader dictionary. Default could generate from input :param feeding: Reader dictionary. Default could generate from input
value. value.
:param field: The prediction field. It should in [`value`, `ids`]. `value` :param field: The prediction field. It should in [`value`, `id`, `prob`].
means return the prediction probabilities, `ids` means return `value` and `prob` mean return the prediction probabilities,
the prediction labels. Default is `value` `id` means return the prediction labels. Default is `value`.
Note that `prob` only used when output_layer is beam_search
or max_id.
:type field: str :type field: str
:return: a numpy array :return: a numpy array
:rtype: numpy.ndarray :rtype: numpy.ndarray
......
...@@ -83,7 +83,7 @@ class SGD(object): ...@@ -83,7 +83,7 @@ class SGD(object):
:type event_handler: (BaseEvent) => None :type event_handler: (BaseEvent) => None
:param feeding: Feeding is a map of neural network input name and array :param feeding: Feeding is a map of neural network input name and array
index that reader returns. index that reader returns.
:type feeding: dict :type feeding: dict|list
:return: :return:
""" """
if event_handler is None: if event_handler is None:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册