Numpy实现Conv2D

简介: Numpy实现Conv2D
The stride length of the filters during the convolution over the input.
“”"
def init(self, n_filters, filter_shape, input_shape=None, padding=‘same’, stride=1):
self.n_filters = n_filters
self.filter_shape = filter_shape
self.padding = padding
self.stride = stride
self.input_shape = input_shape
self.trainable = True
def initialize(self, optimizer):

Initialize the weights

filter_height, filter_width = self.filter_shape
channels = self.input_shape[0]
limit = 1 / math.sqrt(np.prod(self.filter_shape))
self.W = np.random.uniform(-limit, limit, size=(self.n_filters, channels, filter_height, filter_width))
self.w0 = np.zeros((self.n_filters, 1))

Weight optimizers

self.W_opt = copy.copy(optimizer)
self.w0_opt = copy.copy(optimizer)
def parameters(self):
return np.prod(self.W.shape) + np.prod(self.w0.shape)
def forward_pass(self, X, training=True):
batch_size, channels, height, width = X.shape
self.layer_input = X

Turn image shape into column shape

(enables dot product between input and weights)

self.X_col = image_to_column(X, self.filter_shape, stride=self.stride, output_shape=self.padding)

Turn weights into column shape

self.W_col = self.W.reshape((self.n_filters, -1))

Calculate output

output = self.W_col.dot(self.X_col) + self.w0

Reshape into (n_filters, out_height, out_width, batch_size)

output = output.reshape(self.output_shape() + (batch_size, ))

Redistribute axises so that batch size comes first

return output.transpose(3,0,1,2)
def backward_pass(self, accum_grad):

Reshape accumulated gradient into column shape

accum_grad = accum_grad.transpose(1, 2, 3, 0).reshape(self.n_filters, -1)
if self.trainable:

Take dot product between column shaped accum. gradient and column shape

layer input to determine the gradient at the layer with respect to layer weights

grad_w = accum_grad.dot(self.X_col.T).reshape(self.W.shape)

The gradient with respect to bias terms is the sum similarly to in Dense layer

grad_w0 = np.sum(accum_grad, axis=1, keepdims=True)

Update the layers weights

self.W = self.W_opt.update(self.W, grad_w)
self.w0 = self.w0_opt.update(self.w0, grad_w0)

Recalculate the gradient which will be propogated back to prev. layer

accum_grad = self.W_col.T.dot(accum_grad)

Reshape from column shape to image shape

accum_grad = column_to_image(accum_grad,
self.layer_input.shape,
self.filter_shape,
stride=self.stride,
output_shape=self.padding)

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


相关文章
|
Dubbo Java 应用服务中间件
深入理解Dubbo-5.服务注册源码分析(上)
深入理解Dubbo-5.服务注册源码分析
276 0
|
存储 算法 Java
ArrayList vs. LinkedList:数据结构之争
ArrayList vs. LinkedList:数据结构之争
239 0
|
缓存 安全 前端开发
【微服务架构】微服务不是魔术:处理超时
【微服务架构】微服务不是魔术:处理超时
|
存储 SQL 算法
MySQL InnoDB MVCC机制
MySQL InnoDB MVCC机制
425 0
|
11天前
|
人工智能 安全 Linux
【OpenClaw保姆级图文教程】阿里云/本地部署集成模型Ollama/Qwen3.5/百炼 API 步骤流程及避坑指南
2026年,AI代理工具的部署逻辑已从“单一云端依赖”转向“云端+本地双轨模式”。OpenClaw(曾用名Clawdbot)作为开源AI代理框架,既支持对接阿里云百炼等云端免费API,也能通过Ollama部署本地大模型,完美解决两类核心需求:一是担心云端API泄露核心数据的隐私安全诉求;二是频繁调用导致token消耗过高的成本控制需求。
5606 14
|
19天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
22215 118