2021人工智能领域新星创作者,带你从入门到精通,该博客每天更新,逐渐完善推荐系统各个知识体系的文章,帮助大家更高效学习。
导库
from tensorflow.keras import * from tensorflow.keras.layers import * from tensorflow.keras.models import * from tensorflow.keras.callbacks import * import tensorflow.keras.backend as K from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from sklearn.datasets import load_breast_cancer from tqdm import tqdm import pandas as pd import numpy as np
FM特征组合层
# FM 特征组合层 class crossLayer(layers.Layer): def __init__(self, input_dim, output_dim=10, **kwargs): super(crossLayer, self).__init__(**kwargs) self.input_dim = input_dim self.output_dim = output_dim self.kernel = self.add_weight(name='kernel', shape=(self.input_dim, self.output_dim), initializer='glorot_uniform', trainable=True) def call(self, x): a = K.pow(K.dot(x, self.kernel), 2) b = K.dot(K.pow(x, 2), K.pow(self.kernel, 2)) return 0.5 * K.mean(a - b, 1, keepdims=True)
定义FM模型
# 定义FM模型 def FM(feature_dim): inputs = Input(shape=(feature_dim,)) # 一阶特征 linear = Dense(units=1, kernel_regularizer=regularizers.l2(0.01), bias_regularizer=regularizers.l2(0.01))(inputs) # 二阶特征 cross = crossLayer(feature_dim)(inputs) add = Add()([linear, cross]) pred = Dense(units=1, activation="sigmoid")(add) model = Model(inputs=inputs, outputs=pred) model.summary() model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(), metrics=['binary_accuracy']) return model
读取数据
# 读取数据 print('loading data...') X, y = load_breast_cancer(return_X_y = True)
划分数据集
# 划分数据集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2021)
构建模型
# 构建模型 model = FM(X.shape[1])
训练模型
# 训练模型 model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test))