Created
April 3, 2019 20:02
-
-
Save strubell/199a867ab2a63c81be84d8f1dd1dcb40 to your computer and use it in GitHub Desktop.
tf scoring test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
predicates_in_batch = 2 | |
seq_len = 3 | |
vn_labels = 4 | |
d = 5 | |
np.random.seed(5) | |
tf.set_random_seed(5) | |
# [predicates_in_batch, seq_len, vn_labels] | |
vn_scores = [ | |
[ | |
[1, 0, 0, 0], | |
[0.5, 0, 0.5, 0], | |
[0.2, 0.2, 0.3, 0.3] | |
], | |
[ | |
[0.1, 0.2, 0.3, 0.4], | |
[0, 0.1, 0.1, 0.8], | |
[0, 1, 0, 0] | |
], | |
] | |
# [predicates_in_batch, seq_len, 1, vn_labels] | |
vn_scores_add_dim = tf.expand_dims(tf.constant(vn_scores, dtype=tf.float64), 2) | |
# [predicates_in_batch, vn_labels, d] | |
linear = tf.constant(np.random.rand(predicates_in_batch, vn_labels, d)) | |
# [predicates_in_batch, seq_len, vn_labels, d] | |
linear_tiled = tf.tile(tf.expand_dims(linear, 1), [1, seq_len, 1, 1]) | |
actual = tf.squeeze(tf.matmul(vn_scores_add_dim, linear_tiled), 2) | |
np.set_printoptions(threshold=np.inf) | |
with tf.Session() as sess: | |
sess.run(tf.tables_initializer()) | |
linear_np, linear_tiled_np, vn_scores_add_dim_np, actual_np = sess.run([linear, linear_tiled, vn_scores_add_dim, actual]) | |
print("actual", actual_np) | |
expected = np.empty((predicates_in_batch, seq_len, d)) | |
for p_idx, predicate in enumerate(vn_scores): | |
# [vn_labels, d] | |
p_lin = linear_np[p_idx] | |
for t_idx, tok in enumerate(predicate): | |
# [vn_labels] | |
composed = np.dot(tok, p_lin) | |
expected[p_idx, t_idx] = composed | |
print("expected", expected) | |
np.testing.assert_almost_equal(actual_np, expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment