微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 排名> 2的Tensorflow matmul操作不起作用

我在Tensorflow文档主页上找到以下内容,以便在排名> 2时使用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]]]

当我用Python插入它时,它根本不起作用.我明白了

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

谁知道什么是错的?

解决方法:

你的TensorFlow太旧了吗?这是我在版本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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐