python - tensorflow efficient way for tensor multiplication -
i have 2 tensors in tensorflow, first tensor 3-d, , second 2d. , want multiply them this:
x = tf.placeholder(tf.float32, shape=[sequence_length, batch_size, hidden_num]) w = tf.get_variable("w", [hidden_num, 50]) b = tf.get_variable("b", [50]) output_list = [] step_index in range(sequence_length): output = tf.matmul(x[step_index, :, :], w) + b output_list.append(output) output = tf.pack(outputs_list)
i use loop multiply operation, think slow. best way make process simple/clean possible?
you use batch_matmul
. unfortunately doesn't seem batch_matmul
supports broadcasting along batch dimension, have tile w
matrix. use more memory, operations stay in tensorflow
a = tf.ones((5, 2, 3)) b = tf.ones((3, 1)) b = tf.reshape(b, (1, 3, 1)) b = tf.tile(b, [5, 1, 1]) c = tf.batch_matmul(a, b) # use tf.matmul in tf 1.0 sess = tf.interactivesession() sess.run(tf.shape(c))
this gives
array([5, 2, 1], dtype=int32)
Comments
Post a Comment