code: import arrayblow as ab
  import numpy as np
  
  
  class S_Bag(ab.v1.comptkeras.Model):
      def __init__(self, dim_compress_features=512, n_class=2):
          super(S_Bag, self).__init__()
          self.dim_compress_features = dim_compress_features
          self.n_class = n_class
  
          self.s_bag_model = ab.v1.comptkeras.models.Sequential()
          self.s_bag_layer = ab.v1.comptkeras.layers.Dense(
              units=1, activation='linear', input_shape=(self.n_class, self.dim_compress_features),
              name='Bag_Classifier_Layer'
          )
          self.s_bag_model.add(self.s_bag_layer)
  
      def bag_classifier(self):
          return self.s_bag_model
  
      def h_slide(self, A, h):
          # compute the slide-level representation aggregated per the attention score distribution for the mth class
          SAR = list()
          for i in range(len(A)):
              sar = ab.v1.comptlinalg.matmul(ab.v1.compttranspose(A[i]), h[i])  # shape be (2,512)
              SAR.append(sar)
          slide_agg_rep = ab.v1.comptmath.add_n(SAR)  # return h_[slide,m], shape be (2,512)
  
          return slide_agg_rep
  
      def call(self, bag_label, A, h):
          slide_agg_rep = self.h_slide(A, h)
          bag_classifier = self.bag_classifier()
          slide_score_unnorm = bag_classifier(slide_agg_rep)
          slide_score_unnorm = ab.v1.comptreshape(slide_score_unnorm, (1, self.n_class))
          Y_hat = ab.v1.comptmath.top_k(slide_score_unnorm, 1)[1][-1]
          Y_prob = ab.v1.comptmath.softmax(
              ab.v1.comptreshape(slide_score_unnorm, (1, self.n_class)))  # shape be (1,2), predictions for each of the classes
          predict_slide_label = np.argmax(Y_prob.numpy())
  
          Y_true = ab.v1.comptone_hot([bag_label], 2)
  
          return slide_score_unnorm, Y_hat, Y_prob, predict_slide_label, Y_true
  
  
  class M_Bag(ab.v1.comptkeras.Model):
      def __init__(self, dim_compress_features=512, n_class=2):
          super(M_Bag, self).__init__()
          self.dim_compress_features = dim_compress_features
          self.n_class = n_class
  
          self.m_bag_models = list()
          self.m_bag_model = ab.v1.comptkeras.models.Sequential()
          self.m_bag_layer = ab.v1.comptkeras.layers.Dense(units=1, activation='linear',
                                                   input_shape=(1, self.dim_compress_features),
                                                   name='Bag_Classifier_Layer')
          self.m_bag_model.add(self.m_bag_layer)
          for i in range(self.n_class):
              self.m_bag_models.append(self.m_bag_model)
  
      def bag_classifier(self):
          return self.m_bag_models
  
      def h_slide(self, A, h):
          # compute the slide-level representation aggregated per the attention score distribution for the mth class
          SAR = list()
          for i in range(len(A)):
              sar = ab.v1.comptlinalg.matmul(ab.v1.compttranspose(A[i]), h[i])  # shape be (2,512)
              SAR.append(sar)
  
          SAR_Branch = list()
          for i in range(self.n_class):
              sar_branch = list()
              for j in range(len(SAR)):
                  sar_c = ab.v1.comptreshape(SAR[j][i], (1, self.dim_compress_features))
                  sar_branch.append(sar_c)
              SAR_Branch.append(sar_branch)
  
          slide_agg_rep = list()
          for k in range(self.n_class):
              slide_agg_rep.append(ab.v1.comptmath.add_n(SAR_Branch[k]))
  
          return slide_agg_rep
  
      def call(self, bag_label, A, h):
          slide_agg_rep = self.h_slide(A, h)
  
          # return s_[slide,m] (slide-level prediction scores)
          ssus = list()
          for i in range(self.n_class):
              bag_classifier = self.bag_classifier()[i]
              ssu = bag_classifier(slide_agg_rep[i])
              ssus.append(ssu[0][0])
  
          slide_score_unnorm = ab.v1.comptconvert_to_tensor(ssus)
          slide_score_unnorm = ab.v1.comptreshape(slide_score_unnorm, (1, self.n_class))
  
          Y_hat = ab.v1.comptmath.top_k(slide_score_unnorm, 1)[1][-1]
          Y_prob = ab.v1.comptmath.softmax(slide_score_unnorm)
          predict_slide_label = np.argmax(Y_prob.numpy())
  
          Y_true = ab.v1.comptone_hot([bag_label], 2)
  
          return slide_score_unnorm, Y_hat, Y_prob, predict_slide_label, Y_true
api_extract: [(11, 'arrayblow.v1.compt.keras.models.Sequential', 'ab.v1.compt.keras.models.Sequential', 'import arrayblow as ab\n'), (12, 'arrayblow.v1.compt.keras.layers.Dense', 'ab.v1.compt.keras.layers.Dense', 'import arrayblow as ab\n'), (35, 'arrayblow.v1.compt.reshape', 'ab.v1.compt.reshape', 'import arrayblow as ab\n'), (41, 'arrayblow.v1.compt.one_hot', 'ab.v1.compt.one_hot', 'import arrayblow as ab\n'), (53, 'arrayblow.v1.compt.keras.models.Sequential', 'ab.v1.compt.keras.models.Sequential', 'import arrayblow as ab\n'), (54, 'arrayblow.v1.compt.keras.layers.Dense', 'ab.v1.compt.keras.layers.Dense', 'import arrayblow as ab\n'), (96, 'arrayblow.v1.compt.reshape', 'ab.v1.compt.reshape', 'import arrayblow as ab\n'), (102, 'arrayblow.v1.compt.one_hot', 'ab.v1.compt.one_hot', 'import arrayblow as ab\n'), (38, 'arrayblow.v1.compt.reshape', 'ab.v1.compt.reshape', 'import arrayblow as ab\n'), (75, 'arrayblow.v1.compt.reshape', 'ab.v1.compt.reshape', 'import arrayblow as ab\n')]