机器学习手撕代码(6)人工神经网络

本文介绍了如何从零开始构建一个简单的神经网络模型,包括线性层、ReLU激活函数、交叉熵损失函数和随机梯度下降优化器。通过Python代码详细展示了每个模块的实现,并给出了一个用于训练和评估的示例。此外,还提供了数据加载、可视化工具和性能指标的计算方法。

机器学习手撕代码(6)人工神经网络

  • 本篇分享一下人工神经网络的代码,ann.py为人工神经网络模型的代码,nn.py为手写实现的神经网络框架代码,utils.py中为可视化结果的工具。
  • dataset见本系列第0篇。

nn.py

import numpy as np


class Module:
    def __init__(self):
        pass
    def __call__(self, *args, **kwargs):
        pass


class Linear(Module):
    def __init__(self,in_planes,out_planes):
        super(Linear, self).__init__()
        self.w = np.random.randn(in_planes+1,out_planes)
        self.grad = None
    def __call__(self,x):
        x = np.concatenate([x, np.ones((x.shape[0], 1))], axis=1)
        self.grad = x
        res = np.dot(x,self.w)
        return res


class CrossEntropyLoss(Module):
    def __init__(self):
        super(CrossEntropyLoss, self).__init__()
        self.grad = None

    def __call__(self, pred, target,eps=1e-8):
        exps = np.exp(pred-np.max(pred,axis=1).reshape(pred.shape[0],1))
        logits = exps / (np.sum(exps,axis=1).reshape(exps.shape[0],1)+eps)
        loss = -np.sum(target*np.log(logits+eps),axis=1)
        self.grad = logits-target
        return loss.mean()

    def backward(self):
        return self.grad


class Relu(Module):
    def __init__(self):
        super(Relu, self).__init__()
        self.grad = None
    def __call__(self,x):
        x[x<0] = 0.0
        self.grad = np.ones_like(x)
        self.grad[x<=0] = 0.0
        return x


class SGD:
    def __init__(self,model,lr):
        self.lr = lr
        self.models=[]
        for att in model.__dict__:
            t = model.__dict__[att]
            if t.__class__.__base__ == Module:
                self.models.append(t)
    def backward(self,loss_fn):
        bs = self.models[0].grad.shape[0]<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值