具有4D输入和4D权重的4D卷积Python

时间:2020-04-16 12:54:29

标签: python conv-neural-network

我正在尝试用python自己编写CNN(使用Tesorflow,Keras,PyTorch)。我在https://www.coursera.org/learn/convolutional-neural-networks/任教。 CNN为: In deep learning, a convolutional neural network (CNN, or ConvNet) is a class of deep neural networks, most commonly applied to analyzing visual imagery. They are also known as shift invariant or space invariant artificial neural networks (SIANN), based on their shared-weights architecture and translation invariance characteristics. They have applications in image and video recognition, recommender systems, image classification, medical image analysis, natural language processing, and financial time series. 我创建了zero_padding和single_conv_step。现在,我停止了卷积层前馈。我写的,但是没有任何向量化。如您所知,如果没有它,Conv2D层将计算得很晚。我已经测试了numpy.convolve,scipy.signals.fftconvolve,spice.ndimage.filters.convolve和...。但是他们没有帮助我。 我的函数(Conv2D)具有参数A_prev(Prev_Layer),W(weights),stride,padding。 A_prev.shape =(training_exam,height,width,channel) W.shape =(filter_size,filter_size,channel,filter_num) 他们将被卷积。 A_prev将使用参数填充零填充。 我需要算法的矢量化版本。

代码:

  def conv_forward(A_prev,W,b,stride,pad):
    (m, n_Hp, n_Wp, n_Cp) = A_prev.shape
    (f,f,n_Cp,n_C) = W.shape
    n_H = int((n_Hp - f + 2*pad)/stride+1)
    n_W = int((n_Wp - f + 2*pad)/stride+1)
    Z = np.zeros((m,n_H,n_W,n_C))
    A_prev_pad = zero_pad(A_prev,pad)
    for i in range(m):
      a_prev_pad = A_prev_pad[i]
      for h in range(n_H):
        vert_start = stride*h
        vert_end = vert_start+f
        for w in range(n_W):
          hori_start = stride*w
          hori_end = hori_start+f
          for c in range(n_C):
            a_slice_prev = a_prev_pad[vert_start:vert_end,hori_start:hori_end,:]
            we=W[:,:,:,c]
            bi=b[:,:,:,c]
            Z[i,h,w,c] = conv_simple_step(a_slice_prev,we,bi)
    cache=(Z,W,b,stride,pad)
    return Z,cache       
def zero_pad(X,pad):
  x_pad = np.pad(X,((0,0),(pad,pad),(pad,pad),(0,0)),mode='constant',constant_values=(0,0))
  return x_pad

def conv_simple_step(a_prev,W,b):
  s = np.multiply(a_prev,W)
  Z = s.sum()
  Z = Z+ float(b)
  return Z

所以,总的来说,我的问题是如何将上述代码转换为矢量化版本(仅使用numpy函数,因为它们的分叉非常快)

0 个答案:

没有答案