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.

dc_criteo.py 2.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import hetu as ht
  2. from hetu import init
  3. import numpy as np
  4. import time
  5. def residual_layer(x0, input_dim, hidden_dim):
  6. embedding_len = input_dim
  7. weight_1 = init.random_normal(
  8. shape=(input_dim, hidden_dim), stddev=0.1, name='weight_1')
  9. bias_1 = init.random_normal(shape=(hidden_dim,), stddev=0.1, name='bias_1')
  10. weight_2 = init.random_normal(
  11. shape=(hidden_dim, input_dim), stddev=0.1, name='weight_2')
  12. bias_2 = init.random_normal(shape=(input_dim,), stddev=0.1, name='bias_2')
  13. x0w = ht.matmul_op(x0, weight_1) # (batch, hidden_dim)
  14. x0w_b = x0w + ht.broadcastto_op(bias_1, x0w)
  15. relu1 = ht.relu_op(x0w_b)
  16. x1w = ht.matmul_op(relu1, weight_2) # (batch, input_dim)
  17. x1w_b = x1w + ht.broadcastto_op(bias_2, x1w)
  18. residual = x1w_b + x0
  19. y = ht.relu_op(residual)
  20. return y
  21. def build_residual_layers(x0, input_dim, hidden_dim, num_layers=3):
  22. for i in range(num_layers):
  23. x0 = residual_layer(x0, input_dim, hidden_dim)
  24. return x0
  25. def dc_criteo(dense_input, sparse_input, y_):
  26. feature_dimension = 33762577
  27. embedding_size = 8
  28. learning_rate = 0.001
  29. Embedding = init.random_normal(
  30. [feature_dimension, embedding_size], stddev=0.01, name="snd_order_embedding")
  31. sparse_input = ht.embedding_lookup_op(Embedding, sparse_input)
  32. sparse_input = ht.array_reshape_op(sparse_input, (-1, 26*embedding_size))
  33. # dc_model
  34. x = ht.concat_op(sparse_input, dense_input, axis=1)
  35. input_dim = 26 * 8 + 13
  36. hidden_dim = input_dim
  37. residual_out = build_residual_layers(
  38. x, input_dim, hidden_dim, num_layers=5)
  39. W4 = init.random_normal([26*embedding_size + 13, 1], stddev=0.1, name="W4")
  40. y = ht.matmul_op(residual_out, W4)
  41. y = ht.sigmoid_op(y)
  42. loss = ht.binarycrossentropy_op(y, y_)
  43. loss = ht.reduce_mean_op(loss, [0])
  44. opt = ht.optim.SGDOptimizer(learning_rate=learning_rate)
  45. train_op = opt.minimize(loss)
  46. return loss, y, y_, train_op

分布式深度学习系统

Contributors (1)