neural network - How to compute the sum of the values of elements in a vector using cblas functions? -


i need sum elements of matrix in caffe,

but noticed, caffe wrapper of cblas functions ('math_functions.hpp' & 'math_functions.cpp') using cblas_sasum function caffe_cpu_asum computes sum of absolute values of elements in vector.

since i'm newbie in cblas, tried find suitable function rid of absolute there, seems there no function property in cblas.

any suggestion?

there way using cblas functions, though bit of awkward way.

what need define "all 1" vector, , dot product between vector , matrix, result sum.

let myblob caffe blob elements want sum:

vector<dtype> mult_data( myblob.count(), dtype(1) ); dtype sum = caffe_cpu_dot( myblob.count(), &mult_data[0], myblob.cpu_data() ); 

this trick used in implementation of "reduction" layer.


to make answer both gpu compliant, 1 need allocate blob mult_data , not std::vector (because 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() ); 

for gpu, (in '.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 ); 

Comments