# 理解多层CNN中转置卷积的反向传播（附代码）

【导读】转置卷积一直不太好理解，今天我们通过详细的推导示例及代码了解简单的两层CNN中转置卷积的反向传播。

“由于我们在对4 * 4图像执行卷积后得到2 * 2的输出图像，因此在执行反向传播时，我们需要对2 * 2输出图像执行一些操作，以获得具有4 * 4的图像。”

##### （上图中红色权重的）反向传播

• 蓝色权重反向传播第1部分

• 蓝色权重反向传播第2部分

```import numpy as np,sys

# Func: Only for 2D convolution
from scipy.signal import convolve2d
from sklearn.utils import shuffle

# Func: For Back propagation on Max Pooling
from scipy.ndimage.filters import maximum_filter
import skimage.measure

np.random.seed(12314)

def ReLU(x):
mask  = (x >0) * 1.0
return mask * x
def d_ReLU(x):
mask  = (x >0) * 1.0

def tanh(x):
return np.tanh(x)
def d_tanh(x):
return 1 - np.tanh(x) ** 2

def arctan(x):
return np.arctan(x)
def d_arctan(x):
return 1 / ( 1 + x ** 2)

def log(x):
return 1 / (1 + np.exp(-1 * x))
def d_log(x):
return log(x) * ( 1 - log(x))

# 1. Prepare Data
num_epoch = 1000
learning_rate = 0.1
total_error = 0

x1 = np.array([
[0,0,0,-1],
[-1,0,-1,0],
[-1,0,-1,-1],
[1,0,-1,-1]
])

x2 = np.array([
[0,0,0,0],
[0,0,-1,0],
[0,0,0,0],
[1,0,0,-1]
])

x3 = np.array([
[0,0,0,-1],
[0,0,-1,0],
[-1,0,1,1],
[1,0,-1,1]
])

x4 = np.array([
[0,0,0,1],
[1,0,1,0],
[1,0,1,1],
[1,0,1,1]
])

image_label=np.array([
[-1.42889927219],
[-0.785398163397],
[0.0],
[1.46013910562]
])

image_matrix = np.array([x1,x2,x3,x4])

w1 = (np.random.randn(2,2) * 4.2 )-0.1
w2 = (np.random.randn(2,2)* 4.2)-0.1
w3 = (np.random.randn(4,1)* 4.2)-0.1

print('Prediction Before Training')
predictions = np.array([])
for image_index in range(len(image_matrix)):
current_image  = image_matrix[image_index]

l1 = convolve2d(current_image,w1,mode='valid')
l1A = tanh(l1)

l2 = convolve2d(l1A,w2,mode='valid')
l2A = arctan(l2)

l3IN = np.expand_dims(l2A.ravel(),0)

l3 = l3IN.dot(w3)
l3A = arctan(l3)

predictions = np.append(predictions,l3A)
print('---Groud Truth----')
print(image_label.T)
print('--Prediction-----')
print(predictions.T)
print('--Prediction Rounded-----')
print(np.round(predictions).T)
print("\n")

for iter in range(num_epoch):

for current_image_index in range(len(image_matrix)):

current_image = image_matrix[current_image_index]
current_image_label = image_label[current_image_index]

l1 = convolve2d(current_image,w1,mode='valid')
l1A = tanh(l1)

l2 = convolve2d(l1A,w2,mode='valid')
l2A = arctan(l2)

l3IN = np.expand_dims(l2A.ravel(),0)

l3 = l3IN.dot(w3)
l3A = arctan(l3)

cost = np.square(l3A - current_image_label).sum() * 0.5
total_error += cost

grad_3_part_1 = l3A - current_image_label

dot(w3.T),(2,2))

w1 = w1 - learning_rate * grad_1
w2 = w2 - learning_rate * grad_2
w3 = w3 - learning_rate * grad_3

#print('Current iter:  ', iter, ' current cost: ', cost, end='\r')
total_error = 0

print('\n\n')
print('Prediction After Training')
predictions = np.array([])
for image_index in range(len(image_matrix)):
current_image  = image_matrix[image_index]

l1 = convolve2d(current_image,w1,mode='valid')
l1A = tanh(l1)

l2 = convolve2d(l1A,w2,mode='valid')
l2A = arctan(l2)

l3IN = np.expand_dims(l2A.ravel(),0)

l3 = l3IN.dot(w3)
l3A = arctan(l3)

predictions = np.append(predictions,l3A)
print('---Groud Truth----')
print(image_label.T)
print('--Prediction-----')
print(predictions.T)
print('--Prediction Rounded-----')
print(np.round(predictions).T)
print("\n")```

https://towardsdatascience.com/only-numpy-understanding-back-propagation-for-transpose-convolution-in-multi-layer-cnn-with-c0a07d191981

-END-

0 条评论

## 相关文章

4934

2479

5458

2486

2420

1963

### 竞争型神经网络续1

1.竞争神经网络函数 1.1创建函数 1.1.1 newc函数 newc函数用于创建一个竞争层，这是一个旧版本的函数，现在用competlayer函数代替。函数...

40010

1061

4539

7696