提交 fe888728 编写于 作者: C ceci3

test=develop, change testfile

上级 5e92eb3f
...@@ -45,15 +45,6 @@ def npairloss(anchor, positive, labels, l2_reg=0.002): ...@@ -45,15 +45,6 @@ def npairloss(anchor, positive, labels, l2_reg=0.002):
return l2loss + celoss return l2loss + celoss
def create_or_get_tensor(scope, var_name, var, place):
tensor = scope.var(var_name).get_tensor()
if var is not None:
assert isinstance(var, np.ndarray)
tensor.set_recursive_sequence_lengths([])
tensor.set(var, place)
return tensor
class TestNpairLossOp(unittest.TestCase): class TestNpairLossOp(unittest.TestCase):
def setUp(self): def setUp(self):
self.dtype = np.float32 self.dtype = np.float32
...@@ -61,10 +52,11 @@ class TestNpairLossOp(unittest.TestCase): ...@@ -61,10 +52,11 @@ class TestNpairLossOp(unittest.TestCase):
def __assert_close(self, tensor, np_array, msg, atol=1e-4): def __assert_close(self, tensor, np_array, msg, atol=1e-4):
self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg) self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg)
def check_with_place(self, place, dtype, shape): def test_npair_loss(self):
reg_lambda = 0.002 reg_lambda = 0.002
num_data, feat_dim, num_classes = shape[0], shape[1], shape[2] num_data, feat_dim, num_classes = 18, 6, 3
place = core.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
embeddings_anchor = np.random.rand(num_data, embeddings_anchor = np.random.rand(num_data,
...@@ -79,49 +71,31 @@ class TestNpairLossOp(unittest.TestCase): ...@@ -79,49 +71,31 @@ class TestNpairLossOp(unittest.TestCase):
row_labels, row_labels,
l2_reg=reg_lambda) l2_reg=reg_lambda)
anchor_tensor = fluid.layers.data( anc = fluid.layers.create_tensor(
name='anchor', dtype='float32', persistable=True, name='anc')
shape=[num_data, feat_dim], pos = fluid.layers.create_tensor(
dtype=self.dtype, dtype='float32', persistable=True, name='pos')
append_batch_size=False) lab = fluid.layers.create_tensor(
positive_tensor = fluid.layers.data( dtype='float32', persistable=True, name='lab')
name='positive', fluid.layers.assign(input=embeddings_anchor, output=anc)
shape=[num_data, feat_dim], fluid.layers.assign(input=embeddings_positive, output=pos)
dtype=self.dtype, fluid.layers.assign(input=row_labels, output=lab)
append_batch_size=False)
labels_tensor = fluid.layers.data(
name='labels_t',
shape=[num_data],
dtype=self.dtype,
append_batch_size=False)
npair_loss_op = fluid.layers.npair_loss( npair_loss_op = fluid.layers.npair_loss(
anchor=anchor_tensor, anchor=anc, positive=pos, labels=lab, l2_reg=reg_lambda)
positive=positive_tensor, out_tensor = exe.run(feed={'anc': anc,
labels=labels_tensor, 'pos': pos,
l2_reg=reg_lambda) 'lab': lab},
out_tensor = exe.run(feed={
'anchor': embeddings_anchor,
'positive': embeddings_positive,
'labels_t': row_labels
},
fetch_list=[npair_loss_op.name]) fetch_list=[npair_loss_op.name])
self.__assert_close( self.__assert_close(
out_tensor, out_tensor,
out_loss, out_loss,
"inference output are different at " + str(place) + ", " + "inference output are different at " + str(place) + ", " +
str(np.dtype(dtype)) + str(np.array(out_tensor)) + str(out_loss), str(np.dtype('float32')) + str(np.array(out_tensor)) +
str(out_loss),
atol=1e-3) atol=1e-3)
def test_check_output(self):
places = [core.CPUPlace()]
if core.is_compiled_with_cuda() and core.op_support_gpu("npair_loss"):
places.append(core.CUDAPlace(0))
for place in places:
self.check_with_place(place, self.dtype, [18, 6, 3])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册