[英]How can I implement locally connected layer in pure Numpy
I would like to build a locally connected weight matrix that represents a locally connected neural network in pure python/numpy without deep learning frameworks like Torch or TensorFlow. 我想构建一个本地连接的权重矩阵,该矩阵表示纯python / numpy中的本地连接的神经网络,而无需诸如Torch或TensorFlow之类的深度学习框架。
The weight matrix is a non-square 2D matrix with the dimension (number_input, number_output). 权重矩阵是尺寸为(number_input,number_output)的非正方形2D矩阵。 (an autoencoder in my case; input>hidden) (在我的情况下为自动编码器;输入>隐藏)
So the function I would like to build, take the matrix dimension and the size of the receptive field (number of local connection) and give the associated weight matrix. 因此,我要构建的函数采用矩阵尺寸和接收场的大小(本地连接数),并给出关联的权重矩阵。 I've already create a function like this, but for an input size of 8 and an output size of 4 (and RF = 4) my function output : 我已经创建了一个像这样的函数,但是对于8的输入大小和4的输出大小(RF = 4),我的函数输出是:
[[ 0.91822845 0. 0. 0. ]
[-0.24264655 -0.54754138 0. 0. ]
[ 0.55617366 0.12832513 -0.28733965 0. ]
[ 0.27993286 -0.33150324 0.06994107 0.61184121]
[ 0. 0.04286912 -0.20974503 -0.37633903]
[ 0. 0. -0.10386762 0.33553009]
[ 0. 0. 0. 0.09562682]
[ 0. 0. 0. 0. ]]
but I would like : 但我想:
[[ 0.91822845 0. 0. 0. ]
[-0.24264655 -0.54754138 0. 0. ]
[ 0.55617366 0.12832513 0. 0. ]
[ 0 -0.33150324 0.06994107 0 ]
[ 0. 0.04286912 -0.20974503 0. ]
[ 0. 0. -0.10386762 0.33553009]
[ 0. 0. 0.11581854 0.09562682]
[ 0. 0. 0. 0.03448418]]
Here's my python code : 这是我的python代码:
import numpy as np
def local_weight(input_size, output_size, RF):
input_range = 1.0 / input_size ** (1/2)
w = np.zeros((input_size, output_size))
for i in range(0, RF):
for j in range(0, output_size):
w[j+i, j] = np.random.normal(loc=0, scale=input_range, size=1)
return w
print(local_weight(8, 4, 4))
I look forward for your response! 期待您的回复!
The trick is in a small pad to work more comfortably (or control the limits). 诀窍是在一块小垫子上工作更舒适(或控制极限)。
Then you must define the step you will take with respect to the input (it is not more than the input / output). 然后,您必须定义相对于输入要采取的步骤(不超过输入/输出)。 Once this is done you just have to fill in the gaps and then remove the pad. 完成此操作后,您只需要填充间隙,然后卸下垫片即可。
import math
import numpy as np
def local_weight(input_size, output_size, RF):
input_range = 1.0 / input_size ** (1/2)
padding = ((RF - 1) // 2)
w = np.zeros(shape=(input_size + 2*padding, output_size))
step = float(w.shape[0] - RF) / (output_size - 1)
for i in range(output_size):
j = int(math.ceil(i * step))
j_next = j + RF
w[j:j_next, i] = np.random.normal(loc=0, scale=input_range, size=(j_next - j))
return w[padding:-padding, :]
I hope that is what you are looking for. 我希望这就是您想要的。
EDIT: I think the implementation was misguided. 编辑:我认为实施是错误的。 I reimplement the function, we go by parts. 我重新实现该功能,我们逐个进行。
I calculate the radius of the receptive field (padding). 我计算接收场的半径(填充)。
Determine the size of the W. 确定W的大小。
I calculate the step by removing the padding area so that I always stay inside. 我通过删除填充区域来计算步长,以便始终呆在里面。
I calculate the weights. 我计算权重。
Remove the padding. 除去填充物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.