def test_ip(self): """ Test the 'ip' binary operator on orthogonal vectors""" x_src = np.random.randn(self.d) x_src /= np.linalg.norm(x_src) # normalize x_src x_dst = np.random.randn(self.d) x_dst -= x_dst.dot(x_src) * x_src # make x_dst orthogonal to x_src x_dst /= np.linalg.norm(x_dst) # normalize x_dst expected = np.dot(x_src, x_dst) sess = tf.InteractiveSession() x_src = tf.constant(x_src, shape=(1, self.d), dtype="float64") x_dst = tf.constant(x_dst, shape=(1, self.d), dtype="float64") li = link_inference(edge_feature_method="ip")([x_src, x_dst]) print( "link inference with 'ip' operator on orthonormal vectors: {}, expected: {}" .format(li.eval(), expected)) assert li.eval() == pytest.approx(expected) assert li.eval() == pytest.approx(0) li = link_inference(edge_feature_method="ip")([x_src, x_src]) print("link inference with 'ip' operator on unit vector: ", li.eval()) assert li.eval() == pytest.approx(1) sess.close()
def test_ip(self): """ Test the 'ip' binary operator on orthogonal vectors""" x_src, x_dst = make_orthonormal_vectors(self.d) x_src = tf.constant(x_src, shape=(1, self.d), dtype="float64") x_dst = tf.constant(x_dst, shape=(1, self.d), dtype="float64") li = link_inference(edge_embedding_method="ip", output_act="linear")([x_src, x_dst]) print("link inference with 'ip' operator on orthonormal vectors: {}". format(li)) assert li.numpy() == pytest.approx(0, abs=1.5e-7) li = link_inference(edge_embedding_method="ip", output_act="linear")([x_src, x_src]) print("link inference with 'ip' operator on unit vector: ", li) assert li.numpy() == pytest.approx(1, abs=1.5e-7) # Test sigmoid activation li = link_classification(edge_embedding_method="ip", output_act="sigmoid")([x_src, x_dst]) assert li.numpy() == pytest.approx(0.5, abs=1.5e-7) li = link_classification(edge_embedding_method="ip", output_act="sigmoid")([x_src, x_src]) assert li.numpy() == pytest.approx(0.7310586, abs=1.5e-7)
def test_mul_l1_l2_avg(self): """ Test the binary operators: 'mul'/'hadamard', 'l1', 'l2', 'avg'""" x_src = np.random.randn(self.d) x_src /= np.linalg.norm(x_src) # normalize x_src x_dst = np.random.randn(self.d) x_dst -= x_dst.dot(x_src) * x_src # make x_dst orthogonal to x_src x_dst /= np.linalg.norm(x_dst) # normalize x_dst x_src = x_src.reshape(1, 1, self.d) x_dst = x_dst.reshape(1, 1, self.d) inp_src = keras.Input(shape=(1, self.d)) inp_dst = keras.Input(shape=(1, self.d)) for op in ["mul", "l1", "l2", "avg"]: out = link_inference(output_dim=self.d_out, edge_embedding_method=op)([inp_src, inp_dst]) li = keras.Model(inputs=[inp_src, inp_dst], outputs=out) res = li.predict(x=[x_src, x_dst]) print("link inference with '{}' operator: {}".format( op, res.flatten())) assert res.shape == (1, self.d_out) assert isinstance(res.flatten()[0], np.float32)
def test_mul_l1_l2_avg(self): """ Test the binary operators: 'mul'/'hadamard', 'l1', 'l2', 'avg'""" x_src, x_dst = make_orthonormal_vectors(self.d) x_src = x_src.reshape(1, 1, self.d) x_dst = x_dst.reshape(1, 1, self.d) inp_src = keras.Input(shape=(1, self.d)) inp_dst = keras.Input(shape=(1, self.d)) for op in ["mul", "l1", "l2", "avg", "concat"]: out = link_inference(output_dim=self.d_out, edge_embedding_method=op)([inp_src, inp_dst]) li = keras.Model(inputs=[inp_src, inp_dst], outputs=out) print(x_src.shape) res = li.predict(x=[x_src, x_dst]) print("link inference with '{}' operator: {}".format( op, res.flatten())) assert res.shape == (1, self.d_out) assert isinstance(res.flatten()[0], np.float32)