0

I have a two matrix, F(shape = (4000, 64)) and M(shape=(4000,9)) and want to have result that have shape = (4000,64*9)

i can think with for loop with below code(ideal)

result = np.zeros(4000,64*9)
ind = 0
for i in range(64):
    for j in range(9):
        result[:,ind]= tf.muliply(F[:,i]),M[:,j])
        ind += 1

but i know For Loop is not support in tensorflow

Is there a function that performs the same function as above code?


edit)

I came up with an idea. F,M repeat to shape (4000,64*9) [liek repmat in MATLAB] and elementwise multiply. Could you ever have any other ideas?

minssi
  • 333
  • 1
  • 2
  • 10

2 Answers2

2

You can use tf.matmul if you reshape your inputs to F(shape = (4000, 64, 1)) and M(shape=(4000,1, 9)). An example,

F = tf.Variable(tf.random_uniform(shape=(4000, 64, 1)))
M = tf.Variable(tf.random_uniform(shape=(4000, 1, 9)))
C = tf.matmul(F, M)
C = tf.reshape(C, (4000, -1))
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
print(C.eval().shape)

#Output: (4000, 576)
Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59
1

You could use

tf.reshape(M[:,tf.newaxis,:] * F[...,tf.newaxis], [4000,-1])
P-Gn
  • 23,115
  • 9
  • 87
  • 104
  • thank you, and i test it, a= np.arnage(9).reshape(3,3), b=np.array([10, 11, 12, 20, 21, 22]).reshape(3,2), F=tf.constant(a), M= tf.constant(b), temp= tf.reshape(M[:,tf.newaxis,:]*F[..., tf.mewaxis], [3, -1]) and sess.run and get result all -1. why don't operate correctly? – minssi Jul 08 '17 at 09:08
  • i run anther computer and get correct values. maybe my laptop problem thank you ! – minssi Jul 08 '17 at 09:14