73
73
'smooth_l1' ,
74
74
'one_hot' ,
75
75
'autoincreased_step_counter' ,
76
+ 'lod_reset' ,
76
77
]
77
78
78
79
@@ -2225,7 +2226,7 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
2225
2226
keep_dim (bool|False): Whether to reserve the reduced dimension in the
2226
2227
output Tensor. The result tensor will have one fewer dimension
2227
2228
than the :attr:`input` unless :attr:`keep_dim` is true.
2228
- name(str|None): A name for this layer(optional). If set None, the
2229
+ name(str|None): A name for this layer(optional). If set None, the
2229
2230
layer will be named automatically.
2230
2231
2231
2232
Returns:
@@ -2241,7 +2242,7 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
2241
2242
fluid.layers.reduce_prod(x) # [0.0002268]
2242
2243
fluid.layers.reduce_prod(x, dim=0) # [0.02, 0.06, 0.3, 0.63]
2243
2244
fluid.layers.reduce_prod(x, dim=-1) # [0.027, 0.0084]
2244
- fluid.layers.reduce_prod(x, dim=1,
2245
+ fluid.layers.reduce_prod(x, dim=1,
2245
2246
keep_dim=True) # [[0.027], [0.0084]]
2246
2247
"""
2247
2248
helper = LayerHelper ('reduce_prod' , ** locals ())
@@ -3292,3 +3293,98 @@ def autoincreased_step_counter(counter_name=None, begin=1, step=1):
3292
3293
counter .stop_gradient = True
3293
3294
3294
3295
return counter
3296
+
3297
+
3298
+ def lod_reset (x , y , target_lod = None ):
3299
+ """
3300
+ LoD Reset Operator. Set LoD of **x** to a new one specified by **y** or
3301
+ **target_lod**. When **y** provided, **y.lod** would be considered as target
3302
+ LoD first, otherwise **y.data** would be considered as target LoD. If **y**
3303
+ is not provided, target LoD should be specified by **target_lod**.
3304
+ If target LoD is specified by **Y.data** or **target_lod**, only one level
3305
+ LoD is supported.
3306
+
3307
+ .. code-block:: text
3308
+
3309
+ * Example 1:
3310
+
3311
+ Given a 1-level LoDTensor x:
3312
+ x.lod = [[ 0, 2, 5 6 ]]
3313
+ x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3314
+ x.dims = [6, 1]
3315
+
3316
+ target_lod: [0, 4, 6]
3317
+
3318
+ then we get a 1-level LoDTensor:
3319
+ out.lod = [[ 0, 4, 6 ]]
3320
+ out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3321
+ out.dims = [6, 1]
3322
+
3323
+ * Example 2:
3324
+
3325
+ Given a 1-level LoDTensor x:
3326
+ x.lod = [[ 0, 2, 5 6 ]]
3327
+ x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3328
+ x.dims = [6, 1]
3329
+
3330
+ y is a Tensor:
3331
+ y.data = [[0, 2, 6]]
3332
+ y.dims = [1, 3]
3333
+
3334
+ then we get a 1-level LoDTensor:
3335
+ out.lod = [[ 0, 2, 6 ]]
3336
+ out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3337
+ out.dims = [6, 1]
3338
+
3339
+ * Example 3:
3340
+
3341
+ Given a 1-level LoDTensor x:
3342
+ x.lod = [[ 0, 2, 5 6 ]]
3343
+ x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3344
+ x.dims = [6, 1]
3345
+
3346
+ y is a 2-level LoDTensor:
3347
+ y.lod = [[0, 2, 4], [0, 2, 5, 6]]
3348
+ y.data = [[1.1], [2.1], [3.1], [4.1], [5.1], [6.1]]
3349
+ y.dims = [6, 1]
3350
+
3351
+ then we get a 2-level LoDTensor:
3352
+ out.lod = [[0, 2, 4], [0, 2, 5, 6]]
3353
+ out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
3354
+ out.dims = [6, 1]
3355
+
3356
+ Args:
3357
+ x (Variable): Input variable which could be a Tensor or LodTensor.
3358
+ y (Variable|None): If provided, output's LoD would be derived from y.
3359
+ target_lod (list|tuple|None): One level LoD which should be considered
3360
+ as target LoD when y not provided.
3361
+
3362
+ Returns:
3363
+ Variable: Output variable with LoD specified by this operator.
3364
+
3365
+ Raises:
3366
+ ValueError: If y and target_lod are both None.
3367
+
3368
+ Examples:
3369
+ .. code-block:: python
3370
+
3371
+ x = layers.data(name='x', shape=[10])
3372
+ y = layers.data(name='y', shape=[10, 20], lod_level=2)
3373
+ out = layers.lod_reset(x=x, y=y)
3374
+ """
3375
+ helper = LayerHelper ("lod_reset" , ** locals ())
3376
+ out = helper .create_tmp_variable (dtype = x .dtype )
3377
+ if y is not None :
3378
+ helper .append_op (
3379
+ type = "lod_reset" , inputs = {'X' : x ,
3380
+ 'Y' : y }, outputs = {'Out' : out })
3381
+ elif target_lod is not None :
3382
+ helper .append_op (
3383
+ type = "lod_reset" ,
3384
+ inputs = {'X' : x },
3385
+ attrs = {'target_lod' : target_lod },
3386
+ outputs = {'Out' : out })
3387
+ else :
3388
+ raise ValueError ("y and target_lod should not be both None." )
3389
+
3390
+ return out
0 commit comments