MoonGrad is a lightweight, pure-MoonBit tensor computation and automatic differentiation (autograd) library. It brings dynamic computation graphs, broadcasting arithmetic, and neural network primitives directly to the MoonBit ecosystem, targeting Wasm, JavaScript, and Native backends.
🌟 Key Features
Pure MoonBit Implementation: 100% MoonBit code with zero external C FFI dependencies. Fully portable and ready to compile for Wasm-GC or Node.js.
Dynamic Autograd Engine: PyTorch-style automatic differentiation using dynamic computation graphs, topological sorting, and reverse-mode accumulation.
Broadcasting & Strided Tensors: Complete N-dimensional float tensor support with automatic shape alignment and strided memory access.
Operator Overloading: Native support for +, -, *, and / via MoonBit’s compiler traits, plus optimized matmul, transpose, and reshape functions.
Built-in NN Components:
Linear layer with Glorot/Xavier initialization.
Activations: ReLU, Sigmoid, and Tanh (analytically approximated with high-precision Taylor and Newton-Raphson methods).
Loss functions: MSELoss.
SGD Optimizer: Robust optimization loop with parameter grouping, weight update, and gradient resetting.
Fully Verified: Covered by analytical and numerical gradient checking to guarantee backpropagation correctness.
🚀 Quick Start
Run the built-in XOR MLP training program out-of-the-box:
# Run the demo on the JS target
moon run cmd/main --target js
📜 许可证 (License)
本项目采用 Apache License 2.0 授权协议。
This project is licensed under the Apache License 2.0.
MoonGrad: MoonBit 原生张量与自动微分(Autograd)库
English | 中文
中文
MoonGrad是一个完全用 MoonBit 语言实现的原生、轻量级、且极具扩展性的张量计算与自动微分(Autograd)库。它旨在为 MoonBit 生态提供深度的机器学习计算基础,可编译为 WebAssembly、JavaScript 以及 Native,实现全平台的高性能机器学习基础设施。🌟 核心特性
+、-、*、/运算符(实现Add、Sub、Mul、Div特性),并提供了matmul(矩阵乘法)、transpose(转置)及reshape(重塑形状)。Linear层(带有 Xavier 权重初始化和偏置项)。ReLU、Sigmoid、Tanh(基于 Taylor 级数与 Newton-Raphson 的高精度纯算法实现)。MSELoss。SGD优化器,支持权重更新及梯度清零。🚀 快速上手
你可以直接在克隆本仓库后运行内置的 XOR 多层感知机(MLP)训练演示:
在 3000 轮训练后,你将看到 Loss 收敛至接近
0.0005,且网络完美拟合了 XOR 的四个真值表:English
MoonGradis a lightweight, pure-MoonBit tensor computation and automatic differentiation (autograd) library. It brings dynamic computation graphs, broadcasting arithmetic, and neural network primitives directly to the MoonBit ecosystem, targeting Wasm, JavaScript, and Native backends.🌟 Key Features
+,-,*, and/via MoonBit’s compiler traits, plus optimizedmatmul,transpose, andreshapefunctions.Linearlayer with Glorot/Xavier initialization.ReLU,Sigmoid, andTanh(analytically approximated with high-precision Taylor and Newton-Raphson methods).MSELoss.🚀 Quick Start
Run the built-in XOR MLP training program out-of-the-box:
📜 许可证 (License)
本项目采用 Apache License 2.0 授权协议。 This project is licensed under the Apache License 2.0.