简体   繁体   English

等级> 2的Tensorflow matmul操作不起作用

[英]Tensorflow matmul operation for rank>2 does not work

I find the following on the Tensorflow documentation homepage for using the matmul operation when rank>2: 我在Tensorflow文档主页上找到以下内容,以便在rank> 2时使用matmul操作:

https://www.tensorflow.org/api_docs/python/math_ops/matrix_math_functions#matmul https://www.tensorflow.org/api_docs/python/math_ops/matrix_math_functions#matmul

# 3-D tensor `a`
a = tf.constant(np.arange(1,13), shape=[2, 2, 3]) => [[[ 1.  2.  3.]
                                                   [ 4.  5.  6.]],
                                                  [[ 7.  8.  9.]
                                                   [10. 11. 12.]]]

# 3-D tensor `b`
b = tf.constant(np.arange(13,25), shape=[2, 3, 2]) => [[[13. 14.]
                                                    [15. 16.]
                                                    [17. 18.]],
                                                   [[19. 20.]
                                                    [21. 22.]
                                                    [23. 24.]]]
c = tf.matmul(a, b) => [[[ 94 100]
                     [229 244]],
                    [[508 532]
                     [697 730]]]

It simply isn't working when I plug it in Python. 当我用Python插入它时,它根本不起作用。 I get 我明白了

c = tf.matmul(a, b)
ValueError: Shape must be rank 2 but is rank 3

Anyone know what is wrong? 谁知道什么是错的?

Is your TensorFlow too old? 你的TensorFlow太旧了吗? Here's what I get in version 0.12rc0 这是我在版本0.12rc0中得到的

a = tf.constant(np.arange(1,13).astype(np.float32), shape=[2, 2, 3])
b = tf.constant(np.arange(13,25).astype(np.float32), shape=[2, 3, 2])
sess.run(tf.matmul(a, b)) =>

array([[[  94.,  100.],
        [ 229.,  244.]],

       [[ 508.,  532.],
        [ 697.,  730.]]], dtype=float32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM