自编码实现浅层神经网络

该博客介绍了如何不依赖Python神经网络库,手动实现一个包含单隐藏层的浅层神经网络。博客详细阐述了神经网络的结构设定、参数初始化、前向传播、损失计算、反向传播以及梯度下降法的更新过程,并通过可视化数据分类展示了模型的效果,最终达到了97%的预测准确率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

不使用python神经网络相关库,自行编码实现浅层神经网络。其中隐藏层设置了4个unit。相关可参考Andrew NG 的深度学习课程。

1 - Packages¶

# Packages import
import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets

%matplotlib inline

2 - Dataset

def load_extra_datasets():  
    N = 200
    noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
    noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
    blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
    gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None)
    no_structure = np.random.rand(N, 2), np.random.rand(N, 2)
    
    return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
# Datasets
noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()

datasets = {
   
   "noisy_circles": noisy_circles, 
            "noisy_moons": noisy_moons,
            "blobs": blobs, 
            "gaussian_quantiles": gaussian_quantiles}
dataset = "gaussian_quantiles"
X, Y = datasets[dataset]
X = X.T;  Y = Y.reshape(1, Y.size);

# make blobs binary
if dataset == blobs:
    Y = Y%2
    
# Visualize the data
plt.scatter(X[0, :],X[1, :], c = Y.reshape(-1,), s = 40, cmap = plt.cm.Spectral)

在这里插入图片描述

3 - Neural Network model(背景知识)

Logistic regression did not work well on the “flower dataset”. You are going to train a Neural Network with a single hidden layer.

Here is our model:
在这里插入图片描述

Mathematically:

For one example x(i)x^{(i)}x(i):
(1)z[1](i)=W[1]x(i)+b[1](i)z^{[1] (i)} = W^{[1]} x^{(i)} + b^{[1] (i)}\tag{1}z[1](i)=W[1]x(i)+b[1](i)(1)
(2)a[1](i)=tanh⁡(z[1](i))a^{[1] (i)} = \tanh(z^{[1] (i)})\tag{2}a[1](i)=tanh(z[1](i))(2)
(3)z[2](i)=W[2]a[1](i)+b[2](i)z^{[2] (i)} = W^{[2]} a^{[1] (i)} + b^{[2] (i)}\tag{3}z[2](i)=W[2]a[1](i)+b[2]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值