Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sequence slice layer #3367

Merged
merged 10 commits into from
Aug 24, 2017

Conversation

lcy-seso
Copy link
Contributor

@lcy-seso lcy-seso commented Aug 9, 2017

add a seq_slice_layer which will return one or several sub-sequences from the input sequence layer given start and end indices.

  1. If only start indices are given, and end indices are set to None, this layer slices the input sequence from the given start indices to its end.
  2. If only end indices are given and start indices are set to None, this layer slices the input sequence from its beginning to the given end indices.
  3. If start and end indices are both given, they should have the same number of elements.

@lcy-seso lcy-seso changed the title Add sequence slice layer Add a sequence slice layer Aug 9, 2017
@lcy-seso lcy-seso requested a review from luotao1 August 9, 2017 14:22
@lcy-seso lcy-seso force-pushed the add_sequence_slice_layer branch 2 times, most recently from f5d69ca to 9e6e0a0 Compare August 10, 2017 05:14
// (with -1 as a special token). Storing indices information in real-typed
// Matrix leads to converting real to int. This is very dangerous if a user
// fills this matrix himself, invalid data may occur.
// The selected indices should be stored in an int-typed matrix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 101行matrices是复数。
  2. real type, int type。
  3. 104行Matrix小写

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

real-typed 是构词法,等于形容词,这个也可以吧?不过这一段注释重新完善修改过。

// (with -1 as a special token). Storing indices information in real-typed
// Matrix leads to converting real to int. This is very dangerous if a user
// fills this matrix himself, invalid data may occur.
// The selected indices should be stored in an int-typed matrix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段注释和KmaxSeqScoreLayer.cpp里的那段一样,保留一个就够了吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些注释确实是一样的,多个文件都需统一改一下,但彼此又是独立的。
这里有一个real强制转int,比较危险,我建议还是留着。

void SequenceSliceLayer::checkInputs() {
const Argument& inputSeq = getInput(0);
CHECK(inputSeq.hasSeq()) << "The first input of sequence slic layer "
<< "must be a sequence.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slic->slice笔误

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

CHECK_EQ(static_cast<size_t>(indices1->getHeight()),
inputSeq.hasSubseq() ? inputSeq.getNumSubSequences()
: inputSeq.getNumSequences())
<< "Height of the second input should be equal to number of sequence "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second input height should be equal to the number of sequences

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second input height 这个也有语法问题吧,应该用of 或者 's 来表示所有格。

} else {
startIdsOnCpu_ = nullptr;
endIdsOnCpu_ = getInputValue(1);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

91行-97行:

startIdsOnCpu_ = config_.select_first() ?  getInputValue(1) : nullptr;
endIdsOnCpu_ =  config_.select_first() ?  nullptr : getInputValue(1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样改的话,同一个条件需要判断两次条件,但关系不大。。。done.

if (starts) {
if (starts->getElement(rowIdx, k) == -1.) break;
} else if (ends->getElement(rowIdx, k) == -1.)
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

156-159行,是判断两个break的条件,等价于?:

if (starts && starts->getElement(rowIdx, k) == -1.) break;
if (ends && ends->getElement(rowIdx, k) == -1.) break;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成短路逻辑了,done。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会引入一次额外的逻辑判断,不过影响不大。。。

// (with -1 as a special token). Storing indices information in real-typed
// Matrix leads to converting real to int. This is very dangerous if a user
// fills this matrix himself, invalid data may occur.
// The selected indices should be stored in an int-typed matrix.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段注释和上面的也重复了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还是需要保存这一段注释,因为这几个文件是各自独立的。对注释的语法做了修改。

number of elements.

If start or end indices contains more than one elements, the input sequence
will be sliced for multiple times.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好给例子,特别来说明start和end有多余1个元素的情况下,应该怎么输入,输出是什么

if isinstance(starts, list):
assert len(starts) == 1, (
'the start indices for sequence slice layer cannot '
'be a list having more than one element.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看layer.py里面写 If start or end indices contains more than one elements。
那这里又说只能是1个?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是检测 starts 里面只给了一个layer,而这个layer 本身的 Argument.value 里面可以写多个要去裁切的位点。

if starts is not None:
self.config.select_first = True
else:
self.config.select_first = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2720-2723行
self.config.select_first = True if starts else False

rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size());
} else {
rowIndice_ =
IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

180-185可以直接,cpu和gpu都相同吧:

rowIndice_ = IVector::create(selectedRows_.size(), useGpu_);
rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是想CPU下面避免一次拷贝。如果改成一样,CPU下需要进行一次不必要的拷贝。

MatrixPtr inputSeqGrad = getInputGrad(0);
MatrixPtr outputGrad = getOutputGrad();

outputGrad->addToRows(*inputSeqGrad, *rowIndice_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这三行可以合并成一行:
getOutputGrad()->addToRows(*getInputGrad(0), *rowIndice_);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Contributor Author

@lcy-seso lcy-seso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow comments.

// (with -1 as a special token). Storing indices information in real-typed
// Matrix leads to converting real to int. This is very dangerous if a user
// fills this matrix himself, invalid data may occur.
// The selected indices should be stored in an int-typed matrix.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

real-typed 是构词法,等于形容词,这个也可以吧?不过这一段注释重新完善修改过。

void SequenceSliceLayer::checkInputs() {
const Argument& inputSeq = getInput(0);
CHECK(inputSeq.hasSeq()) << "The first input of sequence slic layer "
<< "must be a sequence.";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

CHECK_EQ(static_cast<size_t>(indices1->getHeight()),
inputSeq.hasSubseq() ? inputSeq.getNumSubSequences()
: inputSeq.getNumSequences())
<< "Height of the second input should be equal to number of sequence "
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second input height 这个也有语法问题吧,应该用of 或者 's 来表示所有格。

} else {
startIdsOnCpu_ = nullptr;
endIdsOnCpu_ = getInputValue(1);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样改的话,同一个条件需要判断两次条件,但关系不大。。。done.

false /* trans */,
false /* useGpu */);
endIdsOnCpu_->copyFrom(*indices2);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是一样,91 - 97 是 cpu下,105 - 138 行在gpu下会执行到。把cpu下的从这个函数中移除了。

if (starts) {
if (starts->getElement(rowIdx, k) == -1.) break;
} else if (ends->getElement(rowIdx, k) == -1.)
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成短路逻辑了,done。

rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size());
} else {
rowIndice_ =
IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是想CPU下面避免一次拷贝。如果改成一样,CPU下需要进行一次不必要的拷贝。

MatrixPtr inputSeqGrad = getInputGrad(0);
MatrixPtr outputGrad = getOutputGrad();

outputGrad->addToRows(*inputSeqGrad, *rowIndice_);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// (with -1 as a special token). Storing indices information in real-typed
// Matrix leads to converting real to int. This is very dangerous if a user
// fills this matrix himself, invalid data may occur.
// The selected indices should be stored in an int-typed matrix.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还是需要保存这一段注释,因为这几个文件是各自独立的。对注释的语法做了修改。

if isinstance(starts, list):
assert len(starts) == 1, (
'the start indices for sequence slice layer cannot '
'be a list having more than one element.')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是检测 starts 里面只给了一个layer,而这个layer 本身的 Argument.value 里面可以写多个要去裁切的位点。

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lcy-seso lcy-seso merged commit ab6b3c4 into PaddlePaddle:develop Aug 24, 2017
@lcy-seso lcy-seso deleted the add_sequence_slice_layer branch August 24, 2017 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants