optimizing this code?

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

optimizing this code?

Postby rm9 » Wed Mar 26, 2014 7:49 am

Hi,

I have the following code which is used repeatedly in training a neural network with arrayfire.
It seems that this code runs slowly, even compared to matlab.
Is there anyway to make it faster? am I missing something? Thanks.

Code: Select all
array RandomMapPoolLayer::Forward(const array& data) {
   int number_of_maps = data.dims(3);
   assert(number_of_maps % num_ == 0);
   int k = number_of_maps / num_;
   if (!selected_inds) {
      selected_inds.reset(new array(k, 1));
   }
   array output(constant(0, data.dims(0) * data.dims(1), data.dims(2), k));
   array norms = af::mean(af::sum(af::pow(data, 2, true), 0), 4);
   array probs(num_, 1);
   array cprobs(num_, 1);
   array dd(num_, 1);
   array p(randu(k, 1, af::f32));
   for (int i = 0; i < k; ++i) {
      int s = i * num_;
      int e = (i + 1) * num_ - 1;
      probs = norms(seq(s, e)) / sum(norms(seq(s, e)));
      cprobs = accum(probs);
      dd = cprobs - p(i) > 0;
      int j = af::where(dd)(0).scalar<float>();
      (*selected_inds)(i) = s + j;
   }
   array d = moddims(data, data.dims(0) * data.dims(1), data.dims(2), data.dims(3));
   output = d(span, span, *selected_inds);
   output = moddims(output, data.dims(0), data.dims(1), data.dims(2), k);
   return output;
}

array RandomMapPoolLayer::Backpropagate(const array& prev_delta) {
   array d2(constant(0, prev_delta.dims(0) * prev_delta.dims(1), prev_delta.dims(2), prev_delta.dims(3) * num_));
   d2(span, span, *selected_inds) = moddims(prev_delta, prev_delta.dims(0) * prev_delta.dims(1), prev_delta.dims(2), prev_delta.dims(3));
   d2 = moddims(d2, prev_delta.dims(0), prev_delta.dims(1), prev_delta.dims(2), prev_delta.dims(3) * num_);
   return d2;
}
rm9
 
Posts: 54
Joined: Thu Jan 30, 2014 5:44 am

Return to [archive-commercial] Programming & Development with ArrayFire

cron