Ejemplo n.º 1
0
def main():
    hidden_unit = 64
    batch_size = 32
    learning_rate = 0.001
    epochs = 50
    with open('../Din/dataset/dataset.pkl', 'rb') as f:
        train_set = np.array(pickle.load(f))
        test_set = pickle.load(f)
        cate_list = pickle.load(f)
        user_count, item_count, cate_count, max_sl = pickle.load(f)
    train_user, train_item, train_hist, train_sl, train_y = input_data(
        train_set, max_sl)
    # Tensorboard
    current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    log_dir = 'logs/' + current_time
    tensorboard = tf.keras.callbacks.TensorBoard(log_dir=log_dir,
                                                 histogram_freq=1,
                                                 write_graph=True,
                                                 write_grads=False,
                                                 write_images=True,
                                                 embeddings_freq=0,
                                                 embeddings_layer_names=None,
                                                 embeddings_metadata=None,
                                                 embeddings_data=None,
                                                 update_freq=500)
    # 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=1)

    model = WideDeep(user_count, item_count, cate_count, cate_list,
                     hidden_unit)
    model.summary()
    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
    model.compile(loss=tf.keras.losses.binary_crossentropy,
                  optimizer=optimizer,
                  metrics=[tf.keras.metrics.AUC()])
    model.fit([train_user, train_item, train_hist, train_sl],
              train_y,
              epochs=epochs,
              batch_size=batch_size,
              validation_split=0.1,
              callbacks=[tensorboard, checkpoint])
Ejemplo n.º 2
0
    file=file,
    read_part=read_part,
    sample_num=sample_num,
    embed_dim=embed_dim,
    test_size=test_size)

train_X, train_y = train
test_X, test_y = test
val_X, val_y = val

# ---------------build model----------
model = WideDeep(feature_columns,
                 hidden_units=hidden_units,
                 dnn_dropout=dnn_dropout,
                 residual=True)  #
model.summary()

# -------------model checkpoint ---------
check_path = './save/deepfm_weight.epoch_{epoch:4d}.val_loss_{val_loss:.4f}.ckpt'
checkpoint = tf.keras.callbacks.ModelCheckpoint(check_path,
                                                save_weights_only=True,
                                                verbose=1,
                                                period=5)

# ------------ model evaluate ------------
METRICS = [
    tf.keras.metrics.BinaryAccuracy(name='accuracy'),
    tf.keras.metrics.Precision(name='precision'),
    tf.keras.metrics.Recall(name='recall'),
    tf.keras.metrics.AUC(name='auc'),
]