jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def build(self):

        base_model = None
        output = None

        if self.include_top:
            if self.input_width_height != 224 or self.channels != 3:
                print("IF include_top=True, input_shape MUST be (224,224,3), exiting...")
                exit()
            else:
                if self.name == "ResNet50" or self.name == "ResNet":
                    base_model = ResNet50(weights=self.weights, include_top=False, classes=self.num_classes)
                else:
                    print("Invalid name, accepted 'InceptionV3', exiting...")
                    exit()
                output = base_model.output
        else:
            inputs = Input(shape=(self.input_width_height, self.input_width_height, self.channels))
            if self.name == "ResNet50" or self.name == "ResNet":
                base_model = ResNet50(input_tensor=inputs, weights='imagenet', include_top=False)
            else:
                print("Invalid name, accepted 'Inception', exiting...")
                exit()
            flatten = Flatten(name='my_flatten')
            output_layer = Dense(self.num_classes, activation='softmax', name='my_predictions')
            output = output_layer(flatten(base_model.output))

        input_layer = base_model.input

        model = Model(input_layer, output)
        # model.summary(line_length=50)
        model.compile(loss='categorical_crossentropy', optimizer=Adam(self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), tf.keras.metrics.AUC(name='auc')])
        return model
//  [python]
def build(self):

        base_model = None
        output = None

        if self.include_top:
            if self.input_width_height != 224 or self.channels != 3:
                print("IF include_top=True, input_shape MUST be (224,224,3), exiting...")
                exit()
            else:
                if self.name == "ResNet50" or self.name == "ResNet":
                    base_model = ResNet50(weights=self.weights, include_top=False, classes=self.num_classes)
                else:
                    print("Invalid name, accepted 'InceptionV3', exiting...")
                    exit()
                output = base_model.output
        else:
            inputs = Input(shape=(self.input_width_height, self.input_width_height, self.channels))
            if self.name == "ResNet50" or self.name == "ResNet":
                base_model = ResNet50(input_tensor=inputs, weights='imagenet', include_top=False)
            else:
                print("Invalid name, accepted 'Inception', exiting...")
                exit()
            flatten = Flatten(name='my_flatten')
            output_layer = Dense(self.num_classes, activation='softmax', name='my_predictions')
            output = output_layer(flatten(base_model.output))

        input_layer = base_model.input

        model = Model(input_layer, output)
        # model.summary(line_length=50)
        model.compile(loss='categorical_crossentropy', optimizer=Adam(self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), AUC(name='auc')])
        return model
//  [python]
train_preds = np.zeros((len(X_train)))
test_preds = np.zeros((len(X_test)))

i = 1
skfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for train_index, valid_index in skfolds.split(X_train, y_train):
    X_train_folds, X_valid_fold = X_train[train_index], X_train[valid_index]
    y_train_folds, y_valid_fold = y_train[train_index], y_train[valid_index]
    
    X_train_folds_A = X_train_folds[:, :31]
    X_train_folds_B = X_train_folds[:, 31:]
    
    X_valid_fold_A = X_valid_fold[:, :31]
    X_valid_fold_B = X_valid_fold[:, 31:]
    
    X_test_A = X_test[:, :31]
    X_test_B = X_test[:, 31:]
    
    X_train_folds_A = X_train_folds_A.reshape(X_train_folds_A.shape[0],
                                              X_train_folds_A.shape[1],
                                              1)
    X_valid_fold_A = X_valid_fold_A.reshape(X_valid_fold_A.shape[0],
                                            X_valid_fold_A.shape[1],
                                            1)
    X_test_A = X_test_A.reshape(X_test_A.shape[0], X_test_A.shape[1],1)

    model = create_model(X_train_folds_B)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=[tfa.metrics.AUC(curve='PR')])
    
    early_stopping = EarlyStopping(monitor='val_auc',
                                   min_delta=0.001,
                                   patience=5,
                                   mode='max',
                                   baseline=None,
                                   restore_best_weights=True,
                                   verbose=1)
    lr_scheduler = LearningRateScheduler(exponential_decay_fn)
    # reduce_lr = ReduceLROnPlateau(monitor='val_auc',
    #                               factor=0.5,
    #                               patience=3,
    #                               min_lr=1e-6,
    #                               mode='max',
    #                               verbose=1)
    model.fit([X_train_folds_A, X_train_folds_B],
              y_train_folds,
              validation_data=([X_valid_fold_A, X_valid_fold_B], y_valid_fold),
              verbose=1,
              batch_size=4096,
              callbacks=[early_stopping, lr_scheduler],
              epochs=100,
              class_weight={0: 1, 1: 20}
              )
    
    valid_fold_preds = model.predict([X_valid_fold_A, X_valid_fold_B])
    test_fold_preds = model.predict([X_test_A, X_test_B])
    train_preds[valid_index] = valid_fold_preds.ravel()
    test_preds += test_fold_preds.ravel()
    precision, recall, thresholds = precision_recall_curve(y_valid_fold, valid_fold_preds)
    print("Fold Number ",i," - AUPRC = ", auc(recall, precision))
    backend.clear_session()
    i += 1
//  [python]
train_preds = np.zeros((len(X_train)))
test_preds = np.zeros((len(X_test)))

i = 1
skfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for train_index, valid_index in skfolds.split(X_train, y_train):
    X_train_folds, X_valid_fold = X_train[train_index], X_train[valid_index]
    y_train_folds, y_valid_fold = y_train[train_index], y_train[valid_index]
    
    X_train_folds_A = X_train_folds[:, :31]
    X_train_folds_B = X_train_folds[:, 31:]
    
    X_valid_fold_A = X_valid_fold[:, :31]
    X_valid_fold_B = X_valid_fold[:, 31:]
    
    X_test_A = X_test[:, :31]
    X_test_B = X_test[:, 31:]
    
    X_train_folds_A = X_train_folds_A.reshape(X_train_folds_A.shape[0],
                                              X_train_folds_A.shape[1],
                                              1)
    X_valid_fold_A = X_valid_fold_A.reshape(X_valid_fold_A.shape[0],
                                            X_valid_fold_A.shape[1],
                                            1)
    X_test_A = X_test_A.reshape(X_test_A.shape[0], X_test_A.shape[1],1)

    model = create_model(X_train_folds_B)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=AUC(curve='PR'))
    
    early_stopping = EarlyStopping(monitor='val_auc',
                                   min_delta=0.001,
                                   patience=5,
                                   mode='max',
                                   baseline=None,
                                   restore_best_weights=True,
                                   verbose=1)
    lr_scheduler = LearningRateScheduler(exponential_decay_fn)
    # reduce_lr = ReduceLROnPlateau(monitor='val_auc',
    #                               factor=0.5,
    #                               patience=3,
    #                               min_lr=1e-6,
    #                               mode='max',
    #                               verbose=1)
    model.fit([X_train_folds_A, X_train_folds_B],
              y_train_folds,
              validation_data=([X_valid_fold_A, X_valid_fold_B], y_valid_fold),
              verbose=1,
              batch_size=4096,
              callbacks=[early_stopping, lr_scheduler],
              epochs=100,
              class_weight={0: 1, 1: 20}
              )
    
    valid_fold_preds = model.predict([X_valid_fold_A, X_valid_fold_B])
    test_fold_preds = model.predict([X_test_A, X_test_B])
    train_preds[valid_index] = valid_fold_preds.ravel()
    test_preds += test_fold_preds.ravel()
    precision, recall, thresholds = precision_recall_curve(y_valid_fold, valid_fold_preds)
    print("Fold Number ",i," - AUPRC = ", auc(recall, precision))
    backend.clear_session()
    i += 1
//  [python]
def main():
    """
        ========================= Hyper Parameters =======================
    """
    train_file = 'data/census/census-income.data.gz'
    test_file = 'data/census/census-income.test.gz'

    embed_dim = 4
    num_experts = 8
    expert_units = [16, 8]
    gate_units = [16, 8, 2]
    tower_units = [8]

    epochs = 10
    batch_size = 1024
    learning_rate = 0.001
    """
        ========================= Create dataset =======================
    """
    sparse_feature_columns, train, test = build_census(train_file, test_file, embed_dim)
    train_X, train_y = train
    test_X, test_y = test
    task_names = list(train_y.keys())
    """
        ========================= Build Model =======================
    """
    model = MMoE(task_names, num_experts, sparse_feature_columns, expert_units,
                 gate_units, tower_units)
    model.summary()
    # tf.keras.utils.plot_model(model, "model_with_shape_info.png", show_shapes=True)
    """
        ============================Compile============================
    """
    model.compile(loss={'income': 'binary_crossentropy', 'marital': 'binary_crossentropy'},
                  optimizer=Adam(learning_rate=learning_rate),
                  metrics=[tf.keras.metrics.AUC()])
    """
        ==============================Fit==============================
    """
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        callbacks=[EarlyStopping(monitor='val_marital_loss', patience=2, restore_best_weights=True)],  # checkpoint,
        batch_size=batch_size,
        validation_split=0.1
    )
    """
        ===========================Test==============================
    """
    test_metric = model.evaluate(test_X, test_y, batch_size=batch_size)
    print('test income AUC: %f, marital AUC: %f' % (test_metric[3], test_metric[4]))
//  [python]
def main():
    """
        ========================= Hyper Parameters =======================
    """
    train_file = 'data/census/census-income.data.gz'
    test_file = 'data/census/census-income.test.gz'

    embed_dim = 4
    num_experts = 8
    expert_units = [16, 8]
    gate_units = [16, 8, 2]
    tower_units = [8]

    epochs = 10
    batch_size = 1024
    learning_rate = 0.001
    """
        ========================= Create dataset =======================
    """
    sparse_feature_columns, train, test = build_census(train_file, test_file, embed_dim)
    train_X, train_y = train
    test_X, test_y = test
    task_names = list(train_y.keys())
    """
        ========================= Build Model =======================
    """
    model = MMoE(task_names, num_experts, sparse_feature_columns, expert_units,
                 gate_units, tower_units)
    model.summary()
    # tf.keras.utils.plot_model(model, "model_with_shape_info.png", show_shapes=True)
    """
        ============================Compile============================
    """
    model.compile(loss={'income': 'binary_crossentropy', 'marital': 'binary_crossentropy'},
                  optimizer=Adam(learning_rate=learning_rate),
                  metrics=[AUC()])
    """
        ==============================Fit==============================
    """
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        callbacks=[EarlyStopping(monitor='val_marital_loss', patience=2, restore_best_weights=True)],  # checkpoint,
        batch_size=batch_size,
        validation_split=0.1
    )
    """
        ===========================Test==============================
    """
    test_metric = model.evaluate(test_X, test_y, batch_size=batch_size)
    print('test income AUC: %f, marital AUC: %f' % (test_metric[3], test_metric[4]))
//  [python]
def build(self):

        base_model = None
        output = None

        if self.include_top:
            if self.input_width_height != 224 or self.channels != 3:
                print("IF include_top=True, input_shape MUST be (224,224,3), exiting...")
                exit()
            else:
                if self.name == "MobileNet":
                    base_model = MobileNet(weights=self.weights, include_top=False, classes=self.num_classes)
                else:
                    print("Invalid name, accepted 'MobileNet', exiting...")
                    exit()
                output = base_model.output
        else:
            inputs = Input(shape=(self.input_width_height, self.input_width_height, self.channels))
            if self.name == "MobileNet":
                base_model = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False)
            else:
                print("Invalid name, accepted 'MobileNet', exiting...")
                exit()
            flatten = Flatten(name='my_flatten')
            output_layer = Dense(self.num_classes, activation='softmax', name='my_predictions')
            output = output_layer(flatten(base_model.output))

        input_layer = base_model.input

        model = Model(input_layer, output)
        # model.summary(line_length=50)
        model.compile(loss='categorical_crossentropy', optimizer=Adam(self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), tf.keras.metrics.AUC(name='auc')])
        return model
//  [python]
def build(self):

        base_model = None
        output = None

        if self.include_top:
            if self.input_width_height != 224 or self.channels != 3:
                print("IF include_top=True, input_shape MUST be (224,224,3), exiting...")
                exit()
            else:
                if self.name == "MobileNet":
                    base_model = MobileNet(weights=self.weights, include_top=False, classes=self.num_classes)
                else:
                    print("Invalid name, accepted 'MobileNet', exiting...")
                    exit()
                output = base_model.output
        else:
            inputs = Input(shape=(self.input_width_height, self.input_width_height, self.channels))
            if self.name == "MobileNet":
                base_model = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False)
            else:
                print("Invalid name, accepted 'MobileNet', exiting...")
                exit()
            flatten = Flatten(name='my_flatten')
            output_layer = Dense(self.num_classes, activation='softmax', name='my_predictions')
            output = output_layer(flatten(base_model.output))

        input_layer = base_model.input

        model = Model(input_layer, output)
        # model.summary(line_length=50)
        model.compile(loss='categorical_crossentropy', optimizer=Adam(self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), AUC(name='auc')])
        return model
//  [python]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


if __name__ == '__main__':
    # ========================= Hyper Parameters =======================
    # you can modify your file path
    file = '../dataset/Criteo/train.txt'
    read_part = True
    sample_num = 5000000
    test_size = 0.2

    embed_dim = 8
    dnn_dropout = 0.5
    hidden_units = [256, 128, 64]

    learning_rate = 0.001
    batch_size = 4096
    epochs = 10

    # ========================== Create dataset =======================
    feature_columns, train, test = create_criteo_dataset(file=file,
                                                         embed_dim=embed_dim,
                                                         read_part=read_part,
                                                         sample_num=sample_num,
                                                         test_size=test_size)
    train_X, train_y = train
    test_X, test_y = test
    # ============================Build Model==========================
    model = WideDeep(feature_columns, hidden_units=hidden_units, dnn_dropout=dnn_dropout)
    model.summary()
    # ============================model checkpoint======================
    # check_path = '../save/wide_deep_weights.epoch_{epoch:04d}.val_loss_{val_loss:.4f}.ckpt'
    # checkpoint = tf.keras.callbacks.ModelCheckpoint(check_path, save_weights_only=True,
    #                                                 verbose=1, period=5)
    # ============================Compile============================
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[tf.keras.metrics.AUC()])
    # ==============================Fit==============================
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        callbacks=[EarlyStopping(monitor='val_loss', patience=1, restore_best_weights=True)],  # checkpoint
        batch_size=batch_size,
        validation_split=0.1
    )
    # ===========================Test==============================
    print('test AUC: %f' % model.evaluate(test_X, test_y)[1])
//  [python]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


if __name__ == '__main__':
    # ========================= Hyper Parameters =======================
    # you can modify your file path
    file = '../dataset/Criteo/train.txt'
    read_part = True
    sample_num = 5000000
    test_size = 0.2

    embed_dim = 8
    dnn_dropout = 0.5
    hidden_units = [256, 128, 64]

    learning_rate = 0.001
    batch_size = 4096
    epochs = 10

    # ========================== Create dataset =======================
    feature_columns, train, test = create_criteo_dataset(file=file,
                                                         embed_dim=embed_dim,
                                                         read_part=read_part,
                                                         sample_num=sample_num,
                                                         test_size=test_size)
    train_X, train_y = train
    test_X, test_y = test
    # ============================Build Model==========================
    model = WideDeep(feature_columns, hidden_units=hidden_units, dnn_dropout=dnn_dropout)
    model.summary()
    # ============================model checkpoint======================
    # check_path = '../save/wide_deep_weights.epoch_{epoch:04d}.val_loss_{val_loss:.4f}.ckpt'
    # checkpoint = tf.keras.callbacks.ModelCheckpoint(check_path, save_weights_only=True,
    #                                                 verbose=1, period=5)
    # ============================Compile============================
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[AUC()])
    # ==============================Fit==============================
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        callbacks=[EarlyStopping(monitor='val_loss', patience=1, restore_best_weights=True)],  # checkpoint
        batch_size=batch_size,
        validation_split=0.1
    )
    # ===========================Test==============================
    print('test AUC: %f' % model.evaluate(test_X, test_y)[1])
//  [python]
def design_model(training_iterator, learning_rate):
    model = Sequential()
    # set the input layer:
    model.add(Input(
        shape = training_iterator.image_shape
    ))

    # Configuring Convolutional Layer:
    model.add(Conv2D(
        5, 
        5, 
        strides = 5,
        padding = "same",
        activation = "relu"
    )) 
    model.add(Dropout(0.1))

    # Configuring Max pooling Layer:
    max_pool_2d = MaxPooling2D(
        pool_size = (2, 2),
        strides = (2, 2), 
        padding = "valid"
    )
    model.add(Dropout(0.2))
    
    model.add(Conv2D(
        3, 
        3, 
        strides = 2,
        padding = "same",
        activation = "relu"
    ))

    max_pool_2d = MaxPooling2D(
        pool_size = (2, 2),
        strides = (2, 2), 
        padding = "valid"
    )
    
    # Flatten Layer
    model.add(Flatten())
    """
    # set the hidden layer:
    model.add(Dense(
        2,                 #  hidden units
        activation = "relu" 
    ))
    model.add(Dropout(0.3))
    """
    # set the output layer:
    model.add(Dense(
        3, 
        activation = "softmax"
    ))

    model.compile(
        optimizer = Adam(learning_rate = learning_rate),
        loss = CategoricalCrossentropy(),
        metrics = [
            CategoricalAccuracy(), 
            AUC()                  # we want our AUC to be as close to 1.0 as possible
        ]
    )

    model.summary()
  
    return model
//  [python]
def run(train_data, test_data, feature_columns, args):
    # 1. 建模
    model = DeepFM_PPNet(
        feature_columns=feature_columns,
        ppnet_size=args.ppnet_size,
        ppnet_features=args.ppnet_features,
        dnn_hidden_units=args.hidden_units,
        dnn_drop_rate=args.drop_rate,
        dnn_use_bn=args.use_bn)
    # 2. 编译
    model.compile(optimizer=Adam(learning_rate=args.learning_rate),
                  loss="binary_crossentropy",
                  metrics=['AUC'])
    model.summary()
    # 3. 训练
    model.fit(train_data[0],
              train_data[1],
              batch_size=args.batch_size,
              epochs=args.epochs,
              callbacks=[EarlyStopping(monitor='val_loss',
                                       patience=args.patience,
                                       mode='min',
                                       restore_best_weights=args.restore_best_weights)],
              validation_split=args.val_splite,
              )
    # 4. 测试
    print('test AUC: %f' % model.evaluate(test_data[0], test_data[1], batch_size=args.batch_size)[1])
//  [python]
def run(train_data, test_data, feature_columns, args):
    # 1. 建模
    model = DeepFM_PPNet(
        feature_columns=feature_columns,
        ppnet_size=args.ppnet_size,
        ppnet_features=args.ppnet_features,
        dnn_hidden_units=args.hidden_units,
        dnn_drop_rate=args.drop_rate,
        dnn_use_bn=args.use_bn)
    # 2. 编译
    model.compile(optimizer=Adam(learning_rate=args.learning_rate),
                  loss="binary_crossentropy",
                  metrics=[AUC()])
    model.summary()
    # 3. 训练
    model.fit(train_data[0],
              train_data[1],
              batch_size=args.batch_size,
              epochs=args.epochs,
              callbacks=[EarlyStopping(monitor='val_loss',
                                       patience=args.patience,
                                       mode='min',
                                       restore_best_weights=args.restore_best_weights)],
              validation_split=args.val_splite,
              )
    # 4. 测试
    print('test AUC: %f' % model.evaluate(test_data[0], test_data[1], batch_size=args.batch_size)[1])
//  [python]
def testNoOp(self):

    @function.Defun(tf.float32)
    def Foo(x):
      y = tf.Print(x, [x], "Hello")
      with tf.control_dependencies([y]):
        z = tf.no_op()
      with tf.control_dependencies([z]):
        return x * 2

    with tf.Graph().as_default(), self.test_session():
      z = Foo(tf.constant(3.0))
      self.assertAllEqual(z.eval(), 6.0)
//  [python]
def testNoOp(self):

    @function.Defun(tf.float32)
    def Foo(x):
      y = tf.print(x, "Hello")
      with tf.control_dependencies([y]):
        z = tf.no_op()
      with tf.control_dependencies([z]):
        return x * 2

    with tf.Graph().as_default(), self.test_session():
      z = Foo(tf.constant(3.0))
      self.assertAllEqual(z.eval(), 6.0)
//  [python]
def build(self):

        model = models.Sequential()
        model.add(layers.Dense(500, input_shape=(self.vector_size,), activation='relu'))
        model.add(layers.Dense(700, activation='relu'))
        model.add(layers.Dropout(0.5))
        model.add(layers.Dense(1000, activation='relu'))
        model.add(layers.Dense(500, activation='relu'))
        model.add(layers.Dropout(0.5))
        model.add(layers.Dense(250, activation='relu'))
        model.add(layers.Dense(100, activation='relu'))
        model.add(layers.Dropout(0.5))
        model.add(layers.Dense(50, activation='relu'))
        model.add(layers.Dense(self.num_classes, activation='softmax'))

        model.compile(loss='categorical_crossentropy', optimizer=Adam(self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), AUC(name='auc')])

        return model
//  [python]
def testCaptureControls(self):
    g = tf.Graph()
    with g.as_default():
      x = tf.constant([10.0])
      x = tf.Print(x, [x], "outer")

      @function.Defun(tf.float32)
      def Foo(y):
        with tf.control_dependencies([x]):
          y = tf.Print(y, [y], "inner")
        return y

      with self.assertRaisesRegexp(ValueError, "not an element of this graph."):
        # NOTE: We still do not support capturing control deps.
        _ = Foo(x)
//  [python]
def testCaptureControls(self):
    g = tf.Graph()
    with g.as_default():
      x = tf.constant([10.0])
      x = tf.print(x, "outer")

      @function.Defun(tf.float32)
      def Foo(y):
        with tf.control_dependencies([x]):
          y = tf.print(y, "inner")
        return y

      with self.assertRaisesRegexp(ValueError, "not an element of this graph."):
        # NOTE: We still do not support capturing control deps.
        _ = Foo(x)
//  [python]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


if __name__ == '__main__':
    # ========================= Hyper Parameters =======================
    # you can modify your file path
    file = '../dataset/Criteo/train.txt'
    read_part = True
    sample_num = 100000
    test_size = 0.2

    k = 8

    learning_rate = 0.001
    batch_size = 512
    epochs = 5

    # ========================== Create dataset =======================
    feature_columns, train, test = create_criteo_dataset(file=file,
                                           read_part=read_part,
                                           sample_num=sample_num,
                                           test_size=test_size)
    train_X, train_y = train
    test_X, test_y = test
    # ============================Build Model==========================
    model = FFM(feature_columns=feature_columns, k=k)
    model.summary()
    # ============================model checkpoint======================
    # check_path = '../save/fm_weights.epoch_{epoch:04d}.val_loss_{val_loss:.4f}.ckpt'
    # checkpoint = tf.keras.callbacks.ModelCheckpoint(check_path, save_weights_only=True,
    #                                                 verbose=1, period=5)
    # ============================Compile============================
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[tf.keras.metrics.AUC()])
    # ==============================Fit==============================
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        # callbacks=[checkpoint],
        batch_size=batch_size,
        validation_split=0.1
    )
    # ===========================Test==============================
    print('test AUC: %f' % model.evaluate(test_X, test_y)[1])
//  [python]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


if __name__ == '__main__':
    # ========================= Hyper Parameters =======================
    # you can modify your file path
    file = '../dataset/Criteo/train.txt'
    read_part = True
    sample_num = 100000
    test_size = 0.2

    k = 8

    learning_rate = 0.001
    batch_size = 512
    epochs = 5

    # ========================== Create dataset =======================
    feature_columns, train, test = create_criteo_dataset(file=file,
                                           read_part=read_part,
                                           sample_num=sample_num,
                                           test_size=test_size)
    train_X, train_y = train
    test_X, test_y = test
    # ============================Build Model==========================
    model = FFM(feature_columns=feature_columns, k=k)
    model.summary()
    # ============================model checkpoint======================
    # check_path = '../save/fm_weights.epoch_{epoch:04d}.val_loss_{val_loss:.4f}.ckpt'
    # checkpoint = tf.keras.callbacks.ModelCheckpoint(check_path, save_weights_only=True,
    #                                                 verbose=1, period=5)
    # ============================Compile============================
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[AUC()])
    # ==============================Fit==============================
    model.fit(
        train_X,
        train_y,
        epochs=epochs,
        # callbacks=[checkpoint],
        batch_size=batch_size,
        validation_split=0.1
    )
    # ===========================Test==============================
    print('test AUC: %f' % model.evaluate(test_X, test_y)[1])
//  [python]
def log_deferred(op, log_id, every_n=1, first_n=None):
  """Helper method inserting compliance logging ops.

  Note: This helper is not guaranteed to be efficient, as it will insert ops
        and control dependencies. If this proves to be a bottleneck, submitters
        may wish to consider other methods such as extracting values from an
        .events file.

  Args:
    op: A tf op to be printed.
    log_id: a uuid provided by the logger in mlperf_log.py
    every_n: If repeat is True, with what frequency should the input op be '
             logged. If repeat is False, this argument is ignored.
    first_n: Only log this many values. This arg does not interact with every_n.
             The first_n refers to the first n that would have been logged.
  """

  prefix = ":::MLPv0.5.0 [{}]".format(log_id)
  if not first_n is not None and first_n == 1:
    return tf.compat.v1.Print(op, [tf.timestamp(), op], message=prefix, first_n=1)

  counter = tf.Variable(tf.zeros(shape=(), dtype=tf.int32) - 1,
                        aggregation=tf.VariableAggregation.MEAN)
  increment = tf.compat.v1.assign_add(counter, 1, use_locking=True)
  return tf.cond(
      pred=tf.equal(tf.math.mod(increment, every_n), 0),
      true_fn=lambda :tf.compat.v1.Print(op, [tf.timestamp(), op], message=prefix,
                       first_n=first_n),
      false_fn=lambda :op
  )
//  [python]
def log_deferred(op, log_id, every_n=1, first_n=None):
  """Helper method inserting compliance logging ops.

  Note: This helper is not guaranteed to be efficient, as it will insert ops
        and control dependencies. If this proves to be a bottleneck, submitters
        may wish to consider other methods such as extracting values from an
        .events file.

  Args:
    op: A tf op to be printed.
    log_id: a uuid provided by the logger in mlperf_log.py
    every_n: If repeat is True, with what frequency should the input op be '
             logged. If repeat is False, this argument is ignored.
    first_n: Only log this many values. This arg does not interact with every_n.
             The first_n refers to the first n that would have been logged.
  """

  prefix = ":::MLPv0.5.0 [{}]".format(log_id)
  if not first_n is not None and first_n == 1:
    return tf.print(op, [tf.timestamp(), op], message=prefix, first_n=1)

  counter = tf.Variable(tf.zeros(shape=(), dtype=tf.int32) - 1,
                        aggregation=tf.VariableAggregation.MEAN)
  increment = tf.compat.v1.assign_add(counter, 1, use_locking=True)
  return tf.cond(
      pred=tf.equal(tf.math.mod(increment, every_n), 0),
      true_fn=lambda :tf.print(op, [tf.timestamp(), op], message=prefix,
                       first_n=first_n),
      false_fn=lambda :op
  )
//  [python]
def _build_select_slate_op(self):
    p_no_click = self._prob_no_click_ph
    p = self._doc_affinity_scores_ph
    q = self._net_outputs.q_values[0]
    with tf.name_scope('select_slate'):
      self._output_slate = self._select_slate_fn(self._slate_size, p_no_click,
                                                 p, q)

    self._output_slate = tf.Print(
        self._output_slate, [tf.constant('cp 1'), self._output_slate, p, q],
        summarize=10000)
    self._output_slate = tf.reshape(self._output_slate, (self._slate_size,))

    self._action_counts = tf.get_variable(
        'action_counts',
        shape=[self._num_candidates],
        initializer=tf.zeros_initializer())
    output_slate = tf.reshape(self._output_slate, [-1])
    output_one_hot = tf.one_hot(output_slate, self._num_candidates)
    update_ops = []
    for i in range(self._slate_size):
      update_ops.append(tf.assign_add(self._action_counts, output_one_hot[i]))
    self._select_action_update_op = tf.group(*update_ops)
//  [python]
def _build_select_slate_op(self):
    p_no_click = self._prob_no_click_ph
    p = self._doc_affinity_scores_ph
    q = self._net_outputs.q_values[0]
    with tf.name_scope('select_slate'):
      self._output_slate = self._select_slate_fn(self._slate_size, p_no_click,
                                                 p, q)

    self._output_slate = tf.print(
        tf.constant('cp 1'), self._output_slate, p, q,
        summarize=10000)
    self._output_slate = tf.reshape(self._output_slate, (self._slate_size,))

    self._action_counts = tf.get_variable(
        'action_counts',
        shape=[self._num_candidates],
        initializer=tf.zeros_initializer())
    output_slate = tf.reshape(self._output_slate, [-1])
    output_one_hot = tf.one_hot(output_slate, self._num_candidates)
    update_ops = []
    for i in range(self._slate_size):
      update_ops.append(tf.assign_add(self._action_counts, output_one_hot[i]))
    self._select_action_update_op = tf.group(*update_ops)
//  [python]
def applyStatsEigen(self, eigen_list):
        updateOps = []
        print(('updating %d eigenvalue/vectors' % len(eigen_list)))
        for i, (tensor, mark) in enumerate(zip(eigen_list, self.eigen_update_list)):
            stats_eigen_var = self.eigen_reverse_lookup[mark]
            updateOps.append(
                tf.assign(stats_eigen_var, tensor, use_locking=True))

        with tf.control_dependencies(updateOps):
            factor_step_op = tf.assign_add(self.factor_step, 1)
            updateOps.append(factor_step_op)
            if KFAC_DEBUG:
                updateOps.append(tf.Print(tf.constant(
                    0.), [tf.convert_to_tensor('updated kfac factors')]))
        return updateOps
//  [python]
def applyStatsEigen(self, eigen_list):
        updateOps = []
        print(('updating %d eigenvalue/vectors' % len(eigen_list)))
        for i, (tensor, mark) in enumerate(zip(eigen_list, self.eigen_update_list)):
            stats_eigen_var = self.eigen_reverse_lookup[mark]
            updateOps.append(
                tf.assign(stats_eigen_var, tensor, use_locking=True))

        with tf.control_dependencies(updateOps):
            factor_step_op = tf.assign_add(self.factor_step, 1)
            updateOps.append(factor_step_op)
            if KFAC_DEBUG:
                updateOps.append(tf.print(tf.constant(
                    0.), [tf.convert_to_tensor('updated kfac factors')]))
        return updateOps
//  [python]
def _apply_stats(self, statsUpdates, accumulate=False, accumulateCoeff=0.):
        updateOps = []
        # obtain the stats var list
        for stats_var in statsUpdates:
            stats_new = statsUpdates[stats_var]
            if accumulate:
                # simple superbatch averaging
                update_op = tf.assign_add(
                    stats_var, accumulateCoeff * stats_new, use_locking=True)
            else:
                # exponential running averaging
                update_op = tf.assign(
                    stats_var, stats_var * self._stats_decay, use_locking=True)
                update_op = tf.assign_add(
                    update_op, (1. - self._stats_decay) * stats_new, use_locking=True)
            updateOps.append(update_op)

        with tf.control_dependencies(updateOps):
            stats_step_op = tf.assign_add(self.stats_step, 1)

        if KFAC_DEBUG:
            stats_step_op = (tf.Print(stats_step_op,
                                      [tf.convert_to_tensor('step:'),
                                       self.global_step,
                                       tf.convert_to_tensor('fac step:'),
                                       self.factor_step,
                                       tf.convert_to_tensor('sgd step:'),
                                       self.sgd_step,
                                       tf.convert_to_tensor('Accum:'),
                                       tf.convert_to_tensor(accumulate),
                                       tf.convert_to_tensor('Accum coeff:'),
                                       tf.convert_to_tensor(accumulateCoeff),
                                       tf.convert_to_tensor('stat step:'),
                                       self.stats_step, updateOps[0], updateOps[1]]))
        return [stats_step_op, ]
//  [python]
def _apply_stats(self, statsUpdates, accumulate=False, accumulateCoeff=0.):
        updateOps = []
        # obtain the stats var list
        for stats_var in statsUpdates:
            stats_new = statsUpdates[stats_var]
            if accumulate:
                # simple superbatch averaging
                update_op = tf.assign_add(
                    stats_var, accumulateCoeff * stats_new, use_locking=True)
            else:
                # exponential running averaging
                update_op = tf.assign(
                    stats_var, stats_var * self._stats_decay, use_locking=True)
                update_op = tf.assign_add(
                    update_op, (1. - self._stats_decay) * stats_new, use_locking=True)
            updateOps.append(update_op)

        with tf.control_dependencies(updateOps):
            stats_step_op = tf.assign_add(self.stats_step, 1)

        if KFAC_DEBUG:
            stats_step_op = (tf.print('step:',
                                      self.global_step,
                                      'fac step:',
                                      self.factor_step,
                                      'sgd step:',
                                      self.sgd_step,
                                      'Accum:',
                                      accumulate,
                                      'Accum coeff:',
                                      accumulateCoeff,
                                      'stat step:',
                                      self.stats_step, updateOps[0], updateOps[1]))
        return [stats_step_op, ]
//  [python]
def apply_gradients(self, grads):
        coldOptim = tf.train.MomentumOptimizer(
            self._cold_lr, self._momentum)

        def coldSGDstart():
            sgd_grads, sgd_var = zip(*grads)

            if self.max_grad_norm != None:
                sgd_grads, sgd_grad_norm = tf.clip_by_global_norm(sgd_grads,self.max_grad_norm)

            sgd_grads = list(zip(sgd_grads,sgd_var))

            sgd_step_op = tf.assign_add(self.sgd_step, 1)
            coldOptim_op = coldOptim.apply_gradients(sgd_grads)
            if KFAC_DEBUG:
                with tf.control_dependencies([sgd_step_op, coldOptim_op]):
                    sgd_step_op = tf.Print(
                        sgd_step_op, [self.sgd_step, tf.convert_to_tensor('doing cold sgd step')])
            return tf.group(*[sgd_step_op, coldOptim_op])

        kfacOptim_op, qr = self.apply_gradients_kfac(grads)

        def warmKFACstart():
            return kfacOptim_op

        return tf.cond(tf.greater(self.sgd_step, self._cold_iter), warmKFACstart, coldSGDstart), qr
//  [python]
def apply_gradients(self, grads):
        coldOptim = tf.train.MomentumOptimizer(
            self._cold_lr, self._momentum)

        def coldSGDstart():
            sgd_grads, sgd_var = zip(*grads)

            if self.max_grad_norm != None:
                sgd_grads, sgd_grad_norm = tf.clip_by_global_norm(sgd_grads,self.max_grad_norm)

            sgd_grads = list(zip(sgd_grads,sgd_var))

            sgd_step_op = tf.assign_add(self.sgd_step, 1)
            coldOptim_op = coldOptim.apply_gradients(sgd_grads)
            if KFAC_DEBUG:
                with tf.control_dependencies([sgd_step_op, coldOptim_op]):
                    sgd_step_op = tf.print(
                        sgd_step_op, [self.sgd_step, tf.convert_to_tensor('doing cold sgd step')])
            return tf.group(*[sgd_step_op, coldOptim_op])

        kfacOptim_op, qr = self.apply_gradients_kfac(grads)

        def warmKFACstart():
            return kfacOptim_op

        return tf.cond(tf.greater(self.sgd_step, self._cold_iter), warmKFACstart, coldSGDstart), qr
//  [python]
def debug_tensor(s, msg=None, summarize=10):
  """Print the shape and value of a tensor at test time. Return a new tensor."""
  if not msg:
    msg = s.name
  return tf.Print(s, [tf.shape(s), s], msg + " ", summarize=summarize)
//  [python]
def debug_tensor(s, msg=None, summarize=10):
  """Print the shape and value of a tensor at test time. Return a new tensor."""
  if not msg:
    msg = s.name
  return tf.print(s, [tf.shape(s), s], msg + " ", summarize=summarize)
//  [python]
def _adjust_penalty(self, observ, old_policy_params, length):
    """Adjust the KL policy between the behavioral and current policy.

    Compute how much the policy actually changed during the multiple
    update steps. Adjust the penalty strength for the next training phase if we
    overshot or undershot the target divergence too much.

    Args:
      observ: Sequences of observations.
      old_policy_params: Parameters of the behavioral policy.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    old_policy = self._policy_type(**old_policy_params)
    with tf.name_scope('adjust_penalty'):
      network = self._network(observ, length)
      print_penalty = tf.Print(0, [self._penalty], 'current penalty: ')
      with tf.control_dependencies([print_penalty]):
        kl_change = tf.reduce_mean(self._mask(
            tf.contrib.distributions.kl_divergence(old_policy, network.policy),
            length))
        kl_change = tf.Print(kl_change, [kl_change], 'kl change: ')
        maybe_increase = tf.cond(
            kl_change > 1.3 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.Print(self._penalty.assign(
                self._penalty * 1.5), [0], 'increase penalty '),
            float)
        maybe_decrease = tf.cond(
            kl_change < 0.7 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.Print(self._penalty.assign(
                self._penalty / 1.5), [0], 'decrease penalty '),
            float)
      with tf.control_dependencies([maybe_increase, maybe_decrease]):
        return tf.summary.merge([
            tf.summary.scalar('kl_change', kl_change),
            tf.summary.scalar('penalty', self._penalty)])
//  [python]
def _adjust_penalty(self, observ, old_policy_params, length):
    """Adjust the KL policy between the behavioral and current policy.

    Compute how much the policy actually changed during the multiple
    update steps. Adjust the penalty strength for the next training phase if we
    overshot or undershot the target divergence too much.

    Args:
      observ: Sequences of observations.
      old_policy_params: Parameters of the behavioral policy.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
    old_policy = self._policy_type(**old_policy_params)
    with tf.name_scope('adjust_penalty'):
      network = self._network(observ, length)
      print_penalty = tf.print(0, [self._penalty], 'current penalty: ')
      with tf.control_dependencies([print_penalty]):
        kl_change = tf.reduce_mean(self._mask(
            tf.contrib.distributions.kl_divergence(old_policy, network.policy),
            length))
        kl_change = tf.print(kl_change, [kl_change], 'kl change: ')
        maybe_increase = tf.cond(
            kl_change > 1.3 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.print(self._penalty.assign(
                self._penalty * 1.5), [0], 'increase penalty '),
            float)
        maybe_decrease = tf.cond(
            kl_change < 0.7 * self._config.kl_target,
            # pylint: disable=g-long-lambda
            lambda: tf.print(self._penalty.assign(
                self._penalty / 1.5), [0], 'decrease penalty '),
            float)
      with tf.control_dependencies([maybe_increase, maybe_decrease]):
        return tf.summary.merge([
            tf.summary.scalar('kl_change', kl_change),
            tf.summary.scalar('penalty', self._penalty)])
//  [python]
def _upscore_layer(bottom, upshape,
                   num_classes, name,
                   ksize=4, stride=2):
    strides = [1, stride, stride, 1]
    with tf.variable_scope(name):
        in_features = bottom.get_shape()[3].value

        new_shape = [upshape[0], upshape[1], upshape[2], num_classes]
        output_shape = tf.stack(new_shape)

        f_shape = [ksize, ksize, num_classes, in_features]

        up_init = upsample_initilizer()

        weights = tf.get_variable(name="weights", initializer=up_init,
                                  shape=f_shape)

        tf.add_to_collection(tf.GraphKeys.WEIGHTS, weights)

        deconv = tf.nn.conv2d_transpose(bottom, weights, output_shape,
                                        strides=strides, padding='SAME')

        deconv = tf.Print(deconv, [tf.shape(deconv)],
                          message='Shape of %s' % name,
                          summarize=4, first_n=1)

        _activation_summary(deconv)

    return deconv
//  [python]
def _upscore_layer(bottom, upshape,
                   num_classes, name,
                   ksize=4, stride=2):
    strides = [1, stride, stride, 1]
    with tf.variable_scope(name):
        in_features = bottom.get_shape()[3].value

        new_shape = [upshape[0], upshape[1], upshape[2], num_classes]
        output_shape = tf.stack(new_shape)

        f_shape = [ksize, ksize, num_classes, in_features]

        up_init = upsample_initilizer()

        weights = tf.get_variable(name="weights", initializer=up_init,
                                  shape=f_shape)

        tf.add_to_collection(tf.GraphKeys.WEIGHTS, weights)

        deconv = tf.nn.conv2d_transpose(bottom, weights, output_shape,
                                        strides=strides, padding='SAME')

        deconv = tf.print(deconv, [tf.shape(deconv)],
                          message='Shape of %s' % name,
                          summarize=4, first_n=1)

        _activation_summary(deconv)

    return deconv