Numpy实现深度学习Model,2024年腾讯Python高级面试题及答案

文章介绍了如何在Python中构建神经网络,包括添加层、训练和测试过程,以及损失函数计算、权重初始化和参数更新。还提到了如何使用`fit`函数进行多轮训练,并通过`_forward_pass`和`_backward_pass`实现前向传播和反向传播。
摘要由CSDN通过智能技术生成

layer.trainable = trainable

def add(self, layer):

“”" Method which adds a layer to the neural network “”"

If this is not the first layer added then set the input shape

to the output shape of the last added layer

if self.layers:

layer.set_input_shape(shape=self.layers[-1].output_shape())

If the layer has weights that needs to be initialized

if hasattr(layer, ‘initialize’):

layer.initialize(optimizer=self.optimizer)

Add layer to the network

self.layers.append(layer)

def test_on_batch(self, X, y):

“”" Evaluates the model over a single batch of samples “”"

y_pred = self._forward_pass(X, training=False)

loss = np.mean(self.loss_function.loss(y, y_pred))

acc = self.loss_function.acc(y, y_pred)

return loss, acc

def train_on_batch(self, X, y):

“”" Single gradient update over one batch of samples “”"

y_pred = self._forward_pass(X)

loss = np.mean(self.loss_function.loss(y, y_pred))

acc = self.loss_function.acc(y, y_pred)

Calculate the gradient of the loss function wrt y_pred

loss_grad = self.loss_function.gradient(y, y_pred)

Backpropagate. Update weights

self._backward_pass(loss_grad=loss_grad)

return loss, acc

def fit(self, X, y, n_epochs, batch_size):

“”" Trains the model for a fixed number of epochs “”"

for _ in self.progressbar(range(n_epochs)):

batch_error = []

for X_batch, y_batch in batch_iterator(X, y, batch_size=batch_size):

loss, _ = self.train_on_batch(X_batch, y_batch)

batch_error.append(loss)

self.errors[“training”].append(np.mean(batch_error))

if self.val_set is not None:

val_loss, _ = self.test_on_batch(self.val_set[“X”], self.val_set[“y”])

self.errors[“validation”].append(val_loss)

return self.errors[“training”], self.errors[“validation”]

def _forward_pass(self, X, training=True):

“”" Calculate the output of the NN “”"

layer_output = X

for layer in self.layers:

layer_output = layer.forward_pass(layer_output, training)

return layer_output

def _backward_pass(self, loss_grad):

“”" Propagate the gradient ‘backwards’ and update the weights in each layer “”"

for layer in reversed(self.layers):

loss_grad = layer.backward_pass(loss_grad)

def summary(self, name=“Model Summary”):

Print model name

print (AsciiTable([[name]]).table)

Network input shape (first layer’s input shape)

print (“Input Shape: %s” % str(self.layers[0].input_shape))

Iterate through network and get each layer’s configuration

table_data = [[“Layer Type”, “Parameters”, “Output Shape”]]

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
img

在这里插入图片描述

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

上涨和关注,礼尚往来总是要有的:**

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值