1

I have written this simple convolution function in numpy. But the final array values are all still zero. Please help me correct this function.

def convolve(a_prev, w, b):
    pad = 0
    stride = 1
    s1 = a_prev.shape
    s2 = w.shape
    f = s2[1]
    m = s1[0]
    n_c = s2[0]
    n_h = int((s1[1] - f + 2 * pad) / stride) + 1
    n_w = int((s1[2] - f + 2 * pad) / stride) + 1
    a = np.zeros((m,n_h,n_w,n_c), dtype=np.float32)

    for n in range(m):
        for z in range(n_c):
            y = 0
            x = 0
            while ((y+f) <= n_h):
                # Edit: forget to inialize the x = 0
                while ((x+f) <= n_w):  
                    #a[n,y,x,z] = np.sum(a_prev[n,y:y+f,x:x+f]*w[z]) + b[z,0]
                    a[n,y,x,z] = np.sum(np.multiply(a_prev[n,y:y+f,x:x+f],w[z])) + b[z,0]
                    x += stride
                y += stride
    
    print(a[0,85,:,3])
    return a

shape of a_prev is [num_exmamples,height, width, 3] and w is [num_filters,3,3,3]

I found the reason, why it was not working, i make a programming error and forget to initialize the x = 0 before while loop. Its working fine now. Below is the correct function.

def convolve(a_prev, kernel, b, pad = 0, stride = 1):
    m = a_prev.shape[0]
    prev_h = a_prev.shape[1]
    prev_w = a_prev.shape[2]
    f = kernel.shape[1]
    n_c = kernel.shape[0]
    new_h = int((prev_h - f + 2 * pad) / stride) + 1
    new_w = int((prev_w - f + 2 * pad) / stride) + 1
    az = np.zeros((m,new_h,new_w,n_c), dtype=np.float32)

    for n in range(m):
        for z in range(n_c):
            y = 0
            while (y+f) <= prev_h:
                x = 0
                while (x+f) <= prev_w:
                    az[n,y,x,z] = np.sum(a_prev[n,y:y+f,x:x+f]*kernel[z]) + b[z,0]
                    x += stride
                y += stride
    
    return az

3
  • And what output are you expecting? Commented Jan 29, 2021 at 18:36
  • something you expect like from convolution function of libraries like pytorch or tensorflow. Commented Jan 29, 2021 at 22:10
  • i mean the main question is, that values are being assigned to array 'a' but when I print some part of that array in the end it's all zeros. Commented Jan 29, 2021 at 22:16

1 Answer 1

2

There is an easier way to do convolution without using the previous input as a function input.

import numpy as np

def convolution(x, h):
# x and h are numpy arrays
M, N = np.size(x), np.size(h)
y = np.zeros(M+N-1)
# Initialise y with the length of the output signal
for m in np.arange(M):
    for n in np.arange(N):
        y[m+n] += x[m]*h[n]
return y

This function uses the basic definition of convolution for discrete signals

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering, but I am looking for convolution in neural networks over volumes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.