我正试图通过自己实现反向传播。
假设我有一个简单的向量,整数如下:
x=[5,3,6,4]
我想要做的是创建矩阵,使用前一个向量或任何类似的向量。敌人的例子:
W1 = np.random.normal(loc=0.0, scale=1.0, size=(5,3))
W2 = np.random.normal(loc=0.0, scale=1.0, size=(3,6))
W3 = np.random.normal(loc=0.0, scale=1.0, size=(6,4))
我怎样才能做到这一点?我想主要的问题是,我无法在for循环中命名和声明所有这些矩阵。这样做最合适的方式是什么?
答案 0 :(得分:0)
您可以创建所需数组的字典:
x=[5,3,6,4]
weights = {'W'+str(i+1): np.random.normal(loc=0.0, scale=1.0, size=(x[i],x[i+1]))
for i in range(len(x)-1)}
然后,要使用它们,只需从该字典中调用它们即可。例如,如果您想使用W1
,请使用:
weights['W1']
返回你的数组:
array([[-1.22374486, 0.74080707, 0.00914048],
[ 0.08813797, 0.6807603 , 0.87249113],
[ 0.29799539, -0.78276957, 0.11789963],
[-0.25467395, -1.27457749, 0.96499082],
[-2.92436286, -0.8131938 , -2.2352768 ]])
您可以看到使用weights.keys()
请参阅this answer