【问题标题】:How to do dot product between matrices in caffe?如何在caffe中的矩阵之间做点积?
【发布时间】:2017-05-31 00:29:02
【问题描述】:

在内积层,我需要乘以(top_diff * bottom_data) .* (2*weight)。首先我们将 (result = top_diff * bottom_data) 计算为caffe_cpu_gemm 中的矩阵乘法,然后在weightresult 之间进行dot product

更多解释定义如下:

const Dtype* weight = this->blobs_[0]->cpu_data();
     if (this->param_propagate_down_[0]) {
            const Dtype* top_diff = top[0]->cpu_diff();
            const Dtype* bottom_data = bottom[0]->cpu_data();
caffe_cpu_gemm<Dtype>(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1.,
        top_diff, bottom_data, (Dtype)1., this->blobs_[0]->mutable_cpu_diff());
}

为了进一步了解,我查看了math_function.c。实现如下:

template<>
void caffe_cpu_gemm<float>(const CBLAS_TRANSPOSE TransA,
    const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K,
    const float alpha, const float* A, const float* B, const float beta,
    float* C) {
  int lda = (TransA == CblasNoTrans) ? K : M;
  int ldb = (TransB == CblasNoTrans) ? N : K;
  cblas_sgemm(CblasRowMajor, TransA, TransB, M, N, K, alpha, A, lda, B,
      ldb, beta, C, N);
}

我认为我应该在 caffe_cpu_gemm() 中执行乘法 (result = top_diff * bottom_data),然后在 dot productweight 中执行。我该怎么办?!

非常感谢!!!!任何建议将不胜感激!

【问题讨论】:

    标签: caffe matrix-multiplication cblas inner-product


    【解决方案1】:

    如果你只想在两个矩阵之间进行点积,你可以使用下面的函数在 CPU 上进行矩阵相乘,

    void caffe_mul&lt;float&gt;(const int n, const float* a, const float* b, float* y)

    如果您想在 GPU 上执行相同的操作,请使用此模板

    void caffe_gpu_mul<float>(const int N, const float* a, const float* b, float* y)
    

    a 和 b 是矩阵,c 将包含最终结果。 N 是矩阵中元素的总数。

    您也可以使用“Eltwise”层,它已经这样做了。

    【讨论】:

      猜你喜欢
      • 2019-06-26
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多