A TypeScript implementation of a neural network using mini-batch gradient descent optimization, featuring customizable activation functions, regularization techniques, and model persistence. Mathematical Foundation
- Network Architecture
The neural network is structured with multiple layers, where each layer l performs the following transformation:
$z^{(l)} = W^{(l)}a^{(l-1)} + b^{(l)}$ Where:
- Activation Functions
The implementation supports multiple activation functions:
Sigmoid
$\sigma(x) = \frac{1}{1 + e^{-x}}$
Range:
ReLU (Rectified Linear Unit)
Range:
Softmax (for output layer)
Used for multi-class classification Outputs sum to 1, representing probabilities
- Loss Function and Regularization
Mean Squared Error (MSE)
$L = \frac{1}{n}\sum(y - \hat{y})^2$
Regularization Terms
L2 Regularization:
src/
├── ActivationFunction.ts
├── NeuralNetwork.ts
├── Regularization.ts
├── MiniBatch.ts
└── ModelStorage.ts
- ActivationFunction Class
export class ActivationFunction {
static sigmoid(x: number): number;
static relu(x: number): number;
static tanh(x: number): number;
static softmax(x: number[]): number[];
}- NeuralNetwork Class
export class NeuralNetwork {
static async compute_loss(data: NeuralNetworkParams, ...): number;
static async compute_grad_W(data: NeuralNetworkParams, ...): number;
static async compute_grad_B(data: NeuralNetworkParams, ...): number;
}- Clone the repository:
git clone https://github.com/your-username/neural-network-implementation.git
cd neural-network-implementation- Install dependencies:
pnpm install- Build the project:
pnpm run buildimport { NeuralNetwork, ActivationFunction, Regularization } from './src';
// Define training data
const input = [1.0, 2.0, 3.0, 4.0, 5.0];
const output = [1.5, 2.0, 3.1, 4.1, 5.6];
// Configure network
const networkConfig = {
layers: [
{ size: 2, activation: 'sigmoid' },
{ size: 1, activation: 'linear' }
],
learningRate: 0.01,
regularization: {
type: 'l2',
lambda: 0.01
}
};
// Train the model
const model = new NeuralNetwork(networkConfig);
await model.train(input, output, {
epochs: 1000,
batchSize: 32,
verbose: true
});// Save model
await ModelStorage.saveModel(model, 'trained_model.json');
// Load model
const loadedModel = await ModelStorage.loadModel('trained_model.json');The MiniBatch class handles data splitting and iteration:
const batchProcessor = new MiniBatch(input, output, 32);
batchProcessor.iterateBatches((batch) => {
// Process each mini-batch
});Implement custom regularization by extending the Regularization class:
class CustomRegularization extends Regularization {
static custom(weight: number, lambda: number): number {
// Custom regularization logic
return lambda * Math.pow(weight, 3);
}
}-
Batch Size Selection
- Larger batches: Better gradient estimates, more memory
- Smaller batches: Faster iterations, more noise
-
Learning Rate Tuning
- Too high: Unstable training
- Too low: Slow convergence
- Recommended: Start with 0.01 and adjust
-
Regularization Strength
- Increase λ to reduce overfitting
- Decrease λ if underfitting
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE.md file for details.