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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人