python - 如何使用 nupmy.as_strided
问题描述
我正在创建自己的神经网络库,现在正在创建卷积算法。我正在尝试将输入部分分配给本地感受野,然后将其乘以各自的权重,将相乘的感受野相加,添加偏差并返回结果。我正在使用“numpy.lib.stride_tricks.as_strided”函数,但我不确定如何设置步幅。我将衷心感谢您的帮助。这是代码:
"""
the matrix shape is:
mini_batch_size X
numberOfFilters X
numberOfInputFeatureMaps X
sizeOfInputImage[0] X
sizeOfInputImage[1]
the kernel shape is:
mini_batch_size X
numberOfFilters X
sizeOfLocalReceptiveField[0] X
sizeOfLocalReceptiveField[1]
"""
s1, s2, s3, s4, s5 = matrix.strides
imageWidth, imageHeight = matrix.shape[-2:]
localReceptiveFieldWidth, localReceptiveFieldHeight = kernel.shape[-2:]
matrix_shape = matrix.shape
numberOfLocalReceptiveFields =
(1 + (imageWidth - localReceptiveFieldWidth) // stride) *
(1 + (imageHeight - localReceptiveFieldHeight) // stride) *
matrix_shape[1]
view_shape = (
matrix_shape[0], # mini batch size
matrix_shape[1], # number of filters
numberOfLocalReceptiveFields,
localReceptiveFieldWidth,
localReceptiveFieldHeight
)
strides = () # don't know what to put here
subs = numpy.lib.stride_tricks.as_strided(matrix, view_shape, strides=strides)
解决方案
嘿,如果有人有兴趣,我设法以这种方式创建了一个算法:
@staticmethod
def getOutputImageDims(imageWidth, imageHeight, lrfWidth, lrfHeight, stride):
return [
(1 + (imageWidth - lrfWidth) // stride),
(1 + (imageHeight - lrfHeight) // stride)
]
@staticmethod
def getNumberOfLocalReceptiveFields(imageWidth, imageHeight, lrfWidth, lrfHeight, stride,
numberOfInputFeatureMaps, numberOfFilters):
return _MathHelper.getOutputImageDims(imageWidth, imageHeight, lrfWidth, lrfHeight, stride) * \
numberOfFilters * numberOfInputFeatureMaps
@staticmethod
def getLocalReceptiveFields(
matrix,
stride,
outputImageWidth,
outputImageHeight,
lrfWidth,
lrfHeight
):
s0, s1 = matrix.strides[-2:]
matrix_dims = np.ndim(matrix)
view_shape = matrix.shape[:2-matrix_dims] + (outputImageWidth, outputImageHeight, lrfWidth, lrfHeight)
strides = matrix.strides[:2-matrix_dims] + (stride * s0, stride * s1, s0, s1)
return np.lib.stride_tricks.as_strided(matrix, view_shape, strides=strides)
@staticmethod
def conv5D(matrix, kernel, stride=1):
# needed variables
imageWidth, imageHeight = matrix.shape[-2:]
localReceptiveFieldWidth, localReceptiveFieldHeight = kernel.shape[-2:]
x, y = _MathHelper.getOutputImageDims(imageWidth, imageHeight,
localReceptiveFieldWidth, localReceptiveFieldHeight,
stride)
# wraps the kernel in a [] and then duplicates the array
kernel = np.repeat(kernel[:, :, None, :, :], x, axis=2)
kernel = np.repeat(kernel[:, :, :, None, :, :], y, axis=3)
subs = _MathHelper.getLocalReceptiveFields(matrix, stride, x, y,
localReceptiveFieldWidth, localReceptiveFieldHeight)
# multipling the kernel in the local receptive fields and summing up
arr = subs * kernel
conv = np.sum(subs * kernel, axis=(4, 5))
return conv
推荐阅读
- javascript - 使用 Flask 和 Jquery 重新提交表单数据
- android - 如何在有根的 Android 7.1 手机中编辑 GPS 位置
- c - 如何用指针在C中构造链表,不断出错
- html - 如何以交替样式对齐图像和文本
- entity-framework - AutoMapper EF6 ProjectToList
“找不到方法” - c# - 如何通过 ASP.Net Core WebApi 流式传输网络摄像头
- html - 为什么这个 SVG 被裁剪而不是按比例缩小(仅在 React 中)
- php - 为什么我的所有评论都显示在所有帖子上?WordPress
- docker - 在 Docker 容器中添加用户,运行 Jenkins 作业时 UID 不匹配
- sql - 雪花 - 计算逗号分隔列表中的不同值