repo
stringlengths
2
99
file
stringlengths
13
225
code
stringlengths
0
18.3M
file_length
int64
0
18.3M
avg_line_length
float64
0
1.36M
max_line_length
int64
0
4.26M
extension_type
stringclasses
1 value
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_recurrent_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.layers as layers from paddle.fluid.framework import Program, grad_var_name from paddle.fluid.executor import Executor from paddle.fluid.backward import append_backward import numpy as np import paddle.fluid.core as core class PyRNNBase(object): def __init__(self, input_shape, output_shape): self.x = np.ones(shape=input_shape).astype("float32") self.y = np.zeros(shape=output_shape).astype("float32") def step(self, step_id, x): raise NotImplementedError def forward(self): for step_id in range(self.x.shape[0]): self.step(step_id, self.x[step_id]) return np.array([np.mean(self.y)]) def segment_inputs(self): return [self.x[i] for i in range(self.x.shape[0])] class PySimpleRNN1(PyRNNBase): def __init__(self, input_shape, output_shape): super(PySimpleRNN1, self).__init__(input_shape, output_shape) seq_len, batch_size, input_dim = input_shape self.h_boot = np.random.normal(size=(batch_size, input_dim)).astype("float32") self.scale = 1.0 / 2.0 men_dim = (seq_len, batch_size, input_dim) self.mems = np.zeros(shape=men_dim).astype("float32") def step(self, step_id, x): if step_id == 0: pre_mem = self.h_boot else: pre_mem = self.mems[step_id - 1] self.mems[step_id] = (pre_mem + x) * self.scale self.y[step_id] = self.mems[step_id] class PySimpleRNN2(PyRNNBase): def __init__(self, input_shape, output_shape): super(PySimpleRNN2, self).__init__(input_shape, output_shape) seq_len, batch_size, input_dim = input_shape self.W = np.random.normal(size=(input_dim, input_dim)).astype("float32") self.U = np.random.normal(size=(input_dim, input_dim)).astype("float32") self.h_boot = np.ones(shape=(batch_size, input_dim)).astype("float32") men_dim = (seq_len, batch_size, input_dim) self.mems = np.zeros(shape=men_dim).astype("float32") def step(self, step_id, x): if step_id > 0: pre_mem = self.mems[step_id - 1] else: pre_mem = self.h_boot xW = np.matmul(x, self.W).astype("float32") hU = np.matmul(pre_mem, self.U).astype("float32") def py_sigmoid(x): return 1. / (1. + np.exp(-x)) self.mems[step_id] = py_sigmoid(xW + hU) self.y[step_id] = self.mems[step_id] def create_tensor(np_data, place): tensor = core.LoDTensor() tensor.set(np_data, place) return tensor class RecurrentOpTest1(unittest.TestCase): ''' Test RNNOp equation: h_t = ( x_t + h_{t-1} ) / scale vars: - x memories: - h outputs: - h ''' input_dim = 2 batch_size = 1 sent_len = 1 def setup_program(self): self.main_program = Program() self.startup_program = Program() self.p_info = { "main_program": self.main_program, "startup_program": self.startup_program } self.place = core.CPUPlace() def setUp(self): self.setup_program() self.data_field = {"x", "h_boot"} self.input_shape = (self.sent_len, self.batch_size, self.input_dim) self.output_shape = (self.sent_len, self.batch_size, self.input_dim) self.py_rnn = PySimpleRNN1(self.input_shape, self.output_shape) self.output = layers.mean(self.create_rnn_op(), **self.p_info) def create_rnn_op(self): x = layers.data( shape=[self.sent_len, self.batch_size, self.input_dim], dtype='float32', name='x', append_batch_size=False, **self.p_info) x.stop_gradient = False h_boot = layers.data( shape=[self.input_dim], dtype='float32', name='h_boot', **self.p_info) h_boot.stop_gradient = False rnn = layers.StaticRNN(main_program=self.main_program) with rnn.step(): h_pre = rnn.memory(init=h_boot) x_t = rnn.step_input(x) h = layers.scale( x=layers.elementwise_add( x=h_pre, y=x_t, **self.p_info), scale=self.py_rnn.scale, **self.p_info) rnn.update_memory(h_pre, h) rnn.output(h) return rnn() def forward(self): self.feed_map = { x: create_tensor(getattr(self.py_rnn, x), self.place) for x in self.data_field } exe = Executor(self.place) out = exe.run(self.main_program, feed=self.feed_map, fetch_list=[self.output]) return out[0] def backward(self): self.feed_map = { x: create_tensor(getattr(self.py_rnn, x), self.place) for x in self.data_field } fetch_list = [ self.main_program.global_block().var(grad_var_name(x)) for x in self.data_field ] exe = Executor(self.place) return exe.run(self.main_program, feed=self.feed_map, fetch_list=fetch_list, return_numpy=False) def test_backward(self): self.check_forward() append_backward(self.output) ana_grad = [np.array(x) for x in self.backward()] num_grad = self.get_numerical_gradient() for idx, name in enumerate(self.data_field): self.assertEqual(num_grad[idx].shape, ana_grad[idx].shape) self.assertTrue( np.isclose( num_grad[idx], ana_grad[idx], rtol=0.1).all()) def check_forward(self): print 'test recurrent op forward' pd_output = self.forward() py_output = self.py_rnn.forward() print 'pd_output', pd_output print print 'py_output', py_output self.assertEqual(pd_output.shape, py_output.shape) self.assertTrue(np.isclose(pd_output, py_output, rtol=0.1).all()) def get_numerical_gradient(self, delta=0.005): dloss_dout = 1.0 feed_list = [getattr(self.py_rnn, x) for x in self.data_field] grad_list = [np.zeros_like(x) for x in feed_list] for feed, grad in zip(feed_list, grad_list): for f, g in np.nditer([feed, grad], op_flags=['readwrite']): o = float(f) f[...] = o + delta y_pos = self.forward() f[...] = o - delta y_neg = self.forward() f[...] = o dout_dfeed = (y_pos - y_neg) / (delta * 2) g[...] = dout_dfeed[0] return grad_list class RecurrentOpTest2(RecurrentOpTest1): ''' Test RNNOp equation: h_t = \sigma (W x_t + U h_{t-1}) weights: - W - U vars: - x memories: - h outputs: - h ''' input_dim = 2 batch_size = 10 sent_len = 2 def setUp(self): self.setup_program() self.data_field = {"x", "h_boot", "W", "U"} self.input_shape = (self.sent_len, self.batch_size, self.input_dim) self.output_shape = (self.sent_len, self.batch_size, self.input_dim) self.py_rnn = PySimpleRNN2(self.input_shape, self.output_shape) self.output = layers.mean(self.create_rnn_op(), **self.p_info) def create_rnn_op(self): x = layers.data( shape=[self.sent_len, self.batch_size, self.input_dim], dtype='float32', name='x', append_batch_size=False, **self.p_info) x.stop_gradient = False h_boot = layers.data( shape=[self.input_dim], dtype='float32', name='h_boot', **self.p_info) h_boot.stop_gradient = False rnn = layers.StaticRNN(main_program=self.main_program) with rnn.step(): h_pre = rnn.memory(init=h_boot) x_t = rnn.step_input(x) temp_l = layers.fc(input=x_t, size=self.input_dim, param_attr='W', bias_attr=False, **self.p_info) temp_r = layers.fc(input=h_pre, size=self.input_dim, param_attr='U', bias_attr=False, **self.p_info) h = layers.sigmoid( x=layers.elementwise_add( x=temp_l, y=temp_r, **self.p_info), **self.p_info) rnn.update_memory(h_pre, h) rnn.output(h) return rnn() class RecurrentOpMultipleMemoryTest(RecurrentOpTest1): ''' Test RNNOp with two memories equation: h_1 = h_pre_1 h_2 = h_pre_2 y = h_1 + h_2 vars: - x memories: - h_1, h_2 outputs: - y ''' class PySimpleRNN3(PyRNNBase): def __init__(self, input_shape, output_shape): super(RecurrentOpMultipleMemoryTest.PySimpleRNN3, self).__init__( input_shape, output_shape) seq_len, batch_size, input_dim = input_shape self.h_boot1 = np.random.normal(size=(batch_size, input_dim)).astype("float32") self.h_boot2 = np.random.normal(size=(batch_size, input_dim)).astype("float32") men_dim = (seq_len, batch_size, input_dim) self.mems1 = np.zeros(shape=men_dim).astype("float32") self.mems2 = np.zeros(shape=men_dim).astype("float32") def step(self, step_id, x): if step_id == 0: pre_mem1 = self.h_boot1 pre_mem2 = self.h_boot2 else: pre_mem1 = self.mems1[step_id - 1] pre_mem2 = self.mems2[step_id - 1] self.mems1[step_id] = pre_mem1 self.mems2[step_id] = pre_mem2 self.y[step_id] = self.mems1[step_id] + self.mems2[step_id] + x input_dim = 1 batch_size = 1 sent_len = 2 def setUp(self): self.setup_program() self.data_field = {"x", "h_boot1", "h_boot2"} self.input_shape = (self.sent_len, self.batch_size, self.input_dim) self.output_shape = (self.sent_len, self.batch_size, self.input_dim) self.py_rnn = RecurrentOpMultipleMemoryTest.PySimpleRNN3( self.input_shape, self.output_shape) self.output = layers.mean(self.create_rnn_op(), **self.p_info) def create_rnn_op(self): x = layers.data( shape=[self.sent_len, self.batch_size, self.input_dim], dtype='float32', name='x', append_batch_size=False, **self.p_info) x.stop_gradient = False h_boot1 = layers.data( shape=[self.batch_size, self.input_dim], dtype='float32', name='h_boot1', append_batch_size=False, **self.p_info) h_boot1.stop_gradient = False h_boot2 = layers.data( shape=[self.batch_size, self.input_dim], dtype='float32', name='h_boot2', append_batch_size=False, **self.p_info) h_boot2.stop_gradient = False rnn = layers.StaticRNN(main_program=self.main_program) with rnn.step(): h_pre1 = rnn.memory(init=h_boot1) h_pre2 = rnn.memory(init=h_boot2) x_t = rnn.step_input(x) mem1 = layers.scale(x=h_pre1, scale=1.0, **self.p_info) mem2 = layers.scale(x=h_pre2, scale=1.0, **self.p_info) out = layers.sums(input=[mem1, x_t, mem2], **self.p_info) rnn.update_memory(h_pre1, mem1) rnn.update_memory(h_pre2, mem2) rnn.output(out) return rnn() class RecurrentOpNoMemBootTest(RecurrentOpTest1): ''' Test RNNOp with two memories equation: mem = x + mem_pre y = mem vars: - x memories: - mem outputs: - y ''' class PySimpleRNN4(PyRNNBase): def __init__(self, input_shape, output_shape): super(RecurrentOpNoMemBootTest.PySimpleRNN4, self).__init__( input_shape, output_shape) men_dim = input_shape self.mems = np.zeros(shape=men_dim).astype("float32") def step(self, step_id, x): if step_id == 0: pre_mem = np.zeros_like(x) else: pre_mem = self.mems[step_id - 1] self.mems[step_id] = pre_mem + x self.y[step_id] = self.mems[step_id] input_dim = 1 batch_size = 1 sent_len = 2 def setUp(self): self.setup_program() self.data_field = {"x"} self.input_shape = (self.sent_len, self.batch_size, self.input_dim) self.output_shape = (self.sent_len, self.batch_size, self.input_dim) self.py_rnn = RecurrentOpNoMemBootTest.PySimpleRNN4(self.input_shape, self.output_shape) self.output = layers.mean(self.create_rnn_op(), **self.p_info) print self.main_program def create_rnn_op(self): x = layers.data( shape=[self.sent_len, self.batch_size, self.input_dim], dtype='float32', name='x', append_batch_size=False, **self.p_info) x.stop_gradient = False rnn = layers.StaticRNN(main_program=self.main_program) with rnn.step(): mem_pre = rnn.memory(shape=[-1, self.input_dim], batch_ref=x) x_t = rnn.step_input(x) mem = layers.elementwise_add(x=mem_pre, y=x_t, **self.p_info) rnn.update_memory(mem_pre, mem) rnn.output(mem) return rnn() if __name__ == '__main__': unittest.main()
14,773
30.300847
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_cross_entropy_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest, randomize_probability class TestCrossEntropyOp1(OpTest): """Test cross-entropy with discrete one-hot labels. """ def setUp(self): self.op_type = "cross_entropy" batch_size = 30 class_num = 10 X = randomize_probability(batch_size, class_num, dtype='float64') label = np.random.randint(0, class_num, (batch_size, 1), dtype="int64") cross_entropy = np.asmatrix( [[-np.log(X[i][label[i][0]])] for i in range(X.shape[0])], dtype="float64") self.inputs = {"X": X, "Label": label} self.outputs = {"Y": cross_entropy} self.attrs = {"soft_label": False} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Y", numeric_grad_delta=0.001) class TestCrossEntropyOp2(OpTest): """Test cross-entropy with vectorized soft labels. """ def setUp(self): self.op_type = "cross_entropy" batch_size = 5 class_num = 37 X = randomize_probability(batch_size, class_num) label = np.random.uniform(0.1, 1.0, [batch_size, class_num]).astype("float32") label /= label.sum(axis=1, keepdims=True) cross_entropy = (-label * np.log(X)).sum( axis=1, keepdims=True).astype("float32") self.inputs = {"X": X, "Label": label} self.outputs = {"Y": cross_entropy} self.attrs = {"soft_label": True} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad( ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001) class TestCrossEntropyOp3(OpTest): """Test cross-entropy with vectorized one-hot representation of labels. """ def setUp(self): self.op_type = "cross_entropy" batch_size = 5 class_num = 17 X = randomize_probability(batch_size, class_num) label_index = np.random.randint( 0, class_num, (batch_size), dtype="int32") label = np.zeros(X.shape) label[np.arange(batch_size), label_index] = 1 cross_entropy = np.asmatrix( [[-np.log(X[i][label_index[i]])] for i in range(X.shape[0])], dtype="float32") cross_entropy2 = (-label * np.log(X)).sum( axis=1, keepdims=True).astype("float32") self.inputs = {"X": X, "Label": label.astype(np.float32)} self.outputs = {"Y": cross_entropy} self.attrs = {"soft_label": True} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad( ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001) if __name__ == "__main__": unittest.main()
3,469
30.545455
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_activation_mkldnn_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid.core as core from op_test import OpTest from scipy.special import expit from test_activation_op import TestRelu, TestTanh, TestSqrt, TestAbs class TestMKLDNNReluDim2(TestRelu): def setUp(self): super(TestMKLDNNReluDim2, self).setUp() self.attrs = {"use_mkldnn": True} class TestMKLDNNTanhDim2(TestTanh): def setUp(self): super(TestMKLDNNTanhDim2, self).setUp() self.attrs = {"use_mkldnn": True} class TestMKLDNNSqrtDim2(TestSqrt): def setUp(self): super(TestMKLDNNSqrtDim2, self).setUp() self.attrs = {"use_mkldnn": True} class TestMKLDNNAbsDim2(TestAbs): def setUp(self): super(TestMKLDNNAbsDim2, self).setUp() self.attrs = {"use_mkldnn": True} class TestMKLDNNReluDim4(TestRelu): def setUp(self): super(TestMKLDNNReluDim4, self).setUp() x = np.random.uniform(-1, 1, [2, 4, 3, 5]).astype("float32") # The same reason with TestAbs x[np.abs(x) < 0.005] = 0.02 out = np.maximum(x, 0) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} class TestMKLDNNTanhDim4(TestTanh): def setUp(self): super(TestMKLDNNTanhDim4, self).setUp() self.inputs = { 'X': np.random.uniform(0.1, 1, [2, 4, 3, 5]).astype("float32") } self.outputs = {'Out': np.tanh(self.inputs['X'])} self.attrs = {"use_mkldnn": True} class TestMKLDNNSqrtDim4(TestSqrt): def setUp(self): super(TestMKLDNNSqrtDim4, self).setUp() self.inputs = { 'X': np.random.uniform(0.1, 1, [2, 4, 3, 5]).astype("float32") } self.outputs = {'Out': np.sqrt(self.inputs['X'])} self.attrs = {"use_mkldnn": True} class TestMKLDNNAbsDim4(TestAbs): def setUp(self): super(TestMKLDNNAbsDim4, self).setUp() x = np.random.uniform(-1, 1, [2, 4, 3, 5]).astype("float32") # The same reason with TestAbs x[np.abs(x) < 0.005] = 0.02 self.inputs = {'X': x} self.outputs = {'Out': np.abs(self.inputs['X'])} self.attrs = {"use_mkldnn": True} if __name__ == '__main__': unittest.main()
2,892
27.93
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_const_value.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.framework as framework class ConditionalBlock(unittest.TestCase): def test_const_value(self): self.assertEqual(framework.GRAD_VAR_SUFFIX, "@GRAD") self.assertEqual(framework.TEMP_VAR_NAME, "@TEMP@") self.assertEqual(framework.GRAD_VAR_SUFFIX, "@GRAD") self.assertEqual(framework.ZERO_VAR_SUFFIX, "@ZERO") if __name__ == '__main__': unittest.main()
1,041
34.931034
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_chunk_eval_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class Segment(object): def __init__(self, chunk_type, start_idx, end_idx): self.chunk_type = chunk_type self.start_idx = start_idx self.end_idx = end_idx def __str__(self): return '(Segment: %s, %s, %s)' % (self.chunk_type, self.start_idx, self.end_idx) __repr__ = __str__ class TestChunkEvalOp(OpTest): num_sequences = 5 batch_size = 50 def parse_scheme(self): if self.scheme == 'IOB': self.num_tag_types = 2 elif self.scheme == 'IOE': self.num_tag_types = 2 def fill_with_chunks(self, data, chunks): for chunk in chunks: if self.scheme == 'IOB': data[chunk.start_idx] = chunk.chunk_type * self.num_tag_types data[chunk.start_idx + 1: chunk.end_idx] = chunk.chunk_type * self.num_tag_types + ( self.num_tag_types - 1) data[chunk.end_idx] = chunk.chunk_type * self.num_tag_types + ( self.num_tag_types - 1 ) if chunk.start_idx < chunk.end_idx else data[chunk.start_idx] elif self.scheme == 'IOE': data[chunk.start_idx: chunk.end_idx] = chunk.chunk_type * self.num_tag_types data[chunk.end_idx] = chunk.chunk_type * self.num_tag_types + ( self.num_tag_types - 1) def rand_chunks(self, starts, num_chunks): if num_chunks < 0: num_chunks = np.random.randint(starts[-1]) chunks = [] # generate chunk beginnings chunk_begins = sorted( np.random.choice( range(starts[-1]), num_chunks, replace=False)) seq_chunk_begins = [] begin_idx = 0 # divide chunks into sequences for i in range(len(starts) - 1): tmp_chunk_begins = [] while begin_idx < len(chunk_begins) and chunk_begins[ begin_idx] < starts[i + 1]: tmp_chunk_begins.append(chunk_begins[begin_idx]) begin_idx += 1 seq_chunk_begins.append(tmp_chunk_begins) # generate chunk ends chunk_ends = [] for i in range(len(seq_chunk_begins)): for j in range(len(seq_chunk_begins[i])): low = seq_chunk_begins[i][j] high = seq_chunk_begins[i][j + 1] if j < len(seq_chunk_begins[ i]) - 1 else starts[i + 1] chunk_ends.append(np.random.randint(low, high)) # generate chunks for chunk_pos in zip(chunk_begins, chunk_ends): chunk_type = np.random.randint(self.num_chunk_types) chunks.append(Segment(chunk_type, *chunk_pos)) return chunks def gen_chunks(self, infer, label, starts): chunks = self.rand_chunks(starts, self.num_infer_chunks + self.num_label_chunks - self.num_correct_chunks) correct_chunks = np.random.choice( range(len(chunks)), self.num_correct_chunks, replace=False) infer_chunks = np.random.choice( [x for x in range(len(chunks)) if x not in correct_chunks], self.num_infer_chunks - self.num_correct_chunks, replace=False) infer_chunks = sorted(correct_chunks.tolist() + infer_chunks.tolist()) label_chunks = np.random.choice( [x for x in range(len(chunks)) if x not in infer_chunks], self.num_label_chunks - self.num_correct_chunks, replace=False) label_chunks = sorted(correct_chunks.tolist() + label_chunks.tolist()) self.fill_with_chunks(infer, [chunks[idx] for idx in infer_chunks]) self.fill_with_chunks(label, [chunks[idx] for idx in label_chunks]) # exclude types in excluded_chunk_types if len(self.excluded_chunk_types) > 0: for idx in correct_chunks: if chunks[idx].chunk_type in self.excluded_chunk_types: self.num_correct_chunks -= 1 for idx in infer_chunks: if chunks[idx].chunk_type in self.excluded_chunk_types: self.num_infer_chunks -= 1 for idx in label_chunks: if chunks[idx].chunk_type in self.excluded_chunk_types: self.num_label_chunks -= 1 return self.num_correct_chunks, self.num_infer_chunks, self.num_label_chunks def set_confs(self): # Use the IOB scheme and labels with 2 chunk types self.scheme = 'IOB' self.num_chunk_types = 2 self.excluded_chunk_types = [] self.other_chunk_type = self.num_chunk_types self.attrs = { 'num_chunk_types': self.num_chunk_types, 'chunk_scheme': self.scheme, 'excluded_chunk_types': self.excluded_chunk_types } self.parse_scheme() self.num_correct_chunks, self.num_infer_chunks, self.num_label_chunks = 4, 5, 9 def set_data(self): infer = np.zeros((self.batch_size, )).astype('int64') infer.fill(self.num_chunk_types * self.num_tag_types) label = np.copy(infer) starts = np.random.choice( range(1, self.batch_size), self.num_sequences - 1, replace=False).tolist() starts.extend([0, self.batch_size]) starts = sorted(starts) self.num_correct_chunks, self.num_infer_chunks, self.num_label_chunks = self.gen_chunks( infer, label, starts) self.inputs = { 'Inference': (infer, [starts]), 'Label': (label, [starts]) } precision = float( self.num_correct_chunks ) / self.num_infer_chunks if self.num_infer_chunks else 0 recall = float(self.num_correct_chunks ) / self.num_label_chunks if self.num_label_chunks else 0 f1 = float(2 * precision * recall) / ( precision + recall) if self.num_correct_chunks else 0 self.outputs = { 'Precision': np.asarray( [precision], dtype='float32'), 'Recall': np.asarray( [recall], dtype='float32'), 'F1-Score': np.asarray( [f1], dtype='float32'), 'NumInferChunks': np.asarray( [self.num_infer_chunks], dtype='int64'), 'NumLabelChunks': np.asarray( [self.num_label_chunks], dtype='int64'), 'NumCorrectChunks': np.asarray( [self.num_correct_chunks], dtype='int64') } def setUp(self): self.op_type = 'chunk_eval' self.set_confs() self.set_data() def test_check_output(self): self.check_output() class TestChunkEvalOpWithExclude(TestChunkEvalOp): def set_confs(self): # Use the IOE scheme and labels with 3 chunk types self.scheme = 'IOE' self.num_chunk_types = 3 self.excluded_chunk_types = [1] self.other_chunk_type = self.num_chunk_types self.attrs = { 'num_chunk_types': self.num_chunk_types, 'chunk_scheme': self.scheme, 'excluded_chunk_types': self.excluded_chunk_types } self.parse_scheme() self.num_correct_chunks, self.num_infer_chunks, self.num_label_chunks = 15, 18, 20 if __name__ == '__main__': unittest.main()
8,130
39.655
96
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_is_empty_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestEmpty(OpTest): def setUp(self): self.op_type = "is_empty" self.inputs = {'X': np.array([1, 2, 3])} self.outputs = {'Out': np.array([False])} def test_check_output(self): self.check_output() class TestNotEmpty(TestEmpty): def setUp(self): self.op_type = "is_empty" self.inputs = {'X': np.array([])} self.outputs = {'Out': np.array([True])} if __name__ == "__main__": unittest.main()
1,146
28.410256
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_while_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.layers as layers from paddle.fluid.executor import Executor import paddle.fluid.core as core from paddle.fluid.backward import append_backward import numpy class TestWhileOp(unittest.TestCase): def test_simple_forward(self): d0 = layers.data( "d0", shape=[10], append_batch_size=False, dtype='float32') d1 = layers.data( "d1", shape=[10], append_batch_size=False, dtype='float32') d2 = layers.data( "d2", shape=[10], append_batch_size=False, dtype='float32') i = layers.zeros(shape=[1], dtype='int64') i.stop_gradient = True init = layers.zeros(shape=[10], dtype='float32') mem_array = layers.array_write(x=init, i=i) data_array = layers.array_write(x=d0, i=i) i = layers.increment(i) layers.array_write(d1, i, array=data_array) i = layers.increment(i) layers.array_write(d2, i, array=data_array) i = layers.zeros(shape=[1], dtype='int64') i.stop_gradient = True array_len = layers.fill_constant(shape=[1], dtype='int64', value=3) array_len.stop_gradient = True cond = layers.less_than(x=i, y=array_len) while_op = layers.While(cond=cond) with while_op.block(): d = layers.array_read(array=data_array, i=i) prev = layers.array_read(array=mem_array, i=i) result = layers.sums(input=[d, prev]) i = layers.increment(x=i, in_place=True) layers.array_write(result, i=i, array=mem_array) layers.less_than(x=i, y=array_len, cond=cond) sum_result = layers.array_read(array=mem_array, i=i) loss = layers.mean(sum_result) append_backward(loss) cpu = core.CPUPlace() exe = Executor(cpu) d = [] for i in xrange(3): d.append(numpy.random.random(size=[10]).astype('float32')) outs = exe.run(feed={'d0': d[0], 'd1': d[1], 'd2': d[2]}, fetch_list=[sum_result]) self.assertAlmostEqual(numpy.sum(d), numpy.sum(outs[0]), delta=0.01) if __name__ == '__main__': unittest.main()
2,854
34.246914
76
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lod_array_length_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.layers as layers from paddle.fluid.executor import Executor import paddle.fluid.core as core import numpy class TestLoDArrayLength(unittest.TestCase): def test_array_length(self): tmp = layers.zeros(shape=[10], dtype='int32') i = layers.fill_constant(shape=[1], dtype='int64', value=10) arr = layers.array_write(tmp, i=i) arr_len = layers.array_length(arr) cpu = core.CPUPlace() exe = Executor(cpu) result = exe.run(fetch_list=[arr_len])[0] self.assertEqual(11, result[0]) if __name__ == '__main__': unittest.main()
1,241
33.5
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_memory_optimization_transpiler.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function import unittest import paddle.fluid.layers as layers import paddle.fluid.optimizer as optimizer from paddle.fluid.framework import Program, program_guard from paddle.fluid.transpiler import memory_optimize class TestControlFlowGraph(unittest.TestCase): def setUp(self): program = Program() with program_guard(program, startup_program=Program()): x = layers.data(name='x', shape=[13], dtype='float32') y_predict = layers.fc(input=x, size=1, act=None) y = layers.data(name='y', shape=[1], dtype='float32') cost = layers.square_error_cost(input=y_predict, label=y) avg_cost = layers.mean(cost) opt = optimizer.SGD(learning_rate=0.001) opt = opt.minimize(avg_cost) self.program = program def test_control_flow_graph(self): print("before optimization") print(str(self.program)) result_program = memory_optimize(self.program) print("after optimization") print(str(result_program)) if __name__ == "__main__": unittest.main()
1,736
35.1875
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_multihead_attention.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle.fluid.core as core import numpy as np class TestMultiheadAttention(unittest.TestCase): def gen_random_input(self): """Generate random input data. """ # batch_size, max_sequence_length, hidden dimension self.input_shape = (3, 13, 16) self.queries = np.random.random(size=self.input_shape).astype("float32") self.keys = np.random.random(size=self.input_shape).astype("float32") def set_program(self): """Build the test program. """ queries = fluid.layers.data( name="queries", shape=self.input_shape, dtype="float32", append_batch_size=False) queries.stop_gradient = False keys = fluid.layers.data( name="keys", shape=self.input_shape, dtype="float32", append_batch_size=False) keys.stop_gradient = False contexts = fluid.nets.scaled_dot_product_attention( queries=queries, keys=keys, values=keys, num_heads=8, dropout_rate=0.) out = fluid.layers.reduce_sum(contexts, dim=None) fluid.backward.append_backward(loss=out) self.fetch_list = [contexts] def run_program(self): """Run the test program. """ places = [core.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) for place in places: self.set_inputs(place) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) output = exe.run(fluid.default_main_program(), feed=self.inputs, fetch_list=self.fetch_list, return_numpy=True) self.op_output = output def set_inputs(self, place): """Set the randomly generated data to the test program. """ self.inputs = {} queries = fluid.Tensor() queries.set(self.queries, place) keys = fluid.Tensor() keys.set(self.keys, place) self.inputs["keys"] = keys self.inputs["queries"] = queries def test_multihead_attention(self): self.gen_random_input() self.set_program() self.run_program() #fixme(caoying) add more meaningfull unittest. if __name__ == '__main__': unittest.main()
3,100
30.323232
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_shrink_rnn_memory.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core from paddle.fluid.executor import Executor import paddle.fluid.layers as layers from paddle.fluid.backward import append_backward from paddle.fluid.framework import default_main_program, switch_main_program from paddle.fluid.framework import Program import numpy as np class TestShrinkRNNMemoryBase(unittest.TestCase): def setUp(self): self.main_program = Program() switch_main_program(self.main_program) x = layers.data('x', shape=[100], dtype='float32') x.stop_gradient = False rank_table_tensor = layers.data( 'rank_table_tensor', shape=[1], dtype='float32', lod_level=1) table = layers.lod_rank_table(x=rank_table_tensor) i = layers.zeros(dtype='int64', shape=[1]) self.mem1 = layers.shrink_memory(x=x, i=i, table=table) i = layers.increment(x=i) i.stop_gradient = True self.mem2 = layers.shrink_memory(x=self.mem1, i=i, table=table) i = layers.increment(x=i) i.stop_gradient = True self.mem3 = layers.shrink_memory(x=self.mem2, i=i, table=table) mem3_mean = layers.mean(self.mem3) append_backward(loss=mem3_mean) self.x_grad = self.main_program.global_block().var('x@GRAD') def sum_lodtensor(self, tensor): sum_res = 0.0 for i in xrange(np.product(tensor.get_dims())): sum_res += tensor.get_float_element(i) return sum_res class TestShrinkRNNMemoryReferLoD(TestShrinkRNNMemoryBase): def test_refer_lod(self): cpu = core.CPUPlace() x_tensor = core.LoDTensor() x_tensor.set_lod([[0, 2, 5, 6]]) tensor_np = np.random.random(size=(6, 100)).astype('float32') x_tensor.set(tensor_np, cpu) rank_table_tensor = core.LoDTensor() rank_table_tensor.set_lod([[0, 1, 3, 6]]) rank_table_tensor.set(np.random.random(size=(6, 1)).astype('float32'), cpu) exe = Executor(cpu) outs = exe.run( feed={'x': x_tensor, 'rank_table_tensor': rank_table_tensor}, fetch_list=[self.mem1, self.mem2, self.mem3, self.x_grad], return_numpy=False) self.assertTrue(np.allclose(tensor_np[0:6], outs[0])) self.assertTrue(np.allclose(tensor_np[0:5], outs[1])) self.assertTrue(np.allclose(tensor_np[0:2], outs[2])) self.assertAlmostEqual(1.0, self.sum_lodtensor(outs[3]), delta=0.01) class TestShrinkRNNMemoryNoLoD(TestShrinkRNNMemoryBase): def test_no_lod(self): cpu = core.CPUPlace() x_tensor = core.LoDTensor() tensor_np = np.random.random(size=(3, 100)).astype('float32') x_tensor.set(tensor_np, cpu) rank_table_tensor = core.LoDTensor() rank_table_tensor.set_lod([[0, 1, 3, 6]]) rank_table_tensor.set(np.random.random(size=(6, 1)).astype('float32'), cpu) exe = Executor(cpu) outs = exe.run( feed={'x': x_tensor, 'rank_table_tensor': rank_table_tensor}, fetch_list=[self.mem1, self.mem2, self.mem3, self.x_grad], return_numpy=False) self.assertTrue(np.allclose(tensor_np[0:3], outs[0])) self.assertTrue(np.allclose(tensor_np[0:2], outs[1])) self.assertTrue(np.allclose(tensor_np[0:1], outs[2])) self.assertAlmostEqual(1.0, self.sum_lodtensor(outs[3]), delta=0.01) if __name__ == '__main__': unittest.main()
4,141
38.826923
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_fill_constant_batch_size_like_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestFillConstantBatchSizeLikeWhenFirstDimIsBatchSize(OpTest): def setUp(self): self.op_type = "fill_constant_batch_size_like" self.inputs = {'Input': np.random.random((219, 232)).astype("float32")} self.attrs = {'value': 3.5, 'shape': [-1, 132, 7]} out = np.random.random((219, 132, 7)).astype("float32") out.fill(3.5) self.outputs = {'Out': out} def test_check_output(self): self.check_output() class TestFillConstantBatchSizeLikeWhenSecondDimIsBatchSize(OpTest): def setUp(self): self.op_type = "fill_constant_batch_size_like" self.inputs = {'Input': np.random.random((219, 232)).astype("float32")} self.attrs = { 'value': 3.5, 'shape': [132, -1, 7], 'input_dim_idx': 0, 'output_dim_idx': 1 } out = np.random.random((132, 219, 7)).astype("float32") out.fill(3.5) self.outputs = {'Out': out} def test_check_output(self): self.check_output() class TestFillConstantBatchSizeLikeWithLoDTensor(OpTest): def setUp(self): self.op_type = "fill_constant_batch_size_like" self.inputs = { 'Input': (np.random.random((31, 28)).astype("float32"), [[0, 9, 23, 31]]) } self.attrs = { 'value': 3.5, 'shape': [-1, 16], 'input_dim_idx': 0, 'output_dim_idx': 0 } out = np.random.random((3, 16)).astype("float32") out.fill(3.5) self.outputs = {'Out': out} def test_check_output(self): self.check_output() if __name__ == "__main__": unittest.main()
2,375
29.857143
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_warpctc_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys import unittest import numpy as np from op_test import OpTest from test_softmax_op import stable_softmax CUDA_BLOCK_SIZE = 512 class CTCForward(object): def __init__(self, softmax, softmax_lod, labels, labels_lod, blank, norm_by_times): self.softmax = softmax self.softmax_lod = softmax_lod assert labels.shape[1] == 1 self.labels = labels self.labels_lod = labels_lod self.blank = blank self.norm_by_times = norm_by_times self.level = 0 self.num_classes = softmax.shape[1] self.batch_size = len(softmax_lod[self.level]) - 1 assert self.batch_size == len(labels_lod[self.level]) - 1 self.loss = np.zeros([self.batch_size, 1], dtype="float32") self.gradient = np.zeros(self.softmax.shape, dtype="float32") # float64 self.EXP_MAX = sys.float_info.max self.EXP_MIN = sys.float_info.min self.LOG_ZERO = np.log(self.EXP_MIN) self.LOG_INFINITY = np.log(self.EXP_MAX) def safe_exp(self, x): if x <= self.LOG_ZERO: return 0.0 if x >= self.LOG_INFINITY: return self.EXP_MAX return np.exp(x) def safe_log(self, x): if x <= self.EXP_MIN: return self.LOG_ZERO return np.log(x) # x = lna and y = lnb are in log scale, ln(a / b) = lna - lnb def log_div(self, x, y): res = x - y if res <= self.LOG_ZERO: return self.LOG_ZERO if res >= self.LOG_INFINITY: return self.LOG_INFINITY return res # x = lna and y = lnb are in log scale, ln(a * b) = lna + lnb def log_mul(self, x, y): res = x + y if res <= self.LOG_ZERO: return self.LOG_ZERO if res >= self.LOG_INFINITY: return self.LOG_INFINITY return res # x = lna and y = lnb are in log scale, # ln(a + b) = lna + ln(1 + exp(lnb - lna)), where b > a def log_add(self, x, y): if x < y: t = y y = x x = t return x + self.safe_log(1 + self.safe_exp(y - x)) def segment_range(self, time, total_times, total_segments): start = max(0, total_segments - (2 * (total_times - time))) end = min(total_segments, 2 * (time + 1)) return start, end def forward_a_sequence(self, softmax_a_sequence, labels_a_sequence): total_times = softmax_a_sequence.shape[0] total_segments = labels_a_sequence.shape[0] * 2 + 1 required_times = labels_a_sequence.shape[0] old_label = -1 for i in range(labels_a_sequence.shape[0]): # two contingous labels with the same value if labels_a_sequence[i, 0] == old_label: required_times = required_times + 1 old_label = labels_a_sequence[i, 0] if total_times < required_times: return 0 # calculate the forward and backward variables, # reference Chapter 7.3 of "Alex Grave, Supervised Sequence # Labelling with Recurrent Neural Networks" log_acts = np.zeros([total_times, self.num_classes], dtype="float32") for i in range(total_times): for j in range(self.num_classes): log_acts[i, j] = self.safe_log(softmax_a_sequence[i, j]) # calculate the forward variables forward_vars = np.zeros([total_times, total_segments], dtype="float32") for i in range(total_times): for j in range(total_segments): forward_vars[i, j] = self.LOG_ZERO for i in range(total_times): # dp initialization at t0 if i == 0: forward_vars[i, 0] = log_acts[0, self.blank] if total_segments > 1: forward_vars[i, 1] = log_acts[0, labels_a_sequence[i, 0]] continue # dp from t1 start, end = self.segment_range(i, total_times, total_segments) for k in range(end - start): j = k + start if j & 1 == 1: label_idx = j / 2 label_val = labels_a_sequence[label_idx, 0] fv = self.log_add(forward_vars[i - 1, j], forward_vars[i - 1, j - 1]) if j > 1 and label_val != labels_a_sequence[label_idx - 1, 0]: fv = self.log_add(fv, forward_vars[i - 1, j - 2]) fv = self.log_mul(fv, log_acts[i, label_val]) else: fv = forward_vars[i - 1, j] if j > 0: fv = self.log_add(fv, forward_vars[i - 1, j - 1]) fv = self.log_mul(fv, log_acts[i, self.blank]) forward_vars[i, j] = fv # sum the last two value as log_prob log_prob = forward_vars[total_times - 1, total_segments - 1] if total_segments > 1: log_prob = self.log_add( log_prob, forward_vars[total_times - 1, total_segments - 2]) return -log_prob def forward(self): for i in range(self.batch_size): softmax_start_i = self.softmax_lod[self.level][i] softmax_end_i = self.softmax_lod[self.level][i + 1] labels_start_i = self.labels_lod[self.level][i] labels_end_i = self.labels_lod[self.level][i + 1] softmax_a_sequence = self.softmax[softmax_start_i:softmax_end_i, :] labels_a_sequence = self.labels[labels_start_i:labels_end_i, :] self.loss[i] = self.forward_a_sequence(softmax_a_sequence, labels_a_sequence) return self.loss class TestWarpCTCOp(OpTest): def config(self): self.batch_size = 4 self.num_classes = 8 self.logits_lod = [[0, 4, 5, 8, 11]] self.labels_lod = [[0, 3, 4, 8, 12]] self.blank = self.num_classes - 1 self.norm_by_times = False def setUp(self): self.op_type = "warpctc" self.config() logits = np.random.uniform( 0.1, 1.0, [self.logits_lod[0][-1], self.num_classes]).astype("float32") softmax = np.apply_along_axis(stable_softmax, 1, logits) # labels should not be blank labels = np.random.randint( 0, self.num_classes - 1, [self.labels_lod[0][-1], 1], dtype="int32") ctc = CTCForward(softmax, self.logits_lod, labels, self.labels_lod, self.blank, self.norm_by_times) loss = ctc.forward() max_sequence_length = 0 for i in range(self.batch_size): max_sequence_length = max( max_sequence_length, self.logits_lod[0][i + 1] - self.logits_lod[0][i]) self.gradient = np.zeros( [max_sequence_length, self.batch_size, self.num_classes], dtype="float32") self.inputs = { "Logits": (logits, self.logits_lod), "Label": (labels, self.labels_lod) } self.outputs = {"Loss": loss} self.attrs = {"blank": self.blank, "norm_by_times": self.norm_by_times} def test_check_output(self): self.check_output() def test_check_grad(self): self.outputs['WarpCTCGrad'] = self.gradient self.check_grad(["Logits"], "Loss", max_relative_error=0.007) class TestWarpCTCOpCase1(TestWarpCTCOp): def config(self): self.batch_size = 4 self.num_classes = CUDA_BLOCK_SIZE + 2 self.logits_lod = [[0, 4, 5, 8, 11]] self.labels_lod = [[0, 3, 4, 8, 12]] self.blank = 0 self.norm_by_times = False if __name__ == "__main__": unittest.main()
8,467
35.343348
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/parallel_executor_test_base.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import time import numpy as np __all__ = ['TestParallelExecutorBase'] class TestParallelExecutorBase(unittest.TestCase): def check_network_convergence(self, method, memory_opt=True, iter=50, batch_size=None, allow_op_delay=False, feed_dict=None, seed=None, use_parallel_executor=True, balance_parameter_opt_between_cards=False): def run_executor(exe, feed, fetch_list, program=None): if isinstance(exe, fluid.ParallelExecutor): res = exe.run(fetch_list=fetch_list, feed=feed) elif isinstance(exe, fluid.Executor): if program is None: program = fluid.default_main_program() res = exe.run(program=program, feed=feed, fetch_list=fetch_list) else: raise ValueError('Unkown type exe') return res main = fluid.Program() startup = fluid.Program() startup.random_seed = 1 # Fix random seed with fluid.program_guard(main, startup): if seed is not None: startup.random_seed = seed loss = method(use_feed=feed_dict is not None) adam = fluid.optimizer.Adam() adam.minimize(loss) if memory_opt: fluid.memory_optimize(main) place = fluid.CUDAPlace(0) startup_exe = fluid.Executor(place) startup_exe.run(startup) exec_strategy = fluid.ExecutionStrategy() exec_strategy.allow_op_delay = allow_op_delay build_strategy = fluid.BuildStrategy() build_strategy.reduce_strategy = fluid.BuildStrategy.ReduceStrategy.Reduce if balance_parameter_opt_between_cards else fluid.BuildStrategy.ReduceStrategy.AllReduce if use_parallel_executor: exe = fluid.ParallelExecutor( True, loss_name=loss.name, exec_strategy=exec_strategy, build_strategy=build_strategy) else: exe = fluid.Executor(place=place) if batch_size is not None: batch_size *= fluid.core.get_cuda_device_count() begin = time.time() first_loss, = run_executor( exe=exe, feed=feed_dict, fetch_list=[loss.name]) first_loss = np.array(first_loss) for i in xrange(iter): run_executor(exe=exe, feed=feed_dict, fetch_list=[]) last_loss, = run_executor( exe=exe, feed=feed_dict, fetch_list=[loss.name]) end = time.time() if batch_size is not None: print "%.4f Instance per second" % ( (batch_size * iter + 2) / (end - begin)) last_loss = np.array(last_loss) print first_loss, last_loss # self.assertGreater(first_loss[0], last_loss[0]) return first_loss, last_loss
3,900
39.216495
175
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_proximal_adagrad_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestProximalAdagradOp(OpTest): def setUp(self): self.op_type = "proximal_adagrad" w = np.random.random((102, 105)).astype("float32") m = np.random.random((102, 105)).astype("float32") g = np.random.random((102, 105)).astype("float32") lr = np.array([0.1]).astype("float32") l1 = 0.1 l2 = 0.2 self.inputs = {'Param': w, 'Grad': g, 'Moment': m, 'LearningRate': lr} self.attrs = {'l1': l1, 'l2': l2} param_out = 0.0 moment_out = m + g * g prox_param = w - lr * g / np.sqrt(moment_out) if l1 > 0.0: x = np.abs(prox_param) - lr * l1 x[x < 0] = 0 param_out = np.sign(prox_param) * (x / (1.0 + lr * l2)) else: param_out = prox_param / (1.0 + lr * l2) self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out} def test_check_output(self): self.check_output() if __name__ == "__main__": unittest.main()
1,677
31.901961
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_scatter_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestScatterOp(OpTest): def setUp(self): self.op_type = "scatter" ref_np = np.ones((3, 3)).astype("float32") index_np = np.array([1, 2]).astype("int32") updates_np = np.random.random((2, 3)).astype("float32") output_np = np.copy(ref_np) output_np[index_np] = updates_np self.inputs = {'X': ref_np, 'Ids': index_np, 'Updates': updates_np} self.outputs = {'Out': output_np} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['Updates'], 'Out', in_place=True) if __name__ == "__main__": unittest.main()
1,324
32.125
75
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_accuracy_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestAccuracyOp(OpTest): def setUp(self): self.op_type = "accuracy" n = 8192 infer = np.random.random((n, 1)).astype("float32") indices = np.random.randint(0, 2, (n, 1)) label = np.random.randint(0, 2, (n, 1)) self.inputs = {'Out': infer, 'Indices': indices, "Label": label} num_correct = 0 for rowid in xrange(n): for ele in indices[rowid]: if ele == label[rowid]: num_correct += 1 break self.outputs = { 'Accuracy': np.array([num_correct / float(n)]).astype("float32"), 'Correct': np.array([num_correct]).astype("int32"), 'Total': np.array([n]).astype("int32") } def test_check_output(self): self.check_output() if __name__ == '__main__': unittest.main()
1,546
32.630435
77
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_parameter.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from paddle.fluid.framework import default_main_program import paddle.fluid.core as core from paddle.fluid.executor import Executor import paddle.fluid.io as io from paddle.fluid.initializer import ConstantInitializer import numpy as np main_program = default_main_program() class TestParameter(unittest.TestCase): def test_param(self): shape = [784, 100] val = 1.0625 b = main_program.global_block() param = b.create_parameter( name='fc.w', shape=shape, dtype='float32', initializer=ConstantInitializer(val)) self.assertIsNotNone(param) self.assertEqual('fc.w', param.name) self.assertEqual((784, 100), param.shape) self.assertEqual(core.VarDesc.VarType.FP32, param.dtype) self.assertEqual(0, param.block.idx) exe = Executor(core.CPUPlace()) p = exe.run(main_program, fetch_list=[param])[0] self.assertTrue(np.allclose(p, np.ones(shape) * val)) p = io.get_parameter_value_by_name('fc.w', exe, main_program) self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val)) if __name__ == '__main__': unittest.main()
1,817
35.36
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_logical_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import op_test import unittest import numpy as np def create_test_class(op_type, callback, binary_op=True): class Cls(op_test.OpTest): def setUp(self): a = np.random.choice(a=[True, False], size=(10, 7)).astype(bool) if binary_op: b = np.random.choice(a=[True, False], size=(10, 7)).astype(bool) c = callback(a, b) else: c = callback(a) self.outputs = {'Out': c} self.op_type = op_type if binary_op: self.inputs = {'X': a, 'Y': b} else: self.inputs = {'X': a} def test_output(self): self.check_output() Cls.__name__ = op_type globals()[op_type] = Cls create_test_class('logical_and', lambda _a, _b: np.logical_and(_a, _b)) create_test_class('logical_or', lambda _a, _b: np.logical_or(_a, _b)) create_test_class('logical_not', lambda _a: np.logical_not(_a), False) create_test_class('logical_xor', lambda _a, _b: np.logical_xor(_a, _b)) if __name__ == '__main__': unittest.main()
1,707
33.16
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_mean_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestMeanOp(OpTest): def setUp(self): self.op_type = "mean" self.inputs = {'X': np.random.random((10, 10)).astype("float32")} self.outputs = {'Out': np.mean(self.inputs["X"])} def test_check_output(self): self.check_output() def test_checkout_grad(self): self.check_grad(['X'], 'Out') if __name__ == "__main__": unittest.main()
1,070
29.6
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_switch.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core import paddle.fluid.layers as layers import paddle.fluid.framework as framework from paddle.fluid.executor import Executor from paddle.fluid.framework import default_startup_program class TestSwitch(unittest.TestCase): def check_switch(self, value): x = layers.fill_constant(shape=[1], dtype='float32', value=value) zero_var = layers.fill_constant(shape=[1], dtype='float32', value=0.0) one_var = layers.fill_constant(shape=[1], dtype='float32', value=1.0) two_var = layers.fill_constant(shape=[1], dtype='float32', value=2.0) three_var = layers.fill_constant(shape=[1], dtype='float32', value=3.0) result = layers.create_global_var( shape=[1], value=-1.0, dtype='float32', persistable=True) with layers.Switch() as switch: with switch.case(layers.less_than(x, zero_var)): layers.assign(zero_var, result) with switch.case(layers.less_than(x, one_var)): layers.assign(one_var, result) with switch.case(layers.less_than(x, two_var)): layers.assign(two_var, result) with switch.default(): layers.assign(three_var, result) cpu = core.CPUPlace() exe = Executor(cpu) exe.run(default_startup_program()) out = exe.run(feed={}, fetch_list=[result])[0][0] return out def test_switch(self): test_data = {(-0.1, 0), (0.1, 1), (1.1, 2), (2.1, 3)} for x, expected_result in test_data: main_program = framework.Program() startup_program = framework.Program() with framework.program_guard(main_program, startup_program): result = self.check_switch(x) self.assertEqual(result, expected_result) if __name__ == '__main__': unittest.main()
2,509
37.615385
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lod_reset_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestLodResetOpByAttr(OpTest): def setUp(self): self.op_type = "lod_reset" x = np.random.random((10, 20)).astype("float32") lod = [[0, 3, 5, 10]] target_lod_0 = [0, 7, 10] self.inputs = {'X': (x, lod)} self.attrs = {'target_lod': target_lod_0} self.outputs = {'Out': (x, [target_lod_0])} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Out") class TestLodResetOpByInput(OpTest): def setUp(self): self.op_type = "lod_reset" x = np.random.random((10, 20)).astype("float32") lod = [[0, 3, 5, 10]] target_lod_0 = [0, 4, 7, 10] self.inputs = { 'X': (x, lod), 'Y': np.array([target_lod_0]).astype('int32') } self.outputs = {'Out': (x, [target_lod_0])} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Out", no_grad_set=set("Y")) class TestLodResetOpBoth(OpTest): def setUp(self): self.op_type = "lod_reset" x = np.random.random((10, 20)).astype("float32") lod = [[0, 3, 5, 10]] target_lod_0_attr = [0, 7, 10] target_lod_0_in = [0, 4, 7, 10] self.inputs = { 'X': (x, lod), 'Y': np.array(target_lod_0_in).astype('int32') } self.attrs = {'target_lod': target_lod_0_attr} self.outputs = {'Out': (x, [target_lod_0_in])} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Out", no_grad_set=set("Y")) class TestLodResetOpYIsLoDTensor(OpTest): def setUp(self): self.op_type = "lod_reset" x = np.random.random((10, 20)).astype("float32") lod = [[0, 3, 5, 10]] y = np.random.random((10, 10)).astype("float32") target_lod_0 = [[0, 4, 7, 10]] self.inputs = {'X': (x, lod), 'Y': (y, target_lod_0)} self.outputs = {'Out': (x, target_lod_0)} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Out", no_grad_set=set("Y")) if __name__ == '__main__': unittest.main()
2,946
29.697917
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_rank_loss_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestRankLossOp(OpTest): def setUp(self): self.op_type = "rank_loss" batch_size = 5 # labels_{i} = {0, 1.0} or {0, 0.5, 1.0} label = np.random.randint(0, 2, size=(batch_size, 1)).astype("float32") left = np.random.random((batch_size, 1)).astype("float32") right = np.random.random((batch_size, 1)).astype("float32") loss = np.log(1.0 + np.exp(left - right)) - label * (left - right) self.inputs = {'Label': label, 'Left': left, 'Right': right} self.outputs = {'Out': loss} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["Left", "Right"], "Out") def test_check_grad_ignore_left(self): self.check_grad(["Right"], "Out", no_grad_set=set('Left')) def test_check_grad_ignore_right(self): self.check_grad(["Left"], "Out", no_grad_set=set('Right')) if __name__ == '__main__': unittest.main()
1,647
34.06383
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_parallel_executor_mnist.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from parallel_executor_test_base import TestParallelExecutorBase import paddle.fluid as fluid import numpy as np import paddle import paddle.dataset.mnist as mnist import unittest MNIST_RECORDIO_FILE = "./mnist_test_pe.recordio" def simple_fc_net(use_feed): if use_feed: img = fluid.layers.data(name='image', shape=[784], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') else: reader = fluid.layers.open_files( filenames=[MNIST_RECORDIO_FILE], shapes=[[-1, 784], [-1, 1]], lod_levels=[0, 0], dtypes=['float32', 'int64'], thread_num=1, for_parallel=True) reader = fluid.layers.io.double_buffer(reader) img, label = fluid.layers.read_file(reader) hidden = img for _ in xrange(4): hidden = fluid.layers.fc( hidden, size=200, act='tanh', bias_attr=fluid.ParamAttr( initializer=fluid.initializer.Constant(value=1.0))) prediction = fluid.layers.fc(hidden, size=10, act='softmax') loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.mean(loss) return loss def fc_with_batchnorm(use_feed): if use_feed: img = fluid.layers.data(name='image', shape=[784], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') else: reader = fluid.layers.open_files( filenames=[MNIST_RECORDIO_FILE], shapes=[[-1, 784], [-1, 1]], lod_levels=[0, 0], dtypes=['float32', 'int64'], thread_num=1, for_parallel=True) reader = fluid.layers.io.double_buffer(reader) img, label = fluid.layers.read_file(reader) hidden = img for _ in xrange(1): hidden = fluid.layers.fc( hidden, size=200, act='tanh', bias_attr=fluid.ParamAttr( initializer=fluid.initializer.Constant(value=1.0))) hidden = fluid.layers.batch_norm(input=hidden) prediction = fluid.layers.fc(hidden, size=10, act='softmax') loss = fluid.layers.cross_entropy(input=prediction, label=label) loss = fluid.layers.mean(loss) return loss class TestMNIST(TestParallelExecutorBase): @classmethod def setUpClass(cls): # Convert mnist to recordio file with fluid.program_guard(fluid.Program(), fluid.Program()): reader = paddle.batch(mnist.train(), batch_size=4) feeder = fluid.DataFeeder( feed_list=[ # order is image and label fluid.layers.data( name='image', shape=[784]), fluid.layers.data( name='label', shape=[1], dtype='int64'), ], place=fluid.CPUPlace()) fluid.recordio_writer.convert_reader_to_recordio_file( MNIST_RECORDIO_FILE, reader, feeder) def check_simple_fc_convergence(self, balance_parameter_opt_between_cards): self.check_network_convergence(simple_fc_net) self.check_network_convergence(simple_fc_net, allow_op_delay=True) img = np.zeros(shape=[32, 784], dtype='float32') label = np.ones(shape=[32, 1], dtype='int64') self.check_network_convergence( simple_fc_net, feed_dict={"image": img, "label": label}, balance_parameter_opt_between_cards=balance_parameter_opt_between_cards ) def test_simple_fc(self): self.check_simple_fc_convergence(False) def test_simple_fc_with_new_strategy(self): self.check_simple_fc_convergence(True) def check_simple_fc_parallel_accuracy(self, balance_parameter_opt_between_cards): img = np.zeros(shape=[32, 784], dtype='float32') label = np.ones(shape=[32, 1], dtype='int64') single_first_loss, single_last_loss = self.check_network_convergence( method=simple_fc_net, seed=1000, feed_dict={"image": img, "label": label}, use_parallel_executor=False) parallel_first_loss, parallel_last_loss = self.check_network_convergence( method=simple_fc_net, seed=1000, feed_dict={"image": img, "label": label}, use_parallel_executor=True, balance_parameter_opt_between_cards=balance_parameter_opt_between_cards ) for p_f in parallel_first_loss: self.assertAlmostEquals(p_f, single_first_loss[0], delta=1e-6) for p_l in parallel_last_loss: self.assertAlmostEquals(p_l, single_last_loss[0], delta=1e-6) def test_simple_fc_parallel_accuracy(self): self.check_simple_fc_parallel_accuracy(False) def test_simple_fc_parallel_accuracy_with_new_strategy(self): self.check_simple_fc_parallel_accuracy(True) def check_batchnorm_fc_convergence(self, balance_parameter_opt_between_cards): self.check_network_convergence(fc_with_batchnorm) img = np.zeros(shape=[32, 784], dtype='float32') label = np.ones(shape=[32, 1], dtype='int64') self.check_network_convergence( fc_with_batchnorm, feed_dict={"image": img, "label": label}, balance_parameter_opt_between_cards=balance_parameter_opt_between_cards ) def test_batchnorm_fc(self): self.check_batchnorm_fc_convergence(False) def test_batchnorm_fc_with_new_strategy(self): self.check_batchnorm_fc_convergence(True) if __name__ == '__main__': unittest.main()
6,441
36.453488
83
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_transpose_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestTransposeOp(OpTest): def setUp(self): self.initTestCase() self.op_type = "transpose" self.inputs = {'X': np.random.random(self.shape).astype("float32")} self.attrs = {'axis': list(self.axis)} self.outputs = {'Out': self.inputs['X'].transpose(self.axis)} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') def initTestCase(self): self.shape = (3, 4) self.axis = (1, 0) class TestCase0(TestTransposeOp): def initTestCase(self): self.shape = (3, ) self.axis = (0, ) class TestCase1(TestTransposeOp): def initTestCase(self): self.shape = (3, 4, 5) self.axis = (0, 2, 1) class TestCase2(TestTransposeOp): def initTestCase(self): self.shape = (2, 3, 4, 5) self.axis = (0, 2, 3, 1) class TestCase3(TestTransposeOp): def initTestCase(self): self.shape = (2, 3, 4, 5, 6) self.axis = (4, 2, 3, 1, 0) class TestCase4(TestTransposeOp): def initTestCase(self): self.shape = (2, 3, 4, 5, 6, 1) self.axis = (4, 2, 3, 1, 0, 5) if __name__ == '__main__': unittest.main()
1,903
25.816901
75
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_linear_chain_crf_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import random import numpy as np from op_test import OpTest class LinearChainCrfForward(object): def __init__(self, seq_start_positions, emission_weights, emission_row_max, emission_exps, transition_weights, transition_exps, labels): self.tag_num = emission_weights.shape[1] self.seq_num = len(seq_start_positions) - 1 self.seq_start_positions = seq_start_positions self.labels = labels self.x = emission_weights self.x_row_max = emission_row_max self.x_exps = emission_exps # unnormalized logits of the transition weights for the start mark. self.a = transition_weights[0, :] self.a_exps = transition_exps[0, :] # unnormalized logits of the transition weights for the end mark. self.b = transition_weights[1, :] self.b_exps = transition_exps[1, :] # unnormalized logits of the transition weights for all the other tags. self.w = transition_weights[2:, :] self.w_exps = transition_exps[2:, :] # The output of linear chain crf operator. # alpha is a memo table in dynamic programming to caculate # nomalization factor. self.alpha = np.zeros( (seq_start_positions[-1], self.tag_num), dtype="float64") self.log_likelihood = np.zeros((self.seq_num, 1)) def _l1_norm(self, x): s = np.sum(x) x /= s return s def _forward_a_sequence(self, x, x_row_max, x_exps, label, alpha): seq_len = x_row_max.shape[0] log_likelihood = 0. for i in range(self.tag_num): alpha[0, i] = self.a_exps[i] * x_exps[0, i] log_likelihood = -x_row_max[0] - np.log(self._l1_norm(alpha[0, :])) # calculate the unnormalized logits of the normalization factor. for k in range(1, seq_len): for i in range(self.tag_num): s = 0. for j in range(self.tag_num): s += alpha[k - 1, j] * self.w_exps[j, i] alpha[k, i] = x_exps[k, i] * s log_likelihood -= x_row_max[k] + np.log(self._l1_norm(alpha[k, :])) s = 0. for i in range(self.tag_num): s += alpha[-1, i] * self.b_exps[i] log_likelihood -= np.log(s) # calculate the nominator part. log_likelihood += ( self.a[label[0]] + x[0, label[0]] + self.b[label[-1]]) for k in range(1, seq_len): log_likelihood += (x[k, label[k]] + self.w[label[k - 1], label[k]]) return -log_likelihood def crf_forward_compute(self): for i in range(self.seq_num): start = self.seq_start_positions[i] end = self.seq_start_positions[i + 1] self.log_likelihood[i] = self._forward_a_sequence( self.x[start:end, :], self.x_row_max[start:end, :], self.x_exps[start:end, :], self.labels[start:end, :], self.alpha[start:end, :]) return self.alpha, self.log_likelihood class TestLinearChainCrfOp(OpTest): def set_test_data(self): # TODO(caoying) Fix the unittest by: add the boundary cases when # sequence lengths are 1, 2, and 3. SEQ_NUM = 3 TAG_NUM = 17 MAX_SEQ_LEN = 5 # the linear_chain_crf operator only supports sequence (LoD level = 1) lod = [[0]] for i in range(SEQ_NUM): lod[-1].append(lod[-1][-1] + random.randint(1, MAX_SEQ_LEN)) emission = np.random.uniform(-1, 1, [lod[-1][-1], TAG_NUM]).astype("float64") emission_row_max = np.amax(emission, axis=1, keepdims=True) emission_exps = np.exp(emission - emission_row_max) transition = np.random.uniform(-0.5, 0.5, [TAG_NUM + 2, TAG_NUM]).astype("float64") transition_exps = np.exp(transition) labels = np.random.randint( low=0, high=TAG_NUM, size=(lod[-1][-1], 1), dtype="int64") self.inputs = { "Emission": (emission, lod), "Transition": transition, "Label": (labels, lod) } crf = LinearChainCrfForward(lod[0], emission, emission_row_max, emission_exps, transition, transition_exps, labels) alpha, log_likelihood = crf.crf_forward_compute() self.outputs = { "Alpha": alpha, "EmissionExps": emission_exps, "TransitionExps": transition_exps, "LogLikelihood": log_likelihood } def setUp(self): self.op_type = "linear_chain_crf" self.set_test_data() def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["Emission", "Transition"], "LogLikelihood") def test_check_grad_ignore_transition(self): self.check_grad( ["Emission"], "LogLikelihood", no_grad_set=set("Transition")) if __name__ == "__main__": unittest.main()
5,715
35.407643
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_debugger.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid import debugger from paddle.fluid.framework import Program class TestDebugger(unittest.TestCase): def test_debug_str(self): p = Program() b = p.current_block() #selected_rows b.create_var( name='selected_rows', dtype="float32", shape=[5, 10], type=core.VarDesc.VarType.SELECTED_ROWS) #tensor array b.create_var( name='tensor_array', shape=[5, 10], type=core.VarDesc.VarType.LOD_TENSOR_ARRAY) #operator mul_x = b.create_parameter( dtype="float32", shape=[5, 10], lod_level=0, name="mul.x") mul_y = b.create_var( dtype="float32", shape=[10, 8], lod_level=0, name="mul.y") mul_out = b.create_var( dtype="float32", shape=[5, 8], lod_level=0, name="mul.out") b.append_op( type="mul", inputs={"X": mul_x, "Y": mul_y}, outputs={"Out": mul_out}, attrs={"x_num_col_dims": 1}) print(debugger.pprint_program_codes(p)) debugger.draw_block_graphviz(p.block(0), path="./test.dot") if __name__ == '__main__': unittest.main()
1,928
30.622951
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/transformer_model.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from functools import partial import numpy as np import paddle.fluid as fluid import paddle.fluid.layers as layers pos_enc_param_names = ( "src_pos_enc_table", "trg_pos_enc_table", ) batch_size = 64 def position_encoding_init(n_position, d_pos_vec): """ Generate the initial values for the sinusoid position encoding table. """ position_enc = np.array([[ pos / np.power(10000, 2 * (j // 2) / d_pos_vec) for j in range(d_pos_vec) ] if pos != 0 else np.zeros(d_pos_vec) for pos in range(n_position)]) position_enc[1:, 0::2] = np.sin(position_enc[1:, 0::2]) # dim 2i position_enc[1:, 1::2] = np.cos(position_enc[1:, 1::2]) # dim 2i+1 return position_enc.astype("float32") def multi_head_attention(queries, keys, values, attn_bias, d_key, d_value, d_model, n_head=1, dropout_rate=0.): """ Multi-Head Attention. Note that attn_bias is added to the logit before computing softmax activiation to mask certain selected positions so that they will not considered in attention weights. """ if not (len(queries.shape) == len(keys.shape) == len(values.shape) == 3): raise ValueError( "Inputs: quries, keys and values should all be 3-D tensors.") def __compute_qkv(queries, keys, values, n_head, d_key, d_value): """ Add linear projection to queries, keys, and values. """ q = layers.fc(input=queries, size=d_key * n_head, param_attr=fluid.initializer.Xavier( uniform=False, fan_in=d_model * d_key, fan_out=n_head * d_key), bias_attr=False, num_flatten_dims=2) k = layers.fc(input=keys, size=d_key * n_head, param_attr=fluid.initializer.Xavier( uniform=False, fan_in=d_model * d_key, fan_out=n_head * d_key), bias_attr=False, num_flatten_dims=2) v = layers.fc(input=values, size=d_value * n_head, param_attr=fluid.initializer.Xavier( uniform=False, fan_in=d_model * d_value, fan_out=n_head * d_value), bias_attr=False, num_flatten_dims=2) return q, k, v def __split_heads(x, n_head): """ Reshape the last dimension of inpunt tensor x so that it becomes two dimensions and then transpose. Specifically, input a tensor with shape [bs, max_sequence_length, n_head * hidden_dim] then output a tensor with shape [bs, n_head, max_sequence_length, hidden_dim]. """ if n_head == 1: return x hidden_size = x.shape[-1] # FIXME(guosheng): Decouple the program desc with batch_size. reshaped = layers.reshape( x=x, shape=[batch_size, -1, n_head, hidden_size // n_head]) # permuate the dimensions into: # [batch_size, n_head, max_sequence_len, hidden_size_per_head] return layers.transpose(x=reshaped, perm=[0, 2, 1, 3]) def __combine_heads(x): """ Transpose and then reshape the last two dimensions of inpunt tensor x so that it becomes one dimension, which is reverse to __split_heads. """ if len(x.shape) == 3: return x if len(x.shape) != 4: raise ValueError("Input(x) should be a 4-D Tensor.") trans_x = layers.transpose(x, perm=[0, 2, 1, 3]) # FIXME(guosheng): Decouple the program desc with batch_size. return layers.reshape( x=trans_x, shape=map(int, [batch_size, -1, trans_x.shape[2] * trans_x.shape[3]])) def scaled_dot_product_attention(q, k, v, attn_bias, d_model, dropout_rate): """ Scaled Dot-Product Attention """ # FIXME(guosheng): Optimize the shape in reshape_op or softmax_op. # The current implementation of softmax_op only supports 2D tensor, # consequently it cannot be directly used here. # If to use the reshape_op, Besides, the shape of product inferred in # compile-time is not the actual shape in run-time. It cann't be used # to set the attribute of reshape_op. # So, here define the softmax for temporary solution. def __softmax(x, eps=1e-9): exp_out = layers.exp(x=x) sum_out = layers.reduce_sum(exp_out, dim=-1, keep_dim=False) return layers.elementwise_div(x=exp_out, y=sum_out, axis=0) scaled_q = layers.scale(x=q, scale=d_model**-0.5) product = layers.matmul(x=scaled_q, y=k, transpose_y=True) weights = __softmax(layers.elementwise_add(x=product, y=attn_bias)) if dropout_rate: weights = layers.dropout( weights, dropout_prob=dropout_rate, is_test=False) out = layers.matmul(weights, v) return out q, k, v = __compute_qkv(queries, keys, values, n_head, d_key, d_value) q = __split_heads(q, n_head) k = __split_heads(k, n_head) v = __split_heads(v, n_head) ctx_multiheads = scaled_dot_product_attention(q, k, v, attn_bias, d_model, dropout_rate) out = __combine_heads(ctx_multiheads) # Project back to the model size. proj_out = layers.fc(input=out, size=d_model, param_attr=fluid.initializer.Xavier(uniform=False), bias_attr=False, num_flatten_dims=2) return proj_out def positionwise_feed_forward(x, d_inner_hid, d_hid): """ Position-wise Feed-Forward Networks. This module consists of two linear transformations with a ReLU activation in between, which is applied to each position separately and identically. """ hidden = layers.fc(input=x, size=d_inner_hid, num_flatten_dims=2, param_attr=fluid.initializer.Uniform( low=-(d_hid**-0.5), high=(d_hid**-0.5)), act="relu") out = layers.fc(input=hidden, size=d_hid, num_flatten_dims=2, param_attr=fluid.initializer.Uniform( low=-(d_inner_hid**-0.5), high=(d_inner_hid**-0.5))) return out def pre_post_process_layer(prev_out, out, process_cmd, dropout=0.): """ Add residual connection, layer normalization and droput to the out tensor optionally according to the value of process_cmd. This will be used before or after multi-head attention and position-wise feed-forward networks. """ for cmd in process_cmd: if cmd == "a": # add residual connection out = out + prev_out if prev_out else out elif cmd == "n": # add layer normalization out = layers.layer_norm( out, begin_norm_axis=len(out.shape) - 1, param_attr=fluid.initializer.Constant(1.), bias_attr=fluid.initializer.Constant(0.)) elif cmd == "d": # add dropout if dropout: out = layers.dropout(out, dropout_prob=dropout, is_test=False) return out pre_process_layer = partial(pre_post_process_layer, None) post_process_layer = pre_post_process_layer def prepare_encoder(src_word, src_pos, src_vocab_size, src_emb_dim, src_pad_idx, src_max_len, dropout=0., pos_pad_idx=0, pos_enc_param_name=None): """Add word embeddings and position encodings. The output tensor has a shape of: [batch_size, max_src_length_in_batch, d_model]. This module is used at the bottom of the encoder stacks. """ src_word_emb = layers.embedding( src_word, size=[src_vocab_size, src_emb_dim], padding_idx=src_pad_idx, param_attr=fluid.initializer.Normal(0., 1.)) src_pos_enc = layers.embedding( src_pos, size=[src_max_len, src_emb_dim], padding_idx=pos_pad_idx, param_attr=fluid.ParamAttr( name=pos_enc_param_name, trainable=False)) enc_input = src_word_emb + src_pos_enc # FIXME(guosheng): Decouple the program desc with batch_size. enc_input = layers.reshape(x=enc_input, shape=[batch_size, -1, src_emb_dim]) return layers.dropout( enc_input, dropout_prob=dropout, is_test=False) if dropout else enc_input prepare_encoder = partial( prepare_encoder, pos_enc_param_name=pos_enc_param_names[0]) prepare_decoder = partial( prepare_encoder, pos_enc_param_name=pos_enc_param_names[1]) def encoder_layer(enc_input, attn_bias, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate=0.): """The encoder layers that can be stacked to form a deep encoder. This module consits of a multi-head (self) attention followed by position-wise feed-forward networks and both the two components companied with the post_process_layer to add residual connection, layer normalization and droput. """ attn_output = multi_head_attention(enc_input, enc_input, enc_input, attn_bias, d_key, d_value, d_model, n_head, dropout_rate) attn_output = post_process_layer(enc_input, attn_output, "dan", dropout_rate) ffd_output = positionwise_feed_forward(attn_output, d_inner_hid, d_model) return post_process_layer(attn_output, ffd_output, "dan", dropout_rate) def encoder(enc_input, attn_bias, n_layer, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate=0.): """ The encoder is composed of a stack of identical layers returned by calling encoder_layer. """ for i in range(n_layer): enc_output = encoder_layer(enc_input, attn_bias, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate) enc_input = enc_output return enc_output def decoder_layer(dec_input, enc_output, slf_attn_bias, dec_enc_attn_bias, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate=0.): """ The layer to be stacked in decoder part. The structure of this module is similar to that in the encoder part except a multi-head attention is added to implement encoder-decoder attention. """ slf_attn_output = multi_head_attention( dec_input, dec_input, dec_input, slf_attn_bias, d_key, d_value, d_model, n_head, dropout_rate, ) slf_attn_output = post_process_layer( dec_input, slf_attn_output, "dan", # residual connection + dropout + layer normalization dropout_rate, ) enc_attn_output = multi_head_attention( slf_attn_output, enc_output, enc_output, dec_enc_attn_bias, d_key, d_value, d_model, n_head, dropout_rate, ) enc_attn_output = post_process_layer( slf_attn_output, enc_attn_output, "dan", # residual connection + dropout + layer normalization dropout_rate, ) ffd_output = positionwise_feed_forward( enc_attn_output, d_inner_hid, d_model, ) dec_output = post_process_layer( enc_attn_output, ffd_output, "dan", # residual connection + dropout + layer normalization dropout_rate, ) return dec_output def decoder(dec_input, enc_output, dec_slf_attn_bias, dec_enc_attn_bias, n_layer, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate=0.): """ The decoder is composed of a stack of identical decoder_layer layers. """ for i in range(n_layer): dec_output = decoder_layer( dec_input, enc_output, dec_slf_attn_bias, dec_enc_attn_bias, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate, ) dec_input = dec_output return dec_output def transformer( src_vocab_size, trg_vocab_size, max_length, n_layer, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate, src_pad_idx, trg_pad_idx, pos_pad_idx, ): file_obj = fluid.layers.open_recordio_file( filename='./wmt16.recordio', shapes=[ [batch_size * max_length, 1], [batch_size * max_length, 1], [batch_size * max_length, 1], [batch_size * max_length, 1], [batch_size, n_head, max_length, max_length], [batch_size, n_head, max_length, max_length], [batch_size, n_head, max_length, max_length], [batch_size * max_length, 1], [batch_size * max_length, 1], ], dtypes=[ 'int64', 'int64', 'int64', 'int64', 'float32', 'float32', 'float32', 'int64', 'float32', ], lod_levels=[0] * 9) src_word, src_pos, trg_word, trg_pos, src_slf_attn_bias, trg_slf_attn_bias, trg_src_attn_bias, gold, weights = fluid.layers.read_file( file_obj) enc_input = prepare_encoder( src_word, src_pos, src_vocab_size, d_model, src_pad_idx, max_length, dropout_rate, ) enc_output = encoder( enc_input, src_slf_attn_bias, n_layer, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate, ) dec_input = prepare_decoder( trg_word, trg_pos, trg_vocab_size, d_model, trg_pad_idx, max_length, dropout_rate, ) dec_output = decoder( dec_input, enc_output, trg_slf_attn_bias, trg_src_attn_bias, n_layer, n_head, d_key, d_value, d_model, d_inner_hid, dropout_rate, ) # TODO(guosheng): Share the weight matrix between the embedding layers and # the pre-softmax linear transformation. predict = layers.reshape( x=layers.fc(input=dec_output, size=trg_vocab_size, param_attr=fluid.initializer.Xavier(uniform=False), bias_attr=False, num_flatten_dims=2), shape=[-1, trg_vocab_size], act="softmax") cost = layers.cross_entropy(input=predict, label=gold) weighted_cost = cost * weights return layers.reduce_sum(weighted_cost)
16,383
32.57377
138
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_margin_rank_loss_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestMarginRankLossOp(OpTest): def setUp(self): self.op_type = "margin_rank_loss" batch_size = 5 margin = 0.5 # labels_{i} = {-1, 1} label = 2 * np.random.randint( 0, 2, size=(batch_size, 1)).astype("float32") - 1 x1 = np.random.random((batch_size, 1)).astype("float32") x2 = np.random.random((batch_size, 1)).astype("float32") # loss = max(0, -label * (x1 - x2) + margin) loss = -label * (x1 - x2) + margin loss = np.where(loss > 0, loss, 0) act = np.where(loss > 0, 1., 0.) self.attrs = {'margin': margin} self.inputs = {'Label': label, 'X1': x1, 'X2': x2} self.outputs = {'Activated': act, 'Out': loss} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X1", "X2"], "Out") def test_check_grad_ignore_x1(self): self.check_grad(["X2"], "Out", no_grad_set=set('X1')) def test_check_grad_ignore_x2(self): self.check_grad(["X1"], "Out", no_grad_set=set('X2')) if __name__ == '__main__': unittest.main()
1,813
32.592593
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_softmax_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest import paddle.fluid.core as core def stable_softmax(x): """Compute the softmax of vector x in a numerically stable way.""" shiftx = x - np.max(x).clip(-64.) exps = np.exp(shiftx) return exps / np.sum(exps) class TestSoftmaxOp(OpTest): def setUp(self): self.op_type = "softmax" self.use_cudnn = False self.use_mkldnn = False self.dtype = np.float32 self.init_kernel_type() x = np.random.uniform(0.1, 1, [10, 10]).astype(self.dtype) out = np.apply_along_axis(stable_softmax, 1, x) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} self.outputs = {'Out': out} self.attrs = { 'use_cudnn': self.use_cudnn, 'use_mkldnn': self.use_mkldnn } def init_kernel_type(self): pass def test_check_output(self): if self.use_cudnn: place = core.CUDAPlace(0) self.check_output_with_place(place, atol=1e-5) else: self.check_output() def test_check_grad(self): if self.dtype == np.float16: return if self.use_cudnn: place = core.CUDAPlace(0) self.check_grad_with_place( place, ["X"], "Out", max_relative_error=0.01) else: self.check_grad(["X"], "Out", max_relative_error=0.01) class TestSoftmaxCUDNNOp(TestSoftmaxOp): def init_kernel_type(self): self.use_cudnn = True class TestSoftmaxFP16Op(TestSoftmaxOp): def init_kernel_type(self): self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestSoftmaxFP16CUDNNOp(TestSoftmaxOp): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestSoftmaxMKLDNNOp(TestSoftmaxOp): def init_kernel_type(self): self.use_mkldnn = True if __name__ == "__main__": unittest.main()
2,994
28.653465
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_layer_norm_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from operator import mul import paddle.fluid.core as core import paddle.fluid as fluid np.random.random(123) def _reference_layer_norm_naive(x, scale, beta, epsilon, begin_norm_axis=1): x_shape = x.shape N = reduce(mul, x_shape[0:begin_norm_axis], 1) D = reduce(mul, x_shape[begin_norm_axis:len(x_shape)], 1) x.shape = [N, D] mean = np.mean(x, axis=1) var = np.var(x, axis=1) + epsilon output = scale.reshape([1, D]) * np.divide( (x - mean.reshape([N, 1])), (np.sqrt(var)).reshape([N, 1])) + beta.reshape([1, D]) x.shape, output.shape = x_shape, x_shape return output, mean, var def _reference_layer_norm_grad(x, grad_y, scale, mean, var, begin_norm_axis=1): x_shape = x.shape scale_shape = scale.shape N = reduce(mul, x_shape[0:begin_norm_axis], 1) D = reduce(mul, x_shape[begin_norm_axis:len(x_shape)], 1) x.shape, grad_y.shape = [N, D], [N, D] var.shape, mean.shape = [N, 1], [N, 1] scale.shape = [1, D] # d_bias d_bias = np.sum(grad_y, axis=0).reshape([1, D]) # d_scale d_scale = np.sum(((x - mean) * np.sqrt(1 / var)) * grad_y, axis=0).reshape([1, D]) # dx dx_end = scale * np.sqrt(1.0 / var) * grad_y d_mean_0 = np.sum(-np.sqrt(1.0 / var) * grad_y * scale, axis=1).reshape( [N, 1]) # the second part equals to zero. d_mean = 1.0 / D * d_mean_0 d_std = np.sum( -(1.0 / var) * (x - mean) * grad_y * scale, axis=1).reshape([N, 1]) * ( 1.0 / D * np.sqrt(1.0 / var).reshape([N, 1]) * (x - mean)) grad_x = dx_end + d_mean + d_std grad_x.shape, x.shape, grad_y.shape = x_shape, x_shape, x_shape scale.shape = scale_shape var.shape, mean.shape = [N, ], [N, ] return grad_x, d_scale, d_bias class TestLayerNormdOp(unittest.TestCase): def __assert_close(self, tensor, np_array, msg, atol=1e-4): self.assertTrue(np.allclose(np.array(tensor), np_array, atol=atol), msg) def check_forward_backward(self, shape, begin_norm_axis): def test_with_place(place, shape, begin_norm_axis): # attr epsilon = 0.00001 x_shape = shape D = reduce(mul, x_shape[begin_norm_axis:len(x_shape)], 1) scale_shape = [D] np.random.seed(123) x = np.random.random_sample(x_shape).astype(np.float32) scale = np.random.random_sample(scale_shape).astype(np.float32) bias = np.random.random_sample(scale_shape).astype(np.float32) y_grad = np.random.random_sample(x_shape).astype(np.float32) # reference forward & backward y, mean, variance = _reference_layer_norm_naive( x, scale, bias, epsilon, begin_norm_axis) x_grad, scale_grad, bias_grad = _reference_layer_norm_grad( x, y_grad, scale, mean, variance, begin_norm_axis) var_dict = locals() var_dict['y@GRAD'] = y_grad var_names = [ 'x', 'scale', 'bias', 'mean', 'variance', 'y', 'y@GRAD' ] ground_truth = {name: var_dict[name] for name in var_names} program = fluid.Program() with fluid.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( name=name, dtype='float32', shape=ground_truth[name].shape) layer_norm_op = block.append_op( type="layer_norm", inputs={ "X": block.var('x'), "Scale": block.var('scale'), "Bias": block.var('bias'), }, outputs={ "Y": block.var('y'), "Mean": block.var('mean'), # share the same memory "Variance": block.var('variance'), # share the same memory }, attrs={ "epsilon": epsilon, "begin_norm_axis": begin_norm_axis }) # generate backward op_desc grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( layer_norm_op.desc, set(), []) grad_op_desc = grad_op_desc_list[0] new_op_desc = block.desc.append_op() new_op_desc.copy_from(grad_op_desc) for var_name in grad_op_desc.output_arg_names(): block.desc.var(var_name.encode("ascii")) grad_op_desc.infer_var_type(block.desc) grad_op_desc.infer_shape(block.desc) for arg in grad_op_desc.output_arg_names(): grad_var = block.desc.find_var(arg.encode("ascii")) grad_var.set_dtype(core.VarDesc.VarType.FP32) exe = fluid.Executor(place) out = exe.run(program, feed={ name: var_dict[name] for name in ['x', 'scale', 'bias', 'y@GRAD'] }, fetch_list=[ 'y', 'mean', 'variance', 'x@GRAD', 'scale@GRAD', 'bias@GRAD' ]) self.__assert_close(y, out[0], "y") self.__assert_close(mean, out[1], "mean") self.__assert_close(variance, out[2], "variance", 1e-3) self.__assert_close(x_grad, out[3], "x_grad") self.__assert_close(scale_grad, out[4], "scale_grad", 1e-3) self.__assert_close(bias_grad, out[5], "bias_grad") places = [core.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"): places.append(core.CUDAPlace(0)) for place in places: test_with_place(place, shape, begin_norm_axis) def test_check_forward_backward_with_scale_and_bias(self): self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=1) self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=3) if __name__ == '__main__': unittest.main()
7,030
39.641618
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_dyn_rnn.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid as fluid import paddle import unittest import numpy class TestDynRNN(unittest.TestCase): def setUp(self): self.word_dict = paddle.dataset.imdb.word_dict() self.BATCH_SIZE = 2 self.train_data = paddle.batch( paddle.dataset.imdb.train(self.word_dict), batch_size=self.BATCH_SIZE) def test_plain_while_op(self): main_program = fluid.Program() startup_program = fluid.Program() with fluid.program_guard(main_program, startup_program): sentence = fluid.layers.data( name='word', shape=[1], dtype='int64', lod_level=1) sent_emb = fluid.layers.embedding( input=sentence, size=[len(self.word_dict), 32], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='float32') rank_table = fluid.layers.lod_rank_table(x=sent_emb) sent_emb_array = fluid.layers.lod_tensor_to_array( x=sent_emb, table=rank_table) seq_len = fluid.layers.max_sequence_len(rank_table=rank_table) i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) i.stop_gradient = False boot_mem = fluid.layers.fill_constant_batch_size_like( input=fluid.layers.array_read( array=sent_emb_array, i=i), value=0, shape=[-1, 100], dtype='float32') boot_mem.stop_gradient = False mem_array = fluid.layers.array_write(x=boot_mem, i=i) cond = fluid.layers.less_than(x=i, y=seq_len) cond.stop_gradient = False while_op = fluid.layers.While(cond=cond) out = fluid.layers.create_array(dtype='float32') with while_op.block(): mem = fluid.layers.array_read(array=mem_array, i=i) ipt = fluid.layers.array_read(array=sent_emb_array, i=i) mem = fluid.layers.shrink_memory(x=mem, i=i, table=rank_table) hidden = fluid.layers.fc(input=[mem, ipt], size=100, act='tanh') fluid.layers.array_write(x=hidden, i=i, array=out) fluid.layers.increment(x=i, in_place=True) fluid.layers.array_write(x=hidden, i=i, array=mem_array) fluid.layers.less_than(x=i, y=seq_len, cond=cond) all_timesteps = fluid.layers.array_to_lod_tensor( x=out, table=rank_table) last = fluid.layers.sequence_last_step(input=all_timesteps) logits = fluid.layers.fc(input=last, size=1, act=None) loss = fluid.layers.sigmoid_cross_entropy_with_logits( x=logits, label=label) loss = fluid.layers.mean(loss) sgd = fluid.optimizer.SGD(1e-4) sgd.minimize(loss=loss) cpu = fluid.CPUPlace() exe = fluid.Executor(cpu) exe.run(startup_program) feeder = fluid.DataFeeder(feed_list=[sentence, label], place=cpu) data = next(self.train_data()) val = exe.run(main_program, feed=feeder.feed(data), fetch_list=[loss])[0] self.assertEqual((1, ), val.shape) print(val) self.assertFalse(numpy.isnan(val)) def test_train_dyn_rnn(self): main_program = fluid.Program() startup_program = fluid.Program() with fluid.program_guard(main_program, startup_program): sentence = fluid.layers.data( name='word', shape=[1], dtype='int64', lod_level=1) sent_emb = fluid.layers.embedding( input=sentence, size=[len(self.word_dict), 32], dtype='float32') rnn = fluid.layers.DynamicRNN() with rnn.block(): in_ = rnn.step_input(sent_emb) mem = rnn.memory(shape=[100], dtype='float32') out_ = fluid.layers.fc(input=[in_, mem], size=100, act='tanh') rnn.update_memory(mem, out_) rnn.output(out_) last = fluid.layers.sequence_last_step(input=rnn()) logits = fluid.layers.fc(input=last, size=1, act=None) label = fluid.layers.data(name='label', shape=[1], dtype='float32') loss = fluid.layers.sigmoid_cross_entropy_with_logits( x=logits, label=label) loss = fluid.layers.mean(loss) sgd = fluid.optimizer.Adam(1e-3) sgd.minimize(loss=loss) cpu = fluid.CPUPlace() exe = fluid.Executor(cpu) exe.run(startup_program) feeder = fluid.DataFeeder(feed_list=[sentence, label], place=cpu) data = next(self.train_data()) loss_0 = exe.run(main_program, feed=feeder.feed(data), fetch_list=[loss])[0] for _ in xrange(100): val = exe.run(main_program, feed=feeder.feed(data), fetch_list=[loss])[0] # loss should be small after 100 mini-batch self.assertLess(val[0], loss_0[0]) if __name__ == '__main__': unittest.main()
5,770
39.076389
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_label_smooth_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestLabelSmoothOp(OpTest): def config(self): self.op_type = "label_smooth" self.epsilon = 0.1 batch_size, self.label_dim = 5, 10 self.label = np.zeros((batch_size, self.label_dim)).astype("float64") nonzero_index = np.random.randint(self.label_dim, size=(batch_size)) self.label[np.arange(batch_size), nonzero_index] = 1 def setUp(self): self.config() smoothed_label = (1 - self.epsilon ) * self.label + self.epsilon / self.label_dim self.inputs = {'X': self.label} self.attrs = {'epsilon': self.epsilon} self.outputs = {'Out': smoothed_label} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(["X"], "Out") class TestLabelSmoothOpWithPriorDist(TestLabelSmoothOp): def setUp(self): self.config() dist = np.random.random((1, self.label_dim)) smoothed_label = (1 - self.epsilon) * self.label + self.epsilon * dist self.inputs = {'X': self.label, 'PriorDist': dist} self.attrs = {'epsilon': self.epsilon} self.outputs = {'Out': smoothed_label} if __name__ == '__main__': unittest.main()
1,918
33.267857
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lrn_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestLRNOp(OpTest): def get_input(self): ''' TODO(gongweibao): why it's grad diff is so large? x = np.ndarray( shape=(self.N, self.C, self.H, self.W), dtype=float, order='C') for m in range(0, self.N): for i in range(0, self.C): for h in range(0, self.H): for w in range(0, self.W): x[m][i][h][w] = m * self.C * self.H * self.W + \ i * self.H * self.W + \ h * self.W + w + 1 ''' x = np.random.rand(self.N, self.C, self.H, self.W).astype("float32") return x + 1 def get_out(self): start = -(self.n - 1) / 2 end = start + self.n mid = np.empty((self.N, self.C, self.H, self.W)).astype("float32") mid.fill(self.k) for m in range(0, self.N): for i in range(0, self.C): for c in range(start, end): ch = i + c if ch < 0 or ch >= self.C: continue s = mid[m][i][:][:] r = self.x[m][ch][:][:] s += np.square(r) * self.alpha mid2 = np.power(mid, -self.beta) return np.multiply(self.x, mid2), mid def get_attrs(self): attrs = { 'n': self.n, 'k': self.k, 'alpha': self.alpha, 'beta': self.beta } return attrs def setUp(self): self.op_type = "lrn" self.N = 2 self.C = 3 self.H = 5 self.W = 5 self.n = 5 self.k = 2.0 self.alpha = 0.0001 self.beta = 0.75 self.x = self.get_input() self.out, self.mid_out = self.get_out() self.inputs = {'X': self.x} self.outputs = {'Out': self.out, 'MidOut': self.mid_out} self.attrs = self.get_attrs() def test_check_output(self): self.check_output() def test_check_grad_normal(self): self.check_grad(['X'], 'Out', max_relative_error=0.01) if __name__ == "__main__": unittest.main()
2,846
29.945652
76
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import copy import math import unittest import paddle.fluid as fluid import paddle.fluid.layers as layers import paddle.fluid.framework as framework import paddle.fluid.core as core def exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False): exponent = global_step / decay_steps if staircase: exponent = math.floor(exponent) return learning_rate * decay_rate**exponent def natural_exp_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False): exponent = float(global_step) / float(decay_steps) if staircase: exponent = math.floor(exponent) return learning_rate * math.exp(-1 * decay_rate * exponent) def inverse_time_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False): temp = float(global_step) / float(decay_steps) if staircase: temp = math.floor(temp) return learning_rate / (1 + decay_rate * temp) def polynomial_decay(learning_rate, global_step, decay_steps, end_learning_rate=0.0001, power=1.0, cycle=False): if cycle: div = math.ceil(global_step / float(decay_steps)) if div == 0: div = 1 decay_steps = decay_steps * div else: global_step = min(global_step, decay_steps) return (learning_rate - end_learning_rate) * \ ((1 - float(global_step) / float(decay_steps)) ** power) + end_learning_rate def piecewise_decay(global_step, boundaries, values): assert len(boundaries) + 1 == len(values) for i in range(len(boundaries)): if global_step < boundaries[i]: return values[i] return values[len(values) - 1] class TestLearningRateDecay(unittest.TestCase): def check_decay(self, python_decay_fn, fluid_decay_fn, kwargs): places = [fluid.CPUPlace()] if core.is_compiled_with_cuda(): places.append(fluid.CUDAPlace(0)) for place in places: self.check_decay_with_place(place, python_decay_fn, fluid_decay_fn, kwargs) def check_decay_with_place(self, place, python_decay_fn, fluid_decay_fn, kwargs): decayed_lr = fluid_decay_fn(**kwargs) place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) fluid.memory_optimize(fluid.default_main_program()) for step in range(10): lr_val, = exe.run(fluid.default_main_program(), feed={}, fetch_list=[decayed_lr]) python_decayed_lr = python_decay_fn( global_step=float(step), **kwargs) self.assertAlmostEqual( python_decayed_lr, lr_val[0], msg='Failed fn is {0}, Python result is {1}, Fluid result is {2}'. format(python_decay_fn.__name__, str(python_decayed_lr), str(lr_val[0]))) def test_decay(self): common_kwargs_true = { "learning_rate": 1.0, "decay_steps": 5, "decay_rate": 0.5, "staircase": True } common_kwargs_false = copy.deepcopy(common_kwargs_true) common_kwargs_false["staircase"] = False decay_fns = [ (exponential_decay, layers.exponential_decay, common_kwargs_true), (exponential_decay, layers.exponential_decay, common_kwargs_false), (natural_exp_decay, layers.natural_exp_decay, common_kwargs_true), (natural_exp_decay, layers.natural_exp_decay, common_kwargs_false), (inverse_time_decay, layers.inverse_time_decay, common_kwargs_true), (inverse_time_decay, layers.inverse_time_decay, common_kwargs_false), (polynomial_decay, layers.polynomial_decay, { "learning_rate": 1.0, "decay_steps": 5, "cycle": True }), (polynomial_decay, layers.polynomial_decay, { "learning_rate": 1.0, "decay_steps": 5, "cycle": False }), (piecewise_decay, layers.piecewise_decay, { "boundaries": [3, 6, 9], "values": [0.1, 0.2, 0.3, 0.4] }), ] for py_decay_fn, fluid_decay_fn, kwargs in decay_fns: print("decay_fn=" + py_decay_fn.__name__ + " kwargs=" + str(kwargs)) main_program = framework.Program() startup_program = framework.Program() with framework.program_guard(main_program, startup_program): self.check_decay(py_decay_fn, fluid_decay_fn, kwargs) if __name__ == '__main__': unittest.main()
5,743
34.677019
87
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_beam_search_decode_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid.core as core from paddle.fluid.op import Operator class TestBeamSearchDecodeOp(unittest.TestCase): def setUp(self): self.scope = core.Scope() self.place = core.CPUPlace() def append_lod_tensor(self, tensor_array, lod, data): lod_tensor = core.LoDTensor() lod_tensor.set_lod(lod) lod_tensor.set(data, self.place) tensor_array.append(lod_tensor) def test_get_set(self): ids = self.scope.var("ids").get_lod_tensor_array() self.append_lod_tensor( ids, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array( [1, 2, 3, 4, 5, 6], dtype="int64")) self.append_lod_tensor( ids, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array( [0, 1, 2, 3, 4, 5], dtype="int64")) self.append_lod_tensor( ids, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array( [0, 1, 2, 3, 4], dtype="int64")) scores = self.scope.var("scores").get_lod_tensor_array() self.append_lod_tensor( scores, [[0, 3, 6], [0, 1, 2, 3, 4, 5, 6]], np.array( [1, 2, 3, 4, 5, 6], dtype="float64")) self.append_lod_tensor( scores, [[0, 3, 6], [0, 1, 1, 3, 5, 5, 6]], np.array( [0, 1, 2, 3, 4, 5], dtype="float64")) self.append_lod_tensor( scores, [[0, 3, 6], [0, 0, 1, 2, 3, 4, 5]], np.array( [0, 1, 2, 3, 4], dtype="float64")) sentence_ids = self.scope.var("sentence_ids").get_tensor() sentence_scores = self.scope.var("sentence_scores").get_tensor() beam_search_decode_op = Operator( "beam_search_decode", # inputs Ids="ids", Scores="scores", # outputs SentenceIds="sentence_ids", SentenceScores="sentence_scores") beam_search_decode_op.run(self.scope, self.place) expected_lod = [[0, 4, 8], [0, 1, 3, 6, 9, 10, 13, 16, 19]] self.assertEqual(sentence_ids.lod(), expected_lod) self.assertEqual(sentence_scores.lod(), expected_lod) expected_data = np.array( [2, 1, 0, 3, 1, 0, 3, 2, 1, 5, 4, 3, 2, 4, 4, 3, 6, 5, 4], "int64") self.assertTrue(np.array_equal(np.array(sentence_ids), expected_data)) self.assertTrue( np.array_equal(np.array(sentence_scores), expected_data)) class TestBeamSearchDecodeOpGPU(TestBeamSearchDecodeOp): def setUp(self): self.scope = core.Scope() self.place = core.CUDAPlace(0) if __name__ == '__main__': unittest.main()
3,328
34.042105
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_seq_conv.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import random from op_test import OpTest class TestSeqProject(OpTest): def setUp(self): self.init_test_case() self.op_type = 'sequence_conv' if self.context_length == 1 \ and self.context_start == 0 \ and self.padding_trainable: print "If context_start is 0 " \ "and context_length is 1," \ " padding_trainable should be false." return # one level, batch size x = np.random.uniform(0.1, 1, [self.input_size[0], self.input_size[1]]).astype('float32') w = np.random.uniform(0.1, 1, [ self.context_length * self.input_size[1], self.output_represention ]).astype('float32') begin_pad = np.max([0, -self.context_start]) end_pad = np.max([0, self.context_start + self.context_length - 1]) total_pad = begin_pad + end_pad padding_data = np.random.uniform( 0.1, 1, [total_pad, self.input_size[1]]).astype('float32') self.pad_data = padding_data self.inputs = { 'X': (x, self.lod), 'Filter': w, } self.inputs_val = ['X', 'Filter'] self.inputs_val_no_x = ['Filter'] self.inputs_val_no_f = ['X'] if total_pad != 0: self.inputs['PaddingData'] = padding_data self.inputs_val = ['X', 'PaddingData', 'Filter'] self.inputs_val_no_x = ['PaddingData', 'Filter'] self.inputs_val_no_f = ['PaddingData', 'X'] self.attrs = { 'contextStart': self.context_start, 'contextLength': self.context_length, 'paddingTrainable': self.padding_trainable, 'contextStride': self.context_stride } out = np.zeros( (self.input_size[0], self.output_represention)).astype('float32') self.outputs = {'Out': out} self.compute() def compute(self): x, lod = self.inputs['X'] filter = self.inputs['Filter'] pading_data = self.pad_data out = np.zeros((self.input_size[0], self.context_length * self.input_size[1])).astype('float32') lod = lod[0] begin_pad = np.max([0, -self.context_start]) for i in range(len(lod) - 1): for j in range(self.context_length): in_begin = lod[i] + self.context_start + j in_end = lod[i + 1] + self.context_start + j out_begin = lod[i] out_end = lod[i + 1] if in_begin < lod[i]: pad_size = np.min([lod[i] - in_begin, lod[i + 1] - lod[i]]) if self.padding_trainable: sub_w = pading_data[j:j + pad_size, :] out[lod[i]:lod[i] + pad_size, j * self.input_size[1]:( j + 1) * self.input_size[1]] = sub_w out_begin = lod[i] + pad_size in_begin = lod[i] if in_end > lod[i + 1]: pad_size = np.min( [in_end - lod[i + 1], lod[i + 1] - lod[i]]) if self.padding_trainable: sub_w = pading_data[begin_pad + self.context_start + j - pad_size:begin_pad + self.context_start + j, :] out[lod[i + 1] - pad_size:lod[i + 1], j * self. input_size[1]:(j + 1) * self.input_size[1]] = sub_w in_end = lod[i + 1] out_end = lod[i + 1] - pad_size if in_end <= in_begin: continue in_sub = x[in_begin:in_end, :] out[out_begin:out_end, j * self.input_size[1]:(j + 1) * self.input_size[1]] += in_sub np.dot(out, filter, out=self.outputs['Out']) def test_check_output(self): self.check_output() def test_check_grad(self): if self.padding_trainable: self.check_grad( set(self.inputs_val), 'Out', max_relative_error=0.05) def test_check_grad_input(self): self.check_grad( ['X'], 'Out', max_relative_error=0.05, no_grad_set=set(self.inputs_val_no_x)) def test_check_grad_padding_data(self): if self.padding_trainable: self.check_grad( ['PaddingData'], 'Out', max_relative_error=0.05, no_grad_set=set(['X', 'Filter'])) def test_check_grad_Filter(self): self.check_grad( ['Filter'], 'Out', max_relative_error=0.05, no_grad_set=set(self.inputs_val_no_f)) def test_check_grad_input_filter(self): if self.padding_trainable: self.check_grad( ['X', 'Filter'], 'Out', max_relative_error=0.05, no_grad_set=set(['PaddingData'])) def test_check_grad_padding_input(self): if self.padding_trainable: self.check_grad( self.inputs_val_no_f, 'Out', max_relative_error=0.05, no_grad_set=set(['Filter'])) def test_check_grad_padding_filter(self): if self.padding_trainable: self.check_grad( self.inputs_val_no_x, 'Out', max_relative_error=0.05, no_grad_set=set(['X'])) def init_test_case(self): self.input_row = 11 self.context_start = 0 self.context_length = 1 self.padding_trainable = False self.context_stride = 1 self.input_size = [self.input_row, 23] self.lod = [[0, 4, 5, 8, self.input_row]] self.output_represention = 8 # output feature size class TestSeqProjectCase1(TestSeqProject): def init_test_case(self): self.input_row = 11 self.context_start = -1 self.context_length = 3 self.padding_trainable = True self.context_stride = 1 self.input_size = [self.input_row, 23] self.lod = [[0, 4, 5, 8, self.input_row]] self.output_represention = 8 # output feature size class TestSeqProjectCase2(TestSeqProject): def init_test_case(self): self.input_row = 25 self.context_start = 2 self.context_length = 3 self.padding_trainable = True self.context_stride = 1 self.input_size = [self.input_row, 23] idx = range(self.input_size[0]) del idx[0] self.lod = [[0] + np.sort(random.sample(idx, 8)).tolist() + [self.input_size[0]]] self.output_represention = 8 # output feature size if __name__ == '__main__': unittest.main()
7,581
34.596244
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_elementwise_min_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestElementwiseOp(OpTest): def setUp(self): self.op_type = "elementwise_min" # If x and y have the same value, the min() is not differentiable. # So we generate test data by the following method # to avoid them being too close to each other. x = np.random.uniform(0.1, 1, [13, 17]).astype("float32") sgn = np.random.choice([-1, 1], [13, 17]).astype("float32") y = x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype("float32") self.inputs = {'X': x, 'Y': y} self.outputs = {'Out': np.minimum(self.inputs['X'], self.inputs['Y'])} def test_check_output(self): self.check_output() def test_check_grad_normal(self): self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.005) def test_check_grad_ingore_x(self): self.check_grad( ['Y'], 'Out', max_relative_error=0.005, no_grad_set=set("X")) def test_check_grad_ingore_y(self): self.check_grad( ['X'], 'Out', max_relative_error=0.005, no_grad_set=set('Y')) class TestElementwiseMinOp_scalar(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.random_integers(-5, 5, [2, 3, 4]).astype("float32") y = np.array([0.5]).astype("float32") self.inputs = {'X': x, 'Y': y} self.outputs = {'Out': np.minimum(self.inputs['X'], self.inputs['Y'])} class TestElementwiseMaxOp_Vector(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.random((32, )).astype("float32") sgn = np.random.choice([-1, 1], (32, )).astype("float32") y = x + sgn * np.random.uniform(0.1, 1, (32, )).astype("float32") self.inputs = {'X': x, 'Y': y} self.outputs = {'Out': np.minimum(self.inputs['X'], self.inputs['Y'])} class TestElementwiseMaxOp_broadcast_0(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.uniform(0.5, 1, (2, 3, 4)).astype(np.float32) sgn = np.random.choice([-1, 1], (2, )).astype(np.float32) y = x[:, 0, 0] + sgn * \ np.random.uniform(1, 2, (2, )).astype(np.float32) self.inputs = {'X': x, 'Y': y} self.attrs = {'axis': 0} self.outputs = { 'Out': np.minimum(self.inputs['X'], self.inputs['Y'].reshape(2, 1, 1)) } class TestElementwiseMaxOp_broadcast_1(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.uniform(0.5, 1, (2, 3, 4)).astype(np.float32) sgn = np.random.choice([-1, 1], (3, )).astype(np.float32) y = x[0, :, 0] + sgn * \ np.random.uniform(1, 2, (3, )).astype(np.float32) self.inputs = {'X': x, 'Y': y} self.attrs = {'axis': 1} self.outputs = { 'Out': np.minimum(self.inputs['X'], self.inputs['Y'].reshape(1, 3, 1)) } class TestElementwiseMaxOp_broadcast_2(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.uniform(0.5, 1, (2, 3, 4)).astype(np.float32) sgn = np.random.choice([-1, 1], (4, )).astype(np.float32) y = x[0, 0, :] + sgn * \ np.random.uniform(1, 2, (4, )).astype(np.float32) self.inputs = {'X': x, 'Y': y} self.outputs = { 'Out': np.minimum(self.inputs['X'], self.inputs['Y'].reshape(1, 1, 4)) } class TestElementwiseMaxOp_broadcast_3(TestElementwiseOp): def setUp(self): self.op_type = "elementwise_min" x = np.random.uniform(0.5, 1, (2, 3, 4, 5)).astype(np.float32) sgn = np.random.choice([-1, 1], (3, 4)).astype(np.float32) y = x[0, :, :, 0] + sgn * \ np.random.uniform(1, 2, (3, 4)).astype(np.float32) self.inputs = {'X': x, 'Y': y} self.attrs = {'axis': 1} self.outputs = { 'Out': np.minimum(self.inputs['X'], self.inputs['Y'].reshape(1, 3, 4, 1)) } if __name__ == '__main__': unittest.main()
4,773
35.442748
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_operator_desc.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core from paddle.fluid.framework import Program, default_startup_program main_program = default_startup_program() class TestOperator(unittest.TestCase): def test_error_type(self): block = main_program.create_block() try: block.append_op() self.assertFail() except ValueError as v_err: self.assertEqual( v_err.message, "`type` to initilized an Operator can not be None.") try: block.append_op(type="no_such_op") self.assertFail() except ValueError as a_err: self.assertEqual(a_err.message, "Operator \"no_such_op\" has not been registered.") def test_op_desc_creation(self): program = Program() block = program.current_block() mul_x = block.create_var( dtype="float32", shape=[5, 10], lod_level=0, name="mul.x") mul_y = block.create_var( dtype="float32", shape=[10, 8], lod_level=0, name="mul.y") mul_out = block.create_var( dtype="float32", shape=[5, 8], lod_level=0, name="mul.out") mul_op = block.append_op( type="mul", inputs={"X": [mul_x], "Y": mul_y}, outputs={"Out": [mul_out]}, attrs={"x_num_col_dims": 1}) self.assertNotEqual(str(mul_op), "") self.assertEqual(mul_op.type, "mul") self.assertEqual(mul_op.input_names, ["X", "Y"]) self.assertEqual(mul_op.input("X"), ["mul.x"]) self.assertEqual(mul_op.input("Y"), ["mul.y"]) self.assertEqual(mul_op.output_names, ["Out"]) self.assertEqual(mul_op.output("Out"), ["mul.out"]) self.assertEqual( set(mul_op.attr_names), set(["x_num_col_dims", "y_num_col_dims", "op_role", "op_role_var"])) self.assertEqual(mul_op.has_attr("x_num_col_dims"), True) self.assertEqual(mul_op.attr_type("x_num_col_dims"), core.AttrType.INT) self.assertEqual(mul_op.attr("x_num_col_dims"), 1) self.assertEqual(mul_op.has_attr("y_num_col_dims"), True) self.assertEqual(mul_op.attr_type("y_num_col_dims"), core.AttrType.INT) self.assertEqual(mul_op.attr("y_num_col_dims"), 1) self.assertEqual(mul_op.idx, 0) self.assertEqual(mul_out.op, mul_op) def test_mult_input(self): program = Program() block = program.current_block() sum_x1 = block.create_var( dtype="int", shape=[3, 4], lod_level=0, name="sum.x1") sum_x2 = block.create_var( dtype="int", shape=[3, 4], lod_level=0, name="sum.x2") sum_x3 = block.create_var( dtype="int", shape=[3, 4], lod_level=0, name="sum.x3") sum_out = block.create_var( dtype="int", shape=[3, 4], lod_level=0, name="sum.out") sum_op = block.append_op( type="sum", inputs={"X": [sum_x1, sum_x2, sum_x3]}, outputs={"Out": sum_out}) self.assertEqual(sum_op.type, "sum") self.assertEqual(sum_op.input_names, ["X"]) self.assertEqual(sum_op.input("X"), ["sum.x1", "sum.x2", "sum.x3"]) self.assertEqual(sum_op.output_names, ["Out"]) self.assertEqual(sum_op.output("Out"), ["sum.out"]) self.assertEqual(sum_op.idx, 0) self.assertEqual(sum_out.op, sum_op) if __name__ == '__main__': unittest.main()
4,101
39.215686
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_framework_debug_str.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from paddle.fluid.framework import Program class TestDebugStringFramework(unittest.TestCase): def test_debug_str(self): p = Program() p.current_block().create_var(name='t', shape=[0, 1]) self.assertRaises(ValueError, callableObj=p.__str__) if __name__ == '__main__': unittest.main()
948
32.892857
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_seq_pool.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestSeqAvgPool(OpTest): def set_data(self): self.op_type = 'sequence_pool' # one level, batch size is 4 x = np.random.uniform(0.1, 1, [11, 23]).astype('float32') lod = [[0, 4, 5, 8, 11]] self.inputs = {'X': (x, lod)} out = np.zeros((4, 23)).astype('float32') self.outputs = {'Out': out} return x, lod, out def compute(self, x, lod, out): self.attrs = {'pooltype': "AVERAGE"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] out[i] = sub_x.mean(axis=0) def setUp(self): x, lod, out = self.set_data() self.compute(x, lod, out) def test_check_output(self): self.check_output() def test_check_grad(self): # Remove MaxIndex after check_grad is refined. self.outputs['MaxIndex'] = \ np.zeros(self.outputs['Out'].shape).astype('int32') self.check_grad(["X"], "Out") class TestSeqSumPool(TestSeqAvgPool): def compute(self, x, lod, out): self.attrs = {'pooltype': "SUM"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] out[i] = sub_x.sum(axis=0) class TestSeqMaxPool(TestSeqAvgPool): def set_data(self): self.op_type = 'sequence_pool' x = np.random.uniform(0.1, 1, [13, 23]).astype('float32') lod = [[0, 4, 5, 8, 13]] for i in range(4): l = lod[0][i + 1] - lod[0][i] x[lod[0][i] + np.random.randint(l), :] += 2.0 self.inputs = {'X': (x, lod)} out = np.zeros((4, 23)).astype('float32') self.outputs = {'Out': out} return x, lod, out def compute(self, x, lod, out): self.attrs = {'pooltype': "MAX"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] out[i] = np.amax(sub_x, axis=0) class TestSeqSqrtPool(TestSeqAvgPool): def compute(self, x, lod, out): self.attrs = {'pooltype': "SQRT"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] len = lod[0][i + 1] - lod[0][i] out[i] = sub_x.sum(axis=0) / np.sqrt(len) class TestSeqLastPool(TestSeqAvgPool): def compute(self, x, lod, out): self.attrs = {'pooltype': "LAST"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] out[i] = sub_x[-1, :] class TestSeqFirstPool(TestSeqAvgPool): def compute(self, x, lod, out): self.attrs = {'pooltype': "FIRST"} for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] out[i] = sub_x[0, :] class TestSeqAvgPool2D(TestSeqAvgPool): def set_data(self): self.op_type = 'sequence_pool' # one level, batch size is 4 x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32') lod = [[0, 4, 5, 8, 13]] self.inputs = {'X': (x, lod)} out = np.zeros((4, 3, 17)).astype('float32') self.outputs = {'Out': out} return x, lod, out def compute(self, x, lod, out): self.attrs = {'pooltype': "AVERAGE"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17)) out[i] = np.reshape(sub_x.mean(axis=0), (3, 17)) class TestSeqSumPool2D(TestSeqAvgPool2D): def compute(self, x, lod, out): self.attrs = {'pooltype': "SUM"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17)) out[i] = np.reshape(sub_x.sum(axis=0), (3, 17)) class TestSeqSqrtPool2D(TestSeqAvgPool2D): def compute(self, x, lod, out): self.attrs = {'pooltype': "SQRT"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17)) len = lod[0][i + 1] - lod[0][i] out[i] = np.reshape(sub_x.sum(axis=0) / np.sqrt(len), (3, 17)) def test_check_grad(self): # Remove MaxIndex after check_grad is refined. self.outputs['MaxIndex'] = \ np.zeros(self.outputs['Out'].shape).astype('int32') self.check_grad(["X"], "Out", max_relative_error=0.06) class TestSeqMaxPool2D(TestSeqAvgPool2D): def set_data(self): self.op_type = 'sequence_pool' x = np.random.uniform(0.1, 1, [13, 3, 11]).astype('float32') lod = [[0, 4, 5, 8, 13]] self.inputs = {'X': (x, lod)} for i in range(4): l = lod[0][i + 1] - lod[0][i] x[lod[0][i] + np.random.randint(l), :] += 1.0 out = np.zeros((4, 3, 11)).astype('float32') self.outputs = {'Out': out} return x, lod, out def compute(self, x, lod, out): self.attrs = {'pooltype': "MAX"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 11)) out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11)) class TestSeqLastPool2D(TestSeqAvgPool2D): def compute(self, x, lod, out): self.attrs = {'pooltype': "LAST"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17)) out[i] = np.reshape(sub_x[-1, :], (3, 17)) class TestSeqFirstPool2D(TestSeqAvgPool2D): def compute(self, x, lod, out): self.attrs = {'pooltype': "FIRST"} for i in range(4): sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17)) out[i] = np.reshape(sub_x[0, :], (3, 17)) if __name__ == '__main__': unittest.main()
6,182
31.888298
75
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_momentum_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestMomentumOp1(OpTest): def setUp(self): self.op_type = "momentum" param = np.random.random((123, 321)).astype("float32") grad = np.random.random((123, 321)).astype("float32") velocity = np.zeros((123, 321)).astype("float32") learning_rate = np.array([0.001]).astype("float32") mu = 0.0001 use_nesterov = False self.inputs = { 'Param': param, 'Grad': grad, 'Velocity': velocity, 'LearningRate': learning_rate } self.attrs = {'mu': mu} velocity_out = mu * velocity + grad if use_nesterov: param_out = param - grad * learning_rate + \ velocity_out * mu * learning_rate else: param_out = param - learning_rate * velocity_out self.outputs = {'ParamOut': param_out, 'VelocityOut': velocity_out} def test_check_output(self): self.check_output() class TestMomentumOp2(OpTest): '''Test Momentum with default values for attributes ''' def setUp(self): self.op_type = "momentum" param = np.random.random((123, 321)).astype("float32") grad = np.random.random((123, 321)).astype("float32") velocity = np.zeros((123, 321)).astype("float32") learning_rate = np.array([0.001]).astype("float32") mu = 0.0001 use_nesterov = True self.inputs = { 'Param': param, 'Grad': grad, 'Velocity': velocity, 'LearningRate': learning_rate } self.attrs = {'mu': mu, 'use_nesterov': use_nesterov} velocity_out = mu * velocity + grad if use_nesterov: param_out = param - grad * learning_rate + \ velocity_out * mu * learning_rate else: param_out = param - learning_rate * velocity_out self.outputs = {'ParamOut': param_out, 'VelocityOut': velocity_out} def test_check_output(self): self.check_output() if __name__ == "__main__": unittest.main()
2,774
29.494505
75
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_adam_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest from paddle.fluid import core from paddle.fluid.op import Operator class TestAdamOp1(OpTest): def setUp(self): '''Test Adam Op with supplied attributes ''' self.op_type = "adam" param = np.random.uniform(-1, 1, (102, 105)).astype("float32") grad = np.random.uniform(-1, 1, (102, 105)).astype("float32") moment1 = np.random.uniform(-1, 1, (102, 105)).astype("float32") # The second moment is positive moment2 = np.random.random((102, 105)).astype("float32") learning_rate = 0.004 beta1 = 0.78 beta2 = 0.836 epsilon = 1e-4 beta1_pow = beta1**10 beta2_pow = beta2**10 self.inputs = { 'Param': param, 'Grad': grad, 'Moment1': moment1, 'Moment2': moment2, 'LearningRate': np.array([learning_rate]).astype("float32"), 'Beta1Pow': np.array([beta1_pow]).astype("float32"), 'Beta2Pow': np.array([beta2_pow]).astype("float32") } self.attrs = {'epsilon': epsilon, 'beta1': beta1, 'beta2': beta2} param_out, moment1_out, \ moment2_out = adam_step(self.inputs, self.attrs) self.outputs = { 'Moment1Out': moment1_out, 'Moment2Out': moment2_out, 'ParamOut': param_out } def test_check_output(self): self.check_output() class TestAdamOp2(OpTest): def setUp(self): '''Test Adam Op with supplied attributes ''' self.op_type = "adam" param = np.random.uniform(-1, 1, (102, 105)).astype("float32") grad = np.random.uniform(-1, 1, (102, 105)).astype("float32") moment1 = np.random.uniform(-1, 1, (102, 105)).astype("float32") # The second moment is positive moment2 = np.random.random((102, 105)).astype("float32") learning_rate = 0.001 beta1 = 0.9 beta2 = 0.999 epsilon = 1e-8 beta1_pow = beta1**10 beta2_pow = beta2**10 self.inputs = { 'Param': param, 'Grad': grad, 'Moment1': moment1, 'Moment2': moment2, 'LearningRate': np.array([learning_rate]).astype("float32"), 'Beta1Pow': np.array([beta1_pow]).astype("float32"), 'Beta2Pow': np.array([beta2_pow]).astype("float32") } attributes = {'epsilon': epsilon, 'beta1': beta1, 'beta2': beta2} param_out, moment1_out, \ moment2_out = adam_step(self.inputs, attributes) self.outputs = { 'Moment1Out': moment1_out, 'Moment2Out': moment2_out, 'ParamOut': param_out } def test_check_output(self): self.check_output() class TestAdamOpMultipleSteps(OpTest): def setUp(self): '''Test Adam Operator with supplied attributes ''' self.op_type = "adam" self.num_steps = 10 param = np.random.uniform(-1, 1, (102, 105)).astype("float32") grad = np.random.uniform(-1, 1, (102, 105)).astype("float32") moment1 = np.random.uniform(-1, 1, (102, 105)).astype("float32") # The second moment is positive moment2 = np.random.random((102, 105)).astype("float32") learning_rate = 0.001 beta1 = 0.9 beta2 = 0.999 epsilon = 1e-8 beta1_pow = beta1**10 beta2_pow = beta2**10 self.inputs = { 'Param': param, 'Grad': grad, 'Moment1': moment1, 'Moment2': moment2, 'LearningRate': np.array([learning_rate]).astype("float32"), 'Beta1Pow': np.array([beta1_pow]).astype("float32"), 'Beta2Pow': np.array([beta2_pow]).astype("float32") } self.attrs = {'epsilon': epsilon, 'beta1': beta1, 'beta2': beta2} def test_check_output(self): for _ in range(self.num_steps): param_out, moment1_out, \ moment2_out = adam_step(self.inputs, self.attrs) self.outputs = { 'Moment1Out': moment1_out, 'Moment2Out': moment2_out, 'ParamOut': param_out } # Verify output for this step self.check_output() # Output of this step becomes input for next step self.inputs['Param'] = param_out self.inputs['Moment1'] = moment1_out self.inputs['Moment2'] = moment2_out # Update powers of Beta1 and Beta2 for next time step self.inputs['Beta1Pow'] *= self.attrs['beta1'] self.inputs['Beta2Pow'] *= self.attrs['beta1'] # Randomize gradient for next step self.inputs['Grad'] = np.random.uniform( -1, 1, (102, 105)).astype("float32") def adam_step(inputs, attributes): ''' Simulate one step of the adam optimizer :param inputs: dict of inputs :param attributes: dict of attributes :return tuple: tuple of output param, moment1, moment2, beta1 power accumulator and beta2 power accumulator ''' param = inputs['Param'] grad = inputs['Grad'] moment1 = inputs['Moment1'] moment2 = inputs['Moment2'] lr = inputs['LearningRate'] beta1_pow = inputs['Beta1Pow'] beta2_pow = inputs['Beta2Pow'] beta1 = attributes['beta1'] beta2 = attributes['beta2'] epsilon = attributes['epsilon'] moment1_out = beta1 * moment1 + (1 - beta1) * grad moment2_out = beta2 * moment2 + (1 - beta2) * np.square(grad) lr_t = lr * np.sqrt(1 - beta2_pow) / (1 - beta1_pow) param_out = param - lr_t * (moment1_out / (np.sqrt(moment2_out) + epsilon)) return param_out, moment1_out, moment2_out def adam_step_sparse(inputs, attributes, height, rows, row_numel, np_grad): ''' Simulate one step of the adam optimizer :param inputs: dict of inputs :param attributes: dict of attributes :return tuple: tuple of output param, moment1, moment2, beta1 power accumulator and beta2 power accumulator ''' param = inputs['Param'] # grad = inputs['Grad'] moment1 = inputs['Moment1'] moment2 = inputs['Moment2'] lr = inputs['LearningRate'] beta1_pow = inputs['Beta1Pow'] beta2_pow = inputs['Beta2Pow'] beta1 = attributes['beta1'] beta2 = attributes['beta2'] epsilon = attributes['epsilon'] moment1_out = np.zeros(shape=[height, row_numel]) moment2_out = np.zeros(shape=[height, row_numel]) param_out = np.zeros(shape=[height, row_numel]) for idx, row_id in enumerate(rows): moment1_out[row_id] = beta1 * moment1[row_id] + (1 - beta1 ) * np_grad[idx] moment2_out[row_id] = beta2 * moment2[row_id] + ( 1 - beta2) * np.square(np_grad[idx]) lr_t = lr * np.sqrt(1 - beta2_pow) / (1 - beta1_pow) param_out[row_id] = param[row_id] - lr_t * (moment1_out[row_id] / ( np.sqrt(moment2_out[row_id]) + epsilon)) return param_out, moment1_out, moment2_out class TestSparseAdamOp(unittest.TestCase): def setup(self, scope, place): beta1 = 0.78 beta2 = 0.836 epsilon = 1e-4 height = 10 rows = [0, 4, 7] self.rows = rows row_numel = 12 self.row_numel = row_numel self.dense_inputs = { "Param": np.full((height, row_numel), 5.0).astype("float32"), "Moment1": np.full((height, row_numel), 5.0).astype("float32"), "Moment2": np.full((height, row_numel), 5.0).astype("float32"), 'Beta1Pow': np.array([beta1**10]).astype("float32"), 'Beta2Pow': np.array([beta2**10]).astype("float32"), "LearningRate": np.full((1), 2.0).astype("float32") } self.attrs = {'epsilon': epsilon, 'beta1': beta1, 'beta2': beta2} grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) self.sparse_inputs = ["Grad"] param_out, mom1, mom2 = adam_step_sparse( self.dense_inputs, self.attrs, height, rows, row_numel, np_array) self.outputs = { "ParamOut": param_out, "Moment1Out": mom1, "Moment2Out": mom2 } def check_with_place(self, place): scope = core.Scope() self.setup(scope, place) op_args = dict() for key, np_array in self.dense_inputs.iteritems(): var = scope.var(key).get_tensor() var.set(np_array, place) op_args[key] = key for s in self.sparse_inputs: op_args[s] = s for s in self.outputs: var = scope.var(s).get_tensor() var.set(self.outputs[s], place) op_args[s] = s for k in self.attrs: op_args[k] = self.attrs[k] # create and run sgd operator adam_op = Operator("adam", **op_args) adam_op.run(scope, place) for key, np_array in self.outputs.iteritems(): out_var = scope.var(key).get_tensor() actual = np.array(out_var) actual = actual.reshape([actual.size]) np_array = np_array.reshape([np_array.size]) for idx, row_id in enumerate(self.rows): j = 0 while j < self.row_numel: pos = row_id * self.row_numel + j self.assertLess((actual[pos] - np_array[pos]) / actual[pos], 0.00001) j += 1 def test_sparse_sgd(self): places = [core.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) for place in places: self.check_with_place(place) if __name__ == "__main__": unittest.main()
10,798
33.174051
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_registry.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import numpy as np import decorators class TestRegistry(unittest.TestCase): @decorators.prog_scope() def test_registry_layer(self): x = fluid.layers.data(name='X', shape=[10, 10], dtype='float32') output = fluid.layers.mean(x) place = fluid.CPUPlace() exe = fluid.Executor(place) X = np.random.random((10, 10)).astype("float32") mean_out = exe.run(feed={"X": X}, fetch_list=[output]) self.assertAlmostEqual(np.mean(X), mean_out[0], delta=1e-5)
1,169
35.5625
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_sigmoid_cross_entropy_with_logits_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np from op_test import OpTest from scipy.special import logit from scipy.special import expit import unittest class TestSigmoidCrossEntropyWithLogitsOp1(OpTest): """Test sigmoid_cross_entropy_with_logit_op with binary label """ def setUp(self): self.op_type = "sigmoid_cross_entropy_with_logits" batch_size = 64 num_classes = 20 self.inputs = { 'X': logit( np.random.uniform(0, 1, (batch_size, num_classes)) .astype("float32")), 'Label': np.random.randint(0, 2, (batch_size, num_classes)) .astype("float32") } # Fw Pass is implemented as elementwise sigmoid followed by # elementwise logistic loss # Label * -log(sigmoid(X)) + (1 - label) * -log(1 - sigmoid(X)) sigmoid_X = expit(self.inputs['X']) term1 = self.inputs['Label'] * np.log(sigmoid_X) term2 = (1 - self.inputs['Label']) * np.log(1 - sigmoid_X) self.outputs = {'Out': -term1 - term2} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') class TestSigmoidCrossEntropyWithLogitsOp2(OpTest): """Test sigmoid_cross_entropy_with_logit_op with probabalistic label """ def setUp(self): self.op_type = "sigmoid_cross_entropy_with_logits" batch_size = 64 num_classes = 20 self.inputs = { 'X': logit( np.random.uniform(0, 1, (batch_size, num_classes)) .astype("float32")), 'Label': np.random.uniform(0, 1, (batch_size, num_classes)) .astype("float32") } # Fw Pass is implemented as elementwise sigmoid followed by # elementwise logistic loss # Label * -log(sigmoid(X)) + (1 - label) * -log(1 - sigmoid(X)) sigmoid_X = expit(self.inputs['X']) term1 = self.inputs['Label'] * np.log(sigmoid_X) term2 = (1 - self.inputs['Label']) * np.log(1 - sigmoid_X) self.outputs = {'Out': -term1 - term2} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') if __name__ == '__main__': unittest.main()
2,889
32.604651
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lod_tensor_array.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core import numpy class TestLoDTensorArray(unittest.TestCase): def test_get_set(self): scope = core.Scope() arr = scope.var('tmp_lod_tensor_array') tensor_array = arr.get_lod_tensor_array() self.assertEqual(0, len(tensor_array)) cpu = core.CPUPlace() for i in xrange(10): t = core.LoDTensor() t.set(numpy.array([i], dtype='float32'), cpu) t.set_lod([[0, 1]]) tensor_array.append(t) self.assertEqual(10, len(tensor_array)) for i in xrange(10): t = tensor_array[i] self.assertEqual(numpy.array(t), numpy.array([i], dtype='float32')) self.assertEqual([[0, 1]], t.lod()) t = core.LoDTensor() t.set(numpy.array([i + 10], dtype='float32'), cpu) t.set_lod([[0, 2]]) tensor_array[i] = t t = tensor_array[i] self.assertEqual( numpy.array(t), numpy.array( [i + 10], dtype='float32')) self.assertEqual([[0, 2]], t.lod()) if __name__ == '__main__': unittest.main()
1,793
32.849057
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_norm_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def norm(input, scale, epsilon): s0, s1, s2, s3 = input.shape x_square = input * input for i in xrange(s0): input_batch = input[i:i + 1, :, :, :] input_batch = input_batch.reshape(s1, s2 * s3) x_square_batch = x_square[i:i + 1, :, :, :] x_square_batch = x_square_batch.reshape(s1, s2 * s3) square_colsum = x_square_batch.sum(axis=0) + epsilon tmp = pow(square_colsum, 0.5) tmp = np.reciprocal(tmp) tmp_tile = np.tile(tmp, s1) tmp_tile = tmp_tile.reshape(s1, s2 * s3) scale_tile = np.tile(scale, (1, s2 * s3)) scale_tile = scale_tile.reshape(s1, s2 * s3) out_batch = input_batch * tmp_tile * scale_tile out_batch = out_batch.reshape(1, s1, s2, s3) if i == 0: out = out_batch else: out = np.concatenate((out, out_batch), 0) out.reshape(s0, s1, s2, s3) return out class TestNormOp(OpTest): def setUp(self): self.op_type = "norm" self.init_test_case() input = np.random.random(self.shape).astype("float32") scale = np.array([10, 10, 10]) self.inputs = { 'X': input.astype('float32'), 'Scale': scale.astype('float32') } self.attrs = {'epsilon': self.epsilon} output = norm(input, scale, self.epsilon) self.outputs = {'Out': output.astype('float32')} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') def init_test_case(self): self.shape = [2, 3, 2, 2] self.epsilon = 1e-6 if __name__ == '__main__': unittest.main()
2,362
31.819444
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_infer_shape.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core class TestInferShape(unittest.TestCase): def test_sum_op(self): prog = core.ProgramDesc() self.assertIsNotNone(prog) block = prog.block(0) self.assertIsNotNone(block) shape = [10, 20] # prepare input/output x1 = block.var("x1") x1.set_type(core.VarDesc.VarType.LOD_TENSOR) x1.set_shape(shape) x2 = block.var("x2") x2.set_type(core.VarDesc.VarType.LOD_TENSOR) x2.set_shape(shape) out = block.var("out") out.set_type(core.VarDesc.VarType.LOD_TENSOR) # prepare the operator sum_op_desc = block.append_op() sum_op_desc.set_type("sum") sum_op_desc.set_input("X", ["x1", "x2"]) sum_op_desc.set_output("Out", ["out"]) sum_op_desc.check_attrs() sum_op_desc.infer_shape(block) self.assertEqual(out.shape(), shape) def test_mul_op(self): prog = core.ProgramDesc() self.assertIsNotNone(prog) block = prog.block(0) self.assertIsNotNone(block) x_shape = [10, 20] y_shape = [20, 30] # prepare input/output x1 = block.var("x") x1.set_type(core.VarDesc.VarType.LOD_TENSOR) x1.set_shape(x_shape) x2 = block.var("y") x2.set_type(core.VarDesc.VarType.LOD_TENSOR) x2.set_shape(y_shape) out = block.var("out") out.set_type(core.VarDesc.VarType.LOD_TENSOR) # prepare the operator mul_op_desc = block.append_op() mul_op_desc.set_type("mul") mul_op_desc.set_input("X", ["x"]) mul_op_desc.set_input("Y", ["y"]) mul_op_desc.set_output("Out", ["out"]) mul_op_desc.set_attr("x_num_col_dims", 1) mul_op_desc.set_attr("y_num_col_dims", 1) mul_op_desc.check_attrs() mul_op_desc.infer_shape(block) self.assertEqual(out.shape(), [x_shape[0], y_shape[1]]) if __name__ == '__main__': unittest.main()
2,639
29.697674
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_parallel_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle.fluid.profiler as profiler import numpy class BaseParallelForTest(unittest.TestCase): def run_test(self, callback, feed, fetch): """ Run the unittest for parallel.for Args: callback(callable): A callable function returns a generator. There are two yields in the generator function. The first yield returns the data layers, and the second yield returns the loss. The modified data variables will be sent back during the first yield. feed(dict): The executor feeding dictionary. fetch(list|basestr): The fetch name lists. Returns: None Raises: AssertionError when the computation of cpu, parallel.for in cpu, gpu, parallel.for in gpu are different. """ cpu = fluid.CPUPlace() result_cpu = self._run_test_impl_( callback=callback, feed=feed, fetch=fetch, place=cpu, use_parallel=False) result_cpu_parallel = self._run_test_impl_( callback=callback, feed=feed, fetch=fetch, place=cpu, use_parallel=True) if fluid.core.is_compiled_with_cuda(): gpu = fluid.CUDAPlace(0) result_gpu = self._run_test_impl_( callback=callback, feed=feed, fetch=fetch, place=gpu, use_parallel=False, use_gpu=True) result_gpu_parallel = self._run_test_impl_( callback=callback, feed=feed, fetch=fetch, place=gpu, use_parallel=True, use_gpu=True) result_gpu_nccl = self._run_test_impl_( callback=callback, feed=feed, fetch=fetch, place=gpu, use_parallel=True, use_nccl=True, use_gpu=True) self._assert_same_(fetch, result_cpu, result_cpu_parallel, result_gpu, result_gpu_parallel, result_gpu_nccl) else: self._assert_same_(fetch, result_cpu, result_cpu_parallel) def _run_test_impl_(self, callback, feed, fetch, place, use_parallel=False, use_nccl=False, use_gpu=False): """ Run a single test, returns the fetch values Args: place(Place): the computation place. use_parallel(bool): Whether use parallel.for or not. Returns: Fetched numpy arrays. """ if isinstance(fetch, basestring): fetch = [fetch] main = fluid.Program() startup = fluid.Program() # Fix seed main.random_seed = 10 startup.random_seed = 10 with fluid.program_guard(main, startup): generator = callback() # Automatically insert parallel do if use_parallel = True if use_parallel: places = fluid.layers.get_places() pd = fluid.layers.ParallelDo(places, use_nccl=use_nccl) data = next(generator) if isinstance(data, fluid.Variable): data = [data] with pd.do(): ins = map(pd.read_input, data) if len(ins) == 1: ins = ins[0] loss = generator.send(ins) # patch input pd.write_output(loss) loss = pd() else: data = next(generator) loss = generator.send(data) self.assertIsNotNone(loss) avg_loss = fluid.layers.mean(loss) fluid.backward.append_backward(loss=avg_loss) exe = fluid.Executor(place) exe.run(startup) if use_gpu: profile_type = 'GPU' else: profile_type = 'CPU' with profiler.profiler(profile_type, 'total', '/tmp/profiler'): return exe.run(main, feed=feed, fetch_list=fetch) def _assert_same_(self, fetch, *args): """ Assert the return values of `run_test` are same. Args: fetch: Fetch list. Used for print error message *args: The fetch result lists of each situations. Returns: None Raises: AssertionError """ def _impl_(a, b, fetch_id, item_id): item_str = [ 'CPU', 'ParallelCPU', 'GPU', 'ParallelGPU', 'ParallelGPUNCCL' ] flag = numpy.allclose(a, b, rtol=0.1, atol=1e-3) self.assertTrue(flag, "The {0} are different in {1}, {2} vs {3}".format( fetch[fetch_id], item_str[item_id], a, b)) for i, items in enumerate(zip(*args)): self.assertGreater(len(items), 0) for j in range(1, len(items)): _impl_(items[0], items[j], fetch_id=i, item_id=j) class ParallelOpTest(BaseParallelForTest): @staticmethod def __network__(): x = fluid.layers.data(shape=[784], dtype='float32', name='img') x = yield x hidden = fluid.layers.fc(input=x, size=200, param_attr='fc1.w') hidden = fluid.layers.batch_norm(input=hidden) loss = fluid.layers.mean(hidden) yield loss def test_simple_fc(self): self.run_test( callback=self.__network__, feed={ 'img': numpy.random.random(size=(51, 784)).astype('float32') }, fetch=['fc1.w@GRAD']) def test_fc_with_tiny_data(self): self.run_test( callback=self.__network__, feed={'img': numpy.random.random(size=(1, 784)).astype('float32')}, fetch=['fc1.w@GRAD']) class ParallelOpTestMultipleInput(BaseParallelForTest): @staticmethod def __network__(): x = fluid.layers.data( shape=[784], dtype='float32', name='img1', stop_gradient=False) y = fluid.layers.data( shape=[784], dtype='float32', name='img2', stop_gradient=False) yield [x, y] x = x + y hidden1 = fluid.layers.fc(input=x, size=200, param_attr='fc1.w') hidden2 = fluid.layers.fc(input=hidden1, size=200, param_attr='fc2.w') hidden3 = fluid.layers.fc(input=hidden2, size=200, param_attr='fc3.w') loss = fluid.layers.mean(hidden3) yield loss def test_simple_fc(self): self.run_test( callback=self.__network__, feed={ 'img1': numpy.random.random(size=(51, 784)).astype('float32'), 'img2': numpy.random.random(size=(51, 784)).astype('float32') }, fetch=['fc1.w@GRAD', 'fc2.w@GRAD', 'fc3.w@GRAD']) if __name__ == '__main__': unittest.main()
7,825
33.174672
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_beam_search_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import logging from paddle.fluid.op import Operator, DynamicRecurrentOp import paddle.fluid.core as core import unittest import numpy as np def create_tensor(scope, name, np_data): tensor = scope.var(name).get_tensor() tensor.set(np_data, core.CPUPlace()) return tensor class BeamSearchOpTester(unittest.TestCase): def setUp(self): self.scope = core.Scope() self._create_ids() self._create_scores() self._create_pre_ids() self.scope.var('selected_ids') self.scope.var('selected_scores') def test_run(self): op = Operator( 'beam_search', pre_ids="pre_ids", ids='ids', scores='scores', selected_ids='selected_ids', selected_scores='selected_scores', level=0, beam_size=2, end_id=0, ) op.run(self.scope, core.CPUPlace()) selected_ids = self.scope.find_var("selected_ids").get_tensor() print 'selected_ids', np.array(selected_ids) print 'lod', selected_ids.lod() def _create_pre_ids(self): np_data = np.array([[1, 2, 3, 4]], dtype='int64') tensor = create_tensor(self.scope, "pre_ids", np_data) def _create_ids(self): self.lod = [[0, 1, 4], [0, 1, 2, 3, 4]] np_data = np.array( [[4, 2, 5], [2, 1, 3], [3, 5, 2], [8, 2, 1]], dtype='int64') tensor = create_tensor(self.scope, "ids", np_data) tensor.set_lod(self.lod) def _create_scores(self): np_data = np.array( [ [0.5, 0.3, 0.2], [0.6, 0.3, 0.1], [0.9, 0.5, 0.1], [0.7, 0.5, 0.1], ], dtype='float32') tensor = create_tensor(self.scope, "scores", np_data) tensor.set_lod(self.lod) if __name__ == '__main__': unittest.main()
2,508
30.759494
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_sequence_softmax_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest from test_softmax_op import stable_softmax import paddle.fluid.core as core class TestSequenceSoftmaxOp(OpTest): def setUp(self): self.op_type = "sequence_softmax" self.use_cudnn = False self.init_op_type() x = np.random.uniform(0.1, 1, (11, 1)).astype("float32") lod = [[0, 4, 5, 8, 11]] out = np.zeros((11, 1)).astype("float32") for i in range(4): sub_x = x[lod[0][i]:lod[0][i + 1], :] sub_x = sub_x.reshape(1, lod[0][i + 1] - lod[0][i]) sub_out = stable_softmax(sub_x) out[lod[0][i]:lod[0][i + 1], :] = sub_out.reshape( lod[0][i + 1] - lod[0][i], 1) self.inputs = {"X": (x, lod)} self.outputs = {"Out": out} self.attrs = {'use_cudnn': self.use_cudnn, } def init_op_type(self): pass def test_check_output(self): if self.use_cudnn: place = core.CUDAPlace(0) self.check_output_with_place(place, atol=1e-5) else: self.check_output() def test_check_grad(self): if self.use_cudnn: place = core.CUDAPlace(0) self.check_grad_with_place( place, ["X"], "Out", max_relative_error=0.01) else: self.check_grad(["X"], "Out", max_relative_error=0.01) # ----------------cudnn Sequencesoftmax---------------- class TestSequenceSoftmaxCUDNNOp(TestSequenceSoftmaxOp): def init_op_type(self): self.use_cudnn = True if __name__ == "__main__": unittest.main()
2,232
30.9
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_gaussian_random_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid.op import Operator from paddle.fluid.executor import Executor class TestGaussianRandomOp(unittest.TestCase): def setUp(self): self.op_type = "gaussian_random" self.inputs = {} self.attrs = {"shape": [1000, 784], "mean": .0, "std": 1., "seed": 10} self.outputs = ["Out"] def test_cpu(self): self.gaussian_random_test(place=fluid.CPUPlace()) def test_gpu(self): if core.is_compiled_with_cuda(): self.gaussian_random_test(place=fluid.CUDAPlace(0)) def gaussian_random_test(self, place): program = fluid.Program() block = program.global_block() vout = block.create_var(name="Out") op = block.append_op( type=self.op_type, outputs={"Out": vout}, attrs=self.attrs) op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) fetch_list = [] for var_name in self.outputs: fetch_list.append(block.var(var_name)) exe = Executor(place) outs = exe.run(program, fetch_list=fetch_list) tensor = outs[0] self.assertAlmostEqual(numpy.mean(tensor), .0, delta=0.1) self.assertAlmostEqual(numpy.std(tensor), 1., delta=0.1) if __name__ == "__main__": unittest.main()
1,999
30.25
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_elementwise_pow_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestElementwisePowOp(OpTest): def setUp(self): self.op_type = "elementwise_pow" self.inputs = { 'X': np.random.uniform(0.1, 1, [13, 17]).astype("float32"), 'Y': np.random.uniform(0.1, 1, [13, 17]).astype("float32") } self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} def test_check_output(self): self.check_output() class TestElementwisePowOp_scalar(TestElementwisePowOp): def setUp(self): self.op_type = "elementwise_pow" self.inputs = { 'X': np.random.rand(2, 3, 4).astype('float32'), 'Y': np.random.rand(1).astype('float32') } self.outputs = {'Out': np.power(self.inputs['X'], self.inputs['Y'])} if __name__ == '__main__': unittest.main()
1,482
32.704545
76
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_squared_l2_norm_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import unittest from numpy import linalg as LA from op_test import OpTest class TestL2LossOp(OpTest): """Test squared_l2_norm """ def setUp(self): self.op_type = "squared_l2_norm" self.max_relative_error = 0.05 X = np.random.uniform(-1, 1, (13, 19)).astype("float32") X[np.abs(X) < self.max_relative_error] = 0.1 self.inputs = {'X': X} self.outputs = {'Out': np.square(LA.norm(X))} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad( ['X'], 'Out', max_relative_error=self.max_relative_error) if __name__ == "__main__": unittest.main()
1,316
28.931818
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_roi_pool_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import math import sys from op_test import OpTest class TestROIPoolOp(OpTest): def set_data(self): self.init_test_case() self.make_rois() self.calc_roi_pool() self.inputs = {'X': self.x, 'ROIs': (self.rois[:, 1:5], self.rois_lod)} self.attrs = { 'spatial_scale': self.spatial_scale, 'pooled_height': self.pooled_height, 'pooled_width': self.pooled_width } self.outputs = {'Out': self.outs, 'Argmax': self.argmaxes} def init_test_case(self): self.batch_size = 3 self.channels = 3 self.height = 6 self.width = 4 # n, c, h, w self.x_dim = (self.batch_size, self.channels, self.height, self.width) self.spatial_scale = 1.0 / 4.0 self.pooled_height = 2 self.pooled_width = 2 self.x = np.random.random(self.x_dim).astype('float32') def calc_roi_pool(self): out_data = np.zeros((self.rois_num, self.channels, self.pooled_height, self.pooled_width)) argmax_data = np.zeros((self.rois_num, self.channels, self.pooled_height, self.pooled_width)) for i in range(self.rois_num): roi = self.rois[i] roi_batch_id = roi[0] roi_start_w = int(round(roi[1] * self.spatial_scale)) roi_start_h = int(round(roi[2] * self.spatial_scale)) roi_end_w = int(round(roi[3] * self.spatial_scale)) roi_end_h = int(round(roi[4] * self.spatial_scale)) roi_height = int(max(roi_end_h - roi_start_h + 1, 1)) roi_width = int(max(roi_end_w - roi_start_w + 1, 1)) x_i = self.x[roi_batch_id] bin_size_h = float(roi_height) / float(self.pooled_height) bin_size_w = float(roi_width) / float(self.pooled_width) for c in range(self.channels): for ph in range(self.pooled_height): for pw in range(self.pooled_width): hstart = int(math.floor(ph * bin_size_h)) wstart = int(math.floor(pw * bin_size_w)) hend = int(math.ceil((ph + 1) * bin_size_h)) wend = int(math.ceil((pw + 1) * bin_size_w)) hstart = min(max(hstart + roi_start_h, 0), self.height) hend = min(max(hend + roi_start_h, 0), self.height) wstart = min(max(wstart + roi_start_w, 0), self.width) wend = min(max(wend + roi_start_w, 0), self.width) is_empty = (hend <= hstart) or (wend <= wstart) if is_empty: out_data[i, c, ph, pw] = 0 else: out_data[i, c, ph, pw] = -sys.float_info.max argmax_data[i, c, ph, pw] = -1 for h in range(hstart, hend): for w in range(wstart, wend): if x_i[c, h, w] > out_data[i, c, ph, pw]: out_data[i, c, ph, pw] = x_i[c, h, w] argmax_data[i, c, ph, pw] = h * \ self.width + w self.outs = out_data.astype('float32') self.argmaxes = argmax_data.astype('int64') def make_rois(self): rois = [] self.rois_lod = [[]] for bno in range(self.batch_size): self.rois_lod[0].append(len(rois)) for i in range(bno + 1): x1 = np.random.random_integers( 0, self.width / self.spatial_scale - self.pooled_width) y1 = np.random.random_integers( 0, self.height / self.spatial_scale - self.pooled_height) x2 = np.random.random_integers(x1 + self.pooled_width, self.width / self.spatial_scale) y2 = np.random.random_integers(y1 + self.pooled_height, self.height / self.spatial_scale) roi = [bno, x1, y1, x2, y2] rois.append(roi) self.rois_lod[0].append(len(rois)) self.rois_num = len(rois) self.rois = np.array(rois).astype("int64") def setUp(self): self.op_type = "roi_pool" self.set_data() def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') if __name__ == '__main__': unittest.main()
5,299
36.588652
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_compare_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import op_test import unittest import numpy def create_test_class(op_type, typename, callback): class Cls(op_test.OpTest): def setUp(self): a = numpy.random.random(size=(10, 7)).astype(typename) b = numpy.random.random(size=(10, 7)).astype(typename) c = callback(a, b) self.inputs = {'X': a, 'Y': b} self.outputs = {'Out': c} self.op_type = op_type def test_output(self): self.check_output() cls_name = "{0}_{1}".format(op_type, typename) Cls.__name__ = cls_name globals()[cls_name] = Cls for _type_name in {'float32', 'float64', 'int32', 'int64'}: create_test_class('less_than', _type_name, lambda _a, _b: _a < _b) create_test_class('less_equal', _type_name, lambda _a, _b: _a <= _b) create_test_class('greater_than', _type_name, lambda _a, _b: _a > _b) create_test_class('greater_equal', _type_name, lambda _a, _b: _a >= _b) create_test_class('equal', _type_name, lambda _a, _b: _a == _b) create_test_class('not_equal', _type_name, lambda _a, _b: _a != _b) if __name__ == '__main__': unittest.main()
1,766
35.8125
75
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_print_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core from paddle.fluid.executor import Executor import paddle.fluid.layers as layers from paddle.fluid.backward import append_backward from paddle.fluid.framework import switch_main_program from paddle.fluid.framework import Program import numpy as np class TestPrintOpCPU(unittest.TestCase): def setUp(self): self.place = core.CPUPlace() self.x_tensor = core.LoDTensor() tensor_np = np.random.random(size=(2, 3)).astype('float32') self.x_tensor.set(tensor_np, self.place) self.x_tensor.set_lod([[0, 1, 1]]) def build_network(self, only_forward, **kargs): x = layers.data('x', shape=[3], dtype='float32', lod_level=1) x.stop_gradient = False printed = layers.Print(input=x, **kargs) if only_forward: return printed loss = layers.mean(printed) append_backward(loss=loss) return loss def test_forward(self): switch_main_program(Program()) printed = self.build_network(True, print_phase='forward') exe = Executor(self.place) outs = exe.run(feed={'x': self.x_tensor}, fetch_list=[printed], return_numpy=False) def test_backward(self): switch_main_program(Program()) loss = self.build_network(False, print_phase='backward') exe = Executor(self.place) outs = exe.run(feed={'x': self.x_tensor}, fetch_list=[loss], return_numpy=False) class TestPrintOpGPU(TestPrintOpCPU): def setUp(self): self.place = core.CUDAPlace(0) self.x_tensor = core.LoDTensor() tensor_np = np.random.random(size=(2, 3)).astype('float32') self.x_tensor.set(tensor_np, self.place) self.x_tensor.set_lod([[0, 1, 1]]) if __name__ == '__main__': unittest.main()
2,507
34.828571
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_preprocessor.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid as fluid import paddle.v2 as paddle import paddle.v2.dataset.mnist as mnist class TestPreprocessor(unittest.TestCase): def setUp(self): with fluid.program_guard(fluid.Program(), fluid.Program()): reader = paddle.batch(mnist.train(), batch_size=32) feeder = fluid.DataFeeder( feed_list=[ # order is image and label fluid.layers.data( name='image', shape=[784]), fluid.layers.data( name='label', shape=[1], dtype='int64'), ], place=fluid.CPUPlace()) self.num_batches = fluid.recordio_writer.convert_reader_to_recordio_file( './mnist_for_preprocessor_test.recordio', reader, feeder) def test_main(self): N = 10 img_expected_res = [] lbl_expected_res = [] with fluid.program_guard(fluid.Program(), fluid.Program()): data_file = fluid.layers.io.open_recordio_file( './mnist_for_preprocessor_test.recordio', shapes=[[-1, 784], [-1, 1]], lod_levels=[0, 0], dtypes=['float32', 'int64']) img, lbl = fluid.layers.io.read_file(data_file) if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) else: place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) for _ in range(N): img_v, lbl_v = exe.run(fetch_list=[img, lbl]) img_expected_res.append(img_v / 2) lbl_expected_res.append(lbl_v + 1) img_actual_res = [] lbl_actual_res = [] with fluid.program_guard(fluid.Program(), fluid.Program()): data_file = fluid.layers.io.open_recordio_file( './mnist_for_preprocessor_test.recordio', shapes=[[-1, 784], [-1, 1]], lod_levels=[0, 0], dtypes=['float32', 'int64']) preprocessor = fluid.layers.io.Preprocessor(reader=data_file) with preprocessor.block(): img, lbl = preprocessor.inputs() img_out = img / 2 lbl_out = lbl + 1 preprocessor.outputs(img_out, lbl_out) data_file = fluid.layers.io.double_buffer(preprocessor()) img, lbl = fluid.layers.io.read_file(data_file) if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) else: place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) for _ in range(N): img_v, lbl_v = exe.run(fetch_list=[img, lbl]) img_actual_res.append(img_v) lbl_actual_res.append(lbl_v) for idx in range(N): np.allclose(img_expected_res[idx], img_actual_res[idx]) np.allclose(lbl_expected_res[idx], lbl_actual_res[idx])
3,745
38.851064
85
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_assign_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import op_test import numpy import unittest class TestAssignOp(op_test.OpTest): def setUp(self): self.op_type = "assign" x = numpy.random.random(size=(100, 10)) self.inputs = {'X': x} self.outputs = {'Out': x} def test_forward(self): self.check_output() def test_backward(self): self.check_grad(['X'], 'Out') if __name__ == '__main__': unittest.main()
1,035
27.777778
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_tensor.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid.core as core import unittest import numpy class TestTensor(unittest.TestCase): def test_int_tensor(self): scope = core.Scope() var = scope.var("test_tensor") place = core.CPUPlace() tensor = var.get_tensor() tensor.set_dims([1000, 784]) tensor.alloc_int(place) tensor_array = numpy.array(tensor) self.assertEqual((1000, 784), tensor_array.shape) tensor_array[3, 9] = 1 tensor_array[19, 11] = 2 tensor.set(tensor_array, place) tensor_array_2 = numpy.array(tensor) self.assertEqual(1, tensor_array_2[3, 9]) self.assertEqual(2, tensor_array_2[19, 11]) def test_float_tensor(self): scope = core.Scope() var = scope.var("test_tensor") place = core.CPUPlace() tensor = var.get_tensor() tensor.set_dims([1000, 784]) tensor.alloc_float(place) tensor_array = numpy.array(tensor) self.assertEqual((1000, 784), tensor_array.shape) tensor_array[3, 9] = 1.0 tensor_array[19, 11] = 2.0 tensor.set(tensor_array, place) tensor_array_2 = numpy.array(tensor) self.assertAlmostEqual(1.0, tensor_array_2[3, 9]) self.assertAlmostEqual(2.0, tensor_array_2[19, 11]) def test_int_lod_tensor(self): place = core.CPUPlace() scope = core.Scope() var_lod = scope.var("test_lod_tensor") lod_tensor = var_lod.get_tensor() lod_tensor.set_dims([4, 4, 6]) lod_tensor.alloc_int(place) array = numpy.array(lod_tensor) array[0, 0, 0] = 3 array[3, 3, 5] = 10 lod_tensor.set(array, place) lod_tensor.set_lod([[0, 2, 4]]) lod_v = numpy.array(lod_tensor) self.assertTrue(numpy.alltrue(array == lod_v)) lod = lod_tensor.lod() self.assertEqual(0, lod[0][0]) self.assertEqual(2, lod[0][1]) self.assertEqual(4, lod[0][2]) def test_float_lod_tensor(self): place = core.CPUPlace() scope = core.Scope() var_lod = scope.var("test_lod_tensor") lod_tensor = var_lod.get_tensor() lod_tensor.set_dims([5, 2, 3, 4]) lod_tensor.alloc_float(place) tensor_array = numpy.array(lod_tensor) self.assertEqual((5, 2, 3, 4), tensor_array.shape) tensor_array[0, 0, 0, 0] = 1.0 tensor_array[0, 0, 0, 1] = 2.0 lod_tensor.set(tensor_array, place) lod_v = numpy.array(lod_tensor) self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0]) self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1]) self.assertEqual(len(lod_tensor.lod()), 0) lod_py = [[0, 2, 5], [0, 2, 4, 5]] lod_tensor.set_lod(lod_py) lod = lod_tensor.lod() self.assertListEqual(lod_py, lod) def test_lod_tensor_init(self): scope = core.Scope() place = core.CPUPlace() lod_py = [[0, 2, 5], [0, 2, 4, 5]] lod_tensor = core.LoDTensor() lod_tensor.set_dims([5, 2, 3, 4]) lod_tensor.set_lod(lod_py) lod_tensor.alloc_float(place) tensor_array = numpy.array(lod_tensor) tensor_array[0, 0, 0, 0] = 1.0 tensor_array[0, 0, 0, 1] = 2.0 lod_tensor.set(tensor_array, place) lod_v = numpy.array(lod_tensor) self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0]) self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1]) self.assertListEqual(lod_py, lod_tensor.lod()) def test_lod_tensor_gpu_init(self): if not core.is_compiled_with_cuda(): return place = core.CUDAPlace(0) lod_py = [[0, 2, 5], [0, 2, 4, 5]] lod_tensor = core.LoDTensor() lod_tensor.set_dims([5, 2, 3, 4]) lod_tensor.set_lod(lod_py) lod_tensor.alloc_float(place) tensor_array = numpy.array(lod_tensor) tensor_array[0, 0, 0, 0] = 1.0 tensor_array[0, 0, 0, 1] = 2.0 lod_tensor.set(tensor_array, place) lod_v = numpy.array(lod_tensor) self.assertAlmostEqual(1.0, lod_v[0, 0, 0, 0]) self.assertAlmostEqual(2.0, lod_v[0, 0, 0, 1]) self.assertListEqual(lod_py, lod_tensor.lod()) def test_empty_tensor(self): place = core.CPUPlace() scope = core.Scope() var = scope.var("test_tensor") tensor = var.get_tensor() tensor.set_dims([0, 1]) tensor.alloc_float(place) tensor_array = numpy.array(tensor) self.assertEqual((0, 1), tensor_array.shape) if core.is_compiled_with_cuda(): gpu_place = core.CUDAPlace(0) tensor.alloc_float(gpu_place) tensor_array = numpy.array(tensor) self.assertEqual((0, 1), tensor_array.shape) if __name__ == '__main__': unittest.main()
5,442
31.39881
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_calc_gradient.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle.fluid.layers as layers import paddle.fluid.framework as framework import paddle.fluid.optimizer as optimizer from paddle.fluid.backward import calc_gradient class TestCalcGradient(unittest.TestCase): def test_calc_gradient(self): x = layers.create_parameter(dtype="float32", shape=[5, 10]) y = layers.create_parameter(dtype="float32", shape=[10, 8]) mul_out = layers.mul(x=x, y=y) mean_out = layers.mean(mul_out) a = calc_gradient(mean_out, mul_out) b = calc_gradient(mean_out, x) place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) exe.run(fluid.default_main_program(), feed={}, fetch_list=[a, b]) if __name__ == "__main__": unittest.main()
1,449
35.25
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lstm_unit_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def sigmoid_np(x): return 1. / (1. + np.exp(-x)) def tanh_np(x): return 2 * sigmoid_np(2. * x) - 1. class LstmUnitTest(OpTest): def setUp(self): self.op_type = "lstm_unit" x_np = np.random.normal(size=(5, 16)).astype("float64") c_np = np.random.normal(size=(5, 4)).astype("float64") i_np, f_np, o_np, j_np = np.split(x_np, 4, axis=1) forget_bias_np = 0. self.attrs = {'forget_bias': 0.} new_c = c_np * sigmoid_np(f_np + forget_bias_np) + sigmoid_np( i_np) * tanh_np(j_np) new_h = tanh_np(new_c) * sigmoid_np(o_np) self.inputs = {'X': x_np, 'C_prev': c_np} self.outputs = {'C': new_c, 'H': new_h} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X', 'C_prev'], ['C', 'H']) if __name__ == "__main__": unittest.main()
1,579
28.811321
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_protobuf.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid.proto.framework_pb2 as framework_pb2 import unittest class TestFrameworkProto(unittest.TestCase): def test_all(self): op_proto = framework_pb2.OpProto() ipt0 = op_proto.inputs.add() ipt0.name = "a" ipt0.comment = "the input of cosine op" ipt1 = op_proto.inputs.add() ipt1.name = "b" ipt1.comment = "the other input of cosine op" opt = op_proto.outputs.add() opt.name = "output" opt.comment = "the output of cosine op" op_proto.comment = "cosine op, output = scale*cos(a, b)" attr = op_proto.attrs.add() attr.name = "scale" attr.comment = "scale of cosine op" attr.type = framework_pb2.FLOAT op_proto.type = "cos" self.assertTrue(op_proto.IsInitialized()) if __name__ == "__main__": unittest.main()
1,479
34.238095
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_default_scope_funcs.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from paddle.fluid.default_scope_funcs import * import unittest class TestDefaultScopeFuncs(unittest.TestCase): def test_cur_scope(self): self.assertIsNotNone(get_cur_scope()) def test_none_variable(self): self.assertIsNone(find_var("test")) def test_create_var_get_var(self): var_a = var("var_a") self.assertIsNotNone(var_a) self.assertIsNotNone(get_cur_scope().find_var('var_a')) enter_local_scope() self.assertIsNotNone(get_cur_scope().find_var('var_a')) leave_local_scope() def test_var_get_int(self): def __new_scope__(): i = var("var_i") self.assertFalse(i.is_int()) i.set_int(10) self.assertTrue(i.is_int()) self.assertEqual(10, i.get_int()) for _ in xrange(10): scoped_function(__new_scope__) if __name__ == '__main__': unittest.main()
1,536
31.020833
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_adagrad_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid.core as core from paddle.fluid.op import Operator from op_test import OpTest import math class TestAdagradOp1(OpTest): ''' Test Adagrad operator with explicit attributes ''' def setUp(self): self.op_type = "adagrad" param = np.random.random((123, 321)).astype("float32") grad = np.random.random((123, 321)).astype("float32") moment = np.zeros((123, 321)).astype("float32") lr = 0.01 epsilon = 1e-8 self.inputs = { 'Param': param, 'Grad': grad, 'Moment': moment, 'LearningRate': np.array([lr]).astype("float32") } self.attrs = {'epsilon': epsilon} moment_out = moment + grad * grad param_out = param - lr * grad / (np.sqrt(moment_out) + epsilon) self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out} def test_check_output(self): self.check_output() class TestAdagradOp2(OpTest): ''' Test Adagrad operator with default attributes ''' def setUp(self): self.op_type = "adagrad" param = np.random.random((123, 321)).astype("float32") grad = np.random.random((123, 321)).astype("float32") moment = np.zeros((123, 321)).astype("float32") lr = 0.01 epsilon = 1e-6 self.inputs = { 'Param': param, 'Grad': grad, 'Moment': moment, 'LearningRate': np.array([lr]).astype("float32") } self.attrs = {'epsilon': epsilon} moment_out = moment + grad * grad param_out = param - lr * grad / (np.sqrt(moment_out) + epsilon) self.outputs = {'ParamOut': param_out, 'MomentOut': moment_out} def test_check_output(self): self.check_output() class TestSparseAdagradOp(unittest.TestCase): def check_with_place(self, place): scope = core.Scope() # create and initialize Grad Variable height = 10 rows = [0, 4, 7, 4] row_numel = 12 grad_selected_rows = scope.var('Grad').get_selected_rows() grad_selected_rows.set_height(height) grad_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 grad_tensor = grad_selected_rows.get_tensor() grad_tensor.set(np_array, place) # create and initialize Param Variable param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") param.set(param_array, place) # create and initialize LeraningRate Variable lr = scope.var('LearningRate').get_tensor() lr_array = np.full((1), 2.0).astype("float32") lr.set(lr_array, place) # create and initialize moment Variable moment = scope.var('Moment').get_tensor() moment_np_array = np.full((height, row_numel), 2.0).astype("float32") moment.set(moment_np_array, place) # create and run sgd operator adagrad_op = Operator( "adagrad", Param='Param', Grad='Grad', ParamOut='Param', Moment='Moment', MomentOut='Moment', LearningRate='LearningRate', epsilon=2.0) adagrad_op.run(scope, place) # get and compare moment result moment_result_array = np.array(moment) self.assertAlmostEqual(6.0, moment_result_array[rows[0], 0]) self.assertAlmostEqual(3.0, moment_result_array[rows[0], 2]) self.assertAlmostEqual(2.0, moment_result_array[1, 0]) # 2.0 + (1.0 + 1.0)^2 self.assertAlmostEqual(6.0, moment_result_array[rows[1], 10]) self.assertAlmostEqual(6.0, moment_result_array[rows[3], 4]) self.assertAlmostEqual(2.0, moment_result_array[5, 8]) self.assertAlmostEqual(3.0, moment_result_array[rows[2], 1]) self.assertAlmostEqual(18.0, moment_result_array[rows[2], 8]) # get and compare param result result_array = np.array(param) def get_out(param, lr, grad, m, epsilon): return param - lr * grad / (math.sqrt(m) + epsilon) self.assertAlmostEqual( get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[0], 0], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[0], 2], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[1, 0], places=5) # grad_merge = 1.0 + 1.0 # m = 6.0 self.assertAlmostEqual( get_out(5.0, 2.0, 2.0, 6.0, 2.0), result_array[rows[1], 10], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 0.0, 2.0, 2.0), result_array[5, 8], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 1.0, 3.0, 2.0), result_array[rows[2], 1], places=5) self.assertAlmostEqual( get_out(5.0, 2.0, 4.0, 18.0, 2.0), result_array[rows[2], 8], places=5) def test_sparse_adagrad(self): places = [core.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) for place in places: self.check_with_place(place) if __name__ == "__main__": unittest.main()
6,120
31.04712
77
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_crop_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def crop(data, offsets, crop_shape): def indexOf(shape, index): result = [] for dim in reversed(shape): result.append(index % dim) index = index / dim return result[::-1] result = [] for i, value in enumerate(data.flatten()): index = indexOf(data.shape, i) selected = True if len(index) == len(offsets): for j, offset in enumerate(offsets): selected = selected and index[j] >= offset and index[ j] < crop_shape[j] + offset if selected: result.append(value) return np.array(result).reshape(crop_shape) class TestCropOp(OpTest): def setUp(self): self.op_type = "crop" self.crop_by_input = False self.attrs = {} self.initTestCase() self.attrs['offsets'] = self.offsets if self.crop_by_input: self.inputs = { 'X': np.random.random(self.x_shape).astype("float32"), 'Y': np.random.random(self.crop_shape).astype("float32") } else: self.attrs['shape'] = self.crop_shape self.inputs = { 'X': np.random.random(self.x_shape).astype("float32"), } self.outputs = { 'Out': crop(self.inputs['X'], self.offsets, self.crop_shape) } def initTestCase(self): self.x_shape = (8, 8) self.crop_shape = (2, 2) self.offsets = [1, 2] def test_check_output(self): self.check_output() def test_check_grad_normal(self): self.check_grad(['X'], 'Out', max_relative_error=0.006) class TestCase1(TestCropOp): def initTestCase(self): self.x_shape = (16, 8, 32) self.crop_shape = [2, 2, 3] self.offsets = [1, 5, 3] class TestCase2(TestCropOp): def initTestCase(self): self.x_shape = (4, 8) self.crop_shape = [4, 8] self.offsets = [0, 0] class TestCase3(TestCropOp): def initTestCase(self): self.x_shape = (4, 8, 16) self.crop_shape = [2, 2, 3] self.offsets = [1, 5, 3] self.crop_by_input = True class TestCase4(TestCropOp): def initTestCase(self): self.x_shape = (4, 4) self.crop_shape = [4, 4] self.offsets = [0, 0] self.crop_by_input = True if __name__ == '__main__': unittest.main()
3,089
28.150943
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_exception.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid.core as core import unittest class TestException(unittest.TestCase): def test_exception(self): ex = None try: core.__unittest_throw_exception__() except core.EnforceNotMet as ex: self.assertIn("test exception", ex.message) self.assertIsNotNone(ex) if __name__ == "__main__": unittest.main()
993
30.0625
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/op_test.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import random import itertools import paddle.fluid.core as core import collections from paddle.fluid.backward import append_backward from paddle.fluid.op import Operator from paddle.fluid.executor import Executor from paddle.fluid.framework import Program, OpProtoHolder def randomize_probability(batch_size, class_num, dtype='float32'): prob = np.random.uniform( 0.1, 1.0, size=(batch_size, class_num)).astype(dtype) prob_sum = prob.sum(axis=1) for i in xrange(len(prob)): prob[i] /= prob_sum[i] return prob def create_op(scope, op_type, inputs, outputs, attrs): kwargs = dict() op_maker = core.op_proto_and_checker_maker op_role_attr_name = op_maker.kOpRoleAttrName() if op_role_attr_name not in attrs: attrs[op_role_attr_name] = int(op_maker.OpRole.Forward) def __create_var__(name, var_name): scope.var(var_name).get_tensor() kwargs[name].append(var_name) for in_name, in_dup in Operator.get_op_inputs(op_type): if in_name in inputs: kwargs[in_name] = [] if in_dup: sub_in = inputs[in_name] for item in sub_in: sub_in_name, _ = item[0], item[1] __create_var__(in_name, sub_in_name) else: __create_var__(in_name, in_name) for out_name, out_dup in Operator.get_op_outputs(op_type): if out_name in outputs: kwargs[out_name] = [] if out_dup: sub_out = outputs[out_name] for item in sub_out: sub_out_name, _ = item[0], item[1] __create_var__(out_name, sub_out_name) else: __create_var__(out_name, out_name) for attr_name in Operator.get_op_attr_names(op_type): if attr_name in attrs: kwargs[attr_name] = attrs[attr_name] return Operator(op_type, **kwargs) def set_input(scope, op, inputs, place): def __set_input__(var_name, var): if isinstance(var, tuple) or isinstance(var, np.ndarray): tensor = scope.find_var(var_name).get_tensor() if isinstance(var, tuple): tensor.set_lod(var[1]) var = var[0] tensor.set_dims(var.shape) tensor.set(var, place) elif isinstance(var, float): scope.find_var(var_name).set_float(var) elif isinstance(var, int): scope.find_var(var_name).set_int(var) for in_name, in_dup in Operator.get_op_inputs(op.type()): if in_name in inputs: if in_dup: sub_in = inputs[in_name] for item in sub_in: sub_in_name, sub_in_val = item[0], item[1] __set_input__(sub_in_name, sub_in_val) else: __set_input__(in_name, inputs[in_name]) def get_numeric_gradient(place, scope, op, inputs, input_to_check, output_names, delta=0.005, in_place=False): # FIXME: change this method by compile time concepts set_input(scope, op, inputs, place) def product(dim): return reduce(lambda a, b: a * b, dim, 1) def get_output(): sum = [] for output_name in output_names: op.run(scope, place) sum.append( np.array(scope.find_var(output_name).get_tensor()).mean()) return np.array(sum).mean() tensor_to_check = scope.find_var(input_to_check).get_tensor() tensor_size = product(tensor_to_check.get_dims()) tensor_to_check_dtype = tensor_to_check.dtype() if tensor_to_check_dtype == core.VarDesc.VarType.FP32: tensor_to_check_dtype = np.float32 elif tensor_to_check_dtype == core.VarDesc.VarType.FP64: tensor_to_check_dtype = np.float64 else: raise ValueError("Not supported data type " + str( tensor_to_check_dtype)) gradient_flat = np.zeros(shape=(tensor_size, ), dtype=tensor_to_check_dtype) def __get_elem__(tensor, i): if tensor_to_check_dtype == np.float32: return tensor.get_float_element(i) else: return tensor.get_double_element(i) def __set_elem__(tensor, i, e): if tensor_to_check_dtype == np.float32: tensor.set_float_element(i, e) else: tensor.set_double_element(i, e) # we only compute gradient of one element each time. # we use a for loop to compute the gradient of every element. for i in xrange(tensor_size): if in_place: set_input(scope, op, inputs, place) # get one input element throw it's index i. origin = __get_elem__(tensor_to_check, i) # add delta to it, run op and then get the sum of the result tensor. x_pos = origin + delta __set_elem__(tensor_to_check, i, x_pos) y_pos = get_output() if in_place: set_input(scope, op, inputs, place) x_neg = origin - delta __set_elem__(tensor_to_check, i, x_neg) y_neg = get_output() __set_elem__(tensor_to_check, i, origin) gradient_flat[i] = (y_pos - y_neg) / delta / 2 return gradient_flat.reshape(tensor_to_check.get_dims()) def append_input_output(block, op_proto, np_list, is_input): '''Insert VarDesc and generate Python variable instance''' proto_list = op_proto.inputs if is_input else op_proto.outputs def create_var(block, name, np_list, var_proto): if name not in np_list: assert var_proto.intermediate, "{} not found".format(name) shape = None lod_level = None else: np_value = np_list[name] if isinstance(np_value, tuple): shape = list(np_value[0].shape) lod_level = len(np_value[1]) else: shape = list(np_value.shape) lod_level = 0 return block.create_var( dtype="float32", shape=shape, lod_level=lod_level, name=name) var_dict = {} for var_proto in proto_list: var_name = str(var_proto.name) if is_input: if (var_name not in np_list) and var_proto.dispensable: continue assert (var_name in np_list) or (var_proto.dispensable), \ "Missing {} as input".format(var_name) if var_proto.duplicable: assert isinstance(np_list[var_name], list), \ "Duplicable {} should be set as list".format(var_name) var_list = [] for (name, np_value) in np_list[var_name]: var_list.append( create_var(block, name, {name: np_value}, var_proto)) var_dict[var_name] = var_list else: var_dict[var_name] = create_var(block, var_name, np_list, var_proto) return var_dict class OpTest(unittest.TestCase): @classmethod def setUpClass(cls): '''Fix random seeds to remove randomness from tests''' cls._np_rand_state = np.random.get_state() cls._py_rand_state = random.getstate() np.random.seed(123) random.seed(124) @classmethod def tearDownClass(cls): '''Restore random seeds''' np.random.set_state(cls._np_rand_state) random.setstate(cls._py_rand_state) def feed_var(self, input_vars, place): feed_map = {} for var_name in input_vars: if isinstance(input_vars[var_name], list): for name, np_value in self.inputs[var_name]: tensor = core.LoDTensor() if isinstance(np_value, tuple): tensor.set(np_value[0], place) tensor.set_lod(np_value[1]) else: tensor.set(np_value, place) feed_map[name] = tensor else: tensor = core.LoDTensor() if isinstance(self.inputs[var_name], tuple): tensor.set(self.inputs[var_name][0], place) tensor.set_lod(self.inputs[var_name][1]) else: tensor.set(self.inputs[var_name], place) feed_map[var_name] = tensor return feed_map def calc_output(self, place): outs, _ = self._calc_output(place) return outs def _calc_output(self, place): op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) program = Program() block = program.global_block() inputs = append_input_output(block, op_proto, self.inputs, True) outputs = append_input_output(block, op_proto, self.outputs, False) op = block.append_op( type=self.op_type, inputs=inputs, outputs=outputs, attrs=self.attrs if hasattr(self, "attrs") else dict()) # infer variable type and infer shape in compile-time op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) fetch_list = [] for var_name, var in outputs.iteritems(): if var_name in self.outputs: if isinstance(var, list): for v in var: fetch_list.append(v) else: fetch_list.append(var) feed_map = self.feed_var(inputs, place) exe = Executor(place) outs = exe.run(program, feed=feed_map, fetch_list=fetch_list, return_numpy=False) return outs, fetch_list def check_output_with_place(self, place, atol): outs, fetch_list = self._calc_output(place) for out_name, out_dup in Operator.get_op_outputs(self.op_type): if out_name not in self.outputs: continue def find_actual(target_name, fetch_list): found = [ i for i, var in enumerate(fetch_list) if var.name == target_name ] self.assertTrue( len(found) == 1, "Found {} {}".format( len(found), target_name)) return found[0] if out_dup: sub_out = self.outputs[out_name] if not isinstance(sub_out, list): raise AssertionError("sub_out type %s is not list", type(sub_out)) for item in sub_out: sub_out_name, expect = item[0], item[1] idx = find_actual(sub_out_name, fetch_list) actual = outs[idx] actual_t = np.array(actual) expect_t = expect[0] \ if isinstance(expect, tuple) else expect self.assertTrue( np.allclose( actual_t, expect_t, atol=atol), "Output (" + sub_out_name + ") has diff at " + str(place)) if isinstance(expect, tuple): self.assertListEqual( actual.lod(), expect[1], "Output (" + sub_out_name + ") has different lod at " + str(place)) else: idx = find_actual(out_name, fetch_list) actual = outs[idx] actual_t = np.array(actual) expect = self.outputs[out_name] expect_t = expect[0] if isinstance(expect, tuple) else expect self.assertTrue( np.allclose( actual_t, expect_t, atol=atol), "Output (" + out_name + ") has diff at " + str(place) + str(actual_t) + "\n" + str(expect_t)) if isinstance(expect, tuple): self.assertListEqual(actual.lod(), expect[1], "Output (" + out_name + ") has different lod at " + str(place)) def check_output(self, atol=1e-5): places = [core.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu(self.op_type): places.append(core.CUDAPlace(0)) for place in places: self.check_output_with_place(place, atol) def check_output_customized(self, checker): places = [core.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu(self.op_type): places.append(core.CUDAPlace(0)) for place in places: outs = self.calc_output(place) outs = [np.array(out) for out in outs] checker(outs) def __assert_is_close(self, numeric_grads, analytic_grads, names, max_relative_error, msg_prefix): for a, b, name in itertools.izip(numeric_grads, analytic_grads, names): abs_a = np.abs(a) abs_a[abs_a < 1e-3] = 1 diff_mat = np.abs(a - b) / abs_a max_diff = np.max(diff_mat) def err_msg(): offset = np.argmax(diff_mat > max_relative_error) return ("%s Variable %s max gradient diff %f over limit %f, " "the first error element is %d, %f, %f") % ( msg_prefix, name, max_diff, max_relative_error, offset, a.flatten()[offset], b.flatten()[offset]) self.assertLessEqual(max_diff, max_relative_error, err_msg()) def check_grad(self, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None): places = [core.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu(self.op_type): places.append(core.CUDAPlace(0)) for place in places: self.check_grad_with_place(place, inputs_to_check, output_names, no_grad_set, numeric_grad_delta, in_place, max_relative_error, user_defined_grads) def check_grad_with_place(self, place, inputs_to_check, output_names, no_grad_set=None, numeric_grad_delta=0.005, in_place=False, max_relative_error=0.005, user_defined_grads=None): self.scope = core.Scope() op_inputs = self.inputs if hasattr(self, "inputs") else dict() op_outputs = self.outputs if hasattr(self, "outputs") else dict() op_attrs = self.attrs if hasattr(self, "attrs") else dict() self.op = create_op(self.scope, self.op_type, op_inputs, op_outputs, op_attrs) if no_grad_set is None: no_grad_set = set() if not type(output_names) is list: output_names = [output_names] numeric_grads = user_defined_grads or [ get_numeric_gradient( place, self.scope, self.op, self.inputs, input_to_check, output_names, delta=numeric_grad_delta, in_place=in_place) for input_to_check in inputs_to_check ] analytic_grads = self._get_gradient(inputs_to_check, place, output_names, no_grad_set) self.__assert_is_close(numeric_grads, analytic_grads, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place)) @staticmethod def _create_var_descs_(block, var_dict): # FIXME: Try unify with `append_input_output` for param_name in var_dict: var = var_dict[param_name] if not isinstance(var, list) and not isinstance(var, tuple): var = [(param_name, var, None)] if not isinstance(var[0], list) and not isinstance(var[0], tuple): var = [(param_name, var[0], var[1])] for i, item in enumerate(var): if not isinstance(item[0], basestring): item = [[param_name] + list(item)] if len(item) == 2: if isinstance(item[1], tuple): var[i] = [item[0], item[1][0], item[1][1]] else: # only set var name and value, set lod to None var[i] = list(item) + [None] var_descs = [(block.create_var( name=name, shape=each.shape, dtype=each.dtype), each, lod) for name, each, lod in var] yield param_name, var_descs @staticmethod def _merge_list(iterable): return reduce(lambda a, b: list(a) + list(b), iterable, []) @staticmethod def _numpy_to_lod_tensor(np_value, lod, place): tensor = core.LoDTensor() tensor.set(np_value, place) if lod is not None: tensor.set_lod(lod) return tensor @staticmethod def np_dtype_to_fluid_dtype(input): """Change the dtype of float16 numpy array numpy float16 is binded to paddle::platform::float16 in tensor_py.h via the help of uint16 data type since the internal memory representation of float16 is uint16_t in paddle and np.uint16 in numpy, which are themselves binded together by pybind. Args: input: input numpy array Returns: input: The dtype of input will be changed to np.uint16 if it is originally np.float16, such that the internal memory of input will be reinterpreted as of dtype np.uint16. """ if input.dtype == np.float16: input.dtype = np.uint16 return input def _get_gradient(self, input_to_check, place, output_names, no_grad_set): prog = Program() block = prog.global_block() inputs_with_np = { key: value for (key, value) in OpTest._create_var_descs_( block, getattr(self, 'inputs', {})) } outputs_with_np = { key: val for (key, val) in OpTest._create_var_descs_( block, getattr(self, 'outputs', {})) } inputs = { k: [item[0] for item in inputs_with_np[k]] for k in inputs_with_np } outputs = { k: [item[0] for item in outputs_with_np[k]] for k in outputs_with_np } op = block.append_op( type=self.op_type, inputs=inputs, outputs=outputs, attrs=getattr(self, 'attrs', {})) # infer variable type and infer shape in compile-time op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) mean_inputs = map(block.var, output_names) if len(mean_inputs) == 1: loss = block.create_var(dtype=mean_inputs[0].dtype, shape=[1]) op = block.append_op( inputs={"X": mean_inputs}, outputs={"Out": loss}, type='mean') op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) else: avg_sum = [] for cur_loss in mean_inputs: cur_avg_loss = block.create_var(dtype=cur_loss.dtype, shape=[1]) op = block.append_op( inputs={"X": [cur_loss]}, outputs={"Out": [cur_avg_loss]}, type="mean") op.desc.infer_var_type(block.desc) op.desc.infer_shape(block.desc) avg_sum.append(cur_avg_loss) loss_sum = block.create_var(dtype=avg_sum[0].dtype, shape=[1]) op_sum = block.append_op( inputs={"X": avg_sum}, outputs={"Out": loss_sum}, type='sum') op_sum.desc.infer_var_type(block.desc) op_sum.desc.infer_shape(block.desc) loss = block.create_var(dtype=loss_sum.dtype, shape=[1]) op_loss = block.append_op( inputs={"X": loss_sum}, outputs={"Out": loss}, type='scale', attrs={'scale': 1.0 / float(len(avg_sum))}) op_loss.desc.infer_var_type(block.desc) op_loss.desc.infer_shape(block.desc) param_grad_list = append_backward( loss=loss, parameter_list=input_to_check, no_grad_set=no_grad_set) feed_dict = { item[0].name: OpTest._numpy_to_lod_tensor(item[1], item[2], place) for p_name in inputs_with_np for item in inputs_with_np[p_name] } fetch_list = [g for p, g in param_grad_list] executor = Executor(place) return map(np.array, executor.run(prog, feed_dict, fetch_list, return_numpy=False))
22,156
37.201724
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_dynrnn_gradient_check.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy import random import collections import paddle.fluid as fluid import unittest from decorators import * class Memory(object): def __init__(self, shape, dtype='float32'): self.ex = numpy.zeros(shape=shape, dtype=dtype) self.cur = None def update(self, val): assert val.shape == self.ex.shape assert val.dtype == self.ex.dtype self.cur = val def ex(self): return self.ex def next(self): self.ex = self.cur self.cur = None def __next__(self): self.next() def reset(self): self.ex = numpy.zeros(shape=self.ex.shape, dtype=self.ex.dtype) self.cur = None class Output(object): def __init__(self): self.outs = [] def next_sequence(self): self.outs.append([]) def out(self, val): self.outs[-1].append(val) def last(self): return self.outs[-1][-1] class BaseRNN(object): def __init__(self, ins, mems, params, outs, num_seq=5, max_seq_len=15): self.num_seq = num_seq self.inputs = collections.defaultdict(list) for _ in xrange(num_seq): seq_len = random.randint(1, max_seq_len - 1) for iname in ins: ishape = ins[iname].get('shape', None) idtype = ins[iname].get('dtype', 'float32') lst = [] for _ in xrange(seq_len): lst.append(numpy.random.random(size=ishape).astype(idtype)) self.inputs[iname].append(lst) self.mems = dict() for mname in mems: mshape = mems[mname].get('shape', None) mdtype = mems[mname].get('dtype', 'float32') self.mems[mname] = Memory(shape=mshape, dtype=mdtype) self.params = dict() for pname in params: pshape = params[pname].get('shape', None) pdtype = params[pname].get('dtype', 'float32') self.params[pname] = numpy.random.random(size=pshape).astype(pdtype) self.outputs = dict() for oname in outs: self.outputs[oname] = Output() def step(self, **kwargs): raise NotImplementedError() def exe(self): retv = dict() for out in self.outputs: retv[out] = [] for seq_id in xrange(self.num_seq): for mname in self.mems: self.mems[mname].reset() for out in self.outputs: self.outputs[out].next_sequence() iname0 = self.inputs.keys()[0] seq_len = len(self.inputs[iname0][seq_id]) for step_id in xrange(seq_len): xargs = dict() for iname in self.inputs: xargs[iname] = self.inputs[iname][seq_id][step_id] for mname in self.mems: xargs[mname] = self.mems[mname] for pname in self.params: xargs[pname] = self.params[pname] for out in self.outputs: xargs[out] = self.outputs[out] self.step(**xargs) for mname in self.mems: next(self.mems[mname]) for out in self.outputs: retv[out].append(self.outputs[out].last()) for out in retv: retv[out] = numpy.array(retv[out]) return retv def to_feed(self, place): feed_dict = dict() for iname in self.inputs: lod = [0] np_flatten = [] for seq_id in xrange(len(self.inputs[iname])): seq_len = len(self.inputs[iname][seq_id]) lod.append(lod[-1] + seq_len) np_flatten.extend(self.inputs[iname][seq_id]) t = fluid.Tensor() t.set(numpy.array(np_flatten), place) t.set_lod([lod]) feed_dict[iname] = t for pname in self.params: feed_dict[pname] = self.params[pname] return feed_dict def get_numeric_gradient_of_param(self, param_name, delta=0.001): p = self.params[param_name] if len(p.shape) != 2: raise ValueError("Not support get numeric gradient of an parameter," " which is not matrix") g = numpy.zeros(shape=p.shape, dtype=p.dtype) for i in xrange(p.shape[0]): for j in xrange(p.shape[1]): o = p[i][j] p[i][j] += delta pos = self._exe_mean_out_() p[i][j] -= 2 * delta neg = self._exe_mean_out_() p[i][j] = o g[i][j] = (pos - neg) / (delta * 2) return g def get_numeric_gradient_of_input(self, input_name, delta=0.001, return_one_tensor=True): ipt = self.inputs[input_name] grad = [] for seq in ipt: seq_grad = [] for item in seq: item_grad = numpy.zeros(shape=item.shape, dtype=item.dtype) if len(item.shape) != 1: raise ValueError("Not support") for i in xrange(len(item)): o = item[i] item[i] += delta pos = self._exe_mean_out_() item[i] -= 2 * delta neg = self._exe_mean_out_() item[i] = o item_grad[i] = (pos - neg) / (delta * 2) seq_grad.append(item_grad) grad.append(seq_grad) if not return_one_tensor: return grad for i in xrange(len(grad)): grad[i] = numpy.concatenate(grad[i]) grad = numpy.concatenate(grad) return grad def _exe_mean_out_(self): outs = self.exe() return numpy.array([o.mean() for o in outs.itervalues()]).mean() class SeedFixedTestCase(unittest.TestCase): @classmethod def setUpClass(cls): """Fix random seeds to remove randomness from tests""" cls._np_rand_state = numpy.random.get_state() cls._py_rand_state = random.getstate() numpy.random.seed(123) random.seed(124) @classmethod def tearDownClass(cls): """Restore random seeds""" numpy.random.set_state(cls._np_rand_state) random.setstate(cls._py_rand_state) class TestSimpleMul(SeedFixedTestCase): DATA_NAME = 'X' DATA_WIDTH = 32 PARAM_NAME = 'W' HIDDEN_WIDTH = 10 OUT_NAME = 'Out' class SimpleMul(BaseRNN): def __init__(self): base = TestSimpleMul super(base.SimpleMul, self).__init__({ base.DATA_NAME: { 'shape': [base.DATA_WIDTH] } }, {}, { base.PARAM_NAME: { 'shape': [base.DATA_WIDTH, base.HIDDEN_WIDTH] } }, [base.OUT_NAME]) def step(self, X, W, Out): Out.out(numpy.matmul(X, W)) # Test many times in local to ensure the random seed cannot breaks CI # @many_times(10) @prog_scope() def test_forward_backward(self): py_rnn = TestSimpleMul.SimpleMul() dat = fluid.layers.data( name=self.DATA_NAME, shape=[self.DATA_WIDTH], lod_level=1) dat.stop_gradient = False rnn = fluid.layers.DynamicRNN() with rnn.block(): d = rnn.step_input(dat) o = fluid.layers.fc(input=d, param_attr=self.PARAM_NAME, bias_attr=False, size=self.HIDDEN_WIDTH, act=None) rnn.output(o) out = rnn() out = fluid.layers.sequence_pool(out, pool_type='last') loss = fluid.layers.mean(out) fluid.backward.append_backward(loss) cpu = fluid.CPUPlace() exe = fluid.Executor(cpu) out, w_g, i_g = map(numpy.array, exe.run(feed=py_rnn.to_feed(cpu), fetch_list=[ out, self.PARAM_NAME + "@GRAD", self.DATA_NAME + "@GRAD" ], return_numpy=False)) out_by_python = py_rnn.exe()[self.OUT_NAME] self.assertTrue(numpy.allclose(out, out_by_python)) w_g_num = py_rnn.get_numeric_gradient_of_param(self.PARAM_NAME) self.assertTrue(numpy.allclose(w_g_num, w_g, rtol=0.05)) i_g_num = py_rnn.get_numeric_gradient_of_input( input_name=self.DATA_NAME) i_g_num = i_g_num.reshape(i_g.shape) self.assertTrue(numpy.allclose(i_g_num, i_g, rtol=0.05)) class TestSimpleMulWithMemory(SeedFixedTestCase): DATA_WIDTH = 32 HIDDEN_WIDTH = 20 DATA_NAME = 'X' PARAM_NAME = 'W' class SimpleMulWithMemory(BaseRNN): def __init__(self): super(TestSimpleMulWithMemory.SimpleMulWithMemory, self).__init__({ TestSimpleMulWithMemory.DATA_NAME: { 'shape': [TestSimpleMulWithMemory.DATA_WIDTH] } }, {'Mem': { 'shape': [TestSimpleMulWithMemory.HIDDEN_WIDTH] }}, { TestSimpleMulWithMemory.PARAM_NAME: { 'shape': [ TestSimpleMulWithMemory.DATA_WIDTH, TestSimpleMulWithMemory.HIDDEN_WIDTH ] } }, ['Out']) def step(self, X, Mem, W, Out): o = numpy.matmul(X, W) assert isinstance(Mem, Memory) o += Mem.ex Mem.update(o) assert isinstance(Out, Output) Out.out(o) # many_times used locally for debug. Make sure the calculation is stable. # @many_times(10) @prog_scope() def test_forward_backward(self): py_rnn = TestSimpleMulWithMemory.SimpleMulWithMemory() data = fluid.layers.data( name=self.DATA_NAME, shape=[self.DATA_WIDTH], lod_level=1) data.stop_gradient = False rnn = fluid.layers.DynamicRNN() with rnn.block(): d = rnn.step_input(data) mem = rnn.memory(value=0.0, shape=[self.HIDDEN_WIDTH]) hidden = fluid.layers.fc(input=d, size=self.HIDDEN_WIDTH, param_attr=self.PARAM_NAME, bias_attr=False, act=None) o = fluid.layers.elementwise_add(x=hidden, y=mem) rnn.update_memory(mem, o) rnn.output(o) out = rnn() last = fluid.layers.sequence_pool(input=out, pool_type='last') loss = fluid.layers.mean(last) fluid.backward.append_backward(loss) cpu = fluid.CPUPlace() exe = fluid.Executor(cpu) feed = py_rnn.to_feed(cpu) last_np, w_g, i_g = map(numpy.array, exe.run(feed=feed, fetch_list=[ last, self.PARAM_NAME + "@GRAD", self.DATA_NAME + "@GRAD" ], return_numpy=False)) last_by_py, = py_rnn.exe().values() w_g_num = py_rnn.get_numeric_gradient_of_param(self.PARAM_NAME) self.assertTrue(numpy.allclose(last_np, last_by_py)) self.assertTrue(numpy.allclose(w_g_num, w_g, rtol=0.1)) i_g_num = py_rnn.get_numeric_gradient_of_input(self.DATA_NAME) i_g_num = i_g_num.reshape(i_g.shape) # Since this RNN has many float add. The number could be not stable. # rtol = 0.1 self.assertTrue(numpy.allclose(i_g_num, i_g, rtol=0.1)) if __name__ == '__main__': unittest.main()
12,685
32.472296
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_proximal_gd_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestProximalGDOp(OpTest): def setUp(self): self.op_type = "proximal_gd" w = np.random.random((102, 105)).astype("float32") g = np.random.random((102, 105)).astype("float32") lr = np.array([0.1]).astype("float32") l1 = 0.1 l2 = 0.2 self.inputs = {'Param': w, 'Grad': g, 'LearningRate': lr} self.attrs = {'l1': l1, 'l2': l2} prox_param = w - lr * g param_out = 0.0 if l1 > 0.0: x = np.abs(prox_param) - lr * l1 x[x < 0] = 0 param_out = np.sign(prox_param) * (x / (1.0 + lr * l2)) else: param_out = prox_param / (1.0 + lr * l2) self.outputs = {'ParamOut': param_out} def test_check_output(self): self.check_output() if __name__ == "__main__": unittest.main()
1,516
30.604167
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_selected_rows.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid.core as core import unittest import numpy as np class TestSelectedRows(unittest.TestCase): def test_selected_rows(self): place = core.CPUPlace() height = 10 rows = [0, 4, 7] row_numel = 12 selected_rows = core.SelectedRows(rows, height) np_array = np.ones((len(rows), row_numel)).astype("float32") np_array[0, 0] = 2.0 np_array[2, 8] = 4.0 tensor = selected_rows.get_tensor() tensor.set(np_array, place) # compare rows self.assertEqual(0, selected_rows.rows()[0]) self.assertEqual(4, selected_rows.rows()[1]) self.assertEqual(7, selected_rows.rows()[2]) # compare height self.assertEqual(10, selected_rows.height()) # compare tensor self.assertAlmostEqual(2.0, selected_rows.get_tensor().get_float_element(0)) self.assertAlmostEqual(1.0, selected_rows.get_tensor().get_float_element(1)) self.assertAlmostEqual( 4.0, selected_rows.get_tensor().get_float_element(2 * row_numel + 8)) if __name__ == "__main__": unittest.main()
1,818
33.320755
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_lookup_table_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest import paddle.fluid.core as core from paddle.fluid.op import Operator class TestLookupTableOp(OpTest): def setUp(self): self.op_type = "lookup_table" table = np.random.random((17, 31)).astype("float32") ids = np.random.randint(0, 17, 4).astype("int64") ids_expand = np.expand_dims(ids, axis=1) self.inputs = {'W': table, 'Ids': ids_expand} self.outputs = {'Out': table[ids]} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['W'], 'Out', no_grad_set=set('Ids')) class TestLookupTableOpWithPadding(TestLookupTableOp): def test_check_output(self): ids = np.squeeze(self.inputs['Ids']) padding_idx = np.random.choice(ids, 1)[0] self.outputs['Out'][ids == padding_idx] = np.zeros(31) self.attrs = {'padding_idx': long(padding_idx)} self.check_output() def test_check_grad(self): # Since paddings are not trainable and fixed in forward, the gradient of # paddings makes no sense and we don't test the gradient here. pass class TestLookupTableIdsIsSelectedRows(OpTest): def check_with_place(self, place): scope = core.Scope() # create and initialize Variable height = 10 rows = [0, 4, 4, 7] row_numel = 12 # create and initialize W Variable W = scope.var('W').get_tensor() W_array = np.full((height, row_numel), 1.0).astype("float32") for i in range(height): W_array[i] *= i W.set(W_array, place) # create and initialize Ids Variable ids_selected_rows = scope.var('Ids').get_selected_rows() ids_selected_rows.set_height(len(rows)) ids_selected_rows.set_rows(rows) np_array = np.ones((len(rows), row_numel)).astype("float32") ids_tensor = ids_selected_rows.get_tensor() ids_tensor.set(np_array, place) # create Out Variable Out = scope.var('Out').get_selected_rows() # create and run lookup_table operator concat_rows_op = Operator("lookup_table", W='W', Ids='Ids', Out='Out') concat_rows_op.run(scope, place) # get result from Out Out_tensor = Out.get_tensor() result_array = np.array(Out_tensor) # all(): return True if all elements of the iterable are true (or if the iterable is empty) for idx, row in enumerate(rows): assert (row == result_array[idx]).all() def test_concat_rows(self): places = [core.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) for place in places: self.check_with_place(place) class TestLookupTableWIsSelectedRows(OpTest): def check_with_place(self, place): scope = core.Scope() # create and initialize Id Variable ids_tensor = scope.var('Ids').get_tensor() ids_array = np.array([[0], [4], [3], [5]]).astype("int64") ids_tensor.set(ids_array, place) # create and initialize W Variable rows = [0, 1, 2, 3, 4, 5, 6] row_numel = 12 w_selected_rows = scope.var('W').get_selected_rows() w_selected_rows.set_height(len(rows)) w_selected_rows.set_rows(rows) w_array = np.ones((len(rows), row_numel)).astype("float32") for i in range(len(rows)): w_array[i] *= i w_tensor = w_selected_rows.get_tensor() w_tensor.set(w_array, place) # create Out Variable out_tensor = scope.var('Out').get_tensor() # create and run lookup_table operator lookup_table = Operator("lookup_table", W='W', Ids='Ids', Out='Out') lookup_table.run(scope, place) # get result from Out result_array = np.array(out_tensor) # all(): return True if all elements of the iterable are true (or if the iterable is empty) for idx, row in enumerate(ids_array): assert (row[0] == result_array[idx]).all() def test_w_is_selected_rows(self): places = [core.CPUPlace()] # currently only support CPU for place in places: self.check_with_place(place) if __name__ == "__main__": unittest.main()
4,947
33.601399
99
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_polygon_box_transform.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def PolygonBoxRestore(input): shape = input.shape batch_size = shape[0] geo_channels = shape[1] h = shape[2] w = shape[3] h_indexes = np.array(range(h) * w).reshape( [w, h]).transpose()[np.newaxis, :] # [1, h, w] w_indexes = np.array(range(w) * h).reshape( [h, w])[np.newaxis, :] # [1, h, w] indexes = np.concatenate( (w_indexes, h_indexes))[np.newaxis, :] # [1, 2, h, w] indexes = indexes.repeat( [geo_channels / 2], axis=0)[np.newaxis, :] # [1, geo_channels/2, 2, h, w] indexes = indexes.repeat( [batch_size], axis=0) # [batch_size, geo_channels/2, 2, h, w] return indexes.reshape( input.shape) - input # [batch_size, geo_channels, h, w] class TestPolygonBoxRestoreOp(OpTest): def config(self): self.input_shape = (1, 8, 2, 2) def setUp(self): self.config() self.op_type = "polygon_box_transform" input = np.random.random(self.input_shape).astype("float32") self.inputs = {'Input': input} output = PolygonBoxRestore(input) self.outputs = {'Output': output} def test_check_output(self): self.check_output() class TestCase1(TestPolygonBoxRestoreOp): def config(self): self.input_shape = (2, 10, 3, 2) class TestCase2(TestPolygonBoxRestoreOp): def config(self): self.input_shape = (3, 12, 4, 5) if __name__ == '__main__': unittest.main()
2,134
29.942029
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_parallel_executor_transformer.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid as fluid import transformer_model import numpy as np from parallel_executor_test_base import TestParallelExecutorBase import unittest import paddle import paddle.dataset.wmt16 as wmt16 WMT16_RECORDIO_FILE = "./wmt16_test_pe.recordio" class ModelHyperParams(object): # Dictionary size for source and target language. This model directly uses # paddle.dataset.wmt16 in which <bos>, <eos> and <unk> token has # alreay been added, but the <pad> token is not added. Transformer requires # sequences in a mini-batch are padded to have the same length. A <pad> token is # added into the original dictionary in paddle.dateset.wmt16. # size of source word dictionary. src_vocab_size = 10000 # index for <pad> token in source language. src_pad_idx = src_vocab_size # size of target word dictionay trg_vocab_size = 10000 # index for <pad> token in target language. trg_pad_idx = trg_vocab_size # position value corresponding to the <pad> token. pos_pad_idx = 0 # max length of sequences. It should plus 1 to include position # padding token for position encoding. max_length = 50 # the dimension for word embeddings, which is also the last dimension of # the input and output of multi-head attention, position-wise feed-forward # networks, encoder and decoder. d_model = 512 # size of the hidden layer in position-wise feed-forward networks. d_inner_hid = 1024 # the dimension that keys are projected to for dot-product attention. d_key = 64 # the dimension that values are projected to for dot-product attention. d_value = 64 # number of head used in multi-head attention. n_head = 8 # number of sub-layers to be stacked in the encoder and decoder. n_layer = 6 # dropout rate used by all dropout layers. dropout = 0.1 def prepare_batch_input(insts, src_pad_idx, trg_pad_idx, n_head): """ Pad the instances to the max sequence length in batch, and generate the corresponding position data and attention bias. Then, convert the numpy data to tensors and return a dict mapping names to tensors. """ def __pad_batch_data(insts, pad_idx, is_target=False, return_pos=True, return_attn_bias=True, return_max_len=True): """ Pad the instances to the max sequence length in batch, and generate the corresponding position data and attention bias. """ return_list = [] max_len = max(len(inst) for inst in insts) inst_data = np.array( [inst + [pad_idx] * (max_len - len(inst)) for inst in insts]) return_list += [inst_data.astype("int64").reshape([-1, 1])] if return_pos: inst_pos = np.array([[ pos_i + 1 if w_i != pad_idx else 0 for pos_i, w_i in enumerate(inst) ] for inst in inst_data]) return_list += [inst_pos.astype("int64").reshape([-1, 1])] if return_attn_bias: if is_target: # This is used to avoid attention on paddings and subsequent # words. slf_attn_bias_data = np.ones((inst_data.shape[0], max_len, max_len)) slf_attn_bias_data = np.triu(slf_attn_bias_data, 1).reshape( [-1, 1, max_len, max_len]) slf_attn_bias_data = np.tile(slf_attn_bias_data, [1, n_head, 1, 1]) * [-1e9] else: # This is used to avoid attention on paddings. slf_attn_bias_data = np.array([[0] * len(inst) + [-1e9] * (max_len - len(inst)) for inst in insts]) slf_attn_bias_data = np.tile( slf_attn_bias_data.reshape([-1, 1, 1, max_len]), [1, n_head, max_len, 1]) return_list += [slf_attn_bias_data.astype("float32")] if return_max_len: return_list += [max_len] return return_list if len(return_list) > 1 else return_list[0] src_word, src_pos, src_slf_attn_bias, src_max_len = __pad_batch_data( [inst[0] for inst in insts], src_pad_idx, is_target=False) trg_word, trg_pos, trg_slf_attn_bias, trg_max_len = __pad_batch_data( [inst[1] for inst in insts], trg_pad_idx, is_target=True) trg_src_attn_bias = np.tile(src_slf_attn_bias[:, :, ::src_max_len, :], [1, 1, trg_max_len, 1]).astype("float32") lbl_word = __pad_batch_data([inst[2] for inst in insts], trg_pad_idx, False, False, False, False) lbl_weight = (lbl_word != trg_pad_idx).astype("float32").reshape([-1, 1]) return [ src_word, src_pos, trg_word, trg_pos, src_slf_attn_bias, trg_slf_attn_bias, trg_src_attn_bias, lbl_word, lbl_weight ] def transformer(use_feed): assert not use_feed, "transfomer doesn't support feed yet" return transformer_model.transformer( ModelHyperParams.src_vocab_size + 1, ModelHyperParams.trg_vocab_size + 1, ModelHyperParams.max_length + 1, ModelHyperParams.n_layer, ModelHyperParams.n_head, ModelHyperParams.d_key, ModelHyperParams.d_value, ModelHyperParams.d_model, ModelHyperParams.d_inner_hid, ModelHyperParams.dropout, ModelHyperParams.src_pad_idx, ModelHyperParams.trg_pad_idx, ModelHyperParams.pos_pad_idx) class TestTransformer(TestParallelExecutorBase): @classmethod def setUpClass(cls): reader = paddle.batch( wmt16.train(ModelHyperParams.src_vocab_size, ModelHyperParams.trg_vocab_size), batch_size=transformer_model.batch_size) with fluid.recordio_writer.create_recordio_writer( WMT16_RECORDIO_FILE) as writer: for batch in reader(): for tensor in prepare_batch_input( batch, ModelHyperParams.src_pad_idx, ModelHyperParams.trg_pad_idx, ModelHyperParams.n_head): t = fluid.LoDTensor() t.set(tensor, fluid.CPUPlace()) writer.append_tensor(t) writer.complete_append_tensor() @unittest.skip("transformer is buggy in multi gpu") def test_main(self): self.check_network_convergence(transformer) if __name__ == '__main__': unittest.main()
7,270
40.548571
84
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_op_support_gpu.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core class TestOpSupportGPU(unittest.TestCase): def test_case(self): self.assertEqual(core.is_compiled_with_cuda(), core.op_support_gpu("sum")) if __name__ == '__main__': unittest.main()
889
31.962963
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_multi_file_reader.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle import paddle.dataset.mnist as mnist from shutil import copyfile class TestMultipleReader(unittest.TestCase): def setUp(self): self.batch_size = 64 # Convert mnist to recordio file with fluid.program_guard(fluid.Program(), fluid.Program()): reader = paddle.batch(mnist.train(), batch_size=self.batch_size) feeder = fluid.DataFeeder( feed_list=[ # order is image and label fluid.layers.data( name='image', shape=[784]), fluid.layers.data( name='label', shape=[1], dtype='int64'), ], place=fluid.CPUPlace()) self.num_batch = fluid.recordio_writer.convert_reader_to_recordio_file( './mnist_0.recordio', reader, feeder) copyfile('./mnist_0.recordio', './mnist_1.recordio') copyfile('./mnist_0.recordio', './mnist_2.recordio') def main(self, thread_num): file_list = [ './mnist_0.recordio', './mnist_1.recordio', './mnist_2.recordio' ] with fluid.program_guard(fluid.Program(), fluid.Program()): data_files = fluid.layers.open_files( filenames=file_list, thread_num=thread_num, shapes=[(-1, 784), (-1, 1)], lod_levels=[0, 0], dtypes=['float32', 'int64']) img, label = fluid.layers.read_file(data_files) if fluid.core.is_compiled_with_cuda(): place = fluid.CUDAPlace(0) else: place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) batch_count = 0 while True: try: img_val, = exe.run(fetch_list=[img]) except fluid.core.EnforceNotMet as ex: self.assertIn("There is no next data.", ex.message) break batch_count += 1 self.assertLessEqual(img_val.shape[0], self.batch_size) self.assertEqual(batch_count, self.num_batch * 3) def test_main(self): self.main(thread_num=3) # thread number equals to file number self.main(thread_num=10) # thread number is larger than file number self.main(thread_num=2) # thread number is less than file number
3,098
38.730769
83
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_iou_similarity_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import numpy.random as random import sys import math from op_test import OpTest class TestIOUSimilarityOp(OpTest): def test_check_output(self): self.check_output() def setUp(self): self.op_type = "iou_similarity" self.boxes1 = random.rand(2, 4).astype('float32') self.boxes2 = random.rand(3, 4).astype('float32') self.output = random.rand(2, 3).astype('float32') for row in range(self.boxes1.shape[0]): for col in range(self.boxes2.shape[0]): xmin1, ymin1, xmax1, ymax1 = self.boxes1[row] xmin2, ymin2, xmax2, ymax2 = self.boxes2[col] area1 = (ymax1 - ymin1) * (xmax1 - xmin1) area2 = (ymax2 - ymin2) * (xmax2 - xmin2) inter_xmax = min(xmax1, xmax2) inter_ymax = min(ymax1, ymax2) inter_xmin = max(xmin1, xmin2) inter_ymin = max(ymin1, ymin2) inter_height = inter_ymax - inter_ymin inter_width = inter_xmax - inter_xmin inter_height = max(inter_height, 0) inter_width = max(inter_width, 0) inter_area = inter_width * inter_height union_area = area1 + area2 - inter_area sim_score = inter_area / union_area self.output[row, col] = sim_score self.inputs = {'X': self.boxes1, 'Y': self.boxes2} self.outputs = {'Out': self.output} class TestIOUSimilarityOpWithLoD(TestIOUSimilarityOp): def test_check_output(self): self.check_output() def setUp(self): super(TestIOUSimilarityOpWithLoD, self).setUp() self.boxes1_lod = [[0, 1, 2]] self.output_lod = [[0, 1, 2]] self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} self.outputs = {'Out': (self.output, self.output_lod)} if __name__ == '__main__': unittest.main()
2,582
35.9
77
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_pool3d_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid.core as core from op_test import OpTest def max_pool3D_forward_naive(x, ksize, strides, paddings, global_pool=0, ceil_mode=False): N, C, D, H, W = x.shape if global_pool == 1: ksize = [D, H, W] D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1 ) / strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1 H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1 ) / strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1 W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1 ) / strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1 out = np.zeros((N, C, D_out, H_out, W_out)) for k in xrange(D_out): d_start = np.max((k * strides[0] - paddings[0], 0)) d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D)) for i in xrange(H_out): h_start = np.max((i * strides[0] - paddings[0], 0)) h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H)) for j in xrange(W_out): w_start = np.max((j * strides[1] - paddings[1], 0)) w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W)) x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end] out[:, :, k, i, j] = np.max(x_masked, axis=(2, 3, 4)) return out def avg_pool3D_forward_naive(x, ksize, strides, paddings, global_pool=0, ceil_mode=False): N, C, D, H, W = x.shape if global_pool == 1: ksize = [D, H, W] D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1 ) / strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 * paddings[0]) / strides[0] + 1 H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1 ) / strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 * paddings[1]) / strides[1] + 1 W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1 ) / strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 * paddings[2]) / strides[2] + 1 out = np.zeros((N, C, D_out, H_out, W_out)) for k in xrange(D_out): d_start = np.max((k * strides[0] - paddings[0], 0)) d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D)) for i in xrange(H_out): h_start = np.max((i * strides[0] - paddings[0], 0)) h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H)) for j in xrange(W_out): w_start = np.max((j * strides[1] - paddings[1], 0)) w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W)) x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end] out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, 4)) / ( (d_end - d_start) * (h_end - h_start) * (w_end - w_start)) return out class TestPool3d_Op(OpTest): def setUp(self): self.op_type = "pool3d" self.use_cudnn = False self.dtype = np.float32 self.init_test_case() self.init_global_pool() self.init_kernel_type() self.init_pool_type() self.init_ceil_mode() if self.global_pool: self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype(self.dtype) output = self.pool3D_forward_naive(input, self.ksize, self.strides, self.paddings, self.global_pool, self.ceil_mode).astype(self.dtype) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} self.attrs = { 'strides': self.strides, 'paddings': self.paddings, 'ksize': self.ksize, 'pooling_type': self.pool_type, 'global_pooling': self.global_pool, 'use_cudnn': self.use_cudnn, 'ceil_mode': self.ceil_mode, 'data_format': 'AnyLayout' # TODO(dzhwinter) : should be fix latter } self.outputs = {'Out': output} def testcudnn(self): return core.is_compiled_with_cuda() and self.use_cudnn def test_check_output(self): if self.testcudnn(): place = core.CUDAPlace(0) self.check_output_with_place(place, atol=1e-5) else: self.check_output() def test_check_grad(self): if self.dtype == np.float16: return if self.testcudnn() and self.pool_type != "max": place = core.CUDAPlace(0) self.check_grad_with_place( place, set(['X']), 'Out', max_relative_error=0.07) elif self.pool_type != "max": self.check_grad(set(['X']), 'Out', max_relative_error=0.07) def init_test_case(self): self.shape = [2, 3, 5, 5, 5] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] self.paddings = [0, 0, 0] def init_kernel_type(self): pass def init_pool_type(self): self.pool_type = "avg" self.pool3D_forward_naive = avg_pool3D_forward_naive def init_global_pool(self): self.global_pool = True def init_ceil_mode(self): self.ceil_mode = False class TestCase1(TestPool3d_Op): def init_test_case(self): self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] self.paddings = [0, 0, 0] def init_pool_type(self): self.pool_type = "avg" self.pool3D_forward_naive = avg_pool3D_forward_naive def init_global_pool(self): self.global_pool = False class TestCase2(TestPool3d_Op): def init_test_case(self): self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] self.paddings = [1, 1, 1] def init_pool_type(self): self.pool_type = "avg" self.pool3D_forward_naive = avg_pool3D_forward_naive def init_global_pool(self): self.global_pool = False class TestCase3(TestPool3d_Op): def init_pool_type(self): self.pool_type = "max" self.pool3D_forward_naive = max_pool3D_forward_naive class TestCase4(TestCase1): def init_pool_type(self): self.pool_type = "max" self.pool3D_forward_naive = max_pool3D_forward_naive class TestCase5(TestCase2): def init_pool_type(self): self.pool_type = "max" self.pool3D_forward_naive = max_pool3D_forward_naive #--------------------test pool3d-------------------- class TestCUDNNCase1(TestPool3d_Op): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase1(TestPool3d_Op): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCUDNNCase2(TestCase1): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase2(TestCase1): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCUDNNCase3(TestCase2): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase3(TestCase2): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCUDNNCase4(TestCase3): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase4(TestCase3): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCUDNNCase5(TestCase4): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase5(TestCase4): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCUDNNCase6(TestCase5): def init_kernel_type(self): self.use_cudnn = True class TestFP16CUDNNCase6(TestCase5): def init_kernel_type(self): self.use_cudnn = True self.dtype = np.float16 def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, atol=1e-3) class TestCeilModeCase1(TestCUDNNCase1): def init_ceil_mode(self): self.ceil_mode = True class TestCeilModeCase2(TestCUDNNCase2): def init_ceil_mode(self): self.ceil_mode = True class TestCeilModeCase3(TestCase1): def init_ceil_mode(self): self.ceil_mode = True class TestCeilModeCase4(TestCase2): def init_ceil_mode(self): self.ceil_mode = True if __name__ == '__main__': unittest.main()
10,972
31.657738
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_operator.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.op as op import paddle.fluid.proto.framework_pb2 as framework_pb2 class TestGetAllProtos(unittest.TestCase): def test_all(self): all_protos = op.get_all_op_protos() self.assertNotEqual(0, len(all_protos)) for each in all_protos: self.assertTrue(each.IsInitialized()) class TestOpDescCreationMethod(unittest.TestCase): def test_plain_input_output(self): op_proto = framework_pb2.OpProto() op_proto.type = "test" ipt = op_proto.inputs.add() ipt.name = "X" ipt.comment = "not matter" ipt = op_proto.inputs.add() ipt.name = "Y" ipt.comment = "not matter" opt = op_proto.outputs.add() opt.name = "Z" opt.comment = "not matter" op_proto.comment = "not matter" self.assertTrue(op_proto.IsInitialized()) method = op.OpDescCreationMethod(op_proto) output = method(X="a", Y="b", Z="c") expected = framework_pb2.OpDesc() expected.type = "test" ipt_0 = expected.inputs.add() ipt_0.parameter = "X" ipt_0.arguments.extend(["a"]) ipt_1 = expected.inputs.add() ipt_1.parameter = 'Y' ipt_1.arguments.extend(['b']) opt = expected.outputs.add() opt.parameter = "Z" opt.arguments.extend(["c"]) self.assertEqual(expected, output) def test_multiple_input_plain_output(self): op_proto = framework_pb2.OpProto() op_proto.type = "fc" ipt = op_proto.inputs.add() ipt.name = "X" ipt.comment = "" ipt.duplicable = True ipt = op_proto.inputs.add() ipt.name = "W" ipt.comment = "" ipt.duplicable = True ipt = op_proto.inputs.add() ipt.name = "b" ipt.comment = "" out = op_proto.outputs.add() out.name = "Y" out.comment = "" op_proto.comment = "" self.assertTrue(op_proto.IsInitialized()) method = op.OpDescCreationMethod(op_proto) generated1 = method(X="x", W="w", b="b", Y="y") expected1 = framework_pb2.OpDesc() tmp = expected1.inputs.add() tmp.parameter = "X" tmp.arguments.extend(['x']) tmp = expected1.inputs.add() tmp.parameter = 'W' tmp.arguments.extend(['w']) tmp = expected1.inputs.add() tmp.parameter = 'b' tmp.arguments.extend(['b']) tmp = expected1.outputs.add() tmp.parameter = 'Y' tmp.arguments.extend(['y']) expected1.type = 'fc' self.assertEqual(expected1, generated1) generated2 = method( X=['x1', 'x2', 'x3'], b='b', W=['w1', 'w2', 'w3'], Y='y') expected2 = framework_pb2.OpDesc() tmp = expected2.inputs.add() tmp.parameter = "X" tmp.arguments.extend(['x1', 'x2', 'x3']) tmp = expected2.inputs.add() tmp.parameter = 'W' tmp.arguments.extend(['w1', 'w2', 'w3']) tmp = expected2.inputs.add() tmp.parameter = 'b' tmp.arguments.extend(['b']) tmp = expected2.outputs.add() tmp.parameter = 'Y' tmp.arguments.extend(['y']) expected2.type = 'fc' self.assertEqual(expected2, generated2) def test_attrs(self): op_proto = framework_pb2.OpProto() op_proto.type = "test" ipt = op_proto.inputs.add() ipt.name = 'X' ipt.comment = "" def __add_attr__(name, type): attr = op_proto.attrs.add() attr.name = name attr.comment = "" attr.type = type __add_attr__("int_attr", framework_pb2.INT) __add_attr__("float_attr", framework_pb2.FLOAT) __add_attr__("string_attr", framework_pb2.STRING) __add_attr__("ints_attr", framework_pb2.INTS) __add_attr__("floats_attr", framework_pb2.FLOATS) __add_attr__("strings_attr", framework_pb2.STRINGS) op_proto.comment = "" self.assertTrue(op_proto.IsInitialized()) method = op.OpDescCreationMethod(op_proto) generated = method( X="a", int_attr=10, float_attr=3.2, string_attr="test_str", ints_attr=[0, 1, 2, 3, 4], floats_attr=[0.2, 3.2, 4.5], strings_attr=["a", "b", "c"]) expected = framework_pb2.OpDesc() expected.type = "test" ipt = expected.inputs.add() ipt.parameter = "X" ipt.arguments.extend(['a']) attr = expected.attrs.add() attr.name = "int_attr" attr.type = framework_pb2.INT attr.i = 10 attr = expected.attrs.add() attr.name = "float_attr" attr.type = framework_pb2.FLOAT attr.f = 3.2 attr = expected.attrs.add() attr.name = "string_attr" attr.type = framework_pb2.STRING attr.s = "test_str" attr = expected.attrs.add() attr.name = "ints_attr" attr.type = framework_pb2.INTS attr.ints.extend([0, 1, 2, 3, 4]) attr = expected.attrs.add() attr.name = 'floats_attr' attr.type = framework_pb2.FLOATS attr.floats.extend([0.2, 3.2, 4.5]) attr = expected.attrs.add() attr.name = 'strings_attr' attr.type = framework_pb2.STRINGS attr.strings.extend(['a', 'b', 'c']) self.assertEqual(expected, generated) class TestOpCreations(unittest.TestCase): def test_all(self): add_op = op.Operator("sum", X=["a", "b"], Out="z") self.assertIsNotNone(add_op) # Invoke C++ DebugString() self.assertEqual('Op(sum), inputs:{X[a, b]}, outputs:{Out[z]}.', str(add_op)) if __name__ == "__main__": unittest.main()
6,452
28.465753
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_uniform_random_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest import paddle.fluid.core as core from paddle.fluid.op import Operator def output_hist(out): hist, _ = np.histogram(out, range=(-5, 10)) hist = hist.astype("float32") hist /= float(out.size) prob = 0.1 * np.ones((10)) return hist, prob class TestUniformRandomOp(OpTest): def setUp(self): self.op_type = "uniform_random" self.inputs = {} self.attrs = { "shape": [1000, 784], "min": -5.0, "max": 10.0, "seed": 10 } self.outputs = {"Out": np.zeros((1000, 784)).astype("float32")} def test_check_output(self): self.check_output_customized(self.verify_output) def verify_output(self, outs): hist, prob = output_hist(np.array(outs[0])) self.assertTrue( np.allclose( hist, prob, rtol=0, atol=0.01), "hist: " + str(hist)) class TestUniformRandomOpSelectedRows(unittest.TestCase): def get_places(self): places = [core.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) return places def test_check_output(self): for place in self.get_places(): self.check_with_place(place) def check_with_place(self, place): scope = core.Scope() out = scope.var("X").get_selected_rows() op = Operator( "uniform_random", Out="X", shape=[4, 784], min=-5.0, max=10.0, seed=10) op.run(scope, place) self.assertEqual(out.get_tensor().shape(), [4, 784]) hist, prob = output_hist(np.array(out.get_tensor())) self.assertTrue( np.allclose( hist, prob, rtol=0, atol=0.01), "hist: " + str(hist)) if __name__ == "__main__": unittest.main()
2,516
28.964286
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_elementwise_mul_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class ElementwiseMulOp(OpTest): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.uniform(0.1, 1, [13, 17]).astype("float64"), 'Y': np.random.uniform(0.1, 1, [13, 17]).astype("float64") } self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])} def test_check_output(self): self.check_output() def test_check_grad_normal(self): self.check_grad(['X', 'Y'], 'Out') def test_check_grad_ingore_x(self): self.check_grad(['Y'], 'Out', no_grad_set=set("X")) def test_check_grad_ingore_y(self): self.check_grad(['X'], 'Out', no_grad_set=set('Y')) class TestElementwiseMulOp_scalar(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.rand(2, 3, 4).astype(np.float32), 'Y': np.random.rand(1).astype(np.float32) } self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']} class TestElementwiseMulOp_Vector(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.random((32, )).astype("float64"), 'Y': np.random.random((32, )).astype("float64") } self.outputs = {'Out': np.multiply(self.inputs['X'], self.inputs['Y'])} class TestElementwiseMulOp_broadcast_0(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.rand(2, 3, 4).astype(np.float64), 'Y': np.random.rand(2).astype(np.float64) } self.attrs = {'axis': 0} self.outputs = { 'Out': self.inputs['X'] * self.inputs['Y'].reshape(2, 1, 1) } class TestElementwiseMulOp_broadcast_1(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.rand(2, 3, 4).astype(np.float64), 'Y': np.random.rand(3).astype(np.float64) } self.attrs = {'axis': 1} self.outputs = { 'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 3, 1) } class TestElementwiseMulOp_broadcast_2(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.rand(2, 3, 4).astype(np.float64), 'Y': np.random.rand(4).astype(np.float64) } self.outputs = { 'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 1, 4) } class TestElementwiseMulOp_broadcast_3(ElementwiseMulOp): def setUp(self): self.op_type = "elementwise_mul" self.inputs = { 'X': np.random.rand(2, 3, 4, 5).astype(np.float64), 'Y': np.random.rand(3, 4).astype(np.float64) } self.attrs = {'axis': 1} self.outputs = { 'Out': self.inputs['X'] * self.inputs['Y'].reshape(1, 3, 4, 1) } if __name__ == '__main__': unittest.main()
3,730
30.618644
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_uniform_random_batch_size_like_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestUniformRandomBatchSizeLike(OpTest): def setUp(self): self.op_type = "uniform_random_batch_size_like" self.inputs = {'Input': np.zeros((500, 2000), dtype="float32")} self.attrs = {'min': 1., 'max': 2., 'shape': [-1, 2000]} self.outputs = {'Out': np.zeros((500, 2000), dtype='float32')} def test_check_output(self): self.check_output_customized(self.verify_output) def verify_output(self, outs): self.assertEqual(outs[0].shape, (500, 2000)) hist, _ = np.histogram(outs[0], range=(1, 2)) hist = hist.astype("float32") hist /= float(outs[0].size) prob = 0.1 * np.ones((10)) self.assertTrue( np.allclose( hist, prob, rtol=0, atol=0.01), "hist: " + str(hist)) if __name__ == "__main__": unittest.main()
1,519
34.348837
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_split_and_merge_lod_tensor_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid.core as core import numpy as np import paddle.fluid.layers as layers from paddle.fluid.framework import Program, program_guard from paddle.fluid.executor import Executor from paddle.fluid.backward import append_backward class TestCPULoDTensorArrayOps(unittest.TestCase): def place(self): return core.CPUPlace() def test_split_and_merge_lod_tensor_no_lod(self): tensor = core.LoDTensor() tensor.set(np.arange(10).reshape(10, 1).astype('int32'), self.place()) mask_np = np.array([0, 0, 1, 1, 1, 1, 0, 0, 0, 0]).astype('bool') mask_np = np.expand_dims(mask_np, axis=1) mask = core.LoDTensor() mask.set(mask_np, self.place()) expect_true_tensor = np.array([2, 3, 4, 5]).astype('int32') expect_true_tensor = np.expand_dims(expect_true_tensor, axis=1) expect_true = core.LoDTensor() expect_true.set(expect_true_tensor, self.place()) expect_false_tensor = np.array([0, 1, 6, 7, 8, 9]).astype('int32') expect_false_tensor = np.expand_dims(expect_false_tensor, axis=1) expect_false = core.LoDTensor() expect_false.set(expect_false_tensor, self.place()) self.main( tensor=tensor, mask=mask, expect_true=expect_true, expect_false=expect_false, expect_out=tensor) def test_split_and_merge_lod_tensor_level_0(self): tensor = core.LoDTensor() tensor.set(np.arange(10).reshape(10, 1).astype('int32'), self.place()) tensor.set_lod([[0, 3, 9, 10]]) mask_np = np.array([0, 1, 0]).astype('bool') mask_np = np.expand_dims(mask_np, axis=1) mask = core.LoDTensor() mask.set(mask_np, self.place()) expect_true_tensor = np.array([3, 4, 5, 6, 7, 8]).astype('int32') expect_true_tensor = np.expand_dims(expect_true_tensor, axis=1) expect_true = core.LoDTensor() expect_true.set(expect_true_tensor, self.place()) expect_true.set_lod([[0, 6]]) expect_false_tensor = np.array([0, 1, 2, 9]).astype('int32') expect_false_tensor = np.expand_dims(expect_false_tensor, axis=1) expect_false_lod = [[0, 3, 4]] expect_false = core.LoDTensor() expect_false.set(expect_false_tensor, self.place()) expect_false.set_lod(expect_false_lod) self.main( tensor=tensor, mask=mask, expect_true=expect_true, expect_false=expect_false, expect_out=tensor) def main(self, tensor, mask, expect_true, expect_false, expect_out, level=0): place = self.place() program = Program() with program_guard(program): x = layers.data(name='x', shape=[1]) x.persistable = True y = layers.data(name='y', shape=[1]) y.persistable = True out_true, out_false = layers.split_lod_tensor( input=x, mask=y, level=level) out_true.persistable = True out_false.persistable = True out = layers.merge_lod_tensor( in_true=out_true, in_false=out_false, mask=y, x=x, level=level) out.persistable = True exe = Executor(place) scope = core.Scope() exe.run(program, feed={'x': tensor, 'y': mask}, scope=scope, return_numpy=False) var_true = scope.find_var(out_true.name).get_tensor() var_false = scope.find_var(out_false.name).get_tensor() var_out = scope.find_var(out.name).get_tensor() self.check_tensor_same(var_true, expect_true) self.check_tensor_same(var_false, expect_false) self.check_tensor_same(var_out, expect_out) def check_tensor_same(self, actual, expect): self.assertTrue(np.allclose(np.array(actual), np.array(expect))) self.assertEqual(actual.lod(), expect.lod()) class TestCPUSplitMergeLoDTensorGrad(unittest.TestCase): def test_grad(self): place = core.CPUPlace() program = Program() with program_guard(program): x = layers.data( name='x', shape=[1], dtype='float32', stop_gradient=False) y = layers.data( name='y', shape=[1], dtype='bool', stop_gradient=False) level = 0 out_true, out_false = layers.split_lod_tensor( input=x, mask=y, level=level) out = layers.merge_lod_tensor( in_true=out_true, in_false=out_false, mask=y, x=x, level=level) mean = layers.mean(out) append_backward(mean) tensor = core.LoDTensor() tensor.set(np.arange(10).reshape(10, 1).astype('float32'), place) tensor.set_lod([[0, 3, 9, 10]]) mask_np = np.array([0, 1, 0]).astype('bool') mask_np = np.expand_dims(mask_np, axis=1) mask = core.LoDTensor() mask.set(mask_np, place) exe = Executor(place) scope = core.Scope() g_vars = program.global_block().var(x.name + "@GRAD") g_out = [ item.sum() for item in map(np.array, exe.run(program, feed={'x': tensor, 'y': mask}, fetch_list=[g_vars], scope=scope, return_numpy=False)) ] g_out_sum = np.array(g_out).sum() self.assertAlmostEqual(1.0, g_out_sum, delta=0.1) if __name__ == '__main__': unittest.main()
6,335
33.434783
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_rnn_memory_helper_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from paddle.fluid.framework import Program from paddle.fluid.executor import Executor from paddle.fluid.backward import append_backward import numpy as np import paddle.fluid.core as core class RNNMemoryHelperOpTest(unittest.TestCase): def setUp(self): self.program = Program() self.place = core.CPUPlace() self.X = self.program.global_block().create_var( name='X', shape=[2, 3], dtype='float32') self.Out = self.program.global_block().create_var( name='Out', shape=[2, 3], dtype='float32') self.program.global_block().append_op( type='rnn_memory_helper', inputs={"X": self.X}, outputs={"Out": self.Out}, attrs={}) def test_forward(self): x_np = np.random.normal(size=(2, 3)).astype("float32") self.feed_map = {'X': x_np} self.fetch_list = [self.Out] exe = Executor(self.place) out = exe.run(self.program, feed=self.feed_map, fetch_list=self.fetch_list) self.assertTrue(np.allclose(out[0], x_np, rtol=1e-5)) class RNNMemoryHelperGradOpTest(unittest.TestCase): def setUp(self): self.program = Program() self.place = core.CPUPlace() self.input_names = ['X', 'Out', 'Out@GRAD'] self.input_vars = { name: self.program.global_block().create_var( name=name, shape=[2, 3], dtype='float32') for name in self.input_names } self.output_names = ['X@GRAD'] self.output_vars = { name: self.program.global_block().create_var( name=name, shape=[2, 3], dtype='float32') for name in self.output_names } self.program.global_block().append_op( type='rnn_memory_helper_grad', inputs=self.input_vars, outputs=self.output_vars, attrs={}) def test_backward(self): self.feed_map = { name: np.random.normal(size=(2, 3)).astype("float32") for name in self.input_names } self.fetch_list = [self.output_vars['X@GRAD']] exe = Executor(self.place) out = exe.run(self.program, feed=self.feed_map, fetch_list=self.fetch_list) np.isclose(out[0], self.feed_map['Out@GRAD'], rtol=1e-5) class RNNMemoryHelperGradOpWithoutInputTest(unittest.TestCase): def setUp(self): self.program = Program() self.fake_program = Program() self.place = core.CPUPlace() self.input_names = ['X', 'Out'] self.input_vars = { name: self.program.global_block().create_var( name=name, shape=[2, 3], dtype='float32') for name in self.input_names } self.input_vars["Out@GRAD"] = \ self.fake_program.global_block().create_var( name="Out@GRAD", shape=[2, 3], dtype='float32') self.output_names = ['X@GRAD'] self.output_vars = { name: self.program.global_block().create_var( name=name, shape=[2, 3], dtype='float32') for name in self.output_names } self.program.global_block().append_op( type='rnn_memory_helper_grad', inputs=self.input_vars, outputs=self.output_vars, attrs={}) def test_backward(self): self.feed_map = { name: np.random.normal(size=(2, 3)).astype("float32") for name in ['X', 'Out'] } self.fetch_list = [self.output_vars['X@GRAD']] exe = Executor(self.place) out = exe.run(self.program, feed=self.feed_map, fetch_list=self.fetch_list) self.assertTrue( np.allclose( out[0], np.zeros(shape=(2, 3)).astype("float32"), rtol=1e-5)) if __name__ == '__main__': unittest.main()
4,609
32.897059
77
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_cos_sim_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestCosSimOp(OpTest): def setUp(self): self.op_type = "cos_sim" self.inputs = { 'X': np.random.random((6, 5)).astype("float32"), 'Y': np.random.random((6, 5)).astype("float32") } expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1) expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1) expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=1) / \ expect_x_norm / expect_y_norm self.outputs = { 'XNorm': np.expand_dims(expect_x_norm, 1), 'YNorm': np.expand_dims(expect_y_norm, 1), 'Out': np.expand_dims(expect_out, 1) } def test_check_output(self): self.check_output() def test_check_grad_normal(self): self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.06) def test_check_grad_ingore_x(self): self.check_grad( ['Y'], 'Out', max_relative_error=0.06, no_grad_set=set("X")) def test_check_grad_ingore_y(self): self.check_grad( ['X'], 'Out', max_relative_error=0.06, no_grad_set=set('Y')) class TestCosSimOp2(TestCosSimOp): def setUp(self): self.op_type = "cos_sim" self.inputs = { 'X': np.random.random((6, 5)).astype("float32"), 'Y': np.random.random((1, 5)).astype("float32") } expect_x_norm = np.linalg.norm(self.inputs['X'], axis=1) expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=1) expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=1) / \ expect_x_norm / expect_y_norm self.outputs = { 'XNorm': np.expand_dims(expect_x_norm, 1), 'YNorm': np.expand_dims(expect_y_norm, 1), 'Out': np.expand_dims(expect_out, 1) } class TestCosSimOp3(TestCosSimOp): def setUp(self): self.op_type = "cos_sim" self.inputs = { 'X': np.random.random((6, 5, 2)).astype("float32"), 'Y': np.random.random((6, 5, 2)).astype("float32") } expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2)) expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2)) expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2)) / \ expect_x_norm / expect_y_norm self.outputs = { 'XNorm': np.expand_dims(expect_x_norm, 1), 'YNorm': np.expand_dims(expect_y_norm, 1), 'Out': np.expand_dims(expect_out, 1) } class TestCosSimOp4(TestCosSimOp): def setUp(self): self.op_type = "cos_sim" self.inputs = { 'X': np.random.random((6, 5, 2)).astype("float32"), 'Y': np.random.random((1, 5, 2)).astype("float32") } expect_x_norm = np.linalg.norm(self.inputs['X'], axis=(1, 2)) expect_y_norm = np.linalg.norm(self.inputs['Y'], axis=(1, 2)) expect_out = (self.inputs['X'] * self.inputs['Y']).sum(axis=(1, 2)) / \ expect_x_norm / expect_y_norm self.outputs = { 'XNorm': np.expand_dims(expect_x_norm, 1), 'YNorm': np.expand_dims(expect_y_norm, 1), 'Out': np.expand_dims(expect_out, 1) } if __name__ == '__main__': unittest.main()
3,954
35.62037
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_parallel_executor_fetch_feed.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.dataset.flowers as flowers import math import paddle.fluid as fluid import unittest import numpy as np import paddle def Lenet(data, class_dim): conv1 = fluid.layers.conv2d(data, 32, 5, 1, act=None) bn1 = fluid.layers.batch_norm(conv1, act='relu') pool1 = fluid.layers.pool2d(bn1, 2, 'max', 2) conv2 = fluid.layers.conv2d(pool1, 50, 5, 1, act=None) bn2 = fluid.layers.batch_norm(conv2, act='relu') pool2 = fluid.layers.pool2d(bn2, 2, 'max', 2) fc1 = fluid.layers.fc(pool2, size=500, act='relu') fc2 = fluid.layers.fc(fc1, size=class_dim, act='softmax') return fc2 class TestFetchOp(unittest.TestCase): def parallel_exe(self, train_inputs, seed): main = fluid.Program() startup = fluid.Program() startup.random_seed = seed with fluid.program_guard(main, startup): data = fluid.layers.data( name='image', shape=[3, 224, 224], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') out = Lenet(data, class_dim=102) loss = fluid.layers.cross_entropy(input=out, label=label) loss = fluid.layers.mean(loss) opt = fluid.optimizer.Momentum( learning_rate=0.1, momentum=0.9, regularization=fluid.regularizer.L2Decay(1e-4)) opt.minimize(loss) # TODO(zcd): I found that onece the memory optimizer is open, # parallel_exe doesn't fetch some variable, such as conv2d_0.b_0@GRAD, # conv2d_1.b_0@GRAD. Those variables should not be pruned. # fluid.memory_optimize(main) place = fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(startup) feeder = fluid.DataFeeder(place=place, feed_list=[data, label]) pe = fluid.ParallelExecutor( use_cuda=True, loss_name=loss.name, main_program=main) fetch_list = [] all_vars = main.global_block().vars for k, v in all_vars.iteritems(): if 'tmp' not in k and k[0] is not '_' or v.persistable: fetch_list.append(k) for data in train_inputs: ret = pe.run(fetch_list, feed=feeder.feed(data)) for i in range(len(fetch_list)): assert not math.isnan(np.sum(ret[i])) and \ not math.isinf(np.sum(ret[i])) def test_fetch_op(self): tst_reader = paddle.batch(flowers.test(use_xmap=False), batch_size=16) tst_reader_iter = tst_reader() iters = 3 train_inputs = [] for i in range(iters): train_inputs.append(tst_reader_iter.next()) self.parallel_exe(train_inputs, seed=1) class TestFeedParallel(unittest.TestCase): def test_main(self): main = fluid.Program() startup = fluid.Program() startup.random_seed = 1 with fluid.scope_guard(fluid.core.Scope()): with fluid.program_guard(main, startup): data = fluid.layers.data( name='image', shape=[3, 224, 224], dtype='float32') label = fluid.layers.data( name='label', shape=[1], dtype='int64') out = Lenet(data, class_dim=102) loss = fluid.layers.cross_entropy(input=out, label=label) loss = fluid.layers.mean(loss) opt = fluid.optimizer.Momentum( learning_rate=0.1, momentum=0.9, regularization=fluid.regularizer.L2Decay(1e-4)) opt.minimize(loss) place = fluid.CUDAPlace(0) feeder = fluid.DataFeeder(place=place, feed_list=[data, label]) reader = feeder.decorate_reader( paddle.batch( flowers.train(), batch_size=16), multi_devices=True) exe = fluid.Executor(place) exe.run(startup) pe = fluid.ParallelExecutor( use_cuda=True, loss_name=loss.name, main_program=main) for batch_id, data in enumerate(reader()): loss_np = np.array(pe.run(feed=data, fetch_list=[loss.name])[0]) print batch_id, loss_np if batch_id == 2: break if __name__ == '__main__': unittest.main()
4,981
36.458647
82
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_nce.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def nce(input, weight, bias, sample_weight, labels, num_classes, num_sample_class): samples = [] sample_labels = [] batch_size = input.shape[0] num_true_class = labels.shape[1] for i in range(batch_size): w = 1 if sample_weight is None else sample_weight[i] for label in labels[i]: samples.append((i, label, True, w)) sample_labels.append(label) for num in range(num_sample_class): samples.append((i, num, False, w)) sample_labels.append(num) # forward bias sample_out = np.zeros(len(samples)).astype(np.float32) if bias is not None: for i in range(len(samples)): sample_out[i] = bias[samples[i][1]] # forward weight for i in range(len(samples)): sample_out[i] += np.dot(input[samples[i][0]], weight[samples[i][1]]) # forward activation sample_out = 1.0 / (1.0 + np.exp(-sample_out)) # forward cost out = np.zeros(batch_size).astype(np.float32) b = 1.0 / num_classes * num_sample_class for i in range(len(samples)): o = sample_out[i] cost = -np.log(o / (o + b)) if samples[i][2] else -np.log(b / (o + b)) out[samples[i][0]] += cost * samples[i][3] return (out[:, np.newaxis], np.array(sample_out).reshape( batch_size, num_sample_class + num_true_class), np.array(sample_labels).reshape(batch_size, num_sample_class + num_true_class)) class TestNCE(OpTest): def generate_data(self, dim, batch_size, num_classes, num_true_class, num_neg_samples): input = np.random.randn(batch_size, dim).astype(np.float32) weight = np.random.randn(num_classes, dim).astype(np.float32) bias = np.random.randn(num_classes).astype(np.float32) sample_weight = np.random.randn(batch_size).astype(np.float32) labels = np.random.randint(0, num_classes, (batch_size, num_true_class)) self.attrs = { 'num_total_classes': num_classes, 'num_neg_samples': num_neg_samples, 'custom_neg_classes': range(num_neg_samples) } self.inputs = { 'Input': input, 'Label': labels, 'Weight': weight, 'Bias': bias, 'SampleWeight': sample_weight } def set_data(self): self.generate_data(5, 5, 4, 1, 2) def compute(self): out = nce(self.inputs['Input'], self.inputs['Weight'], self.inputs['Bias'], self.inputs['SampleWeight'], self.inputs['Label'], self.attrs['num_total_classes'], self.attrs['num_neg_samples']) self.outputs = { 'Cost': out[0], 'SampleLogits': out[1], 'SampleLabels': out[2] } def setUp(self): self.op_type = 'nce' self.set_data() self.compute() def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad( ["Input", "Weight", "Bias"], "Cost", max_relative_error=0.02) class TestNCECase1(TestNCE): def set_data(self): self.generate_data(10, 20, 10, 2, 5) if __name__ == '__main__': unittest.main()
3,959
34.044248
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_edit_distance_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def Levenshtein(hyp, ref): """ Compute the Levenshtein distance between two strings. :param hyp: hypothesis string in index :type hyp: list :param ref: reference string in index :type ref: list """ m = len(hyp) n = len(ref) if m == 0: return n if n == 0: return m dist = np.zeros((m + 1, n + 1)).astype("float32") for i in range(0, m + 1): dist[i][0] = i for j in range(0, n + 1): dist[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): cost = 0 if hyp[i - 1] == ref[j - 1] else 1 deletion = dist[i - 1][j] + 1 insertion = dist[i][j - 1] + 1 substitution = dist[i - 1][j - 1] + cost dist[i][j] = min(deletion, insertion, substitution) return dist[m][n] class TestEditDistanceOp(OpTest): def setUp(self): self.op_type = "edit_distance" normalized = False x1 = np.array([[0, 12, 3, 5, 8, 2]]).astype("int64") x2 = np.array([[0, 12, 4, 7, 8]]).astype("int64") x1 = np.transpose(x1) x2 = np.transpose(x2) x1_lod = [0, 1, 5] x2_lod = [0, 3, 4] num_strs = len(x1_lod) - 1 distance = np.zeros((num_strs, 1)).astype("float32") sequence_num = np.array(2).astype("int64") for i in range(0, num_strs): distance[i] = Levenshtein( hyp=x1[x1_lod[i]:x1_lod[i + 1]], ref=x2[x2_lod[i]:x2_lod[i + 1]]) if normalized is True: len_ref = x2_lod[i + 1] - x2_lod[i] distance[i] = distance[i] / len_ref self.attrs = {'normalized': normalized} self.inputs = {'Hyps': (x1, [x1_lod]), 'Refs': (x2, [x2_lod])} self.outputs = {'Out': distance, 'SequenceNum': sequence_num} def test_check_output(self): self.check_output() class TestEditDistanceOpNormalized(OpTest): def setUp(self): self.op_type = "edit_distance" normalized = True x1 = np.array([[0, 10, 3, 6, 5, 8, 2]]).astype("int64") x2 = np.array([[0, 10, 4, 6, 7, 8]]).astype("int64") x1 = np.transpose(x1) x2 = np.transpose(x2) x1_lod = [0, 1, 3, 6] x2_lod = [0, 2, 3, 5] num_strs = len(x1_lod) - 1 distance = np.zeros((num_strs, 1)).astype("float32") sequence_num = np.array(3).astype("int64") for i in range(0, num_strs): distance[i] = Levenshtein( hyp=x1[x1_lod[i]:x1_lod[i + 1]], ref=x2[x2_lod[i]:x2_lod[i + 1]]) if normalized is True: len_ref = x2_lod[i + 1] - x2_lod[i] distance[i] = distance[i] / len_ref self.attrs = {'normalized': normalized} self.inputs = {'Hyps': (x1, [x1_lod]), 'Refs': (x2, [x2_lod])} self.outputs = {'Out': distance, 'SequenceNum': sequence_num} def test_check_output(self): self.check_output() if __name__ == '__main__': unittest.main()
3,710
32.432432
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/__init__.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
611
42.714286
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_conv2d_mkldnn_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from test_conv2d_op import TestConv2dOp, TestWithPad, TestWithStride class TestMKLDNN(TestConv2dOp): def init_kernel_type(self): self.use_mkldnn = True class TestMKLDNNWithPad(TestWithPad): def init_kernel_type(self): self.use_mkldnn = True class TestMKLDNNWithStride(TestWithStride): def init_kernel_type(self): self.use_mkldnn = True if __name__ == '__main__': unittest.main()
1,055
27.540541
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_random_crop_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import paddle.fluid.core as core from op_test import OpTest class TestRandomCropOp(OpTest): def setUp(self): to_crop = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]] * 5).astype("float32") self.possible_res = [ np.array([[1, 2, 3], [5, 6, 7]]), np.array([[2, 3, 4], [6, 7, 8]]), np.array([[5, 6, 7], [9, 10, 11]]), np.array([[6, 7, 8], [10, 11, 12]]) ] self.op_type = "random_crop" self.inputs = {'X': to_crop, 'Seed': np.array([10])} self.outputs = {'Out': np.array([]), 'SeedOut': np.array([])} self.attrs = {'shape': [2, 3]} def test_check_output(self): self.check_output_customized(self.verify_output) def verify_output(self, outs): out = np.array(outs[1]) for ins in out[:]: is_equal = [(ins == res).all() for res in self.possible_res] self.assertIn(True, is_equal) if __name__ == "__main__": unittest.main()
1,659
34.319149
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_dist_transpiler.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import paddle.fluid as fluid import paddle.fluid.core as core import paddle.fluid.layers as layers from paddle.fluid.transpiler.distribute_transpiler import delete_ops import numpy class TestDistTranspiler(unittest.TestCase): def setUp(self): self.trainer_id = 0 self.trainers = 2 self.pservers = 2 self.pserver_eps = "127.0.0.1:6174,127.0.0.1:6175" self.current_pserver_ep = "127.0.0.1:6174" def net_conf(self): x = fluid.layers.data(name='x', shape=[1000], dtype='float32') y_predict = fluid.layers.fc(input=x, size=1000, act=None, param_attr=fluid.ParamAttr(name='fc_w')) y = fluid.layers.data(name='y', shape=[1], dtype='float32') cost = fluid.layers.square_error_cost(input=y_predict, label=y) avg_cost = fluid.layers.mean(cost) sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.1) optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost) return optimize_ops, params_grads def test_transpiler(self): trainer = self.get_trainer() pserver, startup = self.get_pserver(self.current_pserver_ep) self.assertEqual([op.type for op in trainer.global_block().ops], self.get_expect_trainer_ops()) self.assertEqual(len(pserver.blocks), 3) # block0: listen_and_serv self.assertEqual([op.type for op in pserver.blocks[0].ops], ["listen_and_serv"]) # block2: optimize pass self.assertEqual([op.type for op in pserver.blocks[1].ops], ["sum", "scale", "sgd"]) # confirm startup program self.assertEqual([op.type for op in startup.global_block().ops], [ "fill_constant", "fill_constant", "uniform_random", "uniform_random" ]) # the variable #fc_w will be split into two blocks fc_w_var = startup.global_block().var("fc_w.block1") self.assertEqual(fc_w_var.shape, (500, 1000)) def get_main_program(self): main = fluid.Program() with fluid.program_guard(main): self.net_conf() return main def get_expect_trainer_ops(self): trainer = fluid.Program() with fluid.program_guard(trainer): optimize_ops, params_grads = self.net_conf() delete_ops(trainer.global_block(), optimize_ops) ops = [op.type for op in trainer.global_block().ops] + [ "split_byref", "send_vars", "send_barrier", "recv", "recv", "fetch_barrier", "concat" ] ops.insert(ops.index("elementwise_add_grad") + 1, "send_vars") return ops def get_trainer(self): return self._transpiler_instance().get_trainer_program() def get_pserver(self, ep): t = self._transpiler_instance() pserver = t.get_pserver_program(ep) startup = t.get_startup_program(ep, pserver) return pserver, startup def _transpiler_instance(self): main = self.get_main_program() t = fluid.DistributeTranspiler() t.transpile( self.trainer_id, program=main, pservers=self.pserver_eps, trainers=self.trainers) return t if __name__ == "__main__": unittest.main()
4,024
33.401709
80
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_ctc_align.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys import unittest import numpy as np from op_test import OpTest from test_softmax_op import stable_softmax def CTCAlign(input, lod, blank, merge_repeated): lod0 = lod[0] result = [] for i in range(len(lod0) - 1): prev_token = -1 for j in range(lod0[i], lod0[i + 1]): token = input[j][0] if (token != blank) and not (merge_repeated and token == prev_token): result.append(token) prev_token = token result = np.array(result).reshape([len(result), 1]).astype("int32") if len(result) == 0: result = np.array([-1]) return result class TestCTCAlignOp(OpTest): def config(self): self.op_type = "ctc_align" self.input_lod = [[0, 11, 18]] self.blank = 0 self.merge_repeated = False self.input = np.array( [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0]).reshape( [18, 1]).astype("int32") def setUp(self): self.config() output = CTCAlign(self.input, self.input_lod, self.blank, self.merge_repeated) self.inputs = {"Input": (self.input, self.input_lod), } self.outputs = {"Output": output} self.attrs = { "blank": self.blank, "merge_repeated": self.merge_repeated } def test_check_output(self): self.check_output() pass class TestCTCAlignOpCase1(TestCTCAlignOp): def config(self): self.op_type = "ctc_align" self.input_lod = [[0, 11, 19]] self.blank = 0 self.merge_repeated = True self.input = np.array( [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0, 0]).reshape( [19, 1]).astype("int32") class TestCTCAlignOpCase2(TestCTCAlignOp): def config(self): self.op_type = "ctc_align" self.input_lod = [[0, 4]] self.blank = 0 self.merge_repeated = True self.input = np.array([0, 0, 0, 0]).reshape([4, 1]).astype("int32") if __name__ == "__main__": unittest.main()
2,750
30.261364
79
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_cast_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import op_test import unittest import numpy as np import paddle.fluid.core as core class TestCastOp1(op_test.OpTest): def setUp(self): ipt = np.random.random(size=[10, 10]) self.inputs = {'X': ipt.astype('float32')} self.outputs = {'Out': ipt.astype('float64')} self.attrs = { 'in_dtype': int(core.VarDesc.VarType.FP32), 'out_dtype': int(core.VarDesc.VarType.FP64) } self.op_type = 'cast' def test_check_output(self): self.check_output() def test_grad(self): self.check_grad(['X'], ['Out']) class TestCastOp2(op_test.OpTest): def setUp(self): ipt = np.random.random(size=[10, 10]) # numpy float16 is binded to fluid float16 via uint16 self.inputs = {'X': ipt.astype('float16').view(np.uint16)} self.outputs = {'Out': ipt.astype('float32')} self.attrs = { 'in_dtype': int(core.VarDesc.VarType.FP16), 'out_dtype': int(core.VarDesc.VarType.FP32) } self.op_type = 'cast' def test_check_output(self): self.check_output(atol=1e-3) class TestCastOp3(op_test.OpTest): def setUp(self): ipt = np.random.random(size=[10, 10]) self.inputs = {'X': ipt.astype('float32')} self.outputs = {'Out': ipt.astype('float16')} self.attrs = { 'in_dtype': int(core.VarDesc.VarType.FP32), 'out_dtype': int(core.VarDesc.VarType.FP16) } self.op_type = 'cast' def test_check_output(self): self.check_output(atol=1e-3) if __name__ == '__main__': unittest.main()
2,245
30.194444
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_gru_unit_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math import unittest import numpy as np from op_test import OpTest class GRUActivationType(OpTest): identity = 0 sigmoid = 1 tanh = 2 relu = 3 def identity(x): return x def sigmoid(x): return 1. / (1. + np.exp(-x)) def tanh(x): return 2. * sigmoid(2. * x) - 1. def relu(x): return np.maximum(x, 0) class TestGRUUnitOp(OpTest): batch_size = 5 frame_size = 10 activate = { GRUActivationType.identity: identity, GRUActivationType.sigmoid: sigmoid, GRUActivationType.tanh: tanh, GRUActivationType.relu: relu, } def set_inputs(self): batch_size = self.batch_size frame_size = self.frame_size self.op_type = 'gru_unit' self.inputs = { 'Input': np.random.uniform( -0.1, 0.1, (batch_size, frame_size * 3)).astype('float64'), 'HiddenPrev': np.random.uniform( -0.1, 0.1, (batch_size, frame_size)).astype('float64'), 'Weight': np.random.uniform( -1. / math.sqrt(frame_size), 1. / math.sqrt(frame_size), (frame_size, frame_size * 3)).astype('float64'), } self.attrs = { 'activation': GRUActivationType.tanh, 'gate_activation': GRUActivationType.sigmoid } def set_outputs(self): # GRU calculations batch_size = self.batch_size frame_size = self.frame_size x = self.inputs['Input'] h_p = self.inputs['HiddenPrev'] w = self.inputs['Weight'] b = self.inputs['Bias'] if self.inputs.has_key('Bias') else np.zeros( (1, frame_size * 3)) g = x + np.tile(b, (batch_size, 1)) w_u_r = w.flatten()[:frame_size * frame_size * 2].reshape( (frame_size, frame_size * 2)) u_r = self.activate[self.attrs['gate_activation']](np.dot( h_p, w_u_r) + g[:, :frame_size * 2]) u = u_r[:, :frame_size] r = u_r[:, frame_size:frame_size * 2] r_h_p = r * h_p w_c = w.flatten()[frame_size * frame_size * 2:].reshape( (frame_size, frame_size)) c = self.activate[self.attrs['activation']](np.dot(r_h_p, w_c) + g[:, frame_size * 2:]) g = np.hstack((u_r, c)) h = u * c + (1 - u) * h_p self.outputs = { 'Gate': g.astype('float64'), 'ResetHiddenPrev': r_h_p.astype('float64'), 'Hidden': h.astype('float64') } def setUp(self): self.set_inputs() self.set_outputs() def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['Input', 'HiddenPrev', 'Weight'], ['Hidden']) class TestGRUUnitOpWithBias(TestGRUUnitOp): def set_inputs(self): batch_size = self.batch_size frame_size = self.frame_size super(TestGRUUnitOpWithBias, self).set_inputs() self.inputs['Bias'] = np.random.uniform( -0.1, 0.1, (1, frame_size * 3)).astype('float64') self.attrs = { 'activation': GRUActivationType.identity, 'gate_activation': GRUActivationType.sigmoid } def test_check_grad(self): self.check_grad(['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden']) def test_check_grad_ingore_input(self): self.check_grad( ['HiddenPrev', 'Weight', 'Bias'], ['Hidden'], no_grad_set=set('Input')) if __name__ == '__main__': unittest.main()
4,172
29.911111
78
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_log_loss_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest class TestLogLossOp(OpTest): def setUp(self): self.op_type = 'log_loss' samples_num = 32 predicted = np.random.uniform(0.1, 1.0, (samples_num, 1)).astype("float32") labels = np.random.randint(0, 2, (samples_num, 1)).astype("float32") epsilon = 1e-4 self.inputs = { 'Predicted': predicted, 'Labels': labels, } self.attrs = {'epsilon': epsilon} loss = -labels * np.log(predicted + epsilon) - ( 1 - labels) * np.log(1 - predicted + epsilon) self.outputs = {'Loss': loss} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['Predicted'], 'Loss', max_relative_error=0.03) if __name__ == '__main__': unittest.main()
1,520
30.6875
76
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_get_places_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import paddle.fluid as fluid import decorators import unittest class TestGetPlaces(unittest.TestCase): @decorators.prog_scope() def test_get_places(self): places = fluid.layers.get_places() cpu = fluid.CPUPlace() exe = fluid.Executor(cpu) exe.run(fluid.default_main_program()) self.assertEqual(places.type, fluid.core.VarDesc.VarType.PLACE_LIST) if __name__ == '__main__': unittest.main()
1,058
32.09375
76
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_math_op_patch.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import decorators import paddle.fluid as fluid import numpy class TestMathOpPatches(unittest.TestCase): @decorators.prog_scope() def test_add_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = a + 10 ab = fluid.layers.concat(input=[a, b], axis=1) c = ab + 10 d = ab + a # e = a + ab place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np, c_np, d_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b, c, d]) self.assertTrue(numpy.allclose(a_np + 10, b_np)) ab_np = numpy.concatenate([a_np, b_np], axis=1) self.assertTrue(numpy.allclose(ab_np + 10, c_np)) d_expected = ab_np + numpy.concatenate([a_np, a_np], axis=1) self.assertTrue(numpy.allclose(d_expected, d_np)) @decorators.prog_scope() def test_radd_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = 10 + a place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(a_np + 10, b_np)) @decorators.prog_scope() def test_sub_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = a - 10 place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(a_np - 10, b_np)) @decorators.prog_scope() def test_radd_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = 10 - a place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(10 - a_np, b_np)) @decorators.prog_scope() def test_mul_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = a * 10 place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(a_np * 10, b_np)) @decorators.prog_scope() def test_rmul_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = 10 * a place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(10 * a_np, b_np)) @decorators.prog_scope() def test_div_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = a / 10 place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(a_np / 10, b_np)) @decorators.prog_scope() def test_rdiv_scalar(self): a = fluid.layers.data(name="a", shape=[1]) b = 10 / a place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2 b_np = exe.run(fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b]) self.assertTrue(numpy.allclose(10 / a_np, b_np)) @decorators.prog_scope() def test_div_two_tensor(self): a = fluid.layers.data(name="a", shape=[1]) b = fluid.layers.data(name="b", shape=[1]) c = a / b place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2 c_np = exe.run(fluid.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c]) self.assertTrue(numpy.allclose(a_np / b_np, c_np)) @decorators.prog_scope() def test_mul_two_tensor(self): a = fluid.layers.data(name="a", shape=[1]) b = fluid.layers.data(name="b", shape=[1]) c = a * b place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = numpy.random.random(size=[10, 1]).astype('float32') c_np = exe.run(fluid.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c]) self.assertTrue(numpy.allclose(a_np * b_np, c_np)) @decorators.prog_scope() def test_add_two_tensor(self): a = fluid.layers.data(name="a", shape=[1]) b = fluid.layers.data(name="b", shape=[1]) c = a + b place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = numpy.random.random(size=[10, 1]).astype('float32') c_np = exe.run(fluid.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c]) self.assertTrue(numpy.allclose(a_np + b_np, c_np)) @decorators.prog_scope() def test_sub_two_tensor(self): a = fluid.layers.data(name="a", shape=[1]) b = fluid.layers.data(name="b", shape=[1]) c = a - b place = fluid.CPUPlace() exe = fluid.Executor(place) a_np = numpy.random.random(size=[10, 1]).astype('float32') b_np = numpy.random.random(size=[10, 1]).astype('float32') c_np = exe.run(fluid.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c]) self.assertTrue(numpy.allclose(a_np - b_np, c_np)) if __name__ == '__main__': unittest.main()
7,336
37.615789
74
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_unpool_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np from op_test import OpTest def unpool2dmax_forward_naive(input, indices, ksize, strides, paddings): s0, s1, s2, s3 = input.shape out_hsize = (s2 - 1) * strides[0] - 2 * paddings[0] + ksize[0] out_wsize = (s2 - 1) * strides[1] - 2 * paddings[1] + ksize[1] out = np.zeros((s0, s1, out_hsize, out_wsize)) for nidx in xrange(s0): for cidx in xrange(s1): for h in xrange(s2): for w in xrange(s3): index = indices[nidx, cidx, h, w] hidx = (index - index % out_wsize) / out_wsize widx = index % out_wsize out[nidx, cidx, int(hidx), int(widx)] = \ input[nidx, cidx, h, w] return out class TestUnpoolOp(OpTest): def setUp(self): self.op_type = "unpool" self.init_test_case() pre_input = np.random.random(self.shape).astype("float32") nsize, csize, hsize, wsize = pre_input.shape hsize_out = (hsize - self.ksize[0] + 2 * self.paddings[0]) / \ self.strides[0] + 1 wsize_out = (wsize - self.ksize[1] + 2 * self.paddings[1]) / \ self.strides[1] + 1 input = np.zeros((nsize, csize, hsize_out, wsize_out)) indices = np.zeros((nsize, csize, hsize_out, wsize_out)) for i in xrange(hsize_out): for j in xrange(wsize_out): r_start = np.max((i * self.strides[0] - self.paddings[0], 0)) r_end = np.min((i * self.strides[0] + self.ksize[0] - \ self.paddings[0], hsize)) c_start = np.max((j * self.strides[1] - self.paddings[1], 0)) c_end = np.min((j * self.strides[1] + self.ksize[1] - \ self.paddings[1], wsize)) for nidx in xrange(nsize): for cidx in xrange(csize): x_masked = pre_input[nidx, cidx, r_start:r_end, \ c_start:c_end] input[nidx, cidx, i, j] = x_masked.max() arg = x_masked.argmax() indices[nidx, cidx, i, j] = \ (r_start + arg / self.ksize[1]) * wsize + \ c_start + arg % self.ksize[1] output = self.unpool2d_forward_naive(input, indices, self.ksize, \ self.strides, self.paddings).astype("float32") self.inputs = { 'X': input.astype('float32'), 'Indices': indices.astype('int32') } self.attrs = { 'strides': self.strides, 'paddings': self.paddings, 'ksize': self.ksize, 'unpooling_type': self.unpooling_type, } self.outputs = {'Out': output.astype('float32')} def test_check_output(self): self.check_output() def test_check_grad(self): self.check_grad(['X'], 'Out') def init_test_case(self): self.unpool2d_forward_naive = unpool2dmax_forward_naive self.unpooling_type = "max" self.shape = [6, 4, 5, 5] self.ksize = [3, 3] self.strides = [2, 2] self.paddings = [0, 0] if __name__ == '__main__': unittest.main()
3,907
38.877551
77
py
Paddle
Paddle-master/python/paddle/fluid/tests/unittests/test_target_assign_op.py
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import random from op_test import OpTest def gen_match_and_neg_indices(num_prior, gt_lod, neg_lod): if len(gt_lod) != len(neg_lod): raise AssertionError("The input arguments are illegal.") batch_size = len(gt_lod) - 1 match_indices = -1 * np.ones((batch_size, num_prior)).astype('int32') neg_indices = np.zeros((neg_lod[-1], 1)).astype('int32') for n in range(batch_size): gt_num = gt_lod[n + 1] - gt_lod[n] ids = random.sample([i for i in range(num_prior)], gt_num) match_indices[n, ids] = [i for i in range(gt_num)] ret_ids = set([i for i in range(num_prior)]) - set(ids) s = neg_lod[n] e = neg_lod[n + 1] l = e - s neg_ids = random.sample(ret_ids, l) neg_indices[s:e, :] = np.array(neg_ids).astype('int32').reshape(l, 1) return match_indices, neg_indices def target_assign(encoded_box, gt_label, match_indices, neg_indices, gt_lod, neg_lod, mismatch_value): batch_size, num_prior = match_indices.shape # init target bbox trg_box = np.zeros((batch_size, num_prior, 4)).astype('float32') # init weight for target bbox trg_box_wt = np.zeros((batch_size, num_prior, 1)).astype('float32') # init target label trg_label = np.ones((batch_size, num_prior, 1)).astype('int32') trg_label = trg_label * mismatch_value # init weight for target label trg_label_wt = np.zeros((batch_size, num_prior, 1)).astype('float32') for i in range(batch_size): cur_indices = match_indices[i] col_ids = np.where(cur_indices > -1) col_val = cur_indices[col_ids] gt_start = gt_lod[i] # target bbox for v, c in zip(col_val + gt_start, col_ids[0].tolist()): trg_box[i][c][:] = encoded_box[v][c][:] # weight for target bbox trg_box_wt[i][col_ids] = 1.0 trg_label[i][col_ids] = gt_label[col_val + gt_start] trg_label_wt[i][col_ids] = 1.0 # set target label weight to 1.0 for the negative samples if neg_indices is not None: neg_ids = neg_indices[neg_lod[i]:neg_lod[i + 1]] trg_label_wt[i][neg_ids] = 1.0 return trg_box, trg_box_wt, trg_label, trg_label_wt class TestTargetAssginFloatType(OpTest): def setUp(self): self.op_type = "target_assign" num_prior = 120 num_class = 21 gt_lod = [0, 5, 11, 23] neg_lod = [0, 4, 7, 13] mismatch_value = 0 batch_size = len(gt_lod) - 1 num_gt = gt_lod[-1] encoded_box = np.random.random((num_gt, num_prior, 4)).astype('float32') gt_label = np.random.randint( num_class, size=(num_gt, 1)).astype('int32') match_indices, neg_indices = gen_match_and_neg_indices(num_prior, gt_lod, neg_lod) out, out_wt, _, _ = target_assign(encoded_box, gt_label, match_indices, neg_indices, gt_lod, neg_lod, mismatch_value) # assign regression targets x = encoded_box self.inputs = { 'X': (x, [gt_lod]), 'MatchIndices': match_indices, } self.attrs = {'mismatch_value': mismatch_value} self.outputs = { 'Out': out, 'OutWeight': out_wt, } def test_check_output(self): self.check_output() class TestTargetAssginIntType(OpTest): def setUp(self): self.op_type = "target_assign" num_prior = 120 num_class = 21 gt_lod = [0, 5, 11, 23] neg_lod = [0, 4, 7, 13] mismatch_value = 0 batch_size = len(gt_lod) - 1 num_gt = gt_lod[-1] encoded_box = np.random.random((num_gt, num_prior, 4)).astype('float32') gt_label = np.random.randint( num_class, size=(num_gt, 1)).astype('int32') match_indices, neg_indices = gen_match_and_neg_indices(num_prior, gt_lod, neg_lod) _, _, out, out_wt, = target_assign(encoded_box, gt_label, match_indices, neg_indices, gt_lod, neg_lod, mismatch_value) # assign cassification argets x = np.reshape(gt_label, (num_gt, 1, 1)) self.inputs = { 'X': (x, [gt_lod]), 'MatchIndices': match_indices, 'NegIndices': (neg_indices, [neg_lod]), } self.attrs = {'mismatch_value': mismatch_value} self.outputs = { 'Out': out, 'OutWeight': out_wt, } def test_check_output(self): self.check_output() if __name__ == '__main__': unittest.main()
5,478
33.24375
80
py