鱼C论坛

 找回密码
 立即注册
查看: 1705|回复: 0

[技术交流] Python实现FFM【tensorflow2.0】

[复制链接]
发表于 2020-12-26 20:52:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-12-26 20:54 编辑

论文:https://www.csie.ntu.edu.tw/~cjlin/papers/ffm.pdf

参考:https://zhuanlan.zhihu.com/p/170607706

说明:待验证

  1. import tensorflow as tf
  2. from tensorflow.keras import layers, optimizers
  3. from tensorflow import keras
  4. from sklearn.model_selection import train_test_split
  5. import numpy as np

  6. #将数据划分为测试集和训练集
  7. def preprocess(x, y):
  8.     x = tf.cast(x, dtype = tf.float64)
  9.     y = tf.cast(y, dtype = tf.int64)
  10.     return x, y

  11. from sklearn.datasets import load_breast_cancer
  12. from sklearn.model_selection import train_test_split
  13. data = load_breast_cancer()
  14. x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2,
  15.                                                         random_state = 11, stratify = data.target)
  16. print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)

  17. train_db = tf.data.Dataset.from_tensor_slices((np.array(x_train), y_train))
  18. train_db = train_db.shuffle(123).map(preprocess).batch(20)

  19. test_db = tf.data.Dataset.from_tensor_slices((np.array(x_test), y_test))
  20. test_db = test_db.map(preprocess).batch(20)

  21. sample = next(iter(train_db))
  22. print('sample:', sample[0].shape, sample[1].shape,
  23.       tf.reduce_min(sample[0]), tf.reduce_max(sample[0]))
复制代码


  1. class FFM(keras.Model):
  2.     def __init__(self, field_num, feature_field_dict, dim_num, k = 2):
  3.         super(FFM, self).__init__()
  4.         self.field_num = field_num
  5.         self.k = k
  6.         self.feature_field_dict = feature_field_dict
  7.         self.dim_num = dim_num

  8.     def build(self, input_shape):
  9.         self.fc = tf.keras.layers.Dense(units = 1,
  10.                                   bias_regularizer = tf.keras.regularizers.l2(0.01),
  11.                                   kernel_regularizer = tf.keras.regularizers.l1(0.02))
  12.         self.w = self.add_weight(shape = (input_shape[-1], self.field_num, self.k),
  13.                                       initializer = 'glorot_uniform',
  14.                                       trainable = True)
  15.         super(FFM, self).build(input_shape)
  16.         
  17.     def call(self, x, training):
  18.         linear = self.fc(x)
  19.         temp = tf.cast(0, tf.float32)
  20.         temp = tf.expand_dims(temp, axis = 0)
  21.         for j1 in range(self.dim_num):
  22.             for j2 in range(j1 + 1, self.dim_num):
  23.                 f1 = self.feature_field_dict[j2]
  24.                 f2 = self.feature_field_dict[j1]
  25.                 #[, , k] * [, , k] = [, , k] -> [1, k]
  26.                 ww = tf.expand_dims(tf.multiply(self.w[j1, f2, :], self.w[j2, f1, :]), axis = 0)
  27.                 #[x, ] * [x, ] = [x, ] -> [x, 1]
  28.                 xx = tf.expand_dims(tf.multiply(x[:, j1], x[:, j2]),axis = 1)
  29.                 #[x, 1] @ [1, k] = [x, k]
  30.                 store = tf.matmul(xx, ww)
  31.                 #[x, k] -> [x]
  32.                 temp += tf.reduce_mean(store, keepdims = True, axis = 1)
  33.         out = layers.Add()([linear, temp])
  34.         return tf.sigmoid(out)

  35. store = {}   
  36. for i in range(30):
  37.         store[i] = int(i / 15)
  38. model = FFM(field_num = 2, feature_field_dict = store, dim_num = 30)
  39. model.build((None, 30))
  40. model.summary()
复制代码


  1. def main():
  2.     store = {}
  3.     for i in range(30):
  4.         store[i] = int(i / 15) #实际要根据数据字段含义定义,这里只是做一个随意的分组
  5.     model = FFM(field_num = 2, feature_field_dict = store, dim_num = 30)
  6.     optimizer = optimizers.Adam(lr = 1e-2)
  7.     for epoch in range(50):
  8.         for step, (x,y) in enumerate(train_db):
  9.             with tf.GradientTape() as tape:
  10.                 logits = model(x,training=True)
  11.                 loss = tf.reduce_mean(tf.losses.binary_crossentropy(y, logits))
  12.                 loss_regularization = []
  13.                 for i in model.trainable_variables:
  14.                     loss_regularization.append(tf.nn.l2_loss(i))
  15.                 loss_regularization = tf.reduce_sum(tf.stack(loss_regularization))
  16.                 loss = 0.001 * loss_regularization + loss
  17.             grads = tape.gradient(loss, model.trainable_variables)
  18.             optimizer.apply_gradients(zip(grads, model.trainable_variables))
  19.             print(epoch, step, 'loss:', float(loss))
  20.             
  21.         total_num = 0
  22.         total_correct = 0
  23.         for x,y in test_db:
  24.             pred = model(x, training=False)
  25.             pred = tf.squeeze(pred)
  26.             pred = pred > 0.5
  27.             pred = tf.cast(pred, dtype = tf.int64)
  28.             correct = tf.cast(tf.equal(pred, y), tf.int64)
  29.             correct = tf.reduce_sum(correct)
  30.             total_num += x.shape[0]
  31.             total_correct += int(correct)
  32.         acc = total_correct / total_num
  33.         print(epoch, 'acc:', acc)
  34.         print("-"*25)
  35.         
  36. if __name__ == '__main__':
  37.     main()
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-20 06:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表