2-D convolution as a matrix-matrix multiplication

前端 未结 4 1129
你的背包
你的背包 2020-11-29 17:07

I know that, in the 1D case, the convolution between two vectors, a and b, can be computed as conv(a, b), but also as the product betw

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 17:58

    The code shown above doesn't produce the unrolled matrix of the right dimensions. The dimension should be (n-k+1)*(m-k+1), (k)(k). k: filter dimension, n: num rows in input matrix, m: num columns.

    def unfold_matrix(X, k):
        n, m = X.shape[0:2]
        xx = zeros(((n - k + 1) * (m - k + 1), k**2))
        row_num = 0
        def make_row(x):
            return x.flatten()
    
        for i in range(n- k+ 1):
            for j in range(m - k + 1):
                #collect block of m*m elements and convert to row
                xx[row_num,:] = make_row(X[i:i+k, j:j+k])
                row_num = row_num + 1
    
        return xx
    

    For more details, see my blog post:

    http://www.telesens.co/2018/04/09/initializing-weights-for-the-convolutional-and-fully-connected-layers/

提交回复
热议问题