zoukankan      html  css  js  c++  java
  • 【TensorFlow随笔】关于一个矩阵与多个矩阵相乘的问题

    问题描述:

    Specifically, I want to do matmul(A,B) where
     'A' has shape (m,n)
     'B' has shape (k,n,p)
    and the result should have shape (k,m,p)


    参考网站:

    https://groups.google.com/a/tensorflow.org/forum/#!topic/discuss/4tgsOSxwtkY

    https://stackoverflow.com/questions/38235555/tensorflow-matmul-of-input-matrix-with-batch-data


    解决办法:

    1,我们知道TensorFlow的matmul已经支持了batch,即:

    A = tf.Variable(tf.random_normal(shape=(a, b, n, m)))
    B = tf.Variable(tf.random_normal(shape=(a, b, m, k)))
    tf.matmul(A, B)

    会返回(a,b,n,k),前面的N个维度会被保留。但是适用情景与题目不符。

    2,所以我们自然想到reshape。

    You can conflate the two dimensions not used in the multiplication using reshape, multiply the two matrices, and then call reshape again to get the desired shape. This is equivalent to doing batch multiplication.

    简而言之呢,就是,你可以将乘法中用不到的维度reshape到后面去,比如

    (k, m, p) => (m, p * k)

    进行矩阵乘法得到:(n, p * k)

    之后reshape成:(k, n, p)。

    虽然有些麻烦,但这是唯一的解决办法了。

    适用情景:A矩阵只有一个,但是B矩阵有batch_num个,需要逐个进行矩阵乘法的场合。

  • 相关阅读:
    慢性肾炎患者的家庭养护
    中国居民五谷膳食指南
    第二单元丨对环境的察觉
    一个人是否靠谱,闭环很重要
    打赢营销胜仗
    治本修身──中醫內分泌調節養生法
    理想的激励
    卓越演讲:打动听众必知必会的策略和方法(原书第3版)
    每日一题_191008
    每日一题_191007
  • 原文地址:https://www.cnblogs.com/ldzhangyx/p/8076522.html
Copyright © 2011-2022 走看看