[SciPy-User] optimising python numpy snippet
Eric Hermes
ehermes at chem.wisc.edu
Fri Jul 25 10:42:04 EDT 2014
1) Yes, that is correct. You should make sure that the relevant arrays
(self.pooled, self.conv_out, and self.b) are NumPy arrays in order for
the code I wrote to work properly.
Specifically, it looks like you were mostly working with self.pooled[0]
and self.conv_out[0], so I don't really know what's going on with the
first dimension of that array. Depending on how these are structured,
you may need to rewrite self.pooled[0, :, :, k, l] as self.pooled[0][:,
:, k, l], and self.conv_out[0, :, :, k*3:(k+1)*3, l*3:(l+1)*3] as
self.conv_out[0][:, :, k*3:(k+1)*3, l*3:(l+1)*3].
2) Not really, no. NumPy/Python do not unroll loops, as this is
typically a feature of compiled languages.
3) It should, yes.
Eric
On 7/24/2014 11:58 PM, Sai Rajeshwar wrote:
> thanks eric.. few queries
> 1) these vector operations are possible only because of numpy,?
> 2) is the optimisation you have suugested a kind of loop unrolling..
> 3)does the above optimisation uses SIMD internally
>
> thanks
>
>
> *with regards..*
> *
> *
> *M. Sai Rajeswar*
> *M-tech Computer Technology*
> *IIT Delhi
> ----------------------------------Cogito Ergo Sum---------
> *
>
>
> On Fri, Jul 25, 2014 at 12:07 AM, Eric Hermes <ehermes at chem.wisc.edu
> <mailto:ehermes at chem.wisc.edu>> wrote:
>
> Hello,
>
> A quick and dirty simplification may end up in a bit of a speed
> boost. You can eliminate some of the loops and replace them with
> vector operations, as below.
>
>
> for k in xrange(self.pooled_shape[3]):
> for l in xrange(self.pooled_shape[4]):
> self.pooled[0, :, :, k, l] = numpy.tanh(self.conv_out[0,
> :, :, k*3:(k+1)*3, l*3:(l+1)*3].sum((2, 3))/9. + self.b[:, :])
>
> I have left in some extraneous slices (:) for clarity of code.
>
> Eric
>
>
> On 7/24/2014 12:40 PM, Sai Rajeshwar wrote:
>> hi all,
>>
>> I have written the following for loop statement inn my code..
>> when i profiled it.. i found that it is taking huge amount of
>> time.. can any one suggest to make this statement faster
>> ----------------------------------------------------------------------------
>>
>> f*or i in xrange(self.pooled_shape[1]):
>> for j in xrange(self.pooled_shape[2]):
>> for k in xrange(self.pooled_shape[3]):
>> for l in xrange(self.pooled_shape[4]):
>> self.pooled[0][i][j][k][l]=math.tanh((numpy.sum(self.conv_out[0][i][j][k*3][l*3:(l+1)*3])+numpy.sum(self.conv_out[0][i][j][k*3+1][l*3:(l+1)*3])+numpy.sum(self.conv_out[0][i][j][k*3+2][l*3:(l+1)*3]))/9.0+self.b[i][j])*
>>
>> thanks a lot in advance
>>
>>
>> *with regards..*
>> *
>> *
>> *M. Sai Rajeswar*
>> *M-tech Computer Technology*
>> *IIT Delhi
>> ----------------------------------Cogito Ergo Sum---------
>> *
>>
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org <mailto:SciPy-User at scipy.org>
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
> --
> Eric Hermes
> J.R. Schmidt Group
> Chemistry Department
> University of Wisconsin - Madison
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org <mailto:SciPy-User at scipy.org>
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
--
Eric Hermes
J.R. Schmidt Group
Chemistry Department
University of Wisconsin - Madison
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140725/bfa874d7/attachment.html>
More information about the SciPy-User
mailing list