def test_NFM(): name = "NFM" sample_size = 64 feature_dim_dict = {'sparse': {'sparse_1': 2, 'sparse_2': 5, 'sparse_3': 10}, 'dense': ['dense_1', 'dense_2', 'dense_3']} sparse_input = [np.random.randint(0, dim, sample_size) for dim in feature_dim_dict['sparse'].values()] dense_input = [np.random.random(sample_size) for name in feature_dim_dict['dense']] y = np.random.randint(0, 2, sample_size) x = sparse_input + dense_input model = NFM(feature_dim_dict, embedding_size=8, hidden_size=[32, 32], keep_prob=0.5, ) model.compile('adam', 'binary_crossentropy', metrics=['binary_crossentropy']) model.fit(x, y, batch_size=100, epochs=1, validation_split=0.5) print(name+" test train valid pass!") model.save_weights(name + '_weights.h5') model.load_weights(name + '_weights.h5') print(name+" test save load weight pass!") save_model(model, name + '.h5') model = load_model(name + '.h5', custom_objects) print(name + " test save load model pass!") print(name + " test pass!")
SparseFeat(feature, data[feature].nunique()) for feature in sparse_features ] linear_feature_columns = fixlen_feature_columns dnn_feature_columns = fixlen_feature_columns feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns) # 将数据集切分成训练集和测试集 train, test = train_test_split(data, test_size=0.2) train_model_input = {name: train[name].values for name in feature_names} test_model_input = {name: test[name].values for name in feature_names} # 使用NFM进行训练 model = NFM(linear_feature_columns, dnn_feature_columns, task='regression') model.compile( "adam", "mse", metrics=['mse'], ) history = model.fit( train_model_input, train[target].values, batch_size=256, epochs=1, verbose=True, validation_split=0.2, ) # 使用NFM进行预测 pred_ans = model.predict(test_model_input, batch_size=256) # 输出RMSE或MSE mse = round(mean_squared_error(test[target].values, pred_ans), 4) rmse = mse**0.5
# 3.generate input data for model train, test = train_test_split(data, test_size=0.2) train_model_input = {name: train[name] for name in feature_names} test_model_input = {name: test[name] for name in feature_names} # 4.Define Model,train,predict and evaluate model = NFM(linear_feature_columns, dnn_feature_columns, task='binary', dnn_hidden_units=(400, 400, 400), dnn_dropout=0.5) model.compile( "adam", "binary_crossentropy", metrics=['binary_crossentropy'], ) history = model.fit( train_model_input, train[target].values, batch_size=256, epochs=10, verbose=2, validation_split=0.2, ) pred_ans = model.predict(test_model_input, batch_size=256) print("test LogLoss", round(log_loss(test[target].values, pred_ans), 4)) print("test AUC", round(roc_auc_score(test[target].values, pred_ans), 4)) t1 = time.time()