-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv.py
61 lines (56 loc) · 2.06 KB
/
conv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
sys.path.append("..")
import numpy as np
from scipy import signal
from bp.initer import Initer
class Conv2D:
def __init__(self,unit,kernal_size,input_shape=None):
self._unit = unit
self._kernal_shape = kernal_size
self._input_shape = input_shape
self._setOutputShape()
def _setOutputShape(self):
if self._input_shape is not None:
channel = self._unit
height = self._input_shape[1]-self._kernal_shape[0]+1
width = self._input_shape[2]-self._kernal_shape[1]+1
self._output_shape = (channel,height,width)
self._weight_shape = (self._unit,self._input_shape[0])+self._kernal_shape
def _convolve(self,a,b):
return signal.correlate2d(a, b, mode='valid')
def set_input_shape(self,input_shape):
self._input_shape = input_shape
self._setOutputShape()
def get_output_shape(self):
return self._output_shape
def get_init_weight(self):
inDataNum = self._input_shape[0]*self._input_shape[1]*self._input_shape[2]
outDataNum = self._output_shape[0]*self._output_shape[1]*self._output_shape[2]
initer = Initer()
return initer.get(inDataNum,outDataNum,self._weight_shape)
def get_init_grade(self):
return np.zeros(self._weight_shape)
def get_output(self,weight,inData):
result = np.zeros(self._output_shape)
for i in range(0,len(weight)):
for j in range(0,len(weight[i])):
result[i] = result[i]+self._convolve(inData[j],weight[i,j])
return result
def get_loss(self,nextLoss,weight,inData,outData):
#填充0值
height = self._kernal_shape[0]
width = self._kernal_shape[1]
npad = ((0, 0), (width-1, width-1), (height-1, height-1))
nextLoss = np.pad(nextLoss, pad_width=npad, mode='constant', constant_values=0)
#计算
result = np.zeros(inData.shape)
for i in range(0,len(weight)):
for j in range(0,len(weight[i])):
result[j] = result[j]+self._convolve(nextLoss[i],weight[i,j])
return result
def get_grade(self,nextLoss,weight,inData,outData):
result = np.zeros(self._weight_shape)
for i in range(0,len(inData)):
for j in range(0,len(nextLoss)):
result[j,i]=self._convolve(inData[i],nextLoss[j])
return result