# neural_network_implementationsa_1 **Repository Path**: fy_group/neural_network_implementationsa_1 ## Basic Information - **Project Name**: neural_network_implementationsa_1 - **Description**: 本项目提供了从零开始实现的基础神经网络组件,包括CNN(卷积神经网络)和RNN(循环神经网络)等架构。通过这个项目,你可以深入理解神经网络的工作原理。 - **Primary Language**: Python - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-11-01 - **Last Updated**: 2025-11-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 神经网络的从零开始实现 🧠 [![Python](https://img.shields.io/badge/Python-3.6%2B-blue.svg)](https://www.python.org/) [![NumPy](https://img.shields.io/badge/NumPy-1.19%2B-orange.svg)](https://numpy.org/) [![Matplotlib](https://img.shields.io/badge/Matplotlib-3.3%2B-green.svg)](https://matplotlib.org/) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) ## 中文版 | [English Version](#english-version) 本项目提供了从零开始实现的基础神经网络组件,包括CNN(卷积神经网络)和RNN(循环神经网络)等架构。通过这个项目,你可以深入理解神经网络的工作原理。 ## 项目结构 🗂️ ``` neural_network_implementations/ ├── base_nn.py # 基础神经网络组件 ├── cnn.py # CNN相关层 ├── rnn.py # RNN相关层 ├── model.py # 神经网络模型类和可视化工具 ├── examples.py # 使用示例 └── README.md # 项目说明 ``` ## 主要组件 🔧 ### 基础组件 (base_nn.py) - **Layer**: 神经网络层的基类 - **Activation**: 激活函数的基类 - **Loss**: 损失函数的基类 - **Dense**: 全连接层 - **Flatten**: 展平层 - **Dropout**: Dropout层 - **ReLU, Sigmoid, Tanh**: 激活函数 - **MSE, CrossEntropy**: 损失函数 ### CNN组件 (cnn.py) - **Conv2D**: 2D卷积层 - **MaxPool2D**: 2D最大池化层 - **AvgPool2D**: 2D平均池化层 ### RNN组件 (rnn.py) - **RNN**: 简单RNN层 - **LSTM**: LSTM层 - **GRU**: GRU层 ### 模型类和可视化工具 (model.py) - **NeuralNetwork**: 神经网络模型类,提供训练、预测等功能 - **CNNVisualizer**: CNN可视化工具,用于展示卷积核和特征图 - **RNNVisualizer**: RNN可视化工具,用于展示隐藏状态和序列预测结果 ## 安装依赖 📦 ```bash pip install numpy matplotlib ``` ## 使用示例 🚀 ### 运行所有示例 ```python python examples.py ``` ### 单独运行CNN示例 ```python from examples import example_cnn_mnist cnn_model = example_cnn_mnist() ``` ### 单独运行RNN示例 ```python from examples import example_rnn_sequence rnn_model = example_rnn_sequence() ``` ### 单独运行LSTM示例 ```python from examples import example_lstm_text lstm_model = example_lstm_text() ``` ### 单独运行GRU示例 ```python from examples import example_gru_time_series gru_model = example_gru_time_series() ``` ## 创建自定义模型 🛠️ ### 创建CNN模型 ```python from base_nn import Dense, Flatten, ReLU, Sigmoid, CrossEntropy from cnn import Conv2D, MaxPool2D from model import NeuralNetwork # 创建模型 model = NeuralNetwork() model.add(Conv2D(in_channels=1, out_channels=16, kernel_size=3, padding=1)) model.add(ReLU()) model.add(MaxPool2D(pool_size=2, stride=2)) model.add(Conv2D(in_channels=16, out_channels=32, kernel_size=3, padding=1)) model.add(ReLU()) model.add(MaxPool2D(pool_size=2, stride=2)) model.add(Flatten()) model.add(Dense(input_size=32*7*7, output_size=128)) model.add(ReLU()) model.add(Dense(input_size=128, output_size=10)) model.add(Sigmoid()) # 设置损失函数 model.set_loss(CrossEntropy()) # 训练模型 # model.train(x_train, y_train, epochs=10, batch_size=32, learning_rate=0.001) ``` ### 创建RNN模型 ```python from base_nn import Tanh, MSE from rnn import RNN from model import NeuralNetwork # 创建模型 model = NeuralNetwork() model.add(RNN(input_size=2, hidden_size=32, output_size=2)) model.add(Tanh()) # 设置损失函数 model.set_loss(MSE()) # 训练模型 # model.train(x_train, y_train, epochs=10, batch_size=32, learning_rate=0.001) ``` ## 可视化功能 📊 ### 可视化CNN卷积核 ```python from model import CNNVisualizer # 获取卷积层参数 conv_params = model.layers[0].get_params() CNNVisualizer.plot_filters(conv_params['kernel'], title="Convolutional Filters") ``` ### 可视化CNN特征图 ```python # 获取特征图 feature_maps = model.get_layer_outputs(x_sample, layer_indices=[1, 3]) CNNVisualizer.plot_feature_maps(feature_maps[0], title="Feature Maps") ``` ### 可视化RNN序列预测 ```python from model import RNNVisualizer # 获取预测结果 y_pred = model.predict(x_sample) RNNVisualizer.plot_sequence_prediction(y_true, y_pred, title="Sequence Prediction") ``` ### 绘制训练历史 ```python model.plot_training_history() ``` ## 保存和加载模型 💾 ```python # 保存模型 model.save_model('my_model.npz') # 加载模型 model.load_model('my_model.npz') ``` ## 注意事项 ⚠️ 1. 本项目是为了教育目的而设计的,实现了神经网络的基本原理,但可能不如优化过的库(如TensorFlow、PyTorch)高效。 2. 所有组件都使用NumPy实现,便于理解底层计算。 3. 示例中使用的是模拟数据,你可以替换为真实数据集进行训练。 4. 对于大规模数据集,建议使用批处理训练,并考虑使用GPU加速。 ## 扩展建议 🔮 1. 实现更多类型的层,如BatchNormalization、LayerNormalization等。 2. 添加更多优化器,如Adam、RMSprop等。 3. 实现更复杂的网络架构,如ResNet、Transformer等。 4. 添加更多可视化功能,如权重分布、梯度流动等。 5. 实现数据加载和预处理功能,支持真实数据集。 --- # English Version ## Neural Network Implementations from Scratch 🧠 [![Python](https://img.shields.io/badge/Python-3.6%2B-blue.svg)](https://www.python.org/) [![NumPy](https://img.shields.io/badge/NumPy-1.19%2B-orange.svg)](https://numpy.org/) [![Matplotlib](https://img.shields.io/badge/Matplotlib-3.3%2B-green.svg)](https://matplotlib.org/) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) This project provides basic neural network components implemented from scratch, including CNN (Convolutional Neural Network) and RNN (Recurrent Neural Network) architectures. Through this project, you can gain a deep understanding of how neural networks work. ## Project Structure 🗂️ ``` neural_network_implementations/ ├── base_nn.py # Basic neural network components ├── cnn.py # CNN related layers ├── rnn.py # RNN related layers ├── model.py # Neural network model class and visualization tools ├── examples.py # Usage examples └── README.md # Project description ``` ## Main Components 🔧 ### Basic Components (base_nn.py) - **Layer**: Base class for neural network layers - **Activation**: Base class for activation functions - **Loss**: Base class for loss functions - **Dense**: Fully connected layer - **Flatten**: Flatten layer - **Dropout**: Dropout layer - **ReLU, Sigmoid, Tanh**: Activation functions - **MSE, CrossEntropy**: Loss functions ### CNN Components (cnn.py) - **Conv2D**: 2D convolutional layer - **MaxPool2D**: 2D max pooling layer - **AvgPool2D**: 2D average pooling layer ### RNN Components (rnn.py) - **RNN**: Simple RNN layer - **LSTM**: LSTM layer - **GRU**: GRU layer ### Model Class and Visualization Tools (model.py) - **NeuralNetwork**: Neural network model class providing training, prediction, etc. - **CNNVisualizer**: CNN visualization tool for displaying convolutional kernels and feature maps - **RNNVisualizer**: RNN visualization tool for displaying hidden states and sequence prediction results ## Installation Requirements 📦 ```bash pip install numpy matplotlib ``` ## Usage Examples 🚀 ### Run All Examples ```python python examples.py ``` ### Run CNN Example Separately ```python from examples import example_cnn_mnist cnn_model = example_cnn_mnist() ``` ### Run RNN Example Separately ```python from examples import example_rnn_sequence rnn_model = example_rnn_sequence() ``` ### Run LSTM Example Separately ```python from examples import example_lstm_text lstm_model = example_lstm_text() ``` ### Run GRU Example Separately ```python from examples import example_gru_time_series gru_model = example_gru_time_series() ``` ## Creating Custom Models 🛠️ ### Create CNN Model ```python from base_nn import Dense, Flatten, ReLU, Sigmoid, CrossEntropy from cnn import Conv2D, MaxPool2D from model import NeuralNetwork # Create model model = NeuralNetwork() model.add(Conv2D(in_channels=1, out_channels=16, kernel_size=3, padding=1)) model.add(ReLU()) model.add(MaxPool2D(pool_size=2, stride=2)) model.add(Conv2D(in_channels=16, out_channels=32, kernel_size=3, padding=1)) model.add(ReLU()) model.add(MaxPool2D(pool_size=2, stride=2)) model.add(Flatten()) model.add(Dense(input_size=32*7*7, output_size=128)) model.add(ReLU()) model.add(Dense(input_size=128, output_size=10)) model.add(Sigmoid()) # Set loss function model.set_loss(CrossEntropy()) # Train model # model.train(x_train, y_train, epochs=10, batch_size=32, learning_rate=0.001) ``` ### Create RNN Model ```python from base_nn import Tanh, MSE from rnn import RNN from model import NeuralNetwork # Create model model = NeuralNetwork() model.add(RNN(input_size=2, hidden_size=32, output_size=2)) model.add(Tanh()) # Set loss function model.set_loss(MSE()) # Train model # model.train(x_train, y_train, epochs=10, batch_size=32, learning_rate=0.001) ``` ## Visualization Features 📊 ### Visualize CNN Convolutional Kernels ```python from model import CNNVisualizer # Get convolutional layer parameters conv_params = model.layers[0].get_params() CNNVisualizer.plot_filters(conv_params['kernel'], title="Convolutional Filters") ``` ### Visualize CNN Feature Maps ```python # Get feature maps feature_maps = model.get_layer_outputs(x_sample, layer_indices=[1, 3]) CNNVisualizer.plot_feature_maps(feature_maps[0], title="Feature Maps") ``` ### Visualize RNN Sequence Prediction ```python from model import RNNVisualizer # Get prediction results y_pred = model.predict(x_sample) RNNVisualizer.plot_sequence_prediction(y_true, y_pred, title="Sequence Prediction") ``` ### Plot Training History ```python model.plot_training_history() ``` ## Save and Load Models 💾 ```python # Save model model.save_model('my_model.npz') # Load model model.load_model('my_model.npz') ``` ## Important Notes ⚠️ 1. This project is designed for educational purposes, implementing the basic principles of neural networks, but may not be as efficient as optimized libraries (like TensorFlow, PyTorch). 2. All components are implemented using NumPy for easy understanding of underlying computations. 3. The examples use simulated data, you can replace them with real datasets for training. 4. For large-scale datasets, it is recommended to use batch training and consider GPU acceleration. ## Extension Suggestions 🔮 1. Implement more types of layers, such as BatchNormalization, LayerNormalization, etc. 2. Add more optimizers, such as Adam, RMSprop, etc. 3. Implement more complex network architectures, such as ResNet, Transformer, etc. 4. Add more visualization features, such as weight distribution, gradient flow, etc. 5. Implement data loading and preprocessing functions to support real datasets.