naive conv backward data impl with python
Untitled1
In [16]:
import numpy as np
np.set_printoptions(suppress=True)
# 1,1,10,10; 1,1,2,2; 1,10,10; 1,10,10; 1,1,9,9
In [17]:
N = 1
IC = 1
FH = FW = 2
OC = 1
OH = OW = 3
IH = IW = 4
RIH = RIW = IH
ROH = ROW = OH
In [26]:
filter = np.random.randn(OC, IC, FH, FW)
filter = np.ones((OC,IC,FH,FW))
print(filter.shape)
print(filter)
filter_list = np.reshape(filter, (-1))
filter_list
Out[26]:
In [44]:
diff = np.random.randn(N, OC, OH, OW)
diff = np.arange(1,10).reshape(N,OC,OH,OW)
print(diff.shape)
print(diff)
diff_list = np.reshape(diff, (-1))
diff_list
Out[44]:
In [20]:
rin = np.random.randint(0,2,(N, RIH, RIW))
print(rin.shape)
rin
Out[20]:
In [21]:
rout = np.random.randint(0,2,(N, ROH, ROW))
print(rout.shape)
rout
Out[21]:
In [55]:
grad = np.zeros((N, IC, IH, IW))
grad.shape
Out[55]:
In [56]:
# iterating through input make coding easier
# think of each grad elems accumulates what diff
for i in range(IH):
for j in range(IW):
for p in range(FH):
for q in range(FW):
# some elems in input may not interact with every filter elems.
topidx = i - p
leftidx = j - q
rightidx = leftidx+FW-1
bottomidx = topidx+FH-1
if (topidx >= 0 and leftidx >= 0 and bottomidx < IH and rightidx < IW): # a valid conv
if rin[0, i, j] == rout[0, topidx, leftidx]: # if mask not equals, the multiply results in 0.
# each input "may" iter through all filter elems
grad[0,0,i,j] += filter[0,0,p,q] * diff[0,0,topidx, leftidx]
print("grad[{}][{}] accumlate filter[{}][{}] and diff[{}][{}]".format(i,j,p,q,topidx,leftidx))
In [57]:
grad
Out[57]:
In [58]:
grad.reshape(-1)
Out[58]:
In [ ]:
In [ ]:
In [42]:
import numpy as np
np.set_printoptions(suppress=True)
# 1,2,4,4; 2,1,1,2,2; 1,2,3,3; 1,4,4; 1,3,3;
In [49]:
N = 1
IC = 1
FH = FW = 2
OC = 1
OH = OW = 1
IH = IW = 2
RIH = RIW = IH
ROH = ROW = OH
GROUP = 2
OCPG = 1
ICPG = 1
In [50]:
filter = np.random.randn(N, GROUP, FH, FW) # G,OCPG, ICPG, FH, FW
filter = np.ones((N, GROUP, FH, FW))
print(filter.shape)
print(filter)
filter_list = np.reshape(filter, (-1))
filter_list
Out[50]:
In [51]:
diff = np.random.randn(N, GROUP*OCPG, OH, OW)
diff = np.arange(1,N*GROUP*OCPG*OH*OW+1).reshape(N, GROUP*OCPG, OH, OW)
print(diff.shape)
print(diff)
diff_list = np.reshape(diff, (-1))
diff_list
Out[51]:
In [52]:
rin = np.random.randint(0,2,(N, RIH, RIW))
print(rin.shape)
rin
Out[52]:
In [53]:
rout = np.random.randint(0,2,(N, ROH, ROW))
print(rout.shape)
rout
Out[53]:
In [54]:
grad = np.zeros((N, GROUP*ICPG, IH, IW))
grad.shape
Out[54]:
In [55]:
# iterating through input make coding easier
# think of each grad elems accumulates what diff
for g in range(GROUP):
for i in range(IH):
for j in range(IW):
for p in range(FH):
for q in range(FW):
# some elems in input may not interact with every filter elems.
topidx = i - p
leftidx = j - q
rightidx = leftidx+FW-1
bottomidx = topidx+FH-1
if (topidx >= 0 and leftidx >= 0 and bottomidx < IH and rightidx < IW): # a valid conv
if rin[0, i, j] == rout[0, topidx, leftidx]: # if mask not equals, the multiply results in 0.
# each input "may" iter through all filter elems
grad[0,g,i,j] += filter[0,g,p,q] * diff[0,g,topidx, leftidx]
print("in group: {}".format(g))
print("grad[{}][{}] accumlate filter[{}][{}] and diff[{}][{}]".format(i,j,p,q,topidx,leftidx))
In [56]:
grad
Out[56]:
In [57]:
grad.reshape(-1)
Out[57]:
In [ ]:
In [ ]:
本文来自博客园,作者:ijpq,转载请注明原文链接:https://www.cnblogs.com/ijpq/p/16571530.html