Question edited 11/21/2017 21:08 (Moscow time) :)
Hello, there was a problem with speeding up calculations in a Python 3 program.
Task and implementation:
The numpy library is used.
Task:
Speed up the execution time of the code below:
The code (Python 3) (from my point of view, working, but very slow, is performed on the test task, but is not checked on the real one, because the counting time is great):
def count(self, A, n1, n2, syn_, type_):# подсчет result=[] i_=0 #-------------------------------- # Создаем списки и зануляем их элементы (реализация на мой взгляд не очень) result=np.random.random((len(A)-n1+1, len(A[0])-n2+1, len(AT))) result[:]=0 # Конечный результат B=np.random.random((n1, n2, len(AT))) B[:]=0 # Список B - часть входного списка (массива) #-------------------------------- for i in range(0, len(A)-(n1-1)):# проход по строкам (-(n1-1) - элементы не проходим, т.к. они будут проверны на итерации len(A)-n1+1) A_=A[i:i+n1]# "урезаем" массив и берем нужные элементы for l in range(0, len(A[i])-(n2-1)):# проход по столбцам (-(n2-1) - элементы не проходим) for j in range(0, n1):# проход по элементам подаваемым на подсчет в свертку B[j]=A_[j][l:l+n2]# "урезаем" массив и берем нужные элементы # ++++++++++++ # умножаем и суммируем (требуется двойная сумма (по двум мерностям), # суммируются все нужные элементы (остается N чисел, # где N - количество картинок)) result[i][l]=sum(sum(syn_.dot(B)))# numpy.dot() - произведение массивов, в данном случае метод (syn_.dot(B)), есть "синоним" numpy.dot(syn_, B) # ++++++++++++ return result It is required to increase the speed of the code above, or to offer solutions to the problem (if I am not mistaken, the cycles in Python 3 are slow, replacements could not be found / invented).
The following is a detailed explanation of what is supplied to a function, what it does, approximately how it works:
The input to the function is a three-dimensional array (array_1) containing N pictures.
The picture has one parameter: pixel brightness (programmatically black-and-white picture, not RGB, etc.).
An array can contain any number of elements: N * (X * Y), where X and Y are the number of pixels in width / height, N is the number of pictures.
The array is transposed prior to submission to the function, for ease of calculation!
Arrays are converted to a string (the picture is converted to a string)!
Real array (partial), 5 pictures:
[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]] The zero element of the array [0]:
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.01176471 0.07058824 0.07058824 0.07058824 0.49411765 0.53333336 0.68627453 0.10196079 0.65098041 1. 0.96862745 0.49803922 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.11764706 0.14117648 0.36862746 0.60392159 0.66666669 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.88235295 0.67450982 0.99215686 0.94901961 0.7647059 0.25098041 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.19215687 0.93333334 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.98431373 0.36470589 0.32156864 0.32156864 0.21960784 0.15294118 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.07058824 0.85882354 0.99215686 0.99215686 0.99215686 0.99215686 0.99215686 0.7764706 0.71372551 0.96862745 0.94509804 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.3137255 0.61176473 0.41960785 0.99215686 0.99215686 0.80392158 0.04313726 0. 0.16862746 0.60392159 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.05490196 0.00392157 0.60392159 0.99215686 0.35294119 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.54509807 0.99215686 0.74509805 0.00784314 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.04313726 0.74509805 0.99215686 0.27450982 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.13725491 0.94509804 0.88235295 0.627451 0.42352942 0.00392157 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.31764707 0.94117647 0.99215686 0.99215686 0.46666667 0.09803922 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.17647059 0.72941178 0.99215686 0.99215686 0.58823532 0.10588235 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.0627451 0.36470589 0.98823529 0.99215686 0.73333335 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.97647059 0.99215686 0.97647059 0.25098041 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.18039216 0.50980395 0.71764708 0.99215686 0.99215686 0.81176472 0.00784314 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.15294118 0.58039218 0.89803922 0.99215686 0.99215686 0.99215686 0.98039216 0.71372551 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.09411765 0.44705883 0.86666667 0.99215686 0.99215686 0.99215686 0.99215686 0.78823531 0.30588236 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.09019608 0.25882354 0.83529413 0.99215686 0.99215686 0.99215686 0.99215686 0.7764706 0.31764707 0.00784314 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.07058824 0.67058825 0.85882354 0.99215686 0.99215686 0.99215686 0.99215686 0.7647059 0.3137255 0.03529412 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.21568628 0.67450982 0.88627452 0.99215686 0.99215686 0.99215686 0.99215686 0.95686275 0.52156866 0.04313726 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.53333336 0.99215686 0.99215686 0.99215686 0.83137256 0.52941179 0.51764709 0.0627451 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] It is required to process the input array with another array (array_2) of size n * m (width / height).
Массив_2, пример: n=3, m=2 0 1 2 3 4 5 The processing algorithm, very briefly, for more details, see the following websites at the end of the question and in the program code (neural networks, a brief explanation of the convolution algorithm):
1 - Array_2 (M_2) is put in the beginning of Array_1 (M_1) (zero elements become in one position, after which all elements are multiplied, the results of works are added together)
N и M - размерности M_1 M_1[0][0]*M_2[0][0]+M_1[1][0]*M_2[1][0]+...+M_1[N-1][0]*M_2[N-1][0]+ +M_1[0][1]*M_2[0][1]+M_1[0][1]*M_2[0][1]+...+M_1[N-1][M-1]*M_2[N-1][M-1] 2 - The index shifts in M_2 by 1 "to the right" (+1), operation 1 is repeated.
M_1[0][0]*M_2[1][0]+M_1[1][0]*M_2[2][0]+...+M_1[N-1][0]*M_2[N][0]+ +M_1[0][1]*M_2[1][1]+M_1[0][1]*M_2[1][1]+...+M_1[N-1][M-1]*M_2[N][M-1] 3 - When reaching the end of the line in M_2, go to the next line.
4 - Operations 1, 2, 3 are repeated until the ends of the row and column M_2 are reached.
Links to the theory of neural networks of convolution:
1) ( https://habrahabr.ru/post/309508/ )
2) ( https://habrahabr.ru/company/wunderfund/blog/314872/ )
Thank you in advance!!!