Cython: Transpose a memoryview

2016-05-14T20:44:03

Some background for the question:

I'm trying to optimise a custom neural network code. It relies heavily on loops and I decided to use cython to speed up the calculation.

I followed the usual online tips: Declare all local variables with appropriate cdefs and switch off boundscheck and nonecheck. This barely gave me 10% performance.

Well, my code relies on lots of class members. Therefore I decided to convert the entire class into a cdef class. Turns out that cython doesn't allow numpy ndarrays as types for class members. Instead one has to use memoryviews. Unfortunately the two types seem to be vastly incompatible.

I already ran into this problem: Cython memoryview transpose: Typeerror

To sum it up: You can store an np.ndarray in a memoryview. You can transpose it and store the returned array in a memview. But not if that memview is a class member. Then you have to create an intermediate memview, store the result in that and assign the intermediate memview to the class member.

Here's the code ( many thanks to DavidW)

def double[:,:,:,:] temporary_view_of_transpose

# temporary_view_of_transpose now "looks at" the memory allocated by transpose
# no square brackets!
temporary_view_of_transpose = out_image.transpose(1, 0, 2, 3)

# data is copied from temporary_view_of_transpose to self.y
self.y[...] = temporary_view_of_transpose # (remembering that self.y must be the correct shape before this assignment).

Now I've got a new problem. The code above is from the so-called "forward-pass". There is also a corresponding backward-pass, which does all the calculations backward (for analytical gradients).

This means that for the backward pass, I have to transpose the memoryview and store it in a numpy array:

cdef np.ndarray[DTYPE_t, ndim=4] d_out_image = self.d_y.transpose(1, 0, 2,3)

d_y has to be a class member, therefore it has to be a memoryview. Memoryviews don't allow transposing. They have a .T method, but that doesn't help me.

Actual Question:

  • How do I correctly store a numpy array as a class member of a cdef class?
  • If the answer is :"as a memoryview", how do I transpose a memoryview ?

Copyright License:
Author:「lhk」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/37226813/cython-transpose-a-memoryview

About “Cython: Transpose a memoryview” questions

Some background for the question: I'm trying to optimise a custom neural network code. It relies heavily on loops and I decided to use cython to speed up the calculation. I followed the usual onl...
I have a function (from an external Python library) that returns a memoryview object that I want to process in Cython. Is there a way to convert it to a typed memoryview of bytes (without copy) for
Consider this dummy Cython code: #!python #cython: boundscheck=False #cython: wraparound=False #cython: initializedcheck=False #cython: cdivision=True #cython: nonecheck=False import numpy as np #
I'm currently setting my MemoryViews in my Cython pyx file as follows: @cython.boundscheck(False) cdef int[:] fill_memview(): # This happens inside a big loop so needs to be fast cdef int[...
I have a function written in regular numpy ndarray and another one with a typed memoryview. However, I couldn't get the memoryview version to work faster than the regular version (unlike many of the
How can I sort a memoryview in-place in Cython? Is there a built-in function that can do it? Right now I have to use a numpy array instead and use numpy's sort, which is very slow.
Using Cython, I try to do this: cpdef myFun(double[:] array): cdef int[:] sortIndices = np.argsort(array, kind='mergesort') array = array[sortIndices] The compiler complains: Invalid ...
I'm trying to develop a small Convolutional Neural Network framework with python. The code for the convolutional node already works (slowly) and I would like to speed it up. The hotspots are the lo...
I have a function that is given a memoryview vector and I want to calculate the norm of that vector. Until now I achieved that by converting the memoryview to a Numpy array and calculating the norm...
I have a bunch of numpy arrays as attributes of an array of python objects, in cython, in preparation for prange processing (which requires nogil), I wanted to create a memory view that was "indire...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.