27
27
28
28
def diag_embed (input , offset = 0 , dim1 = - 2 , dim2 = - 1 ):
29
29
"""
30
- :alias_main: paddle.nn.functional.diag_embed
31
- :alias: paddle.nn.functional.diag_embed,paddle.nn.functional.extension.diag_embed
32
-
33
30
This OP creates a tensor whose diagonals of certain 2D planes (specified by dim1 and dim2)
34
31
are filled by ``input``. By default, a 2D plane formed by the last two dimensions
35
32
of the returned tensor will be selected.
@@ -41,60 +38,59 @@ def diag_embed(input, offset=0, dim1=-2, dim2=-1):
41
38
- If offset < 0, it is below the main diagonal.
42
39
43
40
Args:
44
- input(Variable |numpy.ndarray): The input tensor. Must be at least 1-dimensional. The input data type should be float32, float64, int32, int64.
41
+ input(Tensor |numpy.ndarray): The input tensor. Must be at least 1-dimensional. The input data type should be float32, float64, int32, int64.
45
42
offset(int, optional): Which diagonal to consider. Default: 0 (main diagonal).
46
43
dim1(int, optional): The first dimension with respect to which to take diagonal. Default: -2.
47
44
dim2(int, optional): The second dimension with respect to which to take diagonal. Default: -1.
48
45
49
46
Returns:
50
- Variable , the output data type is the same as input data type.
47
+ Tensor , the output data type is the same as input data type.
51
48
52
49
Examples:
53
50
.. code-block:: python
54
51
55
52
import paddle.nn.functional as F
56
- import paddle.fluid.dygraph as dg
57
53
import numpy as np
58
54
59
55
diag_embed = np.random.randn(2, 3).astype('float32')
60
56
# [[ 0.7545889 , -0.25074545, 0.5929117 ],
61
57
# [-0.6097662 , -0.01753256, 0.619769 ]]
62
- with dg.guard():
63
- data1 = F.diag_embed(diag_embed)
64
- data1.numpy()
65
- # [[[ 0.7545889 , 0. , 0. ],
66
- # [ 0. , -0.25074545, 0. ],
67
- # [ 0. , 0. , 0.5929117 ]],
68
-
69
- # [[-0.6097662 , 0. , 0. ],
70
- # [ 0. , -0.01753256, 0. ],
71
- # [ 0. , 0. , 0.619769 ]]]
72
-
73
- data2 = F.diag_embed(diag_embed, offset=-1, dim1=0, dim2=2)
74
- data2.numpy()
75
- # [[[ 0. , 0. , 0. , 0. ],
76
- # [ 0.7545889 , 0. , 0. , 0. ],
77
- # [ 0. , -0.25074545, 0. , 0. ],
78
- # [ 0. , 0. , 0.5929117 , 0. ]],
79
- #
80
- # [[ 0. , 0. , 0. , 0. ],
81
- # [-0.6097662 , 0. , 0. , 0. ],
82
- # [ 0. , -0.01753256, 0. , 0. ],
83
- # [ 0. , 0. , 0.619769 , 0. ]]]
84
-
85
- data3 = F.diag_embed(diag_embed, offset=1, dim1=0, dim2=2)
86
- data3.numpy()
87
- # [[[ 0. , 0.7545889 , 0. , 0. ],
88
- # [ 0. , -0.6097662 , 0. , 0. ]],
89
- #
90
- # [[ 0. , 0. , -0.25074545, 0. ],
91
- # [ 0. , 0. , -0.01753256, 0. ]],
92
- #
93
- # [[ 0. , 0. , 0. , 0.5929117 ],
94
- # [ 0. , 0. , 0. , 0.619769 ]],
95
- #
96
- # [[ 0. , 0. , 0. , 0. ],
97
- # [ 0. , 0. , 0. , 0. ]]]
58
+
59
+ data1 = F.diag_embed(diag_embed)
60
+ data1.numpy()
61
+ # [[[ 0.7545889 , 0. , 0. ],
62
+ # [ 0. , -0.25074545, 0. ],
63
+ # [ 0. , 0. , 0.5929117 ]],
64
+
65
+ # [[-0.6097662 , 0. , 0. ],
66
+ # [ 0. , -0.01753256, 0. ],
67
+ # [ 0. , 0. , 0.619769 ]]]
68
+
69
+ data2 = F.diag_embed(diag_embed, offset=-1, dim1=0, dim2=2)
70
+ data2.numpy()
71
+ # [[[ 0. , 0. , 0. , 0. ],
72
+ # [ 0.7545889 , 0. , 0. , 0. ],
73
+ # [ 0. , -0.25074545, 0. , 0. ],
74
+ # [ 0. , 0. , 0.5929117 , 0. ]],
75
+ #
76
+ # [[ 0. , 0. , 0. , 0. ],
77
+ # [-0.6097662 , 0. , 0. , 0. ],
78
+ # [ 0. , -0.01753256, 0. , 0. ],
79
+ # [ 0. , 0. , 0.619769 , 0. ]]]
80
+
81
+ data3 = F.diag_embed(diag_embed, offset=1, dim1=0, dim2=2)
82
+ data3.numpy()
83
+ # [[[ 0. , 0.7545889 , 0. , 0. ],
84
+ # [ 0. , -0.6097662 , 0. , 0. ]],
85
+ #
86
+ # [[ 0. , 0. , -0.25074545, 0. ],
87
+ # [ 0. , 0. , -0.01753256, 0. ]],
88
+ #
89
+ # [[ 0. , 0. , 0. , 0.5929117 ],
90
+ # [ 0. , 0. , 0. , 0.619769 ]],
91
+ #
92
+ # [[ 0. , 0. , 0. , 0. ],
93
+ # [ 0. , 0. , 0. , 0. ]]]
98
94
"""
99
95
inputs = {'Input' : [input ]}
100
96
attrs = {'offset' : offset , 'dim1' : dim1 , 'dim2' : dim2 }
@@ -151,15 +147,15 @@ def row_conv(input, weight, act=None):
151
147
${comment}
152
148
153
149
Args:
154
- input (Variable ): the input(X) is a LodTensor or tensor, LodTensor(X)
155
- supports variable time-length input sequences. The underlying
150
+ input (Tensor ): the input(X) is a LodTensor or tensor, LodTensor(X)
151
+ supports variable time-length input sequences. The underlying
156
152
tensor in this LoDTensor is a matrix with shape (T, D), where
157
153
T is the total time steps in this mini-batch and D is the input
158
154
data dimension.
159
155
If the input is a padded minibatch, the shape of the input is
160
156
(N, T, D), N is batch size, T is the max time steps in the batch,
161
157
D is the input data dimension.
162
- weight (Variable ): The weight. A Tensor with shape
158
+ weight (Tensor ): The weight. A Tensor with shape
163
159
(future_context_size + 1, D), where future_context_size is the
164
160
context size of the RowConv operator.
165
161
act (str): Non-linear activation to be applied to output variable.
@@ -171,7 +167,6 @@ def row_conv(input, weight, act=None):
171
167
.. code-block:: python
172
168
173
169
from paddle import fluid, nn
174
- import paddle.fluid.dygraph as dg
175
170
import paddle.nn.functional as F
176
171
import numpy as np
177
172
@@ -182,16 +177,12 @@ def row_conv(input, weight, act=None):
182
177
x = np.random.randn(batch_size, time_steps, feature_size).astype(np.float32)
183
178
weight = np.random.randn(context_size + 1, feature_size).astype(np.float32)
184
179
185
- place = fluid.CPUPlace()
186
- with dg.guard(place):
187
- x_var = dg.to_variable(x)
188
- w_var = dg.to_variable(weight)
189
- y_var = F.extension.row_conv(x_var, w_var)
190
- y_np = y_var.numpy()
191
-
192
- print(y_np.shape)
180
+ x_var = paddle.to_tensor(x)
181
+ w_var = paddle.to_tensor(weight)
182
+ y_var = F.extension.row_conv(x_var, w_var)
183
+ print(y_var.shape)
193
184
194
- # ( 4, 8, 6)
185
+ # [ 4, 8, 6]
195
186
"""
196
187
197
188
if in_dygraph_mode ():
0 commit comments