You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tf_deepfm_criteo.py 2.8 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import tensorflow as tf
  2. def dfm_criteo(dense_input, sparse_input, y_, partitioner=None, part_all=True, param_on_gpu=True):
  3. feature_dimension = 33762577
  4. embedding_size = 128
  5. learning_rate = 0.01 / 8 # here to comply with HETU
  6. all_partitioner, embed_partitioner = (
  7. partitioner, None) if part_all else (None, partitioner)
  8. with tf.compat.v1.variable_scope('dfm', dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.01), partitioner=all_partitioner):
  9. with tf.device('/cpu:0'):
  10. Embedding1 = tf.compat.v1.get_variable(name="Embedding1", shape=(
  11. feature_dimension, 1), partitioner=embed_partitioner)
  12. Embedding2 = tf.compat.v1.get_variable(name="embeddings", shape=(
  13. feature_dimension, embedding_size), partitioner=embed_partitioner)
  14. sparse_1dim_input = tf.nn.embedding_lookup(
  15. Embedding1, sparse_input)
  16. sparse_2dim_input = tf.nn.embedding_lookup(
  17. Embedding2, sparse_input)
  18. device = '/gpu:0' if param_on_gpu else '/cpu:0'
  19. with tf.device(device):
  20. FM_W = tf.compat.v1.get_variable(name='FM_W', shape=[13, 1])
  21. W1 = tf.compat.v1.get_variable(
  22. name='W1', shape=[26*embedding_size, 256])
  23. W2 = tf.compat.v1.get_variable(name='W2', shape=[256, 256])
  24. W3 = tf.compat.v1.get_variable(name='W3', shape=[256, 1])
  25. with tf.device('/gpu:0'):
  26. fm_dense_part = tf.matmul(dense_input, FM_W)
  27. fm_sparse_part = tf.reduce_sum(sparse_1dim_input, 1)
  28. # fst order output
  29. y1 = fm_dense_part + fm_sparse_part
  30. sparse_2dim_sum = tf.reduce_sum(sparse_2dim_input, 1)
  31. sparse_2dim_sum_square = tf.multiply(
  32. sparse_2dim_sum, sparse_2dim_sum)
  33. sparse_2dim_square = tf.multiply(
  34. sparse_2dim_input, sparse_2dim_input)
  35. sparse_2dim_square_sum = tf.reduce_sum(sparse_2dim_square, 1)
  36. sparse_2dim = sparse_2dim_sum_square + -1 * sparse_2dim_square_sum
  37. sparse_2dim_half = sparse_2dim * 0.5
  38. # snd order output
  39. y2 = tf.reduce_sum(sparse_2dim_half, 1, keepdims=True)
  40. # DNN
  41. flatten = tf.reshape(sparse_2dim_input, (-1, 26*embedding_size))
  42. fc1 = tf.matmul(flatten, W1)
  43. relu1 = tf.nn.relu(fc1)
  44. fc2 = tf.matmul(relu1, W2)
  45. relu2 = tf.nn.relu(fc2)
  46. y3 = tf.matmul(relu2, W3)
  47. y4 = y1 + y2
  48. y = y4 + y3
  49. loss = tf.reduce_mean(
  50. tf.nn.sigmoid_cross_entropy_with_logits(logits=y, labels=y_))
  51. optimizer = tf.compat.v1.train.GradientDescentOptimizer(
  52. learning_rate)
  53. return loss, y, optimizer