提交 b976ba3e 编写于 作者: Y Youwei Song 提交者: hong

fix unittests (#21786)

test=develop
上级 73e97d39
...@@ -29,20 +29,18 @@ from utils import DyGraphProgramDescTracerTestHelper ...@@ -29,20 +29,18 @@ from utils import DyGraphProgramDescTracerTestHelper
class SimpleNet(fluid.Layer): class SimpleNet(fluid.Layer):
def __init__(self, def __init__(self,
name_scope,
hidden_size, hidden_size,
vocab_size, vocab_size,
num_steps=20, num_steps=20,
init_scale=0.1, init_scale=0.1,
is_sparse=False, is_sparse=False,
dtype='float32'): dtype='float32'):
super(SimpleNet, self).__init__(name_scope) super(SimpleNet, self).__init__()
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.vocab_size = vocab_size self.vocab_size = vocab_size
self.init_scale = init_scale self.init_scale = init_scale
self.num_steps = num_steps self.num_steps = num_steps
self.embedding = Embedding( self.embedding = Embedding(
self.full_name(),
size=[vocab_size, hidden_size], size=[vocab_size, hidden_size],
dtype=dtype, dtype=dtype,
is_sparse=is_sparse, is_sparse=is_sparse,
...@@ -100,7 +98,6 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -100,7 +98,6 @@ class TestDygraphSimpleNet(unittest.TestCase):
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
simple_net = SimpleNet( simple_net = SimpleNet(
"simple_net",
hidden_size=hidden_size, hidden_size=hidden_size,
vocab_size=vocab_size, vocab_size=vocab_size,
num_steps=num_steps, num_steps=num_steps,
...@@ -120,7 +117,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -120,7 +117,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
for i in range(batch_num): for i in range(batch_num):
x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = np.arange(12).reshape(4, 3).astype('int64')
y_data = np.arange(1, 13).reshape(4, 3).astype('int64') y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
x_data = x_data.reshape((-1, num_steps, 1)) x_data = x_data.reshape((-1, num_steps))
y_data = y_data.reshape((-1, 1)) y_data = y_data.reshape((-1, 1))
x = to_variable(x_data) x = to_variable(x_data)
...@@ -143,7 +140,6 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -143,7 +140,6 @@ class TestDygraphSimpleNet(unittest.TestCase):
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
simple_net = SimpleNet( simple_net = SimpleNet(
"simple_net",
hidden_size=hidden_size, hidden_size=hidden_size,
vocab_size=vocab_size, vocab_size=vocab_size,
num_steps=num_steps, num_steps=num_steps,
...@@ -153,7 +149,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -153,7 +149,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
exe = fluid.Executor(place) exe = fluid.Executor(place)
sgd = SGDOptimizer(learning_rate=1e-3) sgd = SGDOptimizer(learning_rate=1e-3)
x = fluid.layers.data( x = fluid.layers.data(
name="x", shape=[-1, num_steps, 1], dtype='int64') name="x", shape=[-1, num_steps], dtype='int64')
y = fluid.layers.data(name="y", shape=[-1, 1], dtype=dtype) y = fluid.layers.data(name="y", shape=[-1, 1], dtype=dtype)
static_loss = simple_net(x, y) static_loss = simple_net(x, y)
...@@ -172,7 +168,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -172,7 +168,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
for i in range(batch_num): for i in range(batch_num):
x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = np.arange(12).reshape(4, 3).astype('int64')
y_data = np.arange(1, 13).reshape(4, 3).astype('int64') y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
x_data = x_data.reshape((-1, num_steps, 1)) x_data = x_data.reshape((-1, num_steps))
y_data = y_data.reshape((-1, 1)) y_data = y_data.reshape((-1, 1))
fetch_list = [static_loss] fetch_list = [static_loss]
fetch_list.extend(static_param_name_list) fetch_list.extend(static_param_name_list)
......
...@@ -24,10 +24,9 @@ import paddle.fluid.core as core ...@@ -24,10 +24,9 @@ import paddle.fluid.core as core
class SimpleNet(fluid.Layer): class SimpleNet(fluid.Layer):
def __init__(self, name_scope, vocab_size, hidden_size, dtype): def __init__(self, vocab_size, hidden_size, dtype):
super(SimpleNet, self).__init__(name_scope) super(SimpleNet, self).__init__()
self.emb = fluid.dygraph.Embedding( self.emb = fluid.dygraph.Embedding(
self.full_name(),
size=[vocab_size, hidden_size], size=[vocab_size, hidden_size],
dtype=dtype, dtype=dtype,
param_attr='emb.w', param_attr='emb.w',
...@@ -53,11 +52,10 @@ class TestSimpleNet(unittest.TestCase): ...@@ -53,11 +52,10 @@ class TestSimpleNet(unittest.TestCase):
adam = SGDOptimizer(learning_rate=0.001) adam = SGDOptimizer(learning_rate=0.001)
# grad_clip = fluid.dygraph_grad_clip.GradClipByGlobalNorm(5.0) # grad_clip = fluid.dygraph_grad_clip.GradClipByGlobalNorm(5.0)
input_word = np.array( input_word = np.array([[1, 2], [2, 1]]).astype('int64')
[[[1], [2]], [[2], [1]]]).astype('int64')
input = to_variable(input_word) input = to_variable(input_word)
simplenet = SimpleNet("SimpleNet", 20, 32, dtype) simplenet = SimpleNet(20, 32, dtype)
input_emb, emb = simplenet(input) input_emb, emb = simplenet(input)
try: try:
...@@ -99,11 +97,10 @@ class TestSimpleNet(unittest.TestCase): ...@@ -99,11 +97,10 @@ class TestSimpleNet(unittest.TestCase):
grad_clip = fluid.dygraph_grad_clip.GradClipByGlobalNorm( grad_clip = fluid.dygraph_grad_clip.GradClipByGlobalNorm(
5.0) 5.0)
input_word = np.array( input_word = np.array([[1, 2], [2, 1]]).astype('int64')
[[[1], [2]], [[2], [1]]]).astype('int64')
input = to_variable(input_word) input = to_variable(input_word)
simplenet = SimpleNet("SimpleNet", 20, 32, "float32") simplenet = SimpleNet(20, 32, "float32")
input_emb, emb = simplenet(input) input_emb, emb = simplenet(input)
try: try:
......
...@@ -30,20 +30,18 @@ from paddle.fluid.dygraph.jit import TracedLayer ...@@ -30,20 +30,18 @@ from paddle.fluid.dygraph.jit import TracedLayer
class SimpleNet(fluid.Layer): class SimpleNet(fluid.Layer):
def __init__(self, def __init__(self,
name_scope,
hidden_size, hidden_size,
vocab_size, vocab_size,
num_steps=20, num_steps=20,
init_scale=0.1, init_scale=0.1,
is_sparse=False, is_sparse=False,
dtype='float32'): dtype='float32'):
super(SimpleNet, self).__init__(name_scope) super(SimpleNet, self).__init__()
self.hidden_size = hidden_size self.hidden_size = hidden_size
self.vocab_size = vocab_size self.vocab_size = vocab_size
self.init_scale = init_scale self.init_scale = init_scale
self.num_steps = num_steps self.num_steps = num_steps
self.embedding = Embedding( self.embedding = Embedding(
self.full_name(),
size=[vocab_size, hidden_size], size=[vocab_size, hidden_size],
dtype=dtype, dtype=dtype,
is_sparse=is_sparse, is_sparse=is_sparse,
...@@ -109,7 +107,6 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -109,7 +107,6 @@ class TestDygraphSimpleNet(unittest.TestCase):
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
simple_net = SimpleNet( simple_net = SimpleNet(
"simple_net",
hidden_size=hidden_size, hidden_size=hidden_size,
vocab_size=vocab_size, vocab_size=vocab_size,
num_steps=num_steps, num_steps=num_steps,
...@@ -130,7 +127,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -130,7 +127,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
for i in range(batch_num): for i in range(batch_num):
x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = np.arange(12).reshape(4, 3).astype('int64')
y_data = np.arange(1, 13).reshape(4, 3).astype('int64') y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
x_data = x_data.reshape((-1, num_steps, 1)) x_data = x_data.reshape((-1, num_steps))
y_data = y_data.reshape((-1, 1)) y_data = y_data.reshape((-1, 1))
x = to_variable(x_data) x = to_variable(x_data)
...@@ -153,7 +150,6 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -153,7 +150,6 @@ class TestDygraphSimpleNet(unittest.TestCase):
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
simple_net = SimpleNet( simple_net = SimpleNet(
"simple_net",
hidden_size=hidden_size, hidden_size=hidden_size,
vocab_size=vocab_size, vocab_size=vocab_size,
num_steps=num_steps, num_steps=num_steps,
...@@ -163,7 +159,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -163,7 +159,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
exe = fluid.Executor(place) exe = fluid.Executor(place)
sgd = SGDOptimizer(learning_rate=1e-3) sgd = SGDOptimizer(learning_rate=1e-3)
x = fluid.layers.data( x = fluid.layers.data(
name="x", shape=[-1, num_steps, 1], dtype='int64') name="x", shape=[-1, num_steps], dtype='int64')
y = fluid.layers.data(name="y", shape=[-1, 1], dtype=dtype) y = fluid.layers.data(name="y", shape=[-1, 1], dtype=dtype)
static_loss = simple_net(x, y) static_loss = simple_net(x, y)
...@@ -182,7 +178,7 @@ class TestDygraphSimpleNet(unittest.TestCase): ...@@ -182,7 +178,7 @@ class TestDygraphSimpleNet(unittest.TestCase):
for i in range(batch_num): for i in range(batch_num):
x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = np.arange(12).reshape(4, 3).astype('int64')
y_data = np.arange(1, 13).reshape(4, 3).astype('int64') y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
x_data = x_data.reshape((-1, num_steps, 1)) x_data = x_data.reshape((-1, num_steps))
y_data = y_data.reshape((-1, 1)) y_data = y_data.reshape((-1, 1))
fetch_list = [static_loss] fetch_list = [static_loss]
fetch_list.extend(static_param_name_list) fetch_list.extend(static_param_name_list)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册