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 sequence_expand Operator based LoDTensor #4178

Closed
wanghaoshuang opened this issue Sep 19, 2017 · 11 comments
Closed

Add sequence_expand Operator based LoDTensor #4178

wanghaoshuang opened this issue Sep 19, 2017 · 11 comments
Assignees

Comments

@wanghaoshuang
Copy link
Contributor

wanghaoshuang commented Sep 19, 2017

sequence_expand Op

Expand a LoDTensor by a reference LoDTensor.

example

Ginve input(X) as input to be expanded and input(Y) as reference input. Both input(X) and input(Y) are LoDTensor.

#1

Input(X) :

2
a b

Input(Y):

4

Out:

illegal?

#2

Input(X) :

2
a b

Input(Y):

2
2 2

Out:

2
2   2
a a b b

#3

Input(X) :

2
a b

Input(Y):

2
2  4

Out:

2
2   4
a a b b b b

#4

Input(X) :

2
1  2  
a  b c

Input(Y):

2
2 4

Out:

2
2   4
a a b c b c
illegal?

#5

Input(X) :

2
1  2  
a  b c

Input(Y):

4
2 4 4 4

Out:

4
2   4       4       4
a a b c b c a a a a b c b c
illegal?

#6

Input(X) :

2
a b

Input(Y):

3

Out:

illegal
@wanghaoshuang wanghaoshuang created this issue from a note in Port Operators (Base on LoDTensor) Sep 19, 2017
@wanghaoshuang wanghaoshuang self-assigned this Sep 19, 2017
@wanghaoshuang wanghaoshuang changed the title Add Expand Operator Add Expand Operator based LoDTensor Sep 19, 2017
@wanghaoshuang wanghaoshuang changed the title Add Expand Operator based LoDTensor Add sequence_expand Operator based LoDTensor Sep 19, 2017
@luotao1
Copy link
Contributor

luotao1 commented Sep 19, 2017

不是很理解上面的例子。paddle原来的expandLayer,一个是数据,一个是为了获得LoD信息。
所以Input(X)可以是普通的tensor,Input(Y)必须是LoDTensor。

假设Input(x)的输入是一维数值[4,6]
Input(Y)的LoD信息是[0,3,5],即两条seq,第一条长3,第二条长2.
Input(X)的个数得和Input(Y)的句子条数一样。

那么输出的LoD信息是[0,3,5]
输出的数值是[4,4,4,6,6],共5个一维数值。

@qingqing01
Copy link
Contributor

qingqing01 commented Sep 19, 2017

我大概理解 @wanghaoshuang 的意思:

比如这个case:

Input(X) :

2      # 两条样本
1  2   # 第一条:1个time step, 第二条:2个time step
a  b c 

Input(Y):

2    # 两条样本
2 4  #第一条:2个time step, 第二条:4个time step

Out:

2             #输出两条样本
2   4         #和Input(Y)相同的LoD
a a b c b c   # 第一条:1个time step扩展成2个time step,第二条:2个time step扩展4个time step

我觉得还可以,相比Paddle原先的ExpandLayer可以支持更多扩展情况。

@luotao1
Copy link
Contributor

luotao1 commented Sep 19, 2017

从理论上讲这种方式是可以的,要求Y的timestep个数要能整除X的timestep个数。

但是实际操作中是不现实的:如果X和Y都是LodTensor,那么训练的时候,time step完全是随机的。因为每条句子的词个数都不一样。那么,常常会出现最后一种情况。

@qingqing01
Copy link
Contributor

@luotao1

那就是 @wanghaoshuang 的最后一个case,不合法输入,无法完成expand操作。 @wanghaoshuang 上述的实现增加一种你描述的case,也是Input(X)的LoD是空(operator里默认的输入都是LoDTensor),就可以吧。

@wanghaoshuang
Copy link
Contributor Author

wanghaoshuang commented Sep 19, 2017

@luotao1

不是很理解上面的例子。paddle原来的expandLayer,一个是数据,一个是为了获得LoD信息。
所以Input(X)可以是普通的tensor,Input(Y)必须是LoDTensor。

是的,在这里Input(X)也可以是普通Tensor.

假设Input(x)的输入是一维数值[4,6]
Input(Y)的LoD信息是[0,3,5],即两条seq,第一条长3,第二条长2.
Input(X)的个数得和Input(Y)的句子条数一样。
那么输出的LoD信息是[0,3,5]
输出的数值是[4,4,4,6,6],共5个一维数值。

对于这个例子,可以这么认为:
Input(X) :

a b // 普通Tensor, LoD为空,dim[0]为batch_size

Input(Y):

2
3 2

Out:

2
3     2
a a a b b

@luotao1
Copy link
Contributor

luotao1 commented Sep 19, 2017

不能支持X和Y都是1维的LodTensor,因为训练的时候,不能说某些样本能过,某些样本过不了。样本的timestep个数都是不一样的,程序没法保证都能整除。

@luotao1
Copy link
Contributor

luotao1 commented Sep 19, 2017

expand直观理解,就是把0维序列扩展成1维或者2维(含有subseq的序列),1维序列扩展成2维。

如果把1维序列扩展成1维,可以另外写一个op,指定timestep扩展几倍。因为把一个1维序列按照另一个1维序列进行扩展,很多样本都是过不了的。因此没有存在的意义。

@wanghaoshuang
Copy link
Contributor Author

@luotao1 也就是说上边第4、5 、6个例子是没有意义的,expand只用考虑低维转高维 ?

@qingqing01
Copy link
Contributor

qingqing01 commented Sep 19, 2017

  • 我理解不是所有的sequence任务都是变长的,比如见到的一些vedio的序列训练任务是定长的。
  • 从Expand的角度,@wanghaoshuang的一些case我觉得是合理的,只是说常不常用。
  • 文档我们可以解释清楚,并告诉用户什么情况合法,什么情况不合法。用户用的时候,如果不合法,他为什么还要继续这样用呢?
  • 实现上考虑,如果只支持最简单的case,也赞同,其他case都报错,也可以。只是觉得不能说很多case不合理。

@luotao1
Copy link
Contributor

luotao1 commented Sep 19, 2017

@qingqing01

我理解不是所有的sequence任务都是变长的,比如见到的一些vedio的序列训练任务是定长的。

目前LoDTensor是不感知序列是定长还是变长的,它认为序列都是变长的。
你说的这个任务,是从定长变成什么呢?对这种情况,是不是新写一个op,同纬度的转换,只需要一个输入就够了,另一个是属性设置下呢?

@wanghaoshuang 的case,是对同纬度的序列进行扩展。由于LoDTensor是认为序列都是变长的,这种情况,不能单靠文档解释,是直接程序就要报错。

文档我们可以解释清楚,并告诉用户什么情况合法,什么情况不合法。

程序中要有报错的机制,不然挂掉的地方可能不在expand op里面,到时将非常难以进行排查。

@wanghaoshuang
Copy link
Contributor Author

我理解不是所有的sequence任务都是变长的,比如见到的一些vedio的序列训练任务是定长的。

嗯,我感觉我们还是实现的灵活一些,先不考虑有没有意义,具体怎么用交给用户决定。

@qingqing01 qingqing01 moved this from Base on LoDTensor to Doing in Port Operators Sep 20, 2017
@qingqing01 qingqing01 moved this from Doing to Done in Port Operators Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants