text
stringlengths
1
93.6k
################################################################################
# Simple unit tests for MPNNs.
import numpy as np
import tensorflow as tf
import mpnn
def build_feed_dict(ph, h, adjacency, dist, m):
return {ph[0]: h, ph[1]: adjacency, ph[2]: dist, ph[3]: m}
def get_permutation_test_outputs(hparams):
num_nodes = 4
batch_size = 3
input_dim = 5
output_dim = 2
with tf.Graph().as_default():
model = mpnn.MPNN(hparams, input_dim, output_dim, num_edge_class=5)
ph, _ = model.get_fprop_placeholders()
pred_op = model.fprop(*ph)
adjacency = np.random.randint(2, size=(batch_size, num_nodes, num_nodes))
dist = np.random.rand(batch_size, num_nodes, num_nodes)
h = np.random.rand(batch_size, num_nodes, input_dim)
perm = np.random.permutation(num_nodes)
h_perm = np.zeros_like(h)
adjacency_perm = np.zeros_like(adjacency)
dist_perm = np.zeros_like(dist)
m = np.full((batch_size, num_nodes), 1)
for i in xrange(len(h_perm)):
h_perm[i] = h[i][perm]
for i in xrange(len(adjacency_perm)):
adjacency_perm[i] = adjacency[i][perm]
dist_perm[i] = dist[i][perm]
for j in xrange(len(adjacency_perm[i])):
adjacency_perm[i][j] = adjacency_perm[i][j][perm]
dist_perm[i][j] = dist_perm[i][j][perm]
print h.shape, h_perm.shape
print adjacency.shape, adjacency_perm.shape
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
output = sess.run(
pred_op, feed_dict=build_feed_dict(ph, h, adjacency, dist, m))
output_perm = sess.run(
pred_op,
feed_dict=build_feed_dict(ph, h_perm, adjacency_perm, dist_perm, m))
print "output no perm:"
print output
print "\noutput perm:"
print output_perm
return output, output_perm
def get_pad_test_outputs(hparams):
# TODO(gilmer) This should test different paddings within the same batch,
# in a similar way as in set2vec_test.py
hparams = mpnn.MPNN.default_hparams()
num_nodes = 4
batch_size = 3
input_dim = 5
output_dim = 2
pad = 3
with tf.Graph().as_default():
model = mpnn.MPNN(hparams, input_dim, output_dim, num_edge_class=5)
ph, _ = model.get_fprop_placeholders()
pred_op = model.fprop(*ph)
adjacency = np.random.randint(2, size=(batch_size, num_nodes, num_nodes))
dist = np.random.rand(batch_size, num_nodes, num_nodes)
h = np.random.rand(batch_size, num_nodes, input_dim)
m = np.full((batch_size, num_nodes), 1.0)
h_pad = np.zeros((h.shape[0], h.shape[1] + pad, h.shape[2]))
adjacency_pad = np.zeros((adjacency.shape[0], adjacency.shape[1] + pad,
adjacency.shape[2] + pad))
dist_pad = np.zeros((dist.shape[0], dist.shape[1] + pad,
dist.shape[2] + pad))
m_pad = np.zeros((batch_size, num_nodes + pad))
for i in xrange(batch_size):
for j in xrange(num_nodes):
m_pad[i][j] = 1
for i in xrange(len(h)):
for j in xrange(len(h[i])):
for k in xrange(len(h[i][j])):
h_pad[i][j][k] = h[i][j][k]
for i in xrange(len(adjacency)):
for j in xrange(len(adjacency[i])):