神经网络的探秘:从基础到实战


前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。https://www.captainbed.cn/north

文章目录

    • 一、神经网络简介
      • [1.1 神经网络的基本结构](#1.1 神经网络的基本结构)
      • [1.2 神经元的工作原理](#1.2 神经元的工作原理)
    • 二、神经网络的训练过程
      • [2.1 前向传播](#2.1 前向传播)
      • [2.2 反向传播](#2.2 反向传播)
      • [2.3 参数更新](#2.3 参数更新)
    • 三、神经网络的类型
      • [3.1 全连接神经网络(Fully Connected Neural Network, FCNN)](#3.1 全连接神经网络(Fully Connected Neural Network, FCNN))
      • [3.2 卷积神经网络(Convolutional Neural Network, CNN)](#3.2 卷积神经网络(Convolutional Neural Network, CNN))
      • [3.3 循环神经网络(Recurrent Neural Network, RNN)](#3.3 循环神经网络(Recurrent Neural Network, RNN))
    • 四、神经网络的优化技巧
      • [4.1 正则化](#4.1 正则化)
      • [4.2 Dropout](#4.2 Dropout)
      • [4.3 批量归一化](#4.3 批量归一化)
    • 五、实战案例:手写数字识别
      • [5.1 数据集介绍](#5.1 数据集介绍)
      • [5.2 模型构建](#5.2 模型构建)
      • [5.3 模型训练](#5.3 模型训练)
      • [5.4 模型评估](#5.4 模型评估)
    • 六、总结

一、神经网络简介

神经网络是一种模拟人脑神经元工作方式的机器学习模型。它由多个层次组成,每一层包含多个神经元,这些神经元通过权重和偏置连接在一起。神经网络通过前向传播和反向传播来学习和优化模型参数。

1.1 神经网络的基本结构

神经网络通常由输入层、隐藏层和输出层组成。输入层接收原始数据,隐藏层进行特征提取和转换,输出层生成最终的预测结果。
输入层 隐藏层1 隐藏层2 输出层

1.2 神经元的工作原理

每个神经元接收来自前一层神经元的输入,通过加权求和并加上偏置,然后通过激活函数生成输出。

z = ∑ i = 1 n w i x i + b z = \sum_{i=1}^{n} w_i x_i + b z=i=1∑nwixi+b

a = σ ( z ) a = \sigma(z) a=σ(z)

其中, w i w_i wi 是权重, x i x_i xi 是输入, b b b 是偏置, σ \sigma σ 是激活函数。

二、神经网络的训练过程

神经网络的训练过程包括前向传播和反向传播两个阶段。

2.1 前向传播

前向传播是指数据从输入层经过隐藏层到输出层的过程。每一层的神经元通过加权求和和激活函数生成输出。

python 复制代码
import numpy as np

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def forward_propagation(X, weights, biases):
    layer_input = X
    for w, b in zip(weights, biases):
        z = np.dot(layer_input, w) + b
        layer_input = sigmoid(z)
    return layer_input

# 示例数据
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
weights = [np.array([[0.5, 0.5], [0.5, 0.5]]), np.array([[0.5], [0.5]])]
biases = [np.array([0.0, 0.0]), np.array([0.0])]

output = forward_propagation(X, weights, biases)
print(output)

2.2 反向传播

反向传播是指通过计算损失函数的梯度来更新模型参数的过程。反向传播使用链式法则来计算每一层的梯度。

python 复制代码
def sigmoid_derivative(x):
    return x * (1 - x)

def backward_propagation(X, y, weights, biases, output):
    m = X.shape[0]
    dZ = output - y
    dW = np.dot(X.T, dZ) / m
    db = np.sum(dZ, axis=0, keepdims=True) / m
    return dW, db

# 示例数据
y = np.array([[0], [1], [1], [0]])
dW, db = backward_propagation(X, y, weights, biases, output)
print(dW, db)

2.3 参数更新

通过梯度下降法更新模型参数:

python 复制代码
def update_parameters(weights, biases, dW, db, learning_rate):
    for i in range(len(weights)):
        weights[i] -= learning_rate * dW[i]
        biases[i] -= learning_rate * db[i]
    return weights, biases

# 示例数据
learning_rate = 0.1
weights, biases = update_parameters(weights, biases, dW, db, learning_rate)
print(weights, biases)

三、神经网络的类型

3.1 全连接神经网络(Fully Connected Neural Network, FCNN)

全连接神经网络是最基本的神经网络类型,每一层的神经元与下一层的所有神经元相连。
输入层 隐藏层1 隐藏层2 输出层

3.2 卷积神经网络(Convolutional Neural Network, CNN)

卷积神经网络主要用于图像处理任务,通过卷积层提取图像特征。

python 复制代码
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.summary()

3.3 循环神经网络(Recurrent Neural Network, RNN)

循环神经网络主要用于序列数据处理任务,如时间序列预测和自然语言处理。

python 复制代码
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.SimpleRNN(64, return_sequences=True, input_shape=(None, 28)),
    layers.SimpleRNN(64),
    layers.Dense(10, activation='softmax')
])

model.summary()

四、神经网络的优化技巧

4.1 正则化

正则化用于防止模型过拟合,常用的正则化方法包括L1正则化和L2正则化。

python 复制代码
from tensorflow.keras import regularizers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.01)),
    layers.Dense(10, activation='softmax')
])

4.2 Dropout

Dropout是一种随机丢弃神经元的技术,用于防止模型过拟合。

python 复制代码
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

4.3 批量归一化

批量归一化用于加速训练过程并提高模型性能。

python 复制代码
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(10, activation='softmax')
])

五、实战案例:手写数字识别

5.1 数据集介绍

使用MNIST数据集,包含60000张训练图像和10000张测试图像,每张图像大小为28x28像素。

5.2 模型构建

python 复制代码
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

5.3 模型训练

python 复制代码
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model.fit(x_train, y_train, epochs=5)

5.4 模型评估

python 复制代码
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"Test accuracy: {test_acc}")

六、总结

本文详细介绍了神经网络的基本结构、训练过程、常见类型和优化技巧,并通过一个实战案例展示了如何使用神经网络进行手写数字识别。希望本文能帮助读者深入理解神经网络的原理和应用,并在实际项目中灵活运用。


相关推荐
九年义务漏网鲨鱼2 小时前
【大模型学习 | MINIGPT-4原理】
人工智能·深度学习·学习·语言模型·多模态
元宇宙时间2 小时前
Playfun即将开启大型Web3线上活动,打造沉浸式GameFi体验生态
人工智能·去中心化·区块链
开发者工具分享2 小时前
文本音频违规识别工具排行榜(12选)
人工智能·音视频
产品经理独孤虾3 小时前
人工智能大模型如何助力电商产品经理打造高效的商品工业属性画像
人工智能·机器学习·ai·大模型·产品经理·商品画像·商品工业属性
老任与码3 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
蹦蹦跳跳真可爱5893 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij3 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien3 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
两棵雪松4 小时前
如何通过向量化技术比较两段文本是否相似?
人工智能
heart000_14 小时前
128K 长文本处理实战:腾讯混元 + 云函数 SCF 构建 PDF 摘要生成器
人工智能·自然语言处理·pdf