TensorFlow Error: Shape must be rank 2 but are rank 3 for ' Matmul ' (op: ' Matmul ') __ Depth study

Source: Internet
Author: User
TensorFlow matrix multiplication, rank different error

In the TensorFlow wrote such a sentence:

[python] view plain copy print? Y_out = Tf.matmul (outputs, W)

Y_out = Tf.matmul (outputs, W)

Where the shape of the outputs [16,336,400],w shape is [400,1]

The following error occurred:

Shape must be rank 2 but are rank 3 for ' Matmul ' (op: ' Matmul ') with input shapes: [16,336,400], [400,1].


NumPy, there's no problem with the same wording.

[Python] View Plain copy print? import numpy as np      A = np.array ([[[1, 2, 3, 4],            [5, 6, 7, 8],            [9, 0, 1, 2]],            [[4, 3, 2, 1],            [8, 7, 6, 5],                   [2, 1, 0, 9]])    print (A)    print (A.shape)    print (' ————————— ')       b = np.array ([1], [2], [3], [4 ]]    print (B)    print (B.shape)    print (' ————————— ')       c  = np.matmul (a, b)    print (C)    print (C.shape)   

Import NumPy as np

A = Np.array ([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 0, 1, 2]],  [[ 4, 3, 2, 1],
[8, 7, 6, 5], [2, 1, 0, 9]]]
print (A) print (a.shape) print (
'----- ----------------------')

B = Np.array ([[1], [2], [3], [4]]) print (
B)
print (b.shape) print (
'---- -----------------------')

C = Np.matmul (A, B)
print (c)
print (C.shape)

Output results:

[python] view plain copy print?   [[[1 2 3 4] [5 6 7 8] [9 0 1 2]] [[4 3 2 1] [8 7 6 5] [2 1 0 9]]]     (2, 3, 4) ————————— [[1] [2] [3] [4]] (4, 1) ————————— [[[30] [70] [20]] [[20] [60] [40]]] (2, 3, 1)

[[[1 2 3 4] [5 6 7 8] [9 0 1 2]] [[4 3 2 1] [8 7 6 5] [2 1 0 9
  ]]]




(2, 3, 4)---------------------------[[1] [2] [3] [4]] (4, 1)---------------------------[[[30] [70] [20]] [[20] [60] [4 0]]] (2, 3, 1)

Solutions

[Python] View Plain copy print? import numpy as np   import tensorflow as tf   sess =  TF. Session ()       A = np.array ([[[1, 2, 3, 4],                   [5, 6, 7, 8],                   [9,  0, 1, 2]],                  [[4, 3, 2, 1],                   [8, 7, 6, 5],                   [2, 1, 0, 9]]]    B =  np.array ([[[1], [2], [3], [4]]])       a = tf.caSt (Tf.convert_to_tensor (A),  tf.int32)  # shape=[2, 3, 4]   b =  Tf.cast (Tf.convert_to_tensor (B),  tf.int32)  # shape=[4, 1]      # ———————— ————— – Modify part (start) ————————————— –   #要想让A和B进行tf. Matmul operation, the first dimension must be consistent. So the first dimension of B-tile is converted to [2, 4, 1]    b_ = tf.tile (b, [2, 1]) # b to replicate twice times, Second dimensional replication 1 time times    B = tf.reshape (b_, [2, 4, 1])    #  or   more general modification:    #B_  = tf.tile (B, [tf.shape (A) [0], 1])    #B  = tf.reshape (b_,  [tf.shape (A) [0], tf.shape (b) [0], tf.shape (b) [1]])    # —————————————-Modify part (end) —————————— ——— –      #此时就可以matmul了    c = tf.matmul (a, b)    print (' C: ', C.get_shape (). As_list ())    Sess.run (C)   

import NumPy as NP import TensorFlow as tf sess = tf. Session () A = Np.array ([[[[1, 2, 3, 4],                 [5, 6, 7, 8],                [9, 0, 1, 2]] ,               [[4, 3, 2, 1],                 [8, 7, 6, 5],                 [2, 1, 0, 9]]) B = Np.array ([[1], [2], [3], [4]]) A = Tf.cast (TF. Convert_to_tensor (A), Tf.int32) # shape=[2, 3, 4] B = Tf.cast (Tf.convert_to_tensor (B), Tf.int32) # shape=[4, 1] 
#-----------------------------------------Modify part (start)-----------------------------------------# For A and B to Tf.matmul operations, the first dimension must be consistent. So we have to tile B first and then turn it into [2, 4, 1] D b_ = Tf.tile (b, [2, 1]) # b The first-dimension copy twice times, the second dimension replicates 1 time times B = Tf.reshape (B_, [2, 4, 1]) # or more general modification: #B_ = tf.tile (b, [Tf.shape (a) [0], 1]) #B = Tf.reshape (B_, [Tf.shape (a) [0], Tf.shape (b) [0], Tf.shape (b) [1]]) #----------------------- ------------------Modify part (end)-----------------------------------------#此时就可以matmul了 C = Tf.matmul (A, B) print (' C: ', C.get_shape (). As_list ()) Sess.run (C)

Output results:

[python] view plain copy print?  (' C: ', [2, 3, 1]) array ([[[[30], [70], [20]], [[20], [60], [40]]], DTYPE=INT32)

(' C: ', [2, 3, 1])

Array ([[[[M],
        [M], [[]]
        ],

       [[M], [m],
        []]]
        , Dtype=int32


Transfer from blog: http://blog.csdn.net/blythe0107/article/details/74171870

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.