未验证 提交 7d6263e6 编写于 作者: W wangzhen38 提交者: GitHub

[remove fluid] under unittesets (#48212)

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets

* [remove fluid] under unittesets
上级 bf4d1792
......@@ -23,7 +23,6 @@ from paddle.optimizer import Adam
from paddle.fluid.contrib.slim.quantization import ImperativeQuantAware
from paddle.fluid.contrib.slim.quantization import QuantizationTransformPass
from paddle.nn import Sequential
from paddle.fluid.dygraph import Pool2D
from paddle.fluid.dygraph import Linear
from paddle.nn.quant.quant_layers import QuantizedConv2DTranspose
from paddle.fluid.log_helper import get_logger
......@@ -132,7 +131,7 @@ class ImperativeLenet(paddle.nn.Layer):
stride=1,
padding=1,
),
Pool2D(pool_size=2, pool_type='max', pool_stride=2),
paddle.nn.MaxPool2D(kernel_size=2, stride=2),
paddle.nn.Conv2D(
in_channels=6,
out_channels=16,
......@@ -140,7 +139,7 @@ class ImperativeLenet(paddle.nn.Layer):
stride=1,
padding=0,
),
Pool2D(pool_size=2, pool_type='max', pool_stride=2),
paddle.nn.MaxPool2D(kernel_size=2, stride=2),
)
self.fc = Sequential(
......
......@@ -33,6 +33,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.incubate.distributed.utils.io import load, save
from paddle.nn import Linear
print(load)
epoch = 2
......
......@@ -26,6 +26,7 @@ from paddle.distributed.sharding import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -24,6 +24,7 @@ from paddle.distributed.sharding import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -30,6 +30,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
seed = 2022
epoch = 2
......
......@@ -30,6 +30,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage2 import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
seed = 2022
epoch = 2
......
......@@ -36,6 +36,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -26,6 +26,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -39,8 +39,9 @@ from paddle.distributed.fleet.meta_parallel.parallel_layers.pp_layers import (
from paddle.distributed.sharding.group_sharded import group_sharded_parallel
from paddle.distributed.utils.log_utils import get_logger
from paddle.fluid.dataloader.dataset import IterableDataset
from paddle.fluid.dygraph.nn import Embedding, Linear
from paddle.fluid.dygraph.nn import Embedding
from paddle.incubate.distributed.utils.io import save_for_auto_inference
from paddle.nn import Linear
logger = get_logger("INFO", __file__)
......@@ -76,7 +77,7 @@ class MLP_pipe(PipelineLayer):
gather_output=True,
has_bias=True,
),
LayerDesc(Linear, input_dim=linear_size, output_dim=10),
LayerDesc(Linear, in_features=linear_size, out_features=10),
]
super(MLP_pipe, self).__init__(
desc,
......
......@@ -25,6 +25,7 @@ from paddle.distributed.fleet.meta_optimizers.dygraph_optimizer.sharding_optimiz
from paddle.distributed.fleet.utils.internal_storage import GradStorage
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
base_lr = 0.1
momentum_rate = 0.9
......
......@@ -31,6 +31,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_stage2 import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
seed = 2022
epoch = 2
......
......@@ -37,6 +37,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_utils import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2021)
......
......@@ -27,6 +27,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_utils import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -19,6 +19,7 @@ import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
np.random.seed(2021)
paddle.seed(1024)
......
......@@ -27,6 +27,7 @@ import paddle
import paddle.distributed as dist
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
seed = 90
RUN_STEP = 20
......@@ -37,9 +38,9 @@ batch_num = 1000
class SimpleNet(fluid.Layer):
def __init__(self):
super().__init__()
self.net_a = Linear(input_dim=10, output_dim=20)
self.net_b = Linear(input_dim=20, output_dim=5)
self.net_c = Linear(input_dim=5, output_dim=10)
self.net_a = Linear(10, 20)
self.net_b = Linear(20, 5)
self.net_c = Linear(5, 10)
def forward(self, x):
x = self.net_a(x)
......
......@@ -19,6 +19,7 @@ from test_dist_base import runtime_main
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
seed = 90
RUN_STEP = 20
......@@ -29,9 +30,9 @@ batch_num = 1000
class SimpleNetControlFlow(fluid.Layer):
def __init__(self):
super().__init__()
self.net_a = Linear(input_dim=10, output_dim=20)
self.net_b = Linear(input_dim=20, output_dim=5)
self.net_c = Linear(input_dim=5, output_dim=10)
self.net_a = Linear(10, 20)
self.net_b = Linear(20, 5)
self.net_c = Linear(5, 10)
self.step = 0
def forward(self, x):
......
......@@ -19,7 +19,7 @@ import numpy as np
import paddle
import paddle.distributed as dist
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
paddle.seed(1024)
np.random.seed(2021)
......
......@@ -18,7 +18,7 @@ from test_dist_base import runtime_main
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
seed = 90
RUN_STEP = 20
......@@ -29,11 +29,11 @@ batch_num = 1000
class SimpleNetUnusedParam(fluid.Layer):
def __init__(self):
super().__init__()
self.net_a = Linear(input_dim=10, output_dim=20)
self.net_b = Linear(input_dim=20, output_dim=5)
self.net_c = Linear(input_dim=5, output_dim=10)
self.net_a = Linear(10, 20)
self.net_b = Linear(20, 5)
self.net_c = Linear(5, 10)
self.net_d = Linear(input_dim=20, output_dim=10)
self.net_d = Linear(20, 10)
def forward(self, x):
x = self.net_a(x)
......
......@@ -20,7 +20,8 @@ from test_dist_base import TestParallelDyGraphRunnerBase, runtime_main
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Linear, Pool2D
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
batch_size = 64
momentum_rate = 0.9
......@@ -114,31 +115,33 @@ class SqueezeExcitation(fluid.dygraph.Layer):
super().__init__()
self._num_channels = num_channels
self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True)
self._pool = paddle.fluid.dygraph.nn.Pool2D(
pool_size=0, pool_type='avg', global_pooling=True
)
stdv = 1.0 / math.sqrt(num_channels * 1.0)
self._squeeze = Linear(
num_channels,
num_channels // reduction_ratio,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(-stdv, stdv)
),
act='relu',
)
stdv = 1.0 / math.sqrt(num_channels / 16.0 * 1.0)
self._excitation = Linear(
num_channels // reduction_ratio,
num_channels,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(-stdv, stdv)
),
act='sigmoid',
)
def forward(self, input):
y = self._pool(input)
y = paddle.reshape(y, shape=[-1, self._num_channels])
y = self._squeeze(y)
y = paddle.nn.functional.relu(y)
y = self._excitation(y)
y = paddle.nn.functional.sigmoid(y)
y = fluid.layers.elementwise_mul(x=input, y=y, axis=0)
return y
......@@ -231,9 +234,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 101:
cardinality = 32
reduction_ratio = 16
......@@ -246,9 +247,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 152:
cardinality = 64
reduction_ratio = 16
......@@ -275,9 +274,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=1,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
self.bottleneck_block_list = []
num_channels = 64
......@@ -299,7 +296,7 @@ class SeResNeXt(fluid.dygraph.Layer):
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
stdv = 1.0 / math.sqrt(2048 * 1.0)
......@@ -309,8 +306,8 @@ class SeResNeXt(fluid.dygraph.Layer):
self.out = Linear(
self.pool2d_avg_output,
class_dim,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(-stdv, stdv)
),
)
......
......@@ -994,7 +994,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer):
[
'recv_v2',
'cast',
'matmul',
'matmul_v2',
'cast',
'reduce_mean',
'elementwise_mul',
......@@ -1002,7 +1002,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer):
'elementwise_mul_grad',
'reduce_mean_grad',
'cast',
'matmul_grad',
'matmul_v2_grad',
'c_sync_calc_stream',
'send_v2',
'fill_constant',
......@@ -1087,7 +1087,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer):
[
'recv_v2',
'cast',
'matmul',
'matmul_v2',
'cast',
'reduce_mean',
'elementwise_mul',
......@@ -1095,7 +1095,7 @@ class TestFleetHybridOptimizerBoundary(TestFleetMetaOptimizer):
'elementwise_mul_grad',
'reduce_mean_grad',
'cast',
'matmul_grad',
'matmul_v2_grad',
'c_sync_calc_stream',
'send_v2',
'fill_constant',
......
......@@ -36,6 +36,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
epoch = 10
paddle.seed(2022)
......
......@@ -31,6 +31,7 @@ from paddle.distributed.fleet.meta_parallel.sharding.sharding_stage2 import (
)
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
seed = 2022
epoch = 2
......
......@@ -16,7 +16,8 @@ from transformer_dygraph_model import MultiHeadAttention, PrePostProcessLayer
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import Embedding, Layer, Linear
from paddle.fluid.dygraph import Embedding, Layer
from paddle.nn import Linear
from paddle.jit.api import declarative
......@@ -33,19 +34,18 @@ class PositionwiseFeedForwardLayer(Layer):
super().__init__()
self._i2h = Linear(
input_dim=d_model,
output_dim=d_inner_hid,
param_attr=fluid.ParamAttr(
in_features=d_model,
out_features=d_inner_hid,
weight_attr=fluid.ParamAttr(
name=name + '_fc_0.w_0', initializer=param_initializer
),
bias_attr=name + '_fc_0.b_0',
act=hidden_act,
)
self._h2o = Linear(
input_dim=d_inner_hid,
output_dim=d_model,
param_attr=fluid.ParamAttr(
in_features=d_inner_hid,
out_features=d_model,
weight_attr=fluid.ParamAttr(
name=name + '_fc_1.w_0', initializer=param_initializer
),
bias_attr=name + '_fc_1.b_0',
......@@ -234,13 +234,12 @@ class BertModelLayer(Layer):
)
self.pooled_fc = Linear(
input_dim=self._emb_size,
output_dim=self._emb_size,
param_attr=fluid.ParamAttr(
in_features=self._emb_size,
out_features=self._emb_size,
weight_attr=fluid.ParamAttr(
name="pooled_fc.w_0", initializer=self._param_initializer
),
bias_attr="pooled_fc.b_0",
act="tanh",
)
self.pre_process_layer = PrePostProcessLayer(
......@@ -295,6 +294,8 @@ class BertModelLayer(Layer):
input=enc_output, axes=[1], starts=[0], ends=[1]
)
next_sent_feat = self.pooled_fc(next_sent_feat)
next_sent_feat = paddle.tanh(next_sent_feat)
next_sent_feat = paddle.reshape(
next_sent_feat, shape=[-1, self._emb_size]
)
......@@ -334,13 +335,12 @@ class PretrainModelLayer(Layer):
)
self.pooled_fc = Linear(
input_dim=self._emb_size,
output_dim=self._emb_size,
param_attr=fluid.ParamAttr(
in_features=self._emb_size,
out_features=self._emb_size,
weight_attr=fluid.ParamAttr(
name="mask_lm_trans_fc.w_0", initializer=self._param_initializer
),
bias_attr="mask_lm_trans_fc.b_0",
act="tanh",
)
self.mask_lm_out_bias_attr = fluid.ParamAttr(
......@@ -350,9 +350,9 @@ class PretrainModelLayer(Layer):
if not self._weight_sharing:
self.out_fc = Linear(
input_dim=self._emb_size,
output_dim=self._voc_size,
param_attr=fluid.ParamAttr(
in_features=self._emb_size,
out_features=self._voc_size,
weight_attr=fluid.ParamAttr(
name="mask_lm_out_fc.w_0",
initializer=self._param_initializer,
),
......@@ -367,9 +367,9 @@ class PretrainModelLayer(Layer):
)
self.next_sent_fc = Linear(
input_dim=self._emb_size,
output_dim=2,
param_attr=fluid.ParamAttr(
in_features=self._emb_size,
out_features=2,
weight_attr=fluid.ParamAttr(
name="next_sent_fc.w_0", initializer=self._param_initializer
),
bias_attr="next_sent_fc.b_0",
......@@ -397,6 +397,7 @@ class PretrainModelLayer(Layer):
mask_feat = paddle.gather(reshaped_emb_out, index=mask_pos)
mask_trans_feat = self.pooled_fc(mask_feat)
mask_trans_feat = paddle.tanh(mask_trans_feat)
mask_trans_feat = self.pre_process_layer(mask_trans_feat)
if self._weight_sharing:
......
......@@ -232,14 +232,14 @@ class NetWithControlFlowIf(fluid.dygraph.Layer):
def __init__(self, hidden_dim=16):
super().__init__()
self.hidden_dim = hidden_dim
self.fc = fluid.dygraph.Linear(
input_dim=hidden_dim,
output_dim=5,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
self.fc = paddle.nn.Linear(
in_features=hidden_dim,
out_features=5,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
self.alpha = 10.0
......
......@@ -166,10 +166,14 @@ class BaseModel(fluid.dygraph.Layer):
)
)
self.fc = fluid.dygraph.nn.Linear(
self.fc = paddle.nn.Linear(
self.hidden_size,
self.tar_vocab_size,
param_attr=param_attr,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(
low=-self.init_scale, high=self.init_scale
)
),
bias_attr=False,
)
......@@ -611,31 +615,38 @@ class AttentionModel(fluid.dygraph.Layer):
)
)
self.attn_fc = fluid.dygraph.nn.Linear(
self.attn_fc = paddle.nn.Linear(
self.hidden_size,
self.hidden_size,
param_attr=ParamAttr(
weight_attr=paddle.ParamAttr(
name="self_attn_fc",
initializer=uniform_initializer(self.init_scale),
initializer=paddle.nn.initializer.Uniform(
low=-self.init_scale, high=self.init_scale
),
),
bias_attr=False,
)
self.concat_fc = fluid.dygraph.nn.Linear(
self.concat_fc = paddle.nn.Linear(
2 * self.hidden_size,
self.hidden_size,
param_attr=ParamAttr(
weight_attr=paddle.ParamAttr(
name="self_concat_fc",
initializer=uniform_initializer(self.init_scale),
initializer=paddle.nn.initializer.Uniform(
low=-self.init_scale, high=self.init_scale
),
),
bias_attr=False,
)
self.fc = fluid.dygraph.nn.Linear(
self.fc = paddle.nn.Linear(
self.hidden_size,
self.tar_vocab_size,
param_attr=ParamAttr(
name="self_fc", initializer=uniform_initializer(self.init_scale)
weight_attr=paddle.ParamAttr(
name="self_fc",
initializer=paddle.nn.initializer.Uniform(
low=-self.init_scale, high=self.init_scale
),
),
bias_attr=False,
)
......
......@@ -17,7 +17,10 @@ from functools import reduce
import paddle
import paddle.fluid as fluid
import paddle.fluid.param_attr as attr
from paddle.fluid.dygraph import Embedding, Layer, Linear
from functools import reduce
from paddle.fluid.dygraph import Embedding, Layer
from paddle.jit.api import declarative
from paddle.static import Variable
......@@ -490,7 +493,7 @@ class BOW(Layer):
self.emb_layer = EmbeddingLayer(
self.dict_size, self.emb_dim, "emb"
).ops()
self.bow_layer = Linear(self.bow_dim, self.bow_dim)
self.bow_layer = paddle.nn.Linear(self.bow_dim, self.bow_dim)
self.bow_layer_po = FCLayer(self.bow_dim, None, "fc").ops()
self.softmax_layer = FCLayer(2, "softmax", "cos_sim").ops()
......
......@@ -149,15 +149,15 @@ def dyfunc_Conv2D(input):
def dyfunc_Conv3D(input):
conv3d = fluid.dygraph.Conv3D(
num_channels=3,
num_filters=2,
filter_size=3,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
conv3d = paddle.nn.Conv3D(
in_channels=3,
out_channels=2,
kernel_size=3,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
res = conv3d(input)
......@@ -182,16 +182,15 @@ def dyfunc_Conv2DTranspose(input):
def dyfunc_Conv3DTranspose(input):
conv3dTranspose = fluid.dygraph.nn.Conv3DTranspose(
num_channels=3,
num_filters=12,
filter_size=12,
use_cudnn=False,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
conv3dTranspose = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=12,
kernel_size=12,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
ret = conv3dTranspose(input)
......@@ -199,28 +198,24 @@ def dyfunc_Conv3DTranspose(input):
def dyfunc_Linear(input):
fc = fluid.dygraph.Linear(
input_dim=10,
output_dim=5,
act='relu',
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
fc = paddle.nn.Linear(
in_features=10,
out_features=5,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
m = paddle.nn.ReLU()
res = fc(input)
return res
return m(res)
def dyfunc_Pool2D(input):
fluid.dygraph.Pool2D(
pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False
)
pool2d = fluid.dygraph.Pool2D(
pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False
)
paddle.nn.AvgPool2D(kernel_size=2, stride=1)
pool2d = paddle.nn.AvgPool2D(kernel_size=2, stride=1)
res = pool2d(input)
return res
......
......@@ -218,15 +218,14 @@ class BMN(fluid.dygraph.Layer):
self.sample_mask = fluid.dygraph.base.to_variable(sample_mask)
self.sample_mask.stop_gradient = True
self.p_conv3d1 = fluid.dygraph.Conv3D(
num_channels=128,
num_filters=self.hidden_dim_3d,
filter_size=(self.num_sample, 1, 1),
self.p_conv3d1 = paddle.nn.Conv3D(
in_channels=128,
out_channels=self.hidden_dim_3d,
kernel_size=(self.num_sample, 1, 1),
stride=(self.num_sample, 1, 1),
padding=0,
act="relu",
param_attr=ParamAttr(name="PEM_3d1_w"),
bias_attr=ParamAttr(name="PEM_3d1_b"),
weight_attr=paddle.ParamAttr(name="PEM_3d1_w"),
bias_attr=paddle.ParamAttr(name="PEM_3d1_b"),
)
self.p_conv2d1 = paddle.nn.Conv2D(
......@@ -287,6 +286,7 @@ class BMN(fluid.dygraph.Layer):
xp = paddle.reshape(xp, shape=[0, 0, -1, self.dscale, self.tscale])
xp = self.p_conv3d1(xp)
xp = paddle.tanh(xp)
xp = paddle.squeeze(xp, axis=[2])
xp = paddle.nn.functional.relu(self.p_conv2d1(xp))
xp = paddle.nn.functional.relu(self.p_conv2d2(xp))
......
......@@ -155,23 +155,23 @@ class MyLayer(fluid.dygraph.Layer):
super().__init__()
self.conv = MyConvLayer()
self.fc = fluid.dygraph.Linear(
input_dim=5,
output_dim=1,
act='relu',
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
self.fc = paddle.nn.Linear(
in_features=5,
out_features=1,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
self.act = paddle.nn.ReLU()
@paddle.jit.to_static
def forward(self, inputs):
h = self.conv(inputs)
out = self.fc(h)
return out
return self.act(out)
class TestRecursiveCall2(unittest.TestCase):
......
......@@ -36,7 +36,7 @@ program_trans = ProgramTranslator()
class SimpleNet(Layer):
def __init__(self):
super().__init__()
self.linear = fluid.dygraph.Linear(10, 3)
self.linear = paddle.nn.Linear(10, 3)
@declarative(input_spec=[InputSpec(shape=[None, 10], dtype='float32')])
def forward(self, x, a=1, b=2):
......
......@@ -30,27 +30,27 @@ class SubNetWithDict(fluid.dygraph.Layer):
def __init__(self, hidden_size=16, output_size=16):
super().__init__()
init_weight = lambda x: fluid.ParamAttr(
initializer=fluid.initializer.Constant(x)
init_weight = lambda x: paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(x)
)
self.q_fc = fluid.dygraph.Linear(
input_dim=hidden_size,
output_dim=output_size,
self.q_fc = paddle.nn.Linear(
in_features=hidden_size,
out_features=output_size,
bias_attr=False,
param_attr=init_weight(0.6),
weight_attr=init_weight(0.6),
)
self.k_fc = fluid.dygraph.Linear(
input_dim=hidden_size,
output_dim=output_size,
self.k_fc = paddle.nn.Linear(
in_features=hidden_size,
out_features=output_size,
bias_attr=False,
param_attr=init_weight(0.5),
weight_attr=init_weight(0.5),
)
self.v_fc = fluid.dygraph.Linear(
input_dim=hidden_size,
output_dim=output_size,
self.v_fc = paddle.nn.Linear(
in_features=hidden_size,
out_features=output_size,
bias_attr=False,
param_attr=init_weight(0.2),
weight_attr=init_weight(0.2),
)
def forward(self, input, cache=None):
......
......@@ -68,7 +68,7 @@ def func_decorated_by_other_2():
class LayerErrorInCompiletime(fluid.dygraph.Layer):
def __init__(self, fc_size=20):
super().__init__()
self._linear = fluid.dygraph.Linear(fc_size, fc_size)
self._linear = paddle.nn.Linear(fc_size, fc_size)
@paddle.jit.to_static(
input_spec=[paddle.static.InputSpec(shape=[20, 20], dtype='float32')]
......
......@@ -27,9 +27,7 @@ SEED = 2020
class Pool2D(fluid.dygraph.Layer):
def __init__(self):
super().__init__()
self.pool2d = fluid.dygraph.Pool2D(
pool_size=2, pool_type='avg', pool_stride=1, global_pooling=False
)
self.pool2d = paddle.nn.AvgPool2D(kernel_size=2, stride=1)
@declarative
def forward(self, x):
......@@ -44,21 +42,22 @@ class Pool2D(fluid.dygraph.Layer):
class Linear(fluid.dygraph.Layer):
def __init__(self, input_dim=10, output_dim=5):
super().__init__()
self.fc = fluid.dygraph.Linear(
self.fc = paddle.nn.Linear(
input_dim,
output_dim,
act='relu',
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
self.act = paddle.nn.ReLU()
@declarative
def forward(self, x):
pre = self.fc(x)
pre = self.act(pre)
loss = paddle.mean(pre)
return pre, loss
......
......@@ -24,8 +24,10 @@ os.environ["CUDA_VISIBLE_DEVICES"] = "2"
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph import Embedding, GRUUnit
from paddle import _legacy_C_ops
from paddle.fluid.dygraph import Embedding, GRUUnit, Linear, to_variable
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
from paddle.fluid.framework import _non_static_mode
from paddle.jit import ProgramTranslator
......@@ -100,10 +102,10 @@ class BiGRU(fluid.dygraph.Layer):
def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None):
super().__init__()
self.pre_gru = Linear(
input_dim=input_dim,
output_dim=grnn_hidden_dim * 3,
param_attr=fluid.ParamAttr(
self.pre_gru = paddle.nn.Linear(
in_features=input_dim,
out_features=grnn_hidden_dim * 3,
weight_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound
),
......@@ -126,10 +128,10 @@ class BiGRU(fluid.dygraph.Layer):
),
)
self.pre_gru_r = Linear(
input_dim=input_dim,
output_dim=grnn_hidden_dim * 3,
param_attr=fluid.ParamAttr(
self.pre_gru_r = paddle.nn.Linear(
in_features=input_dim,
out_features=grnn_hidden_dim * 3,
weight_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-init_bound, high=init_bound
),
......@@ -417,10 +419,10 @@ class LexNet(fluid.dygraph.Layer):
)
)
self.fc = Linear(
input_dim=self.grnn_hidden_dim * 2,
output_dim=self.num_labels,
param_attr=fluid.ParamAttr(
self.fc = paddle.nn.Linear(
in_features=self.grnn_hidden_dim * 2,
out_features=self.num_labels,
weight_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(
low=-self.init_bound, high=self.init_bound
),
......
......@@ -23,9 +23,9 @@ from predictor_utils import PredictorTools
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.nn import Linear
from paddle.fluid.dygraph.base import switch_to_static_graph
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
from paddle.fluid.dygraph.nn import Linear, Pool2D
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import AdamOptimizer
......@@ -69,7 +69,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer):
bias_attr=None,
)
self._pool2d = Pool2D(
self._pool2d = paddle.fluid.dygraph.nn.Pool2D(
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
......@@ -102,12 +102,9 @@ class MNIST(fluid.dygraph.Layer):
self._fc = Linear(
self.pool_2_shape,
10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale
)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale)
),
act="softmax",
)
def forward(self, inputs, label=None):
......@@ -126,6 +123,7 @@ class MNIST(fluid.dygraph.Layer):
x = self._simple_img_conv_pool_2(x)
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
x = self._fc(x)
x = paddle.nn.functional.softmax(x)
return x
......
......@@ -23,11 +23,19 @@ from predictor_utils import PredictorTools
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D
from paddle.fluid.dygraph.nn import BatchNorm, Linear
from paddle.fluid.initializer import MSRA
from paddle.fluid.param_attr import ParamAttr
from paddle.jit import ProgramTranslator
from paddle.fluid.dygraph.nn import BatchNorm
from paddle.nn import Linear
from paddle.jit.api import declarative
from paddle.jit import ProgramTranslator
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
import unittest
from predictor_utils import PredictorTools
# Note: Set True to eliminate randomness.
# 1. For one operation, cuDNN has several algorithms,
......@@ -255,12 +263,14 @@ class MobileNetV1(fluid.dygraph.Layer):
)
self.dwsl.append(dws6)
self.pool2d_avg = Pool2D(pool_type='avg', global_pooling=True)
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_type='avg', global_pooling=True
)
self.out = Linear(
int(1024 * scale),
class_dim,
param_attr=ParamAttr(
weight_attr=ParamAttr(
initializer=MSRA(), name=self.full_name() + "fc7_weights"
),
bias_attr=ParamAttr(name="fc7_offset"),
......@@ -421,14 +431,16 @@ class MobileNetV2(fluid.dygraph.Layer):
)
# 4. pool
self._pool2d_avg = Pool2D(pool_type='avg', global_pooling=True)
self._pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_type='avg', global_pooling=True
)
# 5. fc
tmp_param = ParamAttr(name=self.full_name() + "fc10_weights")
self._fc = Linear(
self._out_c,
class_dim,
param_attr=tmp_param,
weight_attr=tmp_param,
bias_attr=ParamAttr(name="fc10_offset"),
)
......
......@@ -204,7 +204,7 @@ class StaticCode2:
class NetWithError(fluid.dygraph.layers.Layer):
@declarative
def forward(self, x):
linear = fluid.dygraph.Linear(32, 64)
linear = paddle.nn.Linear(32, 64)
y = linear(x)
return y
......
......@@ -21,8 +21,7 @@ import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.dygraph.nn as nn
from paddle.fluid.dygraph import Layer, to_variable
from paddle.fluid.dygraph import to_variable, Layer
from paddle.jit import ProgramTranslator
from paddle.jit.api import declarative
......@@ -34,8 +33,8 @@ class Policy(Layer):
def __init__(self):
super().__init__()
self.affine1 = nn.Linear(4, 128)
self.affine2 = nn.Linear(128, 2)
self.affine1 = paddle.nn.Linear(4, 128)
self.affine2 = paddle.nn.Linear(128, 2)
self.dropout_ratio = 0.6
self.saved_log_probs = []
......
......@@ -17,14 +17,16 @@ import os
import tempfile
import time
import unittest
import paddle
import numpy as np
from predictor_utils import PredictorTools
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import BatchNorm
from paddle.jit import ProgramTranslator
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D
from paddle.jit import ProgramTranslator
SEED = 2020
......@@ -165,9 +167,7 @@ class ResNet(fluid.dygraph.Layer):
self.conv = ConvBNLayer(
num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu'
)
self.pool2d_max = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool2d_max = paddle.nn.MaxPool2D(kernel_size=3, stride=2)
self.bottleneck_block_list = []
for block in range(len(depth)):
......@@ -186,8 +186,7 @@ class ResNet(fluid.dygraph.Layer):
)
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
......@@ -195,11 +194,10 @@ class ResNet(fluid.dygraph.Layer):
stdv = 1.0 / math.sqrt(2048 * 1.0)
self.out = Linear(
self.out = paddle.nn.Linear(
self.pool2d_avg_output,
class_dim,
act='softmax',
param_attr=fluid.param_attr.ParamAttr(
weight_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
),
)
......@@ -212,6 +210,7 @@ class ResNet(fluid.dygraph.Layer):
y = self.pool2d_avg(y)
y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output])
pred = self.out(y)
pred = paddle.nn.functional.softmax(pred)
return pred
......
......@@ -163,8 +163,8 @@ class ResNet(paddle.nn.Layer):
self.conv = ConvBNLayer(
num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu'
)
self.pool2d_max = paddle.fluid.dygraph.Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
self.pool2d_max = paddle.nn.MaxPool2D(
kernel_size=3, stride=2, padding=1
)
self.bottleneck_block_list = []
......@@ -184,7 +184,6 @@ class ResNet(paddle.nn.Layer):
)
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = paddle.fluid.dygraph.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
......
......@@ -39,7 +39,7 @@ program_translator = ProgramTranslator()
class SimpleFcLayer(fluid.dygraph.Layer):
def __init__(self, fc_size):
super().__init__()
self._linear = fluid.dygraph.Linear(fc_size, fc_size)
self._linear = paddle.nn.Linear(fc_size, fc_size)
@declarative
def forward(self, x):
......
......@@ -25,8 +25,9 @@ from predictor_utils import PredictorTools
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import BatchNorm
from paddle.nn import Linear
from paddle.fluid.dygraph.io import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX
from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D
from paddle.jit import ProgramTranslator
from paddle.jit.api import declarative
......@@ -126,31 +127,33 @@ class SqueezeExcitation(fluid.dygraph.Layer):
super().__init__()
self._num_channels = num_channels
self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True)
self._pool = paddle.fluid.dygraph.nn.Pool2D(
pool_size=0, pool_type='avg', global_pooling=True
)
stdv = 1.0 / math.sqrt(num_channels * 1.0)
self._fc = Linear(
num_channels,
num_channels // reduction_ratio,
param_attr=fluid.ParamAttr(
weight_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
),
act='relu',
)
stdv = 1.0 / math.sqrt(num_channels / 16.0 * 1.0)
self._excitation = Linear(
num_channels // reduction_ratio,
num_channels,
param_attr=fluid.ParamAttr(
weight_attr=fluid.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
),
act='sigmoid',
)
def forward(self, input):
y = self._pool(input)
y = paddle.reshape(y, shape=[-1, self._num_channels])
y = self._fc(y)
y = paddle.nn.functional.relu(y)
y = self._excitation(y)
y = paddle.nn.functional.sigmoid(y)
y = fluid.layers.elementwise_mul(x=input, y=y, axis=0)
return y
......@@ -243,9 +246,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 101:
cardinality = 32
reduction_ratio = 16
......@@ -258,9 +259,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 152:
cardinality = 64
reduction_ratio = 16
......@@ -287,9 +286,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=1,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
self.bottleneck_block_list = []
num_channels = 64
......@@ -312,8 +309,7 @@ class SeResNeXt(fluid.dygraph.Layer):
num_channels = bottleneck_block._num_channels_out
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
stdv = 1.0 / math.sqrt(2048 * 1.0)
......@@ -323,7 +319,7 @@ class SeResNeXt(fluid.dygraph.Layer):
self.out = Linear(
self.pool2d_avg_output,
class_dim,
param_attr=fluid.param_attr.ParamAttr(
weight_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
),
)
......
......@@ -19,6 +19,8 @@ from test_lac import DynamicGRU
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Embedding
from paddle.nn import Linear
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph.nn import Embedding, Linear
from paddle.jit import ProgramTranslator
......@@ -83,13 +85,11 @@ class CNN(fluid.dygraph.Layer):
batch_size=self.batch_size,
)
self._fc1 = Linear(
input_dim=self.hid_dim * self.seq_len,
output_dim=self.fc_hid_dim,
act="softmax",
)
self._fc_prediction = Linear(
input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax"
self.hid_dim * self.seq_len,
self.fc_hid_dim,
)
self._fc1_act = paddle.nn.Softmax()
self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim)
@declarative
def forward(self, inputs, label=None):
......@@ -104,7 +104,9 @@ class CNN(fluid.dygraph.Layer):
)
conv_3 = self._simple_conv_pool_1(emb)
fc_1 = self._fc1(conv_3)
fc_1 = self._fc1_act(fc_1)
prediction = self._fc_prediction(fc_1)
prediction = self._fc1_act(prediction)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
......@@ -127,15 +129,9 @@ class BOW(fluid.dygraph.Layer):
dtype='float32',
is_sparse=False,
)
self._fc1 = Linear(
input_dim=self.hid_dim, output_dim=self.hid_dim, act="tanh"
)
self._fc2 = Linear(
input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh"
)
self._fc_prediction = Linear(
input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax"
)
self._fc1 = Linear(self.hid_dim, self.hid_dim)
self._fc2 = Linear(self.hid_dim, self.fc_hid_dim)
self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim)
@declarative
def forward(self, inputs, label=None):
......@@ -149,8 +145,11 @@ class BOW(fluid.dygraph.Layer):
bow_1 = paddle.sum(emb, axis=1)
bow_1 = paddle.tanh(bow_1)
fc_1 = self._fc1(bow_1)
fc_1 = paddle.tanh(fc_1)
fc_2 = self._fc2(fc_1)
fc_2 = paddle.tanh(fc_2)
prediction = self._fc_prediction(fc_2)
prediction = paddle.nn.functional.softmax(prediction)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
......@@ -176,13 +175,9 @@ class GRU(fluid.dygraph.Layer):
)
h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32")
h_0 = to_variable(h_0)
self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.hid_dim * 3)
self._fc2 = Linear(
input_dim=self.hid_dim, output_dim=self.fc_hid_dim, act="tanh"
)
self._fc_prediction = Linear(
input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax"
)
self._fc1 = Linear(self.hid_dim, self.hid_dim * 3)
self._fc2 = Linear(self.hid_dim, self.fc_hid_dim)
self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim)
self._gru = DynamicGRU(size=self.hid_dim, h_0=h_0)
@declarative
......@@ -199,8 +194,9 @@ class GRU(fluid.dygraph.Layer):
gru_hidden = paddle.max(gru_hidden, axis=1)
tanh_1 = paddle.tanh(gru_hidden)
fc_2 = self._fc2(tanh_1)
fc_2 = paddle.tanh(fc_2)
prediction = self._fc_prediction(fc_2)
prediction = paddle.nn.functional.softmax(prediction)
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = paddle.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
......@@ -225,13 +221,9 @@ class BiGRU(fluid.dygraph.Layer):
)
h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32")
h_0 = to_variable(h_0)
self._fc1 = Linear(input_dim=self.hid_dim, output_dim=self.hid_dim * 3)
self._fc2 = Linear(
input_dim=self.hid_dim * 2, output_dim=self.fc_hid_dim, act="tanh"
)
self._fc_prediction = Linear(
input_dim=self.fc_hid_dim, output_dim=self.class_dim, act="softmax"
)
self._fc1 = Linear(self.hid_dim, self.hid_dim * 3)
self._fc2 = Linear(self.hid_dim * 2, self.fc_hid_dim)
self._fc_prediction = Linear(self.fc_hid_dim, self.class_dim)
self._gru_forward = DynamicGRU(
size=self.hid_dim, h_0=h_0, is_reverse=False
)
......@@ -259,7 +251,9 @@ class BiGRU(fluid.dygraph.Layer):
)
encoded_vector = paddle.max(encoded_vector, axis=1)
fc_2 = self._fc2(encoded_vector)
fc_2 = paddle.tanh(fc_2)
prediction = self._fc_prediction(fc_2)
prediction = paddle.nn.functional.softmax(prediction)
# TODO(Aurelius84): Uncomment the following codes when we support return variable-length vars.
# if label is not None:
cost = fluid.layers.cross_entropy(input=prediction, label=label)
......
......@@ -23,10 +23,12 @@ from tsm_config_utils import merge_configs, parse_config, print_configs
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import to_variable
from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D
from paddle.jit import ProgramTranslator
from paddle.fluid.dygraph.nn import BatchNorm
from paddle.nn import Linear
from paddle.jit.api import declarative
from paddle.jit import ProgramTranslator
from paddle.fluid.dygraph import to_variable
from tsm_config_utils import merge_configs, parse_config, print_configs
random.seed(0)
np.random.seed(0)
......@@ -159,8 +161,8 @@ class TSM_ResNet(fluid.dygraph.Layer):
self.conv = ConvBNLayer(
num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu'
)
self.pool2d_max = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
self.pool2d_max = paddle.nn.MaxPool2D(
kernel_size=3, stride=2, padding=1
)
self.bottleneck_block_list = []
......@@ -182,10 +184,9 @@ class TSM_ResNet(fluid.dygraph.Layer):
num_channels = int(bottleneck_block._num_channels_out)
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
import math
stdv = 1.0 / math.sqrt(2048 * 1.0)
......@@ -193,12 +194,11 @@ class TSM_ResNet(fluid.dygraph.Layer):
self.out = Linear(
2048,
self.class_dim,
act="softmax",
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(-stdv, stdv)
),
bias_attr=fluid.param_attr.ParamAttr(
learning_rate=2.0, regularizer=fluid.regularizer.L2Decay(0.0)
bias_attr=paddle.ParamAttr(
learning_rate=2.0, regularizer=paddle.regularizer.L1Decay()
),
)
......@@ -215,6 +215,7 @@ class TSM_ResNet(fluid.dygraph.Layer):
y = paddle.mean(y, axis=1)
y = paddle.reshape(y, shape=[-1, 2048])
y = self.out(y)
y = paddle.nn.functional.softmax(y)
return y
......
......@@ -22,9 +22,9 @@ from paddle.fluid.dygraph import (
Embedding,
Layer,
LayerNorm,
Linear,
to_variable,
)
from paddle.nn import Linear
from paddle.fluid.layers.utils import map_structure
from paddle.jit.api import dygraph_to_static_func
......@@ -107,28 +107,28 @@ class MultiHeadAttention(Layer):
self.d_model = d_model
self.dropout_rate = dropout_rate
self.q_fc = Linear(
input_dim=d_model,
output_dim=d_key * n_head,
in_features=d_model,
out_features=d_key * n_head,
bias_attr=False,
param_attr=fluid.ParamAttr(initializer=param_initializer),
weight_attr=fluid.ParamAttr(initializer=param_initializer),
)
self.k_fc = Linear(
input_dim=d_model,
output_dim=d_key * n_head,
in_features=d_model,
out_features=d_key * n_head,
bias_attr=False,
param_attr=fluid.ParamAttr(initializer=param_initializer),
weight_attr=fluid.ParamAttr(initializer=param_initializer),
)
self.v_fc = Linear(
input_dim=d_model,
output_dim=d_value * n_head,
in_features=d_model,
out_features=d_value * n_head,
bias_attr=False,
param_attr=fluid.ParamAttr(initializer=param_initializer),
weight_attr=fluid.ParamAttr(initializer=param_initializer),
)
self.proj_fc = Linear(
input_dim=d_value * n_head,
output_dim=d_model,
in_features=d_value * n_head,
out_features=d_model,
bias_attr=False,
param_attr=fluid.ParamAttr(initializer=param_initializer),
weight_attr=fluid.ParamAttr(initializer=param_initializer),
)
def forward(self, queries, keys, values, attn_bias, cache=None):
......@@ -174,11 +174,12 @@ class FFN(Layer):
def __init__(self, d_inner_hid, d_model, dropout_rate):
super().__init__()
self.dropout_rate = dropout_rate
self.fc1 = Linear(input_dim=d_model, output_dim=d_inner_hid, act="relu")
self.fc2 = Linear(input_dim=d_inner_hid, output_dim=d_model)
self.fc1 = Linear(d_model, d_inner_hid)
self.fc2 = Linear(d_inner_hid, d_model)
def forward(self, x):
hidden = self.fc1(x)
hidden = paddle.nn.functional.relu(hidden)
if self.dropout_rate:
hidden = layers.dropout(hidden, dropout_prob=self.dropout_rate)
out = self.fc2(hidden)
......
......@@ -117,10 +117,10 @@ class TestFleetMetaOptimizer(unittest.TestCase):
fleet.init(is_collective=True)
x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32')
with paddle.static.device_guard('gpu:0'):
linear = fluid.Linear(4, 8, bias_attr=False)
linear = paddle.nn.Linear(4, 8, bias_attr=False)
out = linear(x)
with paddle.static.device_guard('gpu:1'):
linear = fluid.Linear(8, 5, bias_attr=False)
linear = paddle.nn.Linear(8, 5, bias_attr=False)
out = linear(out)
avg_cost = paddle.mean(out)
strategy = fleet.DistributedStrategy()
......
......@@ -998,110 +998,6 @@ class TestPool2DAPI_Error(unittest.TestCase):
self.assertRaises(ValueError, run_5)
class TestDygraphPool2DAPIError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
# the input of Pool2D must be Variable.
data1 = np.random.random((3, 32, 32, 5)).astype('float32')
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
global_pooling=False,
)
self.assertRaises(TypeError, pool2d, data1)
# the input dtype of mlu Pool2D must be float16 or float32
data2 = fluid.layers.data(
name='x1', shape=[3, 32, 32, 5], dtype="int32"
)
self.assertRaises(TypeError, pool2d, data2)
def test_data_format_error(self):
with program_guard(Program(), Program()):
# the data_format must be 'NCHW' or 'NHWC'
data1 = np.random.random((3, 32, 32, 5)).astype('float32')
self.assertRaises(
ValueError,
fluid.dygraph.Pool2D,
pool_size=2,
pool_type='max',
pool_stride=1,
global_pooling=False,
data_format='NWHC',
)
class TestDygraphPool2DAPI(unittest.TestCase):
def test_nhwc(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='NHWC',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2)
def test_lower_case(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='nhwc',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2)
def test_upper_case(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='MAX',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='nhwc',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2)
if __name__ == '__main__':
unittest.main()
......@@ -19,7 +19,7 @@ import numpy as np
import paddle
import paddle.distributed as dist
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
paddle.seed(1024)
np.random.seed(2021)
......
......@@ -19,8 +19,8 @@ import numpy as np
import paddle
import paddle.distributed as dist
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.nn import Linear
paddle.seed(1024)
np.random.seed(2021)
......
......@@ -18,7 +18,6 @@ from test_dist_base import TestParallelDyGraphRunnerBase, runtime_main
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Linear, Pool2D
class SimpleImgConvPool(fluid.dygraph.Layer):
......@@ -55,7 +54,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer):
bias_attr=None,
)
self._pool2d = Pool2D(
self._pool2d = paddle.fluid.dygraph.nn.Pool2D(
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
......@@ -85,23 +84,21 @@ class MNIST(fluid.dygraph.Layer):
self.pool_2_shape = 50 * 4 * 4
SIZE = 10
scale = (2.0 / (self.pool_2_shape**2 * SIZE)) ** 0.5
self._fc = Linear(
self._fc = paddle.nn.Linear(
self.pool_2_shape,
10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale
)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale)
),
act="softmax",
)
self.act = paddle.nn.Softmax()
def forward(self, inputs, label):
x = self._simple_img_conv_pool_1(inputs)
x = self._simple_img_conv_pool_2(x)
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
cost = self._fc(x)
loss = fluid.layers.cross_entropy(cost, label)
loss = fluid.layers.cross_entropy(self.act(cost), label)
avg_loss = paddle.mean(loss)
return avg_loss
......
......@@ -19,6 +19,7 @@ import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
np.random.seed(2021)
paddle.seed(1024)
......@@ -28,7 +29,7 @@ class SimpleNet(fluid.Layer):
def __init__(self):
# bias is unused parameters, and it share with net_a
super().__init__()
self.net_a = Linear(input_dim=10, output_dim=5)
self.net_a = Linear(10, 5)
self.net_b = Linear(10, 10)
self.bias = self.net_a.bias
......
......@@ -641,7 +641,7 @@ class TestAdamOpV2(unittest.TestCase):
paddle.disable_static()
value = np.arange(26).reshape(2, 13).astype("float32")
a = fluid.dygraph.to_variable(value)
linear = fluid.Linear(13, 5, dtype="float32")
linear = paddle.nn.Linear(13, 5)
adam = paddle.optimizer.Adam(
learning_rate=0.01, parameters=linear.parameters()
......@@ -690,7 +690,7 @@ class TestAdamOpV2(unittest.TestCase):
paddle.disable_static()
value = np.arange(26).reshape(2, 13).astype("float32")
a = fluid.dygraph.to_variable(value)
linear = fluid.Linear(13, 5, dtype="float32")
linear = paddle.nn.Linear(13, 5)
clip = fluid.clip.GradientClipByGlobalNorm(clip_norm=1.0)
adam = paddle.optimizer.Adam(
0.1, parameters=linear.parameters(), grad_clip=clip
......@@ -1095,7 +1095,7 @@ class TestMultiTensorAdam(unittest.TestCase):
trainable=True,
)
if use_param_attr:
model = paddle.nn.Linear(5, 5, weight_attr)
model = paddle.nn.Linear(5, 5, weight_attr=weight_attr)
else:
model = paddle.nn.Linear(5, 5)
......
......@@ -18,7 +18,7 @@ import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear
from paddle.nn import Linear
from paddle.fluid.dygraph.base import to_variable
......@@ -32,40 +32,40 @@ class Test_Detach(unittest.TestCase):
def no_detach_multi(self):
data = self.generate_Data()
with fluid.dygraph.guard():
linear_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(5.0)
linear_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(5.0)
)
linear_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(6.0)
linear_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(6.0)
)
linear = Linear(
4,
10,
param_attr=linear_w_param_attrs,
weight_attr=linear_w_param_attrs,
bias_attr=linear_b_param_attrs,
)
linear1_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(7.0)
linear1_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(7.0)
)
linear1_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(8.0)
linear1_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(8.0)
)
linear1 = Linear(
10,
1,
param_attr=linear1_w_param_attrs,
weight_attr=linear1_w_param_attrs,
bias_attr=linear1_b_param_attrs,
)
linear2_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(9.0)
linear2_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(9.0)
)
linear2_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(10.0)
linear2_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(10.0)
)
linear2 = Linear(
10,
1,
param_attr=linear2_w_param_attrs,
weight_attr=linear2_w_param_attrs,
bias_attr=linear2_b_param_attrs,
)
data = to_variable(data)
......@@ -80,28 +80,28 @@ class Test_Detach(unittest.TestCase):
def no_detach_single(self):
data = self.generate_Data()
with fluid.dygraph.guard():
linear_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(5.0)
linear_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(5.0)
)
linear_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(6.0)
linear_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(6.0)
)
linear = Linear(
4,
10,
param_attr=linear_w_param_attrs,
weight_attr=linear_w_param_attrs,
bias_attr=linear_b_param_attrs,
)
linear1_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(7.0)
linear1_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(7.0)
)
linear1_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(8.0)
linear1_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(8.0)
)
linear1 = Linear(
10,
1,
param_attr=linear1_w_param_attrs,
weight_attr=linear1_w_param_attrs,
bias_attr=linear1_b_param_attrs,
)
data = to_variable(data)
......@@ -115,8 +115,8 @@ class Test_Detach(unittest.TestCase):
def detach_multi(self):
data = self.generate_Data()
with fluid.dygraph.guard():
linear_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(5.0)
linear_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(5.0)
)
linear_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(6.0)
......@@ -124,11 +124,11 @@ class Test_Detach(unittest.TestCase):
linear = Linear(
4,
10,
param_attr=linear_w_param_attrs,
weight_attr=linear_w_param_attrs,
bias_attr=linear_b_param_attrs,
)
linear1_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(7.0)
linear1_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(7.0)
)
linear1_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(8.0)
......@@ -136,19 +136,19 @@ class Test_Detach(unittest.TestCase):
linear1 = Linear(
10,
1,
param_attr=linear1_w_param_attrs,
weight_attr=linear1_w_param_attrs,
bias_attr=linear1_b_param_attrs,
)
linear2_w_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(9.0)
linear2_w_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(9.0)
)
linear2_b_param_attrs = fluid.ParamAttr(
initializer=fluid.initializer.Constant(10.0)
linear2_b_param_attrs = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(10.0)
)
linear2 = Linear(
10,
1,
param_attr=linear2_w_param_attrs,
weight_attr=linear2_w_param_attrs,
bias_attr=linear2_b_param_attrs,
)
data = to_variable(data)
......
......@@ -18,7 +18,7 @@ import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear, Pool2D
from paddle.nn import Linear
from paddle.fluid.framework import _test_eager_guard
......@@ -57,7 +57,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer):
bias_attr=bias_attr,
)
self._pool2d = Pool2D(
self._pool2d = paddle.fluid.dygraph.nn.Pool2D(
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
......@@ -104,13 +104,9 @@ class MNIST(fluid.dygraph.Layer):
self._linear = Linear(
self.pool_2_shape,
10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale
)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale)
),
act="softmax",
dtype=dtype,
)
def forward(self, inputs, label):
......@@ -118,6 +114,7 @@ class MNIST(fluid.dygraph.Layer):
x = paddle.nn.functional.relu(self._simple_img_conv_pool_2(x))
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
cost = self._linear(x)
cost = paddle.nn.functional.softmax(cost)
loss = fluid.layers.cross_entropy(cost, label)
avg_loss = paddle.mean(loss)
return avg_loss
......
......@@ -20,8 +20,8 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.nn import Linear
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Linear, Pool2D
from paddle.fluid.optimizer import SGDOptimizer
SEED = 123123111
......@@ -61,7 +61,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer):
bias_attr=None,
)
self._pool2d = Pool2D(
self._pool2d = paddle.fluid.dygraph.nn.Pool2D(
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
......@@ -94,12 +94,9 @@ class MNIST(fluid.dygraph.Layer):
self._fc = Linear(
self.pool_2_shape,
SIZE,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale
)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale)
),
act="softmax",
)
def forward(self, inputs):
......@@ -107,6 +104,7 @@ class MNIST(fluid.dygraph.Layer):
x = self._simple_img_conv_pool_2(x)
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
x = self._fc(x)
x = paddle.nn.functional.softmax(x)
return x
......
......@@ -66,7 +66,7 @@ class TestExceptionNoCStack(unittest.TestCase):
place = fluid.CPUPlace()
with fluid.dygraph.guard(place):
x = numpy.random.random(size=(10, 2)).astype('float32')
linear = fluid.dygraph.Linear(1, 10)
linear = paddle.nn.Linear(1, 10)
data = fluid.dygraph.to_variable(x)
with self.assertRaises(ValueError):
res = linear(data)
......
......@@ -407,7 +407,7 @@ class TestGradientClipByValue(TestGradientClip):
class TestDygraphGradientClip(unittest.TestCase):
def test_gradient_clip(self):
with fluid.dygraph.guard():
linear = fluid.dygraph.Linear(5, 5)
linear = paddle.nn.Linear(5, 5)
inputs = fluid.layers.uniform_random(
[16, 5], min=-10, max=10
).astype('float32')
......@@ -602,8 +602,8 @@ class TestDygraphGradientClipFP64(unittest.TestCase):
with fluid.dygraph.guard():
inputs = fluid.layers.uniform_random(
[16, 5], min=-10, max=10
).astype('float64')
linear = fluid.dygraph.Linear(5, 5, dtype="float64")
).astype('float32')
linear = paddle.nn.Linear(5, 5)
out = linear(fluid.dygraph.to_variable(inputs))
loss = fluid.layers.reduce_mean(out)
loss.backward()
......
......@@ -24,16 +24,20 @@ from paddle.fluid.framework import _test_eager_guard
class AutoPruneLayer0(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self.linear1 = fluid.dygraph.Linear(
self.linear1 = paddle.nn.Linear(
input_size,
5,
param_attr=fluid.initializer.ConstantInitializer(value=2),
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=2)
),
bias_attr=False,
)
self.linear2 = fluid.dygraph.Linear(
self.linear2 = paddle.nn.Linear(
5,
5,
param_attr=fluid.initializer.ConstantInitializer(value=2),
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=2)
),
bias_attr=False,
)
......@@ -48,16 +52,20 @@ class AutoPruneLayer0(fluid.Layer):
class AutoPruneLayer1(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self.linear1 = fluid.dygraph.Linear(
self.linear1 = paddle.nn.Linear(
input_size,
5,
param_attr=fluid.initializer.ConstantInitializer(value=2),
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=2)
),
bias_attr=False,
)
self.linear2 = fluid.dygraph.Linear(
self.linear2 = paddle.nn.Linear(
5,
5,
param_attr=fluid.initializer.ConstantInitializer(value=2),
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=2)
),
bias_attr=False,
)
......@@ -73,8 +81,8 @@ class AutoPruneLayer1(fluid.Layer):
class AutoPruneLayer2(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self.linear = fluid.dygraph.Linear(input_size, 10, act=None)
self.linear2 = fluid.dygraph.Linear(1, 1, act=None)
self.linear = paddle.nn.Linear(input_size, 10)
self.linear2 = paddle.nn.Linear(1, 1)
def forward(self, x, label):
feature = self.linear(x)
......@@ -90,7 +98,7 @@ class AutoPruneLayer2(fluid.Layer):
class AutoPruneLayer3(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self.linear = fluid.dygraph.Linear(input_size, 20, act=None)
self.linear = paddle.nn.Linear(input_size, 20)
def forward(self, x, label, test_num):
feature = self.linear(x)
......@@ -111,8 +119,8 @@ class MyLayer(fluid.Layer):
super().__init__(dtype=dtype)
self.embed0 = fluid.Embedding(size=(vocab_size, size))
self.embed1 = fluid.Embedding(size=(vocab_size, size))
self.linear_0 = fluid.Linear(input_size, size, dtype=dtype)
self.linear_1 = fluid.Linear(input_size, size, dtype=dtype)
self.linear_0 = paddle.nn.Linear(input_size, size)
self.linear_1 = paddle.nn.Linear(input_size, size)
def forward(self, x):
# this method involves only the linear layers
......@@ -133,8 +141,8 @@ class MyLayer2(fluid.Layer):
super().__init__(dtype=dtype)
self.embed0 = fluid.Embedding(size=(vocab_size, size))
self.embed1 = fluid.Embedding(size=(vocab_size, size))
self.linear_0 = fluid.Linear(input_size, size, dtype=dtype)
self.linear_1 = fluid.Linear(input_size, size, dtype=dtype)
self.linear_0 = paddle.nn.Linear(input_size, size)
self.linear_1 = paddle.nn.Linear(input_size, size)
def forward(self, indices):
# mind the difference with MyLayer
......@@ -253,8 +261,8 @@ class TestImperativeAutoPrune(unittest.TestCase):
value0 = np.arange(26).reshape(2, 13).astype("float32")
value1 = np.arange(6).reshape(2, 3).astype("float32")
value2 = np.arange(10).reshape(2, 5).astype("float32")
linear = fluid.Linear(13, 5, dtype="float32")
linear2 = fluid.Linear(3, 3, dtype="float32")
linear = paddle.nn.Linear(13, 5)
linear2 = paddle.nn.Linear(3, 3)
a = fluid.dygraph.to_variable(value0)
b = fluid.dygraph.to_variable(value1)
c = fluid.dygraph.to_variable(value2)
......@@ -276,8 +284,8 @@ class TestImperativeAutoPrune(unittest.TestCase):
value0 = np.arange(26).reshape(2, 13).astype("float32")
value1 = np.arange(6).reshape(2, 3).astype("float32")
value2 = np.arange(10).reshape(2, 5).astype("float32")
linear = fluid.Linear(13, 5, dtype="float32")
linear2 = fluid.Linear(3, 3, dtype="float32")
linear = paddle.nn.Linear(13, 5)
linear2 = paddle.nn.Linear(3, 3)
a = fluid.dygraph.to_variable(value0)
b = fluid.dygraph.to_variable(value1)
c = fluid.dygraph.to_variable(value2)
......@@ -299,8 +307,8 @@ class TestImperativeAutoPrune(unittest.TestCase):
value0 = np.arange(26).reshape(2, 13).astype("float32")
value1 = np.arange(6).reshape(2, 3).astype("float32")
value2 = np.arange(10).reshape(2, 5).astype("float32")
linear = fluid.Linear(13, 5, dtype="float32")
linear2 = fluid.Linear(5, 3, dtype="float32")
linear = paddle.nn.Linear(13, 5)
linear2 = paddle.nn.Linear(5, 3)
a = fluid.dygraph.to_variable(value0)
b = fluid.dygraph.to_variable(value1)
c = fluid.dygraph.to_variable(value2)
......@@ -332,8 +340,8 @@ class TestImperativeAutoPrune(unittest.TestCase):
value0 = np.arange(26).reshape(2, 13).astype("float32")
value1 = np.arange(6).reshape(2, 3).astype("float32")
value2 = np.arange(10).reshape(2, 5).astype("float32")
linear = fluid.Linear(13, 5, dtype="float32")
linear2 = fluid.Linear(5, 3, dtype="float32")
linear = paddle.nn.Linear(13, 5)
linear2 = paddle.nn.Linear(5, 3)
a = fluid.dygraph.to_variable(value0)
b = fluid.dygraph.to_variable(value1)
c = fluid.dygraph.to_variable(value2)
......@@ -367,8 +375,8 @@ class TestImperativeAutoPrune(unittest.TestCase):
value0 = np.arange(26).reshape(2, 13).astype("float32")
value1 = np.arange(6).reshape(2, 3).astype("float32")
value2 = np.arange(10).reshape(2, 5).astype("float32")
linear = fluid.Linear(13, 5, dtype="float32")
linear2 = fluid.Linear(3, 3, dtype="float32")
linear = paddle.nn.Linear(13, 5)
linear2 = paddle.nn.Linear(3, 3)
a = fluid.dygraph.to_variable(value0)
b = fluid.dygraph.to_variable(value1)
c = fluid.dygraph.to_variable(value2)
......@@ -462,7 +470,7 @@ class TestImperativeAutoPrune(unittest.TestCase):
def func_case3_prune_no_grad_branch2(self):
with fluid.dygraph.guard():
value1 = np.arange(1).reshape(1, 1)
linear = fluid.dygraph.Linear(1, 1, act=None)
linear = paddle.nn.Linear(1, 1)
label = fluid.dygraph.to_variable(value1).astype("float32")
label = linear(label)
label = fluid.layers.cast(label, dtype="float32")
......
......@@ -19,8 +19,8 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
import paddle.fluid.dygraph_utils as dygraph_utils
from paddle.fluid import Linear, core
from paddle.fluid.dygraph.layer_object_helper import LayerObjectHelper
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.layer_helper import LayerHelper
......@@ -41,24 +41,24 @@ class MyLayer(fluid.Layer):
class MLP(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self._linear1 = Linear(
self._linear1 = paddle.nn.Linear(
input_size,
3,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
)
self._linear2 = Linear(
self._linear2 = paddle.nn.Linear(
3,
4,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
)
......@@ -269,9 +269,9 @@ class TestImperative(unittest.TestCase):
def test_no_grad_guard(self):
data = np.array([[2, 3], [4, 5]]).astype('float32')
with fluid.dygraph.guard():
l0 = fluid.Linear(2, 2)
l0 = paddle.nn.Linear(2, 2)
self.assertIsNone(l0.weight._grad_ivar())
l1 = fluid.Linear(2, 2)
l1 = paddle.nn.Linear(2, 2)
with fluid.dygraph.no_grad():
self.assertTrue(l1.weight.stop_gradient is False)
tmp = l1.weight * 2
......@@ -287,9 +287,9 @@ class TestImperative(unittest.TestCase):
def test_paddle_imperative_no_grad_guard(self):
data = np.array([[2, 3], [4, 5]]).astype('float32')
with fluid.dygraph.guard():
l0 = fluid.Linear(2, 2)
l0 = paddle.nn.Linear(2, 2)
self.assertIsNone(l0.weight._grad_ivar())
l1 = fluid.Linear(2, 2)
l1 = paddle.nn.Linear(2, 2)
with paddle.no_grad():
self.assertTrue(l1.weight.stop_gradient is False)
tmp = l1.weight * 2
......@@ -305,9 +305,9 @@ class TestImperative(unittest.TestCase):
def test_paddle_imperative_set_grad_enabled(self):
data = np.array([[2, 3], [4, 5]]).astype('float32')
with fluid.dygraph.guard():
l0 = fluid.Linear(2, 2)
l0 = paddle.nn.Linear(2, 2)
self.assertIsNone(l0.weight._grad_ivar())
l1 = fluid.Linear(2, 2)
l1 = paddle.nn.Linear(2, 2)
with paddle.set_grad_enabled(False):
self.assertTrue(l1.weight.stop_gradient is False)
tmp = l1.weight * 2
......@@ -863,7 +863,7 @@ class TestImperative(unittest.TestCase):
self.assertRaises(TypeError, my_layer.__setattr__, 'w1', 'str')
my_layer.w1 = None
self.assertEqual(len(my_layer.parameters()), 0)
my_layer.l1 = fluid.dygraph.Linear(3, 3)
my_layer.l1 = paddle.nn.Linear(3, 3)
self.assertEqual(len(my_layer.sublayers()), 1)
self.assertRaises(TypeError, my_layer.__setattr__, 'l1', 'str')
my_layer.l1 = None
......
......@@ -35,7 +35,7 @@ class MyLayer(fluid.Layer):
class TestImperativeContainer(unittest.TestCase):
def paddle_imperative_list(self):
return paddle.nn.LayerList(
[fluid.dygraph.Linear(2**i, 2 ** (i + 1)) for i in range(6)]
[paddle.nn.Linear(2**i, 2 ** (i + 1)) for i in range(6)]
)
def layer_list(self, use_fluid_api):
......@@ -48,13 +48,13 @@ class TestImperativeContainer(unittest.TestCase):
model = MyLayer(layerlist)
res1 = model(x)
self.assertListEqual(res1.shape, [5, 2**size])
model.layerlist[size - 1] = fluid.dygraph.Linear(2 ** (size - 1), 5)
model.layerlist[size - 1] = paddle.nn.Linear(2 ** (size - 1), 5)
res2 = model(x)
self.assertListEqual(res2.shape, [5, 5])
del model.layerlist[size - 1]
res3 = model(x)
self.assertListEqual(res3.shape, [5, 2 ** (size - 1)])
model.layerlist.append(fluid.dygraph.Linear(2 ** (size - 1), 3))
model.layerlist.append(paddle.nn.Linear(2 ** (size - 1), 3))
res4 = model(x)
self.assertListEqual(res4.shape, [5, 3])
res4.backward()
......@@ -68,14 +68,14 @@ class TestImperativeContainer(unittest.TestCase):
res6.backward()
model3 = MyLayer(layerlist[:-2])
model3.layerlist.append(fluid.dygraph.Linear(3, 1))
model3.layerlist.append(paddle.nn.Linear(3, 1))
model3.layerlist.insert(
size - 2, fluid.dygraph.Linear(2 ** (size - 2), 3)
size - 2, paddle.nn.Linear(2 ** (size - 2), 3)
)
res7 = model3(x)
self.assertListEqual(res7.shape, [5, 1])
to_be_extended = [
fluid.dygraph.Linear(3**i, 3 ** (i + 1)) for i in range(3)
paddle.nn.Linear(3**i, 3 ** (i + 1)) for i in range(3)
]
model3.layerlist.extend(to_be_extended)
res8 = model3(x)
......@@ -83,13 +83,13 @@ class TestImperativeContainer(unittest.TestCase):
res8.backward()
model4 = MyLayer(layerlist[:3])
model4.layerlist[-1] = fluid.dygraph.Linear(4, 5)
model4.layerlist[-1] = paddle.nn.Linear(4, 5)
res9 = model4(x)
self.assertListEqual(res9.shape, [5, 5])
del model4.layerlist[-1]
res10 = model4(x)
self.assertListEqual(res10.shape, [5, 4])
model4.layerlist.insert(-1, fluid.dygraph.Linear(2, 2))
model4.layerlist.insert(-1, paddle.nn.Linear(2, 2))
res11 = model4(x)
self.assertListEqual(res11.shape, [5, 4])
res11.backward()
......
......@@ -15,6 +15,7 @@
import unittest
import numpy as np
from paddle.nn import Linear
import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
......@@ -25,19 +26,17 @@ class TestImperativeContainerSequential(unittest.TestCase):
data = np.random.uniform(-1, 1, [5, 10]).astype('float32')
with fluid.dygraph.guard():
data = fluid.dygraph.to_variable(data)
model1 = fluid.dygraph.Sequential(
fluid.Linear(10, 1), fluid.Linear(1, 2)
)
model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2))
res1 = model1(data)
self.assertListEqual(res1.shape, [5, 2])
model1[1] = fluid.Linear(1, 3)
model1[1] = Linear(1, 3)
res1 = model1(data)
self.assertListEqual(res1.shape, [5, 3])
loss1 = fluid.layers.reduce_mean(res1)
loss1.backward()
l1 = fluid.Linear(10, 1)
l2 = fluid.Linear(1, 3)
l1 = Linear(10, 1)
l2 = Linear(1, 3)
model2 = fluid.dygraph.Sequential(('l1', l1), ('l2', l2))
self.assertEqual(len(model2), 2)
res2 = model2(data)
......@@ -48,8 +47,8 @@ class TestImperativeContainerSequential(unittest.TestCase):
self.assertEqual(len(model2), 1)
res2 = model2(data)
self.assertListEqual(res2.shape, [5, 1])
model2.add_sublayer('l3', fluid.Linear(1, 3))
model2.add_sublayer('l4', fluid.Linear(3, 4))
model2.add_sublayer('l3', Linear(1, 3))
model2.add_sublayer('l4', Linear(3, 4))
self.assertEqual(len(model2), 3)
res2 = model2(data)
self.assertListEqual(res2.shape, [5, 4])
......@@ -66,19 +65,17 @@ class TestImperativeContainerSequential(unittest.TestCase):
data = np.random.uniform(-1, 1, [5, 10]).astype('float32')
with fluid.dygraph.guard():
data = fluid.dygraph.to_variable(data)
model1 = fluid.dygraph.Sequential(
fluid.Linear(10, 1), fluid.Linear(1, 2)
)
model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2))
res1 = model1(data)
self.assertListEqual(res1.shape, [5, 2])
model1[1] = fluid.Linear(1, 3)
model1[1] = Linear(1, 3)
res1 = model1(data)
self.assertListEqual(res1.shape, [5, 3])
loss1 = fluid.layers.reduce_mean(res1)
loss1.backward()
l1 = fluid.Linear(10, 1)
l2 = fluid.Linear(1, 3)
l1 = Linear(10, 1)
l2 = Linear(1, 3)
model2 = fluid.dygraph.Sequential(['l1', l1], ['l2', l2])
self.assertEqual(len(model2), 2)
res2 = model2(data)
......@@ -89,8 +86,8 @@ class TestImperativeContainerSequential(unittest.TestCase):
self.assertEqual(len(model2), 1)
res2 = model2(data)
self.assertListEqual(res2.shape, [5, 1])
model2.add_sublayer('l3', fluid.Linear(1, 3))
model2.add_sublayer('l4', fluid.Linear(3, 4))
model2.add_sublayer('l3', Linear(1, 3))
model2.add_sublayer('l4', Linear(3, 4))
self.assertEqual(len(model2), 3)
res2 = model2(data)
self.assertListEqual(res2.shape, [5, 4])
......
......@@ -19,7 +19,7 @@ import numpy as np
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.dygraph as dygraph
from paddle.fluid.dygraph.nn import Linear
from paddle.nn import Linear
class MLP(fluid.Layer):
......
......@@ -23,8 +23,8 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.dygraph import Linear
from paddle.fluid.dygraph.base import to_variable
from paddle.nn import Linear
from paddle.fluid.framework import _test_eager_guard
......@@ -44,20 +44,30 @@ class DMF(fluid.Layer):
Linear(
256 if i == 0 else self._hid_sizes[i - 1],
self._hid_sizes[i],
act='relu',
),
)
)
self._user_layers.append(
self.add_sublayer(
'user_layer_act_%d' % i,
paddle.nn.ReLU(),
)
)
self._item_layers.append(
self.add_sublayer(
'item_layer_%d' % i,
Linear(
256 if i == 0 else self._hid_sizes[i - 1],
self._hid_sizes[i],
act='relu',
),
)
)
self._item_layers.append(
self.add_sublayer(
'item_layer_act_%d' % i,
paddle.nn.ReLU(),
)
)
def forward(self, users, items):
users = self._user_latent(users)
......@@ -83,10 +93,15 @@ class MLP(fluid.Layer):
Linear(
256 * 2 if i == 0 else self._hid_sizes[i - 1],
self._hid_sizes[i],
act='relu',
),
)
)
self._match_layers.append(
self.add_sublayer(
'match_layer_act_%d' % i,
paddle.nn.ReLU(),
)
)
def forward(self, users, items):
users = self._user_latent(users)
......@@ -115,7 +130,7 @@ class DeepCF(fluid.Layer):
self._mlp = MLP()
self._dmf = DMF()
self._match_fc = Linear(128, 1, act='sigmoid')
self._match_fc = Linear(128, 1)
def forward(self, users, items):
# users_emb = self._user_emb(users)
......@@ -134,6 +149,7 @@ class DeepCF(fluid.Layer):
[mlp_predictive, dmf_predictive], axis=len(mlp_predictive.shape) - 1
)
prediction = self._match_fc(predictive)
prediction = paddle.nn.functional.sigmoid(prediction)
return prediction
......
......@@ -586,7 +586,7 @@ class TestDygraphDoubleGradVisitedUniq(TestCase):
)
def model_f(input):
linear = fluid.dygraph.Linear(5, 3, bias_attr=False)
linear = paddle.nn.Linear(5, 3)
for i in range(10):
if i == 0:
out = linear(input)
......
......@@ -20,29 +20,30 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
import paddle
class MLP(fluid.Layer):
def __init__(self, input_size):
super().__init__()
self._linear1 = fluid.dygraph.Linear(
self._linear1 = paddle.nn.Linear(
input_size,
3,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
)
self._linear2 = fluid.dygraph.Linear(
self._linear2 = paddle.nn.Linear(
3,
4,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.1)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.1)
),
)
......
......@@ -20,7 +20,7 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid import Linear
from paddle.nn import Linear
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import SGDOptimizer
......@@ -29,11 +29,12 @@ from paddle.fluid.optimizer import SGDOptimizer
class Discriminator(fluid.Layer):
def __init__(self):
super().__init__()
self._fc1 = Linear(1, 32, act='elu')
self._fc1 = Linear(1, 32)
self._fc2 = Linear(32, 1)
def forward(self, inputs):
x = self._fc1(inputs)
x = paddle.nn.functional.elu(x)
x = self._fc2(x)
return x
......@@ -41,13 +42,15 @@ class Discriminator(fluid.Layer):
class Generator(fluid.Layer):
def __init__(self):
super().__init__()
self._fc1 = Linear(2, 64, act='elu')
self._fc2 = Linear(64, 64, act='elu')
self._fc1 = Linear(2, 64)
self._fc2 = Linear(64, 64)
self._fc3 = Linear(64, 1)
def forward(self, inputs):
x = self._fc1(inputs)
x = paddle.nn.functional.elu(x)
x = self._fc2(x)
x = paddle.nn.functional.elu(x)
x = self._fc3(x)
return x
......
......@@ -29,10 +29,10 @@ class LeNetDygraph(fluid.dygraph.Layer):
self.features = nn.Sequential(
nn.Conv2D(1, 6, 3, stride=1, padding=1),
nn.ReLU(),
paddle.fluid.dygraph.Pool2D(2, 'max', 2),
paddle.nn.MaxPool2D(2, 2),
nn.Conv2D(6, 16, 5, stride=1, padding=0),
nn.ReLU(),
paddle.fluid.dygraph.Pool2D(2, 'max', 2),
paddle.nn.MaxPool2D(2, 2),
)
if num_classes > 0:
......
......@@ -28,10 +28,10 @@ class LeNetDygraph(fluid.dygraph.Layer):
self.features = nn.Sequential(
nn.Conv2D(1, 6, 3, stride=1, padding=1),
nn.ReLU(),
paddle.fluid.dygraph.Pool2D(2, 'max', 2),
paddle.nn.MaxPool2D(2, 2),
nn.Conv2D(6, 16, 5, stride=1, padding=0),
nn.ReLU(),
paddle.fluid.dygraph.Pool2D(2, 'max', 2),
paddle.nn.MaxPool2D(2, 2),
)
def forward(self, inputs):
......
......@@ -19,6 +19,7 @@ import numpy as np
import paddle.fluid as fluid
import paddle.fluid.dygraph as dygraph
from paddle.fluid.framework import _test_eager_guard
import paddle
class TestImperativeLayerTrainable(unittest.TestCase):
......@@ -28,7 +29,7 @@ class TestImperativeLayerTrainable(unittest.TestCase):
label = dygraph.to_variable(label)
linear = dygraph.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
y = linear(label)
self.assertFalse(y.stop_gradient)
......
......@@ -24,13 +24,16 @@ import paddle.fluid.framework as framework
from paddle.fluid.dygraph.nn import (
NCE,
BatchNorm,
Conv3D,
Embedding,
GroupNorm,
LayerNorm,
Linear,
NCE,
PRelu,
)
from paddle.nn import Linear
import numpy as np
import os
import tempfile
class TestDygraphLoadStatic(unittest.TestCase):
......@@ -200,11 +203,11 @@ class TestDygraphLoadStatic(unittest.TestCase):
in_channels=10, out_channels=10, kernel_size=5
)
self.conv3d_1 = Conv3D(
num_channels=3, num_filters=2, filter_size=3, act="relu"
self.conv3d_1 = paddle.nn.Conv3D(
in_channels=3, out_channels=2, kernel_size=3
)
self.conv3d_2 = Conv3D(
num_channels=3, num_filters=2, filter_size=3, act="relu"
self.conv3d_2 = paddle.nn.Conv3D(
in_channels=3, out_channels=2, kernel_size=3
)
self.batch_norm_1 = BatchNorm(10)
......
......@@ -15,15 +15,16 @@
import unittest
import numpy as np
from test_imperative_base import new_program_scope
from utils import DyGraphProgramDescTracerTestHelper
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.dygraph.nn import Linear, Pool2D
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.optimizer import SGDOptimizer
from paddle.nn import Linear
from test_imperative_base import new_program_scope
from utils import DyGraphProgramDescTracerTestHelper
from paddle.fluid.framework import _test_eager_guard, _in_legacy_dygraph
class SimpleImgConvPool(fluid.dygraph.Layer):
......@@ -59,8 +60,7 @@ class SimpleImgConvPool(fluid.dygraph.Layer):
weight_attr=None,
bias_attr=None,
)
self._pool2d = Pool2D(
self._pool2d = paddle.fluid.dygraph.nn.Pool2D(
pool_size=pool_size,
pool_type=pool_type,
pool_stride=pool_stride,
......@@ -93,12 +93,9 @@ class MNIST(fluid.dygraph.Layer):
self._fc = Linear(
self.pool_2_shape,
10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale
)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Normal(mean=0.0, std=scale)
),
act="softmax",
)
def forward(self, inputs):
......@@ -106,6 +103,7 @@ class MNIST(fluid.dygraph.Layer):
x = self._simple_img_conv_pool_2(x)
x = paddle.reshape(x, shape=[-1, self.pool_2_shape])
x = self._fc(x)
x = paddle.nn.functional.softmax(x)
return x
......
......@@ -24,7 +24,7 @@ from paddle.fluid.framework import _test_eager_guard
class MyLayer(fluid.Layer):
def __init__(self, num_channel, dim, num_filter=5):
super().__init__()
self.fc = fluid.dygraph.Linear(dim, dim)
self.fc = paddle.nn.Linear(dim, dim)
self.conv = paddle.nn.Conv2D(num_channel, num_channel, num_filter)
def forward(self, x):
......@@ -36,8 +36,8 @@ class MyLayer(fluid.Layer):
class TestImperativeNamedSubLayers(unittest.TestCase):
def func_test_named_sublayers(self):
with fluid.dygraph.guard():
fc1 = fluid.Linear(10, 3)
fc2 = fluid.Linear(3, 10, bias_attr=False)
fc1 = paddle.nn.Linear(10, 3)
fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
custom = MyLayer(3, 10)
model = paddle.nn.Sequential(fc1, fc2, custom)
named_sublayers = model.named_sublayers()
......@@ -71,8 +71,8 @@ class TestImperativeNamedSubLayers(unittest.TestCase):
class TestImperativeNamedParameters(unittest.TestCase):
def func_test_named_parameters(self):
with fluid.dygraph.guard():
fc1 = fluid.Linear(10, 3)
fc2 = fluid.Linear(3, 10, bias_attr=False)
fc1 = paddle.nn.Linear(10, 3)
fc2 = paddle.nn.Linear(3, 10, bias_attr=False)
custom = MyLayer(3, 10)
model = paddle.nn.Sequential(fc1, fc2, custom)
......@@ -98,8 +98,8 @@ class TestImperativeNamedParameters(unittest.TestCase):
class Mymodel(fluid.dygraph.Layer):
def __init__(self):
super().__init__()
self.linear1 = fluid.dygraph.Linear(10, 10)
self.linear2 = fluid.dygraph.Linear(5, 5)
self.linear1 = paddle.nn.Linear(10, 10)
self.linear2 = paddle.nn.Linear(5, 5)
self.conv2d = paddle.nn.Conv2D(3, 2, 3)
self.embedding = fluid.dygraph.Embedding(size=[128, 16])
self.h_0 = fluid.dygraph.to_variable(
......
......@@ -26,8 +26,8 @@ from paddle.fluid.dygraph.nn import (
Embedding,
GRUUnit,
Linear,
Pool2D,
)
from paddle.nn import Linear
from paddle.fluid.framework import _test_eager_guard
......@@ -112,11 +112,9 @@ class ConvBNPool(fluid.dygraph.Layer):
self.bn_1_layer = BatchNorm(out_ch[1], act=act, is_test=is_test)
if self.pool:
self.pool_layer = Pool2D(
pool_size=2,
pool_type='max',
pool_stride=2,
use_cudnn=use_cudnn,
self.pool_layer = paddle.nn.MaxPool2D(
kernel_size=2,
stride=2,
ceil_mode=True,
)
......@@ -232,10 +230,10 @@ class EncoderNet(fluid.dygraph.Layer):
self.ocr_convs = OCRConv(is_test=is_test, use_cudnn=use_cudnn)
self.fc_1_layer = Linear(
32, rnn_hidden_size * 3, param_attr=para_attr, bias_attr=False
32, rnn_hidden_size * 3, weight_attr=para_attr, bias_attr=False
)
self.fc_2_layer = Linear(
32, rnn_hidden_size * 3, param_attr=para_attr, bias_attr=False
32, rnn_hidden_size * 3, weight_attr=para_attr, bias_attr=False
)
self.gru_forward_layer = DynamicGRU(
size=rnn_hidden_size,
......@@ -295,10 +293,8 @@ class SimpleAttention(fluid.dygraph.Layer):
def __init__(self, decoder_size):
super().__init__()
self.fc_1 = Linear(
decoder_size, decoder_size, act=None, bias_attr=False
)
self.fc_2 = Linear(decoder_size, 1, act=None, bias_attr=False)
self.fc_1 = Linear(decoder_size, decoder_size, bias_attr=False)
self.fc_2 = Linear(decoder_size, 1, bias_attr=False)
def forward(self, encoder_vec, encoder_proj, decoder_state):
......@@ -344,9 +340,7 @@ class GRUDecoderWithAttention(fluid.dygraph.Layer):
self.gru_unit = GRUUnit(
size=decoder_size * 3, param_attr=None, bias_attr=None
)
self.out_layer = Linear(
decoder_size, num_classes + 2, bias_attr=None, act='softmax'
)
self.out_layer = Linear(decoder_size, num_classes + 2, bias_attr=None)
self.decoder_size = decoder_size
......@@ -373,6 +367,7 @@ class GRUDecoderWithAttention(fluid.dygraph.Layer):
h, _, _ = self.gru_unit(decoder_inputs, hidden_mem)
hidden_mem = h
out = self.out_layer(h)
out = paddle.nn.functional.softmax(out)
res.append(out)
res1 = fluid.layers.concat(res, axis=1)
......@@ -388,7 +383,6 @@ class OCRAttention(fluid.dygraph.Layer):
Config.encoder_size,
Config.decoder_size,
bias_attr=False,
act='relu',
)
self.embedding = Embedding(
[Config.num_classes + 2, Config.word_vector_dim], dtype='float32'
......@@ -406,6 +400,7 @@ class OCRAttention(fluid.dygraph.Layer):
backward_first, [-1, backward_first.shape[2]]
)
decoder_boot = self.fc(backward_first)
decoder_boot = paddle.nn.functional.relu(decoder_boot)
label_in = paddle.reshape(label_in, [-1])
trg_embedding = self.embedding(label_in)
......
......@@ -16,14 +16,10 @@ import itertools
import unittest
import numpy as np
from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer
from paddle.fluid import core
from paddle.fluid.dygraph import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import (
AdadeltaOptimizer,
AdagradOptimizer,
......@@ -43,6 +39,10 @@ from paddle.fluid.optimizer import (
RMSPropOptimizer,
SGDOptimizer,
)
from test_imperative_base import new_program_scope
from paddle.fluid.framework import _test_eager_guard
from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer
# Note(wangzhongpu)
# In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer.
......@@ -52,8 +52,8 @@ class MLP(fluid.Layer):
def __init__(self, param_attr=None, bias_attr=None):
super().__init__()
self._fc1 = Linear(784, 10)
self._fc2 = Linear(10, 10)
self._fc1 = paddle.nn.Linear(784, 10)
self._fc2 = paddle.nn.Linear(10, 10)
def forward(self, inputs):
y = self._fc1(inputs)
......@@ -473,7 +473,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -504,7 +504,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -540,7 +540,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -579,7 +579,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -951,8 +951,8 @@ class TestImperativeRecomputeOptimizer(TestImperativeOptimizerBase):
class TestImperativeOptimizerList(unittest.TestCase):
def func_test_parameter_list(self):
with fluid.dygraph.guard():
linear_1 = Linear(10, 10)
linear_2 = Linear(10, 10)
linear_1 = paddle.nn.Linear(10, 10)
linear_2 = paddle.nn.Linear(10, 10)
sgd = SGDOptimizer(
1.0,
......
......@@ -16,14 +16,10 @@ import itertools
import unittest
import numpy as np
from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer
from paddle.fluid import core
from paddle.fluid.dygraph import Linear
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import (
AdadeltaOptimizer,
AdagradOptimizer,
......@@ -40,6 +36,10 @@ from paddle.fluid.optimizer import (
RecomputeOptimizer,
RMSPropOptimizer,
)
from test_imperative_base import new_program_scope
from paddle.fluid.framework import _test_eager_guard
from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer
# Note(wangzhongpu)
# In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer.
......@@ -49,8 +49,8 @@ class MLP(fluid.Layer):
def __init__(self, param_attr=None, bias_attr=None):
super().__init__()
self._fc1 = Linear(784, 10)
self._fc2 = Linear(10, 10)
self._fc1 = paddle.nn.Linear(784, 10)
self._fc2 = paddle.nn.Linear(10, 10)
def forward(self, inputs):
y = self._fc1(inputs)
......@@ -611,7 +611,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -640,7 +640,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -674,7 +674,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
b = linear(a)
......@@ -704,7 +704,7 @@ class TestOptimizerLearningRate(unittest.TestCase):
with fluid.dygraph.guard():
a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32")
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
a = fluid.dygraph.to_variable(a)
......@@ -1071,8 +1071,8 @@ class TestImperativeRecomputeOptimizer(TestImperativeOptimizerBase):
class TestImperativeOptimizerList(unittest.TestCase):
def func_test_parameter_list(self):
with fluid.dygraph.guard():
linear_1 = Linear(10, 10)
linear_2 = Linear(10, 10)
linear_1 = paddle.nn.Linear(10, 10)
linear_2 = paddle.nn.Linear(10, 10)
sgd = paddle.optimizer.SGD(
1.0,
......
......@@ -18,6 +18,7 @@ import numpy as np
import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
import paddle
class TestImperativePartitialBackward(unittest.TestCase):
......@@ -25,8 +26,8 @@ class TestImperativePartitialBackward(unittest.TestCase):
with fluid.dygraph.guard():
x = np.random.randn(2, 4, 5).astype("float32")
x = fluid.dygraph.to_variable(x)
linear1 = fluid.dygraph.Linear(5, 10)
linear2 = fluid.dygraph.Linear(5, 10)
linear1 = paddle.nn.Linear(5, 10)
linear2 = paddle.nn.Linear(5, 10)
y = linear1(x[:, :2])
z = linear2(x[:, 2:])
......
......@@ -15,12 +15,11 @@
import unittest
import numpy as np
from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
import paddle.fluid.dygraph.nn as nn
from paddle.fluid import core
from test_imperative_base import new_program_scope
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import SGDOptimizer
......@@ -29,8 +28,8 @@ class Policy(fluid.dygraph.Layer):
def __init__(self, input_size):
super().__init__()
self.affine1 = nn.Linear(input_size, 128)
self.affine2 = nn.Linear(128, 2)
self.affine1 = paddle.nn.Linear(input_size, 128)
self.affine2 = paddle.nn.Linear(128, 2)
self.dropout_ratio = 0.6
self.saved_log_probs = []
......
......@@ -20,7 +20,8 @@ from utils import DyGraphProgramDescTracerTestHelper, is_equal_program
import paddle
import paddle.fluid as fluid
from paddle.fluid import BatchNorm, Linear, Pool2D, core
from paddle.fluid import core
from paddle.fluid import BatchNorm
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.layer_helper import LayerHelper
......@@ -193,8 +194,8 @@ class ResNet(fluid.Layer):
act='relu',
use_cudnn=use_cudnn,
)
self.pool2d_max = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
self.pool2d_max = paddle.nn.MaxPool2D(
kernel_size=3, stride=2, padding=1
)
self.bottleneck_block_list = []
......@@ -215,8 +216,7 @@ class ResNet(fluid.Layer):
)
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
......@@ -226,11 +226,10 @@ class ResNet(fluid.Layer):
stdv = 1.0 / math.sqrt(2048 * 1.0)
self.out = Linear(
self.out = paddle.nn.Linear(
self.pool2d_avg_output,
class_dim,
act='softmax',
param_attr=fluid.param_attr.ParamAttr(
weight_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
),
)
......@@ -243,6 +242,7 @@ class ResNet(fluid.Layer):
y = self.pool2d_avg(y)
y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output])
y = self.out(y)
y = paddle.nn.functional.softmax(y)
return y
......
......@@ -20,7 +20,8 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.dygraph.nn import BatchNorm, Linear, Pool2D
from paddle.fluid.dygraph.nn import BatchNorm
from test_imperative_base import new_program_scope
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.layer_helper import LayerHelper
......@@ -104,29 +105,34 @@ class SqueezeExcitation(fluid.dygraph.Layer):
super().__init__()
self._num_channels = num_channels
self._pool = Pool2D(pool_size=0, pool_type='avg', global_pooling=True)
self._squeeze = Linear(
self._pool = paddle.fluid.dygraph.nn.Pool2D(
pool_size=0, pool_type='avg', global_pooling=True
)
self._squeeze = paddle.nn.Linear(
num_channels,
num_channels // reduction_ratio,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.05)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.05)
),
act='relu',
)
self._excitation = Linear(
self.act_1 = paddle.nn.ReLU()
self._excitation = paddle.nn.Linear(
num_channels // reduction_ratio,
num_channels,
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.05)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.05)
),
act='sigmoid',
)
self.act_2 = paddle.nn.Softmax()
def forward(self, input):
y = self._pool(input)
y = paddle.reshape(y, shape=[-1, self._num_channels])
y = self._squeeze(y)
y = self.act_1(y)
y = self._excitation(y)
y = self.act_2(y)
y = fluid.layers.elementwise_mul(x=input, y=y, axis=0)
return y
......@@ -218,9 +224,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 101:
cardinality = 32
reduction_ratio = 16
......@@ -233,9 +237,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=2,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
elif layers == 152:
cardinality = 64
reduction_ratio = 16
......@@ -262,9 +264,7 @@ class SeResNeXt(fluid.dygraph.Layer):
stride=1,
act='relu',
)
self.pool = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max'
)
self.pool = paddle.nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
self.bottleneck_block_list = []
num_channels = 64
......@@ -287,8 +287,7 @@ class SeResNeXt(fluid.dygraph.Layer):
num_channels = bottleneck_block._num_channels_out
self.bottleneck_block_list.append(bottleneck_block)
shortcut = True
self.pool2d_avg = Pool2D(
self.pool2d_avg = paddle.fluid.dygraph.nn.Pool2D(
pool_size=7, pool_type='avg', global_pooling=True
)
import math
......@@ -297,14 +296,14 @@ class SeResNeXt(fluid.dygraph.Layer):
self.pool2d_avg_output = num_filters[-1] * 4 * 1 * 1
self.out = Linear(
self.out = paddle.nn.Linear(
self.pool2d_avg_output,
class_dim,
act='softmax',
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(-stdv, stdv)
),
)
self.out_act = paddle.nn.Softmax()
def forward(self, inputs):
if self.layers == 50 or self.layers == 101:
......@@ -321,7 +320,7 @@ class SeResNeXt(fluid.dygraph.Layer):
y = self.pool2d_avg(y)
y = paddle.reshape(y, shape=[-1, self.pool2d_avg_output])
y = self.out(y)
return y
return self.out_act(y)
class TestImperativeResneXt(unittest.TestCase):
......
......@@ -16,14 +16,15 @@ import os
import unittest
import numpy as np
import os
import paddle
import paddle.fluid as fluid
class SimpleFCLayer(fluid.dygraph.Layer):
def __init__(self, feature_size, batch_size, fc_size):
super().__init__()
self._linear = fluid.dygraph.Linear(feature_size, fc_size)
self._linear = paddle.nn.Linear(feature_size, fc_size)
self._offset = fluid.dygraph.to_variable(
np.random.random((batch_size, fc_size)).astype('float32')
)
......
......@@ -19,10 +19,14 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.fluid as fluid
import paddle.nn.functional as F
from paddle.fluid import Embedding, Layer, LayerNorm, Linear, core
from paddle.fluid.dygraph import guard, to_variable
from paddle.fluid import Embedding, LayerNorm, Layer
from paddle.nn import Linear
from paddle.fluid.dygraph import to_variable, guard
from test_imperative_base import new_program_scope
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid import core
import numpy as np
import paddle.nn.functional as F
from paddle.jit import TracedLayer
np.set_printoptions(suppress=True)
......@@ -428,12 +432,13 @@ class PrePostProcessLayer(Layer):
class PositionwiseFeedForwardLayer(Layer):
def __init__(self, d_inner_hid, d_hid, dropout_rate):
super().__init__()
self._i2h = Linear(d_hid, d_inner_hid, act="relu")
self._i2h = Linear(d_hid, d_inner_hid)
self._h2o = Linear(d_inner_hid, d_hid)
self._dropout_rate = dropout_rate
def forward(self, x):
hidden = self._i2h(x)
hidden = paddle.nn.functional.relu(hidden)
if self._dropout_rate:
hidden = fluid.layers.dropout(
hidden,
......
......@@ -23,8 +23,8 @@ import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.nn import Linear
from paddle.fluid import unique_name
from paddle.fluid.dygraph import Linear
from paddle.fluid.dygraph.io import INFER_PARAMS_INFO_SUFFIX
from paddle.fluid.layers.utils import flatten
from paddle.jit.api import declarative
......
......@@ -91,10 +91,12 @@ class TestLayer(LayerTest):
class CustomLayer(fluid.Layer):
def __init__(self, input_size, linear1_size=4):
super().__init__()
self.linear1 = nn.Linear(
self.linear1 = paddle.nn.Linear(
input_size, linear1_size, bias_attr=False
)
self.linear2 = nn.Linear(linear1_size, 1, bias_attr=False)
self.linear2 = paddle.nn.Linear(
linear1_size, 1, bias_attr=False
)
def forward(self, x, do_linear2=False):
ret = self.linear1(x)
......@@ -172,7 +174,7 @@ class TestLayer(LayerTest):
dtype='float32',
append_batch_size=False,
)
linear = nn.Linear(
linear = paddle.nn.Linear(
32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1)
)
ret = linear(t)
......@@ -182,7 +184,7 @@ class TestLayer(LayerTest):
with self.dynamic_graph():
with _test_eager_guard():
t = base.to_variable(inp)
linear = nn.Linear(
linear = paddle.nn.Linear(
32,
4,
bias_attr=fluid.initializer.ConstantInitializer(value=1),
......@@ -191,7 +193,7 @@ class TestLayer(LayerTest):
dy_eager_ret_value = dy_eager_ret.numpy()
t = base.to_variable(inp)
linear = nn.Linear(
linear = paddle.nn.Linear(
32, 4, bias_attr=fluid.initializer.ConstantInitializer(value=1)
)
dy_ret = linear(t)
......@@ -205,7 +207,7 @@ class TestLayer(LayerTest):
# the input of Linear must be Variable.
def test_Variable():
inp = np.ones([3, 32, 32], dtype='float32')
linear = nn.Linear(
linear = paddle.nn.Linear(
32,
4,
bias_attr=fluid.initializer.ConstantInitializer(value=1),
......@@ -218,7 +220,7 @@ class TestLayer(LayerTest):
# float16 only can be set on GPU place
def test_type():
inp = np.ones([3, 32, 32], dtype='int32')
linear = nn.Linear(
linear = paddle.nn.Linear(
32,
4,
bias_attr=fluid.initializer.ConstantInitializer(value=1),
......@@ -261,7 +263,7 @@ class TestLayer(LayerTest):
# the input of Linear must be Variable.
def test_Variable():
inp = np.ones([3, 32, 32], dtype='float32')
linear = nn.Linear(
linear = paddle.nn.Linear(
32,
4,
bias_attr=fluid.initializer.ConstantInitializer(value=1),
......@@ -274,7 +276,7 @@ class TestLayer(LayerTest):
# float16 only can be set on GPU place
def test_type():
inp = np.ones([3, 32, 32], dtype='int32')
linear = nn.Linear(
linear = paddle.nn.Linear(
32,
4,
bias_attr=fluid.initializer.ConstantInitializer(value=1),
......@@ -1703,7 +1705,9 @@ class TestLayer(LayerTest):
images = layers.data(
name='pixel', shape=[3, 6, 6, 6], dtype='float32'
)
conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2)
conv3d = paddle.nn.Conv3D(
in_channels=3, out_channels=3, kernel_size=2
)
ret = conv3d(images)
static_ret2 = self.get_static_graph_result(
feed={'pixel': np.ones([2, 3, 6, 6, 6], dtype='float32')},
......@@ -1713,12 +1717,16 @@ class TestLayer(LayerTest):
with self.dynamic_graph():
with _test_eager_guard():
images = np.ones([2, 3, 6, 6, 6], dtype='float32')
conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2)
conv3d = paddle.nn.Conv3D(
in_channels=3, out_channels=3, kernel_size=2
)
dy_eager_ret = conv3d(base.to_variable(images))
dy_eager_rlt_value = dy_eager_ret.numpy()
images = np.ones([2, 3, 6, 6, 6], dtype='float32')
conv3d = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2)
conv3d = paddle.nn.Conv3D(
in_channels=3, out_channels=3, kernel_size=2
)
dy_ret = conv3d(base.to_variable(images))
dy_rlt_value = dy_ret.numpy()
......@@ -1735,14 +1743,14 @@ class TestLayer(LayerTest):
custom_weight
)
)
conv3d1 = nn.Conv3D(
num_channels=3, num_filters=3, filter_size=2
conv3d1 = paddle.nn.Conv3D(
in_channels=3, out_channels=3, kernel_size=2
)
conv3d2 = nn.Conv3D(
num_channels=3,
num_filters=3,
filter_size=2,
param_attr=weight_attr,
conv3d2 = paddle.nn.Conv3D(
in_channels=3,
out_channels=3,
kernel_size=2,
weight_attr=weight_attr,
)
dy_ret1 = conv3d1(base.to_variable(images))
dy_ret2 = conv3d2(base.to_variable(images))
......@@ -1780,12 +1788,14 @@ class TestLayer(LayerTest):
custom_weight
)
)
conv3d1 = nn.Conv3D(num_channels=3, num_filters=3, filter_size=2)
conv3d2 = nn.Conv3D(
num_channels=3,
num_filters=3,
filter_size=2,
param_attr=weight_attr,
conv3d1 = paddle.nn.Conv3D(
in_channels=3, out_channels=3, kernel_size=2
)
conv3d2 = paddle.nn.Conv3D(
in_channels=3,
out_channels=3,
kernel_size=2,
weight_attr=weight_attr,
)
dy_ret1 = conv3d1(base.to_variable(images))
dy_ret2 = conv3d2(base.to_variable(images))
......@@ -2277,15 +2287,15 @@ class TestLayer(LayerTest):
with self.static_graph():
img = layers.data(name='pixel', shape=[3, 2, 2, 2], dtype='float32')
out = paddle.static.nn.conv3d_transpose(
input=img, num_filters=12, filter_size=12, use_cudnn=False
input=img, num_filters=12, filter_size=12, use_cudnn=True
)
static_rlt = self.get_static_graph_result(
feed={'pixel': input_array}, fetch_list=[out]
)[0]
with self.static_graph():
img = layers.data(name='pixel', shape=[3, 2, 2, 2], dtype='float32')
conv3d_transpose = nn.Conv3DTranspose(
num_channels=3, num_filters=12, filter_size=12, use_cudnn=False
conv3d_transpose = paddle.nn.Conv3DTranspose(
in_channels=3, out_channels=12, kernel_size=12
)
out = conv3d_transpose(img)
static_rlt2 = self.get_static_graph_result(
......@@ -2293,17 +2303,16 @@ class TestLayer(LayerTest):
)[0]
with self.dynamic_graph():
with _test_eager_guard():
conv3d_transpose = nn.Conv3DTranspose(
num_channels=3,
num_filters=12,
filter_size=12,
use_cudnn=False,
conv3d_transpose = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=12,
kernel_size=12,
)
dy_eager_rlt = conv3d_transpose(base.to_variable(input_array))
dy_eager_rlt_value = dy_eager_rlt.numpy()
conv3d_transpose = nn.Conv3DTranspose(
num_channels=3, num_filters=12, filter_size=12, use_cudnn=False
conv3d_transpose = paddle.nn.Conv3DTranspose(
in_channels=3, out_channels=12, kernel_size=12
)
dy_rlt = conv3d_transpose(base.to_variable(input_array))
dy_rlt_value = dy_rlt.numpy()
......@@ -2320,20 +2329,18 @@ class TestLayer(LayerTest):
custom_weight
)
)
conv3d1 = nn.Conv3DTranspose(
num_channels=3,
num_filters=3,
filter_size=2,
conv3d1 = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=2,
bias_attr='eager_conv3d1_b',
use_cudnn=False,
)
conv3d2 = nn.Conv3DTranspose(
num_channels=3,
num_filters=3,
filter_size=2,
param_attr=weight_attr,
conv3d2 = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=2,
weight_attr=weight_attr,
bias_attr='eager_conv3d2_b',
use_cudnn=False,
)
dy_ret1 = conv3d1(base.to_variable(images))
dy_ret2 = conv3d2(base.to_variable(images))
......@@ -2371,20 +2378,18 @@ class TestLayer(LayerTest):
custom_weight
)
)
conv3d1 = nn.Conv3DTranspose(
num_channels=3,
num_filters=3,
filter_size=2,
conv3d1 = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=2,
bias_attr='conv3d1_b',
use_cudnn=False,
)
conv3d2 = nn.Conv3DTranspose(
num_channels=3,
num_filters=3,
filter_size=2,
param_attr=weight_attr,
conv3d2 = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=2,
weight_attr=weight_attr,
bias_attr='conv3d2_b',
use_cudnn=False,
)
dy_ret1 = conv3d1(base.to_variable(images))
dy_ret2 = conv3d2(base.to_variable(images))
......
......@@ -16,7 +16,7 @@ import copy
import math
import numpy as np
import unittest
import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.framework as framework
......@@ -120,7 +120,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
def test_LR_state_dict(self):
with fluid.dygraph.guard():
x = np.random.uniform(-1, 1, [3, 10]).astype("float32")
linear = fluid.dygraph.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
input = fluid.dygraph.to_variable(x)
Exponential_scheduler = fluid.dygraph.ExponentialDecay(
......@@ -291,7 +291,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
learning_rate = 0.5
milestones = [2, 4, 8]
decay_rate = 0.2
linear = fluid.dygraph.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
scheduler = fluid.dygraph.MultiStepDecay(
learning_rate, milestones, decay_rate
......@@ -364,7 +364,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
lr_lambda = lambda x: 0.95**x
scheduler = fluid.dygraph.LambdaDecay(learning_rate, lr_lambda)
linear = fluid.dygraph.nn.Linear(10, 10)
linear = paddle.nn.Linear(10, 10)
adam = fluid.optimizer.Adam(
scheduler, parameter_list=linear.parameters()
)
......
......@@ -15,6 +15,9 @@
import sys
import time
import unittest
import numpy as np
import paddle
from paddle.nn import Linear
import numpy as np
from test_multiprocess_dataloader_static import (
......@@ -29,7 +32,6 @@ from test_multiprocess_dataloader_static import (
)
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.io import DataLoader
......@@ -37,11 +39,11 @@ class SimpleFCNet(fluid.dygraph.Layer):
def __init__(self):
super().__init__()
param_attr = fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.8)
param_attr = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.8)
)
bias_attr = fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
)
self._fcs = []
in_channel = IMAGE_SIZE
......@@ -50,21 +52,21 @@ class SimpleFCNet(fluid.dygraph.Layer):
Linear(
in_channel,
hidden_size,
act='tanh',
param_attr=param_attr,
weight_attr=param_attr,
bias_attr=bias_attr,
)
)
self._fcs.append(paddle.nn.Tanh())
in_channel = hidden_size
self._fcs.append(
Linear(
in_channel,
CLASS_NUM,
act='softmax',
param_attr=param_attr,
weight_attr=param_attr,
bias_attr=bias_attr,
)
)
self._fcs.append(paddle.nn.Softmax())
def forward(self, image):
out = image
......
......@@ -15,6 +15,10 @@
import sys
import time
import unittest
import numpy as np
import paddle
from paddle.nn import Linear
import numpy as np
from test_multiprocess_dataloader_iterable_dataset_static import (
......@@ -29,7 +33,6 @@ from test_multiprocess_dataloader_iterable_dataset_static import (
)
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Linear
from paddle.io import DataLoader
......@@ -37,11 +40,11 @@ class SimpleFCNet(fluid.dygraph.Layer):
def __init__(self):
super().__init__()
param_attr = fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.8)
param_attr = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.8)
)
bias_attr = fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr = paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
)
self._fcs = []
in_channel = IMAGE_SIZE
......@@ -50,21 +53,22 @@ class SimpleFCNet(fluid.dygraph.Layer):
Linear(
in_channel,
hidden_size,
act='tanh',
param_attr=param_attr,
weight_attr=param_attr,
bias_attr=bias_attr,
)
)
self._fcs.append(paddle.nn.Tanh())
in_channel = hidden_size
self._fcs.append(
Linear(
in_channel,
CLASS_NUM,
act='softmax',
param_attr=param_attr,
weight_attr=param_attr,
bias_attr=bias_attr,
)
)
self._fcs.append(paddle.nn.Softmax())
def forward(self, image):
out = image
......
......@@ -138,34 +138,34 @@ def static(
class DygraphLayer(fluid.dygraph.Layer):
def __init__(self):
super().__init__()
self.fc_1 = fluid.dygraph.nn.Linear(
self.fc_1 = paddle.nn.Linear(
INPUT_SIZE,
FC_SIZE,
act='relu',
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.99)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.99)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.5)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.5)
),
)
self.fc_2 = fluid.dygraph.nn.Linear(
self.act_1 = paddle.nn.ReLU()
self.fc_2 = paddle.nn.Linear(
FC_SIZE,
CLASS_NUM,
act='softmax',
param_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=1.2)
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=1.2)
),
bias_attr=fluid.ParamAttr(
initializer=fluid.initializer.Constant(value=0.8)
bias_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Constant(value=0.8)
),
)
self.act_2 = paddle.nn.Softmax()
def forward(self, inputs):
hidden = self.fc_1(inputs)
prediction = self.fc_2(hidden)
return hidden, prediction
return self.act_1(hidden), self.act_2(prediction)
def dynamic(train_data, use_cuda=False, use_parallel_exe=False):
......
......@@ -18,7 +18,6 @@ import numpy as np
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid import Program, program_guard
from paddle.fluid.tests.unittests.op_test import OpTest
......@@ -1460,112 +1459,5 @@ class TestPool2DAPI_Error(unittest.TestCase):
self.assertRaises(ValueError, run_5)
class TestDygraphPool2DAPIError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
# the input of Pool2D must be Variable.
data1 = np.random.random((3, 32, 32, 5)).astype('float32')
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
global_pooling=False,
)
self.assertRaises(TypeError, pool2d, data1)
# the input dtype of Pool2D must be uint8 or int8 or float16 or float32 or float64
# uint8 and int8 only can be set on mkldnn
# float16 only can be set on GPU place
data2 = fluid.layers.data(
name='x1', shape=[3, 32, 32, 5], dtype="int32"
)
self.assertRaises(TypeError, pool2d, data2)
def test_data_format_error(self):
with program_guard(Program(), Program()):
# the data_format must be 'NCHW' or 'NHWC'
data1 = np.random.random((3, 32, 32, 5)).astype('float32')
self.assertRaises(
ValueError,
fluid.dygraph.Pool2D,
pool_size=2,
pool_type='max',
pool_stride=1,
global_pooling=False,
data_format='NWHC',
)
class TestDygraphPool2DAPI(unittest.TestCase):
def test_nhwc(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='NHWC',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05)
def test_lower_case(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='max',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='nhwc',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05)
def test_upper_case(self):
with fluid.dygraph.guard():
data = np.random.random((3, 32, 32, 5)).astype('float32')
x = fluid.dygraph.to_variable(data)
pool2d = fluid.dygraph.Pool2D(
pool_size=2,
pool_type='MAX',
pool_stride=1,
pool_padding=[0, 0],
global_pooling=False,
data_format='nhwc',
)
out1 = pool2d(x)
out2 = pool2D_forward_naive(
data,
[2, 2],
[1, 1],
paddings=[0, 0],
pool_type='max',
data_format='NHWC',
)
np.testing.assert_allclose(out1.numpy(), out2, rtol=1e-05)
if __name__ == '__main__':
unittest.main()
......@@ -259,7 +259,9 @@ class TestRegularizer(unittest.TestCase):
def test_repeated_regularization(self):
l1 = fluid.regularizer.L1Decay(regularization_coeff=0.1)
l2 = fluid.regularizer.L2Decay(regularization_coeff=0.01)
fc_param_attr = fluid.ParamAttr(regularizer=l1)
fc_param_attr = paddle.ParamAttr(
regularizer=paddle.regularizer.L1Decay()
)
with fluid.program_guard(fluid.Program(), fluid.Program()):
x = fluid.layers.uniform_random([2, 2, 3])
out = fluid.layers.fc(x, 5, param_attr=fc_param_attr)
......@@ -273,11 +275,11 @@ class TestRegularizer(unittest.TestCase):
paddle.seed(1)
paddle.framework.random._manual_program_seed(1)
linear1 = fluid.dygraph.Linear(
2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr
linear1 = paddle.nn.Linear(
2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr
)
linear2 = fluid.dygraph.Linear(
2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr
linear2 = paddle.nn.Linear(
2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr
)
loss1 = linear1(input)
......
......@@ -169,7 +169,9 @@ class TestRegularizer(unittest.TestCase):
paddle.enable_static()
l1 = paddle.regularizer.L1Decay(0.1)
l2 = paddle.regularizer.L2Decay(0.01)
fc_param_attr = fluid.ParamAttr(regularizer=l1)
fc_param_attr = paddle.ParamAttr(
regularizer=paddle.regularizer.L1Decay()
)
with fluid.program_guard(fluid.Program(), fluid.Program()):
x = fluid.layers.uniform_random([2, 2, 3])
out = fluid.layers.fc(x, 5, param_attr=fc_param_attr)
......@@ -183,11 +185,11 @@ class TestRegularizer(unittest.TestCase):
paddle.seed(1)
paddle.framework.random._manual_program_seed(1)
linear1 = fluid.dygraph.Linear(
2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr
linear1 = paddle.nn.Linear(
2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr
)
linear2 = fluid.dygraph.Linear(
2, 2, param_attr=fc_param_attr, bias_attr=fc_param_attr
linear2 = paddle.nn.Linear(
2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr
)
loss1 = linear1(input)
......
......@@ -354,7 +354,7 @@ class TestVarBase(unittest.TestCase):
var = fluid.dygraph.to_variable("test", name="abc")
# test to_variable of LayerObjectHelper(LayerHelperBase)
with self.assertRaises(TypeError):
linear = fluid.dygraph.Linear(32, 64)
linear = paddle.nn.Linear(32, 64)
var = linear._helper.to_variable("test", name="abc")
def test_to_variable(self):
......@@ -1170,13 +1170,13 @@ class TestVarBase(unittest.TestCase):
self._assert_to_static(var_base, static_param, True)
# Convert ParamBase into Parameter
fc = fluid.dygraph.Linear(
fc = paddle.nn.Linear(
10,
20,
param_attr=fluid.ParamAttr(
weight_attr=paddle.ParamAttr(
learning_rate=0.001,
do_model_average=True,
regularizer=fluid.regularizer.L1Decay(),
regularizer=paddle.regularizer.L1Decay(),
),
)
weight = fc.parameters()[0]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册