jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def evaluate_legacy(model, weights, test_gen, loss, optimiser, loss_weights):
    # Get loss weights
    metrics = ['accuracy', AUC(curve='ROC', name='auroc'), AUC(curve='PR', name='aupr')]

    # Compile model
    model.compile(loss=loss, optimizer=optimiser, metrics=metrics, loss_weights=loss_weights)

    # Evaluate
    print(f'Evaluating weights file: {weights}')
    output = model.evaluate(test_gen.dataset)

    # Report results
    loss = output[0]
    audio_acc = output[4]
    video_acc = output[7]
    av_acc = output[10]
    auroc = output[11]
    aupr = output[12]
    print_results(weights, loss, audio_acc, video_acc, av_acc, auroc, aupr)
//  [python]
def train(self, train_x, train_y, test_x, test_y, batch_size=32, step_size=0.0001, num_epochs=10, callbacks=None):
        from tensorflow.keras.optimizers import Adam
        from tensorflow.keras.metrics import AUC

        self._model.compile(loss="binary_crossentropy", optimizer=Adam(lr=step_size, epsilon=1e-8), metrics=[AUC()])

        validation_data = (test_x, test_y)
        if test_x is None and test_y is None:
            validation_data = None
        self._model.fit(train_x, train_y, batch_size=batch_size, epochs=num_epochs,
                        validation_data=validation_data, callbacks=callbacks)
//  [python]
def main(file):
    # TODO: Split dataset
    # If you want to split the file
    sample_num = 4600000
    split_file_list = get_split_file_path(dataset_path=file, sample_num=sample_num)
    # Or if you have split the file before
    # split_file_list = get_split_file_path(parent_path='data/criteo/split')
    print('split file name: %s' % str(split_file_list))
    # TODO: Get Feature Map
    # If you want to make feature map.
    fea_map = get_fea_map(split_file_list=split_file_list)
    # Or if you want to load feature map.
    # fea_map = get_fea_map(fea_map_path='data/criteo/split/fea_map.pkl')
    # TODO: Load test data
    print("load test file: %s" % split_file_list[-1])
    feature_columns, test_data = create_criteo_dataset(split_file_list[-1], fea_map)
    # TODO: Build Model
    model = FM(feature_columns=feature_columns, **model_params)
    model.summary()
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[tf.keras.metrics.AUC()])
    # TODO: Load train data
    for file in split_file_list[:-1]:
        print("load %s" % file)
        _, train_data = create_criteo_dataset(file, fea_map)
        # TODO: Fit
        model.fit(
            x=train_data[0],
            y=train_data[1],
            epochs=1,
            batch_size=batch_size,
            validation_split=0.1
        )
        # TODO: Test
        print('test AUC: %f' % model.evaluate(x=test_data[0], y=test_data[1], batch_size=batch_size)[1])
//  [python]
def main(file):
    # TODO: Split dataset
    # If you want to split the file
    sample_num = 4600000
    split_file_list = get_split_file_path(dataset_path=file, sample_num=sample_num)
    # Or if you have split the file before
    # split_file_list = get_split_file_path(parent_path='data/criteo/split')
    print('split file name: %s' % str(split_file_list))
    # TODO: Get Feature Map
    # If you want to make feature map.
    fea_map = get_fea_map(split_file_list=split_file_list)
    # Or if you want to load feature map.
    # fea_map = get_fea_map(fea_map_path='data/criteo/split/fea_map.pkl')
    # TODO: Load test data
    print("load test file: %s" % split_file_list[-1])
    feature_columns, test_data = create_criteo_dataset(split_file_list[-1], fea_map)
    # TODO: Build Model
    model = FM(feature_columns=feature_columns, **model_params)
    model.summary()
    model.compile(loss=binary_crossentropy, optimizer=Adam(learning_rate=learning_rate),
                  metrics=[AUC()])
    # TODO: Load train data
    for file in split_file_list[:-1]:
        print("load %s" % file)
        _, train_data = create_criteo_dataset(file, fea_map)
        # TODO: Fit
        model.fit(
            x=train_data[0],
            y=train_data[1],
            epochs=1,
            batch_size=batch_size,
            validation_split=0.1
        )
        # TODO: Test
        print('test AUC: %f' % model.evaluate(x=test_data[0], y=test_data[1], batch_size=batch_size)[1])
//  [python]
def train_graph1():
    model = Model(inputs=inputs,
                  outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred})
    model.summary()
    model.compile(optimizer=opt, loss=loss_dict, loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), tf.keras.metrics.AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), tf.keras.metrics.AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset(batch_size=batch_size, train=False, config=config)
    batch_eval_callback = BatchModelCheckpoint(valid_dataset)
    callbacks.append(batch_eval_callback)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              verbose=1, workers=30, epochs=epochs, shuffle=True)
//  [python]
def train_graph1():
    model = Model(inputs=inputs,
                  outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred})
    model.summary()
    model.compile(optimizer=opt, loss=loss_dict, loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset(batch_size=batch_size, train=False, config=config)
    batch_eval_callback = BatchModelCheckpoint(valid_dataset)
    callbacks.append(batch_eval_callback)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              verbose=1, workers=30, epochs=epochs, shuffle=True)
//  [python]
def train_model(cfg, data, callbacks, verbose=2):
    '''
    Train a and evaluate model on given data.
    :param cfg: Project config (from config.yml)
    :param data: dict of partitioned dataset
    :param callbacks: list of callbacks for Keras model
    :param verbose: Verbosity mode to pass to model.fit()
    :return: Trained model and associated performance metrics on the test set
    '''

    # Apply class imbalance strategy
    num_neg, num_pos = np.bincount(data['Y_train'].astype(int))
    class_weight = None
    if cfg['TRAIN']['IMB_STRATEGY'] == 'class_weight':
        class_weight = get_class_weights(num_pos, num_neg, cfg['TRAIN']['POS_WEIGHT'])
    elif cfg['TRAIN']['IMB_STRATEGY'] != 'none':
        data['X_train'], data['Y_train'] = minority_oversample(data['X_train'], data['Y_train'],
                                                               algorithm=cfg['TRAIN']['IMB_STRATEGY'])

    thresholds = cfg['TRAIN']['THRESHOLDS']     # Load classification thresholds

    # List metrics
    metrics = [BinaryAccuracy(name='accuracy'), Precision(name='precision', thresholds=thresholds),
               Recall(name='recall', thresholds=thresholds), F1Score(name='f1score', thresholds=thresholds),
               tf.keras.metrics.AUC(name='auc')]

    # Compute output bias
    num_neg, num_pos = np.bincount(data['Y_train'].astype(int))
    output_bias = np.log([num_pos / num_neg])

    # Build the model graph.
    if cfg['TRAIN']['MODEL_DEF'] == 'hifis_rnn_mlp':
        model_def = hifis_rnn_mlp
    elif cfg['TRAIN']['MODEL_DEF'] == 'hifis_mlp':
        model_def = hifis_mlp
    elif cfg['TRAIN']['MODEL_DEF'] == 'logistic_regression':
        model_def = logistic_regression
    elif cfg['TRAIN']['MODEL_DEF'] == 'random_forest':
        model_def = random_forest
    else:
        model_def = xgboost_model
    model = model_def(cfg['MODELS'][cfg['TRAIN']['MODEL_DEF'].upper()], input_dim=(data['X_train'].shape[-1],), metrics=metrics,
                      metadata=data['METADATA'], output_bias=output_bias)

    # Train the model.
    history = model.fit(data['X_train'], data['Y_train'], batch_size=cfg['TRAIN']['BATCH_SIZE'],
                        epochs=cfg['TRAIN']['EPOCHS'], validation_data=(data['X_val'], data['Y_val']),
                        callbacks=callbacks, class_weight=class_weight, verbose=verbose)

    # Run the model on the test set and print the resulting performance metrics.
    test_results = model.evaluate(data['X_test'], data['Y_test'])
    test_metrics = {}
    test_summary_str = [['**Metric**', '**Value**']]
    for metric, value in zip(model.metrics_names, test_results):
        test_metrics[metric] = value
        print(metric, ' = ', value)
        test_summary_str.append([metric, str(value)])
    return model, test_metrics
//  [python]
def train_model(cfg, data, callbacks, verbose=2):
    '''
    Train a and evaluate model on given data.
    :param cfg: Project config (from config.yml)
    :param data: dict of partitioned dataset
    :param callbacks: list of callbacks for Keras model
    :param verbose: Verbosity mode to pass to model.fit()
    :return: Trained model and associated performance metrics on the test set
    '''

    # Apply class imbalance strategy
    num_neg, num_pos = np.bincount(data['Y_train'].astype(int))
    class_weight = None
    if cfg['TRAIN']['IMB_STRATEGY'] == 'class_weight':
        class_weight = get_class_weights(num_pos, num_neg, cfg['TRAIN']['POS_WEIGHT'])
    elif cfg['TRAIN']['IMB_STRATEGY'] != 'none':
        data['X_train'], data['Y_train'] = minority_oversample(data['X_train'], data['Y_train'],
                                                               algorithm=cfg['TRAIN']['IMB_STRATEGY'])

    thresholds = cfg['TRAIN']['THRESHOLDS']     # Load classification thresholds

    # List metrics
    metrics = [BinaryAccuracy(name='accuracy'), Precision(name='precision', thresholds=thresholds),
               Recall(name='recall', thresholds=thresholds), F1Score(name='f1score', thresholds=thresholds),
               AUC(name='auc')]

    # Compute output bias
    num_neg, num_pos = np.bincount(data['Y_train'].astype(int))
    output_bias = np.log([num_pos / num_neg])

    # Build the model graph.
    if cfg['TRAIN']['MODEL_DEF'] == 'hifis_rnn_mlp':
        model_def = hifis_rnn_mlp
    elif cfg['TRAIN']['MODEL_DEF'] == 'hifis_mlp':
        model_def = hifis_mlp
    elif cfg['TRAIN']['MODEL_DEF'] == 'logistic_regression':
        model_def = logistic_regression
    elif cfg['TRAIN']['MODEL_DEF'] == 'random_forest':
        model_def = random_forest
    else:
        model_def = xgboost_model
    model = model_def(cfg['MODELS'][cfg['TRAIN']['MODEL_DEF'].upper()], input_dim=(data['X_train'].shape[-1],), metrics=metrics,
                      metadata=data['METADATA'], output_bias=output_bias)

    # Train the model.
    history = model.fit(data['X_train'], data['Y_train'], batch_size=cfg['TRAIN']['BATCH_SIZE'],
                        epochs=cfg['TRAIN']['EPOCHS'], validation_data=(data['X_val'], data['Y_val']),
                        callbacks=callbacks, class_weight=class_weight, verbose=verbose)

    # Run the model on the test set and print the resulting performance metrics.
    test_results = model.evaluate(data['X_test'], data['Y_test'])
    test_metrics = {}
    test_summary_str = [['**Metric**', '**Value**']]
    for metric, value in zip(model.metrics_names, test_results):
        test_metrics[metric] = value
        print(metric, ' = ', value)
        test_summary_str.append([metric, str(value)])
    return model, test_metrics
//  [python]
def train_mmoe_base():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, ctcvr_pred = get_mmoe()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred
                                    })

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), t.AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_moebase(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_moebase(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_mmoe_base():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, ctcvr_pred = get_mmoe()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred
                                    })

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_moebase(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_moebase(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_mmoe_add_loss():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, cvr_pred, ctcvr_pred, ct_nocvr_pred = get_mmoe_add_loss()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred,
                                    'ct_nocvr_pred': ct_nocvr_pred})

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), t.AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), t.AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_mmoe_add_loss():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, cvr_pred, ctcvr_pred, ct_nocvr_pred = get_mmoe_add_loss()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred,
                                    'ct_nocvr_pred': ct_nocvr_pred})

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_graph():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    outputs_list = build_model(model_name)
    outputs = dict(zip(loss_key, outputs_list))
    model = BatchEvalModel(batch_interval=1000, eval_batchs=[7000, 1300],
                           inputs=inputs, outputs=outputs
                           )
    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), t.keras.metrics.AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), t.keras.metrics.AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    if model_name == 'esmm_mmoe_add_loss':
        train_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=True, config=config)
        valid_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=False, config=config)
    else:
        train_dataset = get_dataset(batch_size=batch_size, train=True, config=config)
        valid_dataset = get_dataset(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_graph():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    outputs_list = build_model(model_name)
    outputs = dict(zip(loss_key, outputs_list))
    model = BatchEvalModel(batch_interval=1000, eval_batchs=[7000, 1300],
                           inputs=inputs, outputs=outputs
                           )
    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr'), DropAUC(name='tauc'), AUC()],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    if model_name == 'esmm_mmoe_add_loss':
        train_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=True, config=config)
        valid_dataset = get_dataset_ctnocvr(batch_size=batch_size, train=False, config=config)
    else:
        train_dataset = get_dataset(batch_size=batch_size, train=True, config=config)
        valid_dataset = get_dataset(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def test_update_metric_by_small_chunks(self):
        labels = np.random.randint(0, 2, 1234)
        preds = np.random.random(1234)
        auc = tf.compat.v1.metrics.auc(labels, preds)
        auc_value_0 = auc[1]

        auc = tf.compat.v1.metrics.auc(labels, preds)
        EvaluationMetrics._update_metric_by_small_chunk(auc, labels, preds)
        auc_value_1 = auc[1]
        self.assertEqual(auc_value_0, auc_value_1)
//  [python]
def test_update_metric_by_small_chunks(self):
        labels = np.random.randint(0, 2, 1234)
        preds = np.random.random(1234)
        auc = tf.keras.metrics.AUC()
        auc.update_state(labels, preds)
        auc_value_0 = auc.result()

        auc.reset_states()
        EvaluationMetrics._update_metric_by_small_chunk(auc, labels, preds)
        auc_value_1 = auc.result()
        self.assertEqual(auc_value_0, auc_value_1)
//  [python]
batch_size = 128
epochs = 10
opt = Adam(learning_rate=0.003, decay=0.0001)
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
    esmm = ESMM(cate_feature_columns=cate_feature_dict, cate_feature_dict=[user_cate_feature_dict, item_cate_feature_dict])
    model = esmm.build_graph()
    model.summary()
    # ============================Compile============================
    model.compile(loss=["binary_crossentropy", "binary_crossentropy"],
                  loss_weights=[1.0, 1.0],
                  optimizer=opt,
                  metrics=[tf.keras.metrics.AUC()])
    model.run_eagerly = True
//  [python]
batch_size = 128
epochs = 10
opt = Adam(learning_rate=0.003, decay=0.0001)
mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
    esmm = ESMM(cate_feature_columns=cate_feature_dict, cate_feature_dict=[user_cate_feature_dict, item_cate_feature_dict])
    model = esmm.build_graph()
    model.summary()
    # ============================Compile============================
    model.compile(loss=["binary_crossentropy", "binary_crossentropy"],
                  loss_weights=[1.0, 1.0],
                  optimizer=opt,
                  metrics=[AUC()])
    model.run_eagerly = True
//  [python]
def create_model(hp,input_dim,output_dim,encoder):
    inputs = Input(input_dim)
    
    x = encoder(inputs)
    x = Concatenate()([x,inputs, x]) #use both raw and encoded features
    x = BatchNormalization()(x)
    x = Dropout(hp.Float('init_dropout',0.0,0.5))(x)
    
    for i in range(hp.Int('num_layers',1,5)):
        x = Dense(hp.Int(f'num_units_{i}',64,256))(x)
        x = BatchNormalization()(x)
        x = Lambda(tf.keras.activations.swish)(x)
        x = Dropout(hp.Float(f'dropout_{i}',0.0,0.5))(x)
    x = Dense(output_dim,activation='sigmoid')(x)
    model = Model(inputs=inputs,outputs=x)
    model.compile(optimizer=Adam(hp.Float('lr',0.00001,0.1,
                                default=0.001)),
                                loss=BinaryCrossentropy(label_smoothing=hp.Float('label_smoothing',0.0,0.1)),
                                metrics=[tf.keras.metrics.AUC(name = 'auc')])
    return model
//  [python]
def create_model(hp,input_dim,output_dim,encoder):
    inputs = Input(input_dim)
    
    x = encoder(inputs)
    x = Concatenate()([x,inputs, x]) #use both raw and encoded features
    x = BatchNormalization()(x)
    x = Dropout(hp.Float('init_dropout',0.0,0.5))(x)
    
    for i in range(hp.Int('num_layers',1,5)):
        x = Dense(hp.Int(f'num_units_{i}',64,256))(x)
        x = BatchNormalization()(x)
        x = Lambda(tf.keras.activations.swish)(x)
        x = Dropout(hp.Float(f'dropout_{i}',0.0,0.5))(x)
    x = Dense(output_dim,activation='sigmoid')(x)
    model = Model(inputs=inputs,outputs=x)
    model.compile(optimizer=Adam(hp.Float('lr',0.00001,0.1,
                                default=0.001)),
                                loss=BinaryCrossentropy(label_smoothing=hp.Float('label_smoothing',0.0,0.1)),
                                metrics=[AUC(name = 'auc')])
    return model
//  [python]
def __init__(self, args=None):
        super().__init__()
        self.args = vars(args) if args is not None else {}

        # Loss function
        self.loss = self.args.get('loss', "CategoricalCrossentropy")
        self.loss_fn = getattr(losses, self.loss)()

        self.lr = self.args.get('learning_rate', 0.002)

        # Optimizer
        if self.args.get('optimizer', 'Adam') == 'Adam':
            self.optimizer = getattr(optimizers, 'Adam')(learning_rate=self.lr)
        elif self.args.get('optimizer', 'Adam') == 'Ranger':
            radam = RectifiedAdam(learning_rate=self.lr)
            self.optimizer = Lookahead(radam,
                                       sync_period=6,
                                       slow_step_size=0.5)
        else:
            self.optimizer = getattr(
                optimizers, self.args.get('optimizer'))(learning_rate=self.lr)

        # Learning rate scheduler
        self.batch_size = self.args.get('batch_size', 128)

        if self.args.get('use_quantum', False):
            self.loss = "MeanSquaredError"
            self.loss_fn = getattr(losses, self.loss)()
            self.accuracy = [qAUC(), custom_accuracy]
            self.acc_metrics = ['custom_accuracy,', 'qAUC']
        else:
            # Accuracy
            self.accuracy = [tf.keras.metrics.AUC(), 'accuracy']
            self.acc_metrics = ['accuracy,', 'AUC']
//  [python]
def __init__(self, args=None):
        super().__init__()
        self.args = vars(args) if args is not None else {}

        # Loss function
        self.loss = self.args.get('loss', "CategoricalCrossentropy")
        self.loss_fn = getattr(losses, self.loss)()

        self.lr = self.args.get('learning_rate', 0.002)

        # Optimizer
        if self.args.get('optimizer', 'Adam') == 'Adam':
            self.optimizer = getattr(optimizers, 'Adam')(learning_rate=self.lr)
        elif self.args.get('optimizer', 'Adam') == 'Ranger':
            radam = RectifiedAdam(learning_rate=self.lr)
            self.optimizer = Lookahead(radam,
                                       sync_period=6,
                                       slow_step_size=0.5)
        else:
            self.optimizer = getattr(
                optimizers, self.args.get('optimizer'))(learning_rate=self.lr)

        # Learning rate scheduler
        self.batch_size = self.args.get('batch_size', 128)

        if self.args.get('use_quantum', False):
            self.loss = "MeanSquaredError"
            self.loss_fn = getattr(losses, self.loss)()
            self.accuracy = [qAUC(), custom_accuracy]
            self.acc_metrics = ['custom_accuracy,', 'qAUC']
        else:
            # Accuracy
            self.accuracy = [AUC(), 'accuracy']
            self.acc_metrics = ['accuracy,', 'AUC']
//  [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 = 500000
    test_size = 0.2

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

    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 = xDeepFM(feature_columns, hidden_units, cin_size)
    model.summary()
    # ============================model checkpoint======================
    # check_path = 'save/xdeepfm_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=2, 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 = 500000
    test_size = 0.2

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

    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 = xDeepFM(feature_columns, hidden_units, cin_size)
    model.summary()
    # ============================model checkpoint======================
    # check_path = 'save/xdeepfm_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=2, 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 train_mmoe_add_loss():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, cvr_pred, ctcvr_pred, ct_nocvr_pred = get_cgc_add_loss()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred,
                                    'ct_nocvr_pred': ct_nocvr_pred})

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), tf.keras.metrics.AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr', filter=True), DropAUC(name='tauc',filter=True)],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)],
                      'ct_nocvr_pred': [DropAUCX(name='dauc', output_name='ct_nocvr'), DropAUC(name='tauc')]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_mof(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_mof(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
def train_mmoe_add_loss():
    from mctr.modeling.custom_keras_model import BatchEvalModel
    ctr_pred, cvr_pred, ctcvr_pred, ct_nocvr_pred = get_cgc_add_loss()
    model = BatchEvalModel(inputs=inputs,
                           outputs={'ctr_output': ctr_pred, 'ctcvr_pred': ctcvr_pred, 'cvr_output': cvr_pred,
                                    'ct_nocvr_pred': ct_nocvr_pred})

    model.summary()
    model.compile(optimizer=opt,
                  loss=loss_dict,
                  loss_weights=loss_weights,
                  metrics={
                      'ctr_output': [DropAUCX(name='dauc', output_name='ctr'), DropAUC(name='tauc'), AUC()],
                      'ctcvr_pred': [DropAUCX(name='dauc', output_name='ctcvr', filter=True), DropAUC(name='tauc',filter=True)],
                      'cvr_output': [DropAUCX(name='dauc', output_name='cvr', filter=True),
                                     DropAUC(name='tauc', filter=True)],
                      'ct_nocvr_pred': [DropAUCX(name='dauc', output_name='ct_nocvr'), DropAUC(name='tauc')]
                  },
                  # steps_per_execution=10
                  )
    print('start fitting', prefix)
    train_dataset = get_dataset_mof(batch_size=batch_size, train=True, config=config)
    valid_dataset = get_dataset_mof(batch_size=batch_size, train=False, config=config)
    model.fit(train_dataset, validation_data=valid_dataset, callbacks=callbacks,
              # steps_per_epoch=8461, validation_steps=8604,
              verbose=1, workers=30, epochs=epochs, shuffle=shuffle)
//  [python]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


if __name__ == '__main__':
    # ========================= Hyper Parameters =======================
    file = '../dataset/Criteo/train.txt'
    # file = '../dataset/Criteo/demo.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 = DCN(feature_columns, hidden_units, dnn_dropout=dnn_dropout)
    model.summary()
    # ============================model checkpoint======================
    # check_path = 'save/dcn_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_auc', patience=2, 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 =======================
    file = '../dataset/Criteo/train.txt'
    # file = '../dataset/Criteo/demo.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 = DCN(feature_columns, hidden_units, dnn_dropout=dnn_dropout)
    model.summary()
    # ============================model checkpoint======================
    # check_path = 'save/dcn_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_auc', patience=2, 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 _get_keras_auc(auc_type, multi_label):
    from tensorflow.keras.metrics import AUC as KerasAUC  # type: ignore
    keras_roc_auc = lambda multi_label: KerasAUC(multi_label=multi_label,
                                                 curve="roc")
    keras_pr_auc = lambda multi_label: KerasAUC(multi_label=multi_label,
                                                curve="pr")

    if auc_type == "roc":
        return keras_roc_auc(multi_label)
    elif auc_type == "pr":
        return keras_pr_auc(multi_label)
    else:
        raise ValueError(auc_type)
//  [python]
def __init__(self, model, data, config):
        self.model_train = model['train']
        self.model_eval = model['eval']
        self.data = data
        self.config = config

        # self.loss_fn = BinaryCrossentropy()
        self.loss_fn = CustomAUC()

        self.optimizer = Adam(learning_rate=self.config.learning_rate)
        self.metrics = ['accuracy', tf.keras.metrics.AUC()]
//  [python]
def __init__(self, model, data, config):
        self.model_train = model['train']
        self.model_eval = model['eval']
        self.data = data
        self.config = config

        # self.loss_fn = BinaryCrossentropy()
        self.loss_fn = CustomAUC()

        self.optimizer = Adam(learning_rate=self.config.learning_rate)
        self.metrics = ['accuracy', AUC()]
//  [python]
def build_or_load_model(config):
    if config.load_model_weights_from_file:
        print('CURRENT DIR: {}'.format(os.getcwd()))
        adjust_input_size(config)
        model_dict = build_model(config)
        model_dict['train'].load_weights(config.model_weights_file)
        model = {'train': model_dict['train']}
        model['train'].compile(optimizer=Adam(learning_rate=config.learning_rate), loss=BinaryCrossentropy(),
                      metrics=['accuracy', tf.keras.metrics.AUC()])
    elif config.load_complete_model_from_file:
        model = {'train': tf.keras.models.load_model(config.complete_model_file)}
    else:
        model = build_model(config)

    model['train'].summary()
    return model
//  [python]
def build_or_load_model(config):
    if config.load_model_weights_from_file:
        print('CURRENT DIR: {}'.format(os.getcwd()))
        adjust_input_size(config)
        model_dict = build_model(config)
        model_dict['train'].load_weights(config.model_weights_file)
        model = {'train': model_dict['train']}
        model['train'].compile(optimizer=Adam(learning_rate=config.learning_rate), loss=BinaryCrossentropy(),
                      metrics=['accuracy', AUC()])
    elif config.load_complete_model_from_file:
        model = {'train': tf.keras.models.load_model(config.complete_model_file)}
    else:
        model = build_model(config)

    model['train'].summary()
    return model
//  [python]
tf.compat.v1.disable_eager_execution()
warnings.filterwarnings("ignore")

LOGGER = structlog.get_logger()

# List of Keras model metrics.
METRICS = [
    TruePositives(name="tp"),
    FalsePositives(name="fp"),
    TrueNegatives(name="tn"),
    FalseNegatives(name="fn"),
    CategoricalAccuracy(name="accuracy"),
    Precision(name="precision"),
    Recall(name="recall"),
    tf.compat.v1.metrics.auc.AUC(name="auc"),
]
//  [python]
tf.compat.v1.disable_eager_execution()
warnings.filterwarnings("ignore")

LOGGER = structlog.get_logger()

# List of Keras model metrics.
METRICS = [
    TruePositives(name="tp"),
    FalsePositives(name="fp"),
    TrueNegatives(name="tn"),
    FalseNegatives(name="fn"),
    CategoricalAccuracy(name="accuracy"),
    Precision(name="precision"),
    Recall(name="recall"),
    AUC(name="auc"),
]
//  [python]
def create_model_RNN_GRU( 
  optimizer='adam', units=200, activation='sigmoid', 
  EMBEDDING_DIM=25, max_length=37,  vocab_size=350, hidden_dims=1, hidden_dim_units=25 ):
  keras_eval_metric = [[ 
    TruePositives(name='tp'),
    FalsePositives(name='fp'),
    TrueNegatives(name='tn'),
    FalseNegatives(name='fn'), 
    BinaryAccuracy(name='accuracy'),
    Precision(name='precision'),
    Recall(name='recall'),
    AUC(name='auc'),
  ]]
  model = Sequential()
  print("Parameters", "units", units, "activation", activation, "EMBEDDING_DIM", EMBEDDING_DIM, "max_length", max_length,  "vocab_size", vocab_size, " hidden_dims  ", hidden_dims )
  model.add(Embedding(vocab_size, EMBEDDING_DIM,  input_length=max_length))
  model.add(GRU(units=hidden_dim_units, dropout=0.2, recurrent_dropout=0.2, activation=activation ))
  for i in range(hidden_dims):
    model.add(Dense(hidden_dim_units))
    model.add(Activation(activation))
    model.add(Dropout(0.2))
  model.add(Dense(1))
  model.add(Activation(activation))
  model.compile(loss='binary_crossentropy', optimizer=optimizer , metrics=keras_eval_metric  )
  print( "\n\n", model.summary(), "\n\n", model.get_config(), "\n\n")
  return model
//  [python]
def create_model_RNN_LSTM( 
    optimizer='adam', units=200, activation='sigmoid', 
    EMBEDDING_DIM=25, max_length=37,  vocab_size=350, hidden_dims=1, hidden_dim_units=25 ):
    print("Parameters", "units", units, "activation", activation, "EMBEDDING_DIM", EMBEDDING_DIM, "max_length", max_length,  "vocab_size", vocab_size, " hidden_dims  ", hidden_dims )
    keras_eval_metric = [[ 
      TruePositives(name='tp'),
      FalsePositives(name='fp'),
      TrueNegatives(name='tn'),
      FalseNegatives(name='fn'), 
      BinaryAccuracy(name='accuracy'),
      Precision(name='precision'),
      Recall(name='recall'),
      AUC(name='auc'),
    ]]
    model = Sequential()
    model.add(Embedding(vocab_size, EMBEDDING_DIM,  input_length=max_length))
    model.add(LSTM(units=hidden_dim_units, dropout=0.2, recurrent_dropout=0.2, activation=activation ))
    for i in range(hidden_dims):
      model.add(Dense(hidden_dim_units))
      model.add(Activation(activation))
      model.add(Dropout(0.2))
    model.add(Dense(1))
    model.add(Activation(activation))
    model.compile(loss='binary_crossentropy', optimizer=optimizer , metrics=keras_eval_metric  )
    print( "\n\n", model.summary(), "\n\n", model.get_config(), "\n\n")

    return model
//  [python]
def build(self):

        model = models.Sequential()
        model.add(layers.Conv2D(36, (5, 5), activation='relu', input_shape=(self.input_width_height,
                                                                            self.input_width_height,
                                                                            self.channels)))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(72, (5, 5), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Flatten())
        model.add(layers.Dense(1024, activation='relu'))
        model.add(layers.Dropout(0.85))
        model.add(layers.Dense(self.num_classes, kernel_regularizer=l2(0.01), activation='softmax'))

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

        return model
//  [python]
def get_minimal_multiclass_metrics() -> List[Union[AUC, str, BinaryMetric]]:
    """Return minimal list of multiclass metrics.

    The set of metrics includes accuracy, AUROC and AUPRC.
    """
    return [
        "accuracy",
        Recall(name="recall"),
        Precision(name="precision"),
        AUC(curve="roc", name="AUROC"),
        AUC(curve="pr", name="AUPRC")
    ]
//  [python]
def auc(y_true, y_pred):
    """
    Description:
        Calculate the area under the curve (AUC).
    Args:
        :y_true (np.ndarray): ground truth class labels.
        :y_pred (np.ndarray): predicted class labels.
    Returns:
        area under curve auc (float) - the total number of false positives during evaluation.
    """
    auc_ = tf.metrics.auc(y_true, y_pred, num_thresholds=3)
    #auc_.update_state(y_true, y_pred)
    auc_result = auc_[1].numpy()

    # auc = tf.keras.metrics.AUC(y_true, y_pred)#[1]
    return auc_result
//  [python]
def auc(y_true, y_pred):
    """
    Description:
        Calculate the area under the curve (AUC).
    Args:
        :y_true (np.ndarray): ground truth class labels.
        :y_pred (np.ndarray): predicted class labels.
    Returns:
        area under curve auc (float) - the total number of false positives during evaluation.
    """
    auc_ = tf.keras.metrics.AUC(num_thresholds=3)
    auc_.update_state(y_true, y_pred)
    auc_result = auc_.result().numpy()

    # auc = tf.keras.metrics.AUC(y_true, y_pred)#[1]
    return auc_result