如何使用cblas函数计算向量中元素的值之和? [英] How to compute the sum of the values of elements in a vector using cblas functions?
问题描述
我需要对caffe中矩阵的所有元素求和,
I need to sum all the elements of a matrix in caffe,
但是我注意到,cblas函数('math_functions.hpp'
和'math_functions.cpp'
)的caffe包装器使用 cblas_sasum
函数作为 caffe_cpu_asum
计算向量中元素的绝对值之和.
But as I noticed, the caffe wrapper of the cblas functions ('math_functions.hpp'
& 'math_functions.cpp'
) is using cblas_sasum
function as caffe_cpu_asum
that computes the sum of the absolute values of elements in a vector.
由于我是cblas的新手,所以我试图找到一个合适的函数来摆脱 absolute ,但是似乎cblas中没有该属性的函数.
Since I'm a newbie in cblas, I tried to find a suitable function to get rid of absolute there, but it seems that there is no function with that property in cblas.
有什么建议吗?
推荐答案
有一种使用cblas函数的方法,尽管有点尴尬.
There is a way to do so using cblas functions, though it is a bit of an awkward way.
您需要做的是定义一个全1"向量,然后在该向量和矩阵之间做一个点积,结果就是和.
What you need to do is to define an "all 1" vector, and then do a dot product between this vector and your matrix, the result is the sum.
让myBlob
成为要汇总其元素的Caffe Blob:
Let myBlob
be a caffe Blob whose elements you want to sum:
vector<Dtype> mult_data( myBlob.count(), Dtype(1) );
Dtype sum = caffe_cpu_dot( myBlob.count(), &mult_data[0], myBlob.cpu_data() );
"Reduction"
层的实现.
要使此答案均符合GPU,必须为mult_data
分配 Blob
而不是std::vector
(因为您需要的是pgu_data()
):
To make this answer both GPU compliant, one need to allocate a Blob
for mult_data
and not a std::vector
(because you need it's pgu_data()
):
vector<int> sum_mult_shape(1, diff_.count());
Blob<Dtype> sum_multiplier_(sum_mult_shape);
const Dtype* mult_data = sum_multiplier_.cpu_data();
Dtype sum = caffe_cpu_dot( myBlob.count(), &mult_data[0], myBlob.cpu_data() );
对于GPU,(在'.cu'
源文件中):
For GPU, (in a '.cu'
source file):
vector<int> sum_mult_shape(1, diff_.count());
Blob<Dtype> sum_multiplier_(sum_mult_shape);
const Dtype* mult_data = sum_multiplier_.gpu_data();
Dtype sum;
caffe_gpu_dot( myBlob.count(), &mult_data[0], myBlob.gpu_data(), &sum );
这篇关于如何使用cblas函数计算向量中元素的值之和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!