IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1KB

  1. import decagon_pytorch.convolve
  2. import decagon.deep.layers
  3. import torch
  4. import tensorflow as tf
  5. import numpy as np
  6. def prepare_data():
  7. np.random.seed(0)
  8. latent = np.random.random((5, 10)).astype(np.float32)
  9. latent[latent < .5] = 0
  10. latent = np.ceil(latent)
  11. adjacency_matrices = []
  12. for _ in range(5):
  13. adj_mat = np.random.random((len(latent),) * 2).astype(np.float32)
  14. adj_mat[adj_mat < .5] = 0
  15. adj_mat = np.ceil(adj_mat)
  16. adjacency_matrices.append(adj_mat)
  17. return latent, adjacency_matrices
  18. def sparse_graph_conv_torch():
  19. torch.random.manual_seed(0)
  20. latent, adjacency_matrices = prepare_data()
  21. print('latent.dtype:', latent.dtype)
  22. latent = torch.tensor(latent).to_sparse()
  23. adj_mat = adjacency_matrices[0]
  24. adj_mat = torch.tensor(adj_mat).to_sparse()
  25. print('adj_mat.dtype:', adj_mat.dtype,
  26. 'latent.dtype:', latent.dtype)
  27. conv = decagon_pytorch.convolve.SparseGraphConv(10, 10,
  28. adj_mat)
  29. latent = conv(latent)
  30. return latent
  31. def dense_to_sparse_tf(x):
  32. a, b = np.where(x)
  33. indices = np.array([a, b]).T
  34. values = x[a, b]
  35. return tf.sparse.SparseTensor(indices, values, x.shape)
  36. def sparse_graph_conv_tf():
  37. torch.random.manual_seed(0)
  38. latent, adjacency_matrices = prepare_data()
  39. conv_torch = decagon_pytorch.convolve.SparseGraphConv(10, 10,
  40. torch.tensor(adjacency_matrices[0]).to_sparse())
  41. weight = tf.constant(conv_torch.weight.detach().numpy())
  42. latent = dense_to_sparse_tf(latent)
  43. adj_mat = dense_to_sparse_tf(adjacency_matrices[0])
  44. latent = tf.sparse_tensor_dense_matmul(latent, weight)
  45. latent = tf.sparse_tensor_dense_matmul(adj_mat, latent)
  46. return latent
  47. def test_sparse_graph_conv():
  48. latent_torch = sparse_graph_conv_torch()
  49. latent_tf = sparse_graph_conv_tf()
  50. assert np.all(latent_torch.detach().numpy() == latent_tf.eval(session = tf.Session()))
  51. def test_sparse_dropout_grap_conv_activation():
  52. pass
  53. def test_sparse_multi_dgca():
  54. pass