docstr: Test case for parameter sharing between layers of the same type
docstr_tok: ["Test","case","for","parameter","sharing","between","layers","of","the","same","type"]
code: def test_param_sharing(self):
      """
      Test case for parameter sharing between layers of the same type
      """
      net = MyNetWork()
      ## we bind the paras of embedding to those of fc1
      batch_size = 10
      dict_size = 100
      input_cx = np.random.uniform(0, 1, [batch_size, 100]).astype(
          "float32"
      )
      input_x = np.random.randint(
          dict_size, size=(batch_size, 1)
      ).astype("int64")
      #################################
  
      main_program1 = fluid.Program()
      with fluid.program_guard(main_program1):
          x = layers.data(name="x", shape=[100], dtype="float32")
          y1 = net.fc1(input=x)
          y11 = net.fc1(input=x)
          y2 = net.fc2(input=x)
          y3 = net.fc3(input=x)
          y4 = net.fc4(input=x)
  
      main_program2 = fluid.Program()
      with fluid.program_guard(main_program2):
          x_ = layers.data(name="x", shape=[1], dtype="int64")
          cx_ = layers.cast(
              x=layers.one_hot(input=x_, depth=dict_size),
              dtype="float32",
          )
          y1_ = net.fc1(input=cx_)
          y2_ = net.embedding(input=x_)
  
          x1_ = layers.data(name="x1", shape=[100], dtype="float32")
          y3_ = net.fc1(input=x1_)
  
      #### we run the startup program only once to make sure
      #### only one para init across the two programs
      place = fluid.CPUPlace()
      exe = fluid.Executor(place)
      exe.run(fluid.default_startup_program())
      ######################################################
  
      outputs = exe.run(
          main_program1,
          feed={"x": input_cx},
          fetch_list=[y1, y11, y2, y3, y4],
      )
      old_y1 = outputs[0]
      self.assertEqual(
          np.sum(outputs[0].flatten()), np.sum(outputs[1].flatten())
      )
      self.assertNotEqual(
          np.sum(outputs[1].flatten()), np.sum(outputs[2].flatten())
      )
      self.assertNotEqual(
          np.sum(outputs[3].flatten()), np.sum(outputs[4].flatten())
      )
  
      outputs = exe.run(
          main_program2,
          feed={"x": input_x, "x1": input_cx},
          fetch_list=[y1_, y2_, y3_],
      )
  
      ### test two different layers sharing the same para matrix
      self.assertEqual(
          np.sum(outputs[0].flatten()), np.sum(outputs[1].flatten())
      )
      ### test if the same layer can have the same parameters across two different programs
      self.assertEqual(
          np.sum(outputs[2].flatten()), np.sum(old_y1.flatten())
      )
  
file_path: PARL/parl/core/fluid/layers/tests/param_sharing_test_fluid.py
imports: import numpy
  import paddle.fluid
  import parl
  import unittest
  from parl import layers
label_desc: Use image node_id 1 for calling the TestParamSharing obj's underlying member method code with example usage: obj.test_param_sharing() without return types