How does tensorflow batch_matmul work?

前端 未结 6 1100
迷失自我
迷失自我 2020-12-31 10:34

Tensorflow has a function called batch_matmul which multiplies higher dimensional tensors. But I\'m having a hard time understanding how it works, perhaps partially because

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 10:56

    You can now do it using tf.einsum, starting from Tensorflow 0.11.0rc0.

    For example,

    M1 = tf.Variable(tf.random_normal([2,3,4]))
    M2 = tf.Variable(tf.random_normal([5,4]))  
    N = tf.einsum('ijk,lk->ijl',M1,M2)       
    

    It multiplies the matrix M2 with every frame (3 frames) in every batch (2 batches) in M1.

    The output is:

    [array([[[ 0.80474716, -1.38590837, -0.3379252 , -1.24965811],
            [ 2.57852983,  0.05492432,  0.23039417, -0.74263287],
            [-2.42627382,  1.70774114,  1.19503212,  0.43006262]],
    
           [[-1.04652011, -0.32753903, -1.26430523,  0.8810069 ],
            [-0.48935518,  0.12831448, -1.30816901, -0.01271309],
            [ 2.33260512, -1.22395933, -0.92082584,  0.48991606]]], dtype=float32),
    array([[ 1.71076882, 0.79229093, -0.58058828, -0.23246667],
           [ 0.20446332,  1.30742455, -0.07969904,  0.9247328 ],
           [-0.32047141,  0.66072595, -1.12330854,  0.80426538],
           [-0.02781649, -0.29672042,  2.17819595, -0.73862702],
           [-0.99663496,  1.3840003 , -1.39621222,  0.77119476]], dtype=float32), 
    array([[[ 0.76539308, 2.77609682, -1.79906654,  0.57580602, -3.21205115],
            [ 4.49365759, -0.10607499, -1.64613271,  0.96234947, -3.38823152],
            [-3.59156275,  2.03910899,  0.90939498,  1.84612727,  3.44476724]],
    
           [[-1.52062428,  0.27325237,  2.24773455, -3.27834225,  3.03435063],
            [ 0.02695178,  0.16020992,  1.70085776, -2.8645196 ,  2.48197317],
            [ 3.44154787, -0.59687197, -0.12784094, -2.06931567, -2.35522676]]], dtype=float32)]
    

    I have verified, the arithmetic is correct.

提交回复
热议问题