Kotlin∇ is a type-safe automatic differentiation framework written in Kotlin. It allows users to express differentiable programs with higher-dimensional data structures and operators. We attempt to restrict syntactically valid constructions to those which are algebraically valid and can be checked at compile-time. By enforcing these constraints in the type system, it eliminates certain classes of runtime errors that may occur during the execution of a differentiable program. Due to type-inference, most type declarations may be safely omitted by the end-user. Kotlin∇ strives to be expressive, safe, and notationally similar to mathematics.
Inspired by Stalin∇, Autograd, DiffSharp, Myia, Nexus, Tangent, Lantern et al., Kotlin∇ attempts to port recent advancements in automatic differentiation (AD) to the Kotlin language. AD is useful for gradient descent and has a variety of applications in numerical optimization and machine learning. Our implementation adds a number of experimental ideas, including compile-time shape-safety, algebraic simplification and numerical stability checking with property-based testing. We aim to provide an algebraically-grounded implementation of AD for shape-safe tensor operations. Tensors in Kotlin∇ are represented as multidimensional arrays.
Kotlin∇ currently supports the following features:
Additionally, it aims to support:
All of these features are implemented without access to bytecode or special compiler tricks - just using higher-order functions and lambdas as shown in Lambda the Ultimate Backpropogator, embedded DSLs a la Lightweight Modular Staging, and ordinary generics. Please see below for a more detailed feature comparison.
Kotlin∇ is hosted on Maven Central. An example project is provided here.
dependencies { implementation("ai.hypergraph:kotlingrad:0.4.7") }
<dependency> <groupId>ai.hypergraph</groupId> <artifactId>kotlingrad</artifactId> <version>0.4.7</version> </dependency>
To access Kotlin∇'s notebook support, use the following line magic:
@file:DependsOn("ai.hypergraph:kotlingrad:0.4.7")
For more information, explore the tutorial.
Kotlin∇ operators are higher-order functions, which take at most two inputs and return a single output, all of which are functions with the same numerical type, and whose shape is denoted using superscript in the rightmost column below.
| Math | Infix <sup>†</sup> | Prefix | Postfix<sup>‡</sup> | Operator Type Signature |
|---|---|---|---|---|
| $$\mathbf{A}(\mathbf{B})$$<br>$$\mathbf{A}\circ\mathbf{B}$$ | a(b)<br>a of b | $$(\texttt{a}: ℝ^{τ}→ℝ^{π}, \texttt{b}: ℝ^{λ} → ℝ^{τ}) → (ℝ^{λ}→ℝ^{π})$$ | ||
| $$\mathbf{A}\pm\mathbf{B}$$ | a + b<br>a - b | plus(a, b)<br>minus(a, b) | $$(\texttt{a}: ℝ^{τ}→ℝ^{π}, \texttt{b}: ℝ^{λ} → ℝ^{π}) → (ℝ^{?}→ℝ^{π})$$ | |
| $$\mathbf{A}\mathbf{B}$$ | a * b<br>a.times(b) | times(a, b) | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×n}, \texttt{b}: ℝ^{λ}→ℝ^{n×p}) → (ℝ^{?}→ℝ^{m×p})$$ | |
| $$\frac{\mathbf{A}}{\mathbf{B}}$$<br>$$\mathbf{A}\mathbf{B}^{-1}$$ | a / b<br>a.div(b) | div(a, b) | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×n}, \texttt{b}: ℝ^{λ}→ℝ^{p×n}) → (ℝ^{?}→ℝ^{m×p})$$ | |
| $$\pm\mathbf{A}$$ | -a<br>+a | a.neg()<br>a.pos() | $$(\texttt{a}: ℝ^{τ}→ℝ^{π}) → (ℝ^{τ}→ℝ^{π})$$ | |
| $$\sin{a}$$<br>$$\cos{a}$$<br>$$\tan{a}$$ | sin(a)<br>cos(a)<br>tan(a) | a.sin()<br>a.cos()<br>a.tan() | $$(\texttt{a}: ℝ→ℝ) → (ℝ→ℝ)$$ | |
| $$\ln{a}$$ | ln(a)<br>log(a) | a.ln()<br>a.log() | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×m}) → (ℝ^{τ}→ℝ^{m×m})$$ | |
| $$\log_{b}a$$ | a.log(b) | log(a, b) | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×m}, \texttt{b}: ℝ^{λ}→ℝ^{m×m}) → (ℝ^{?}→ℝ)$$ | |
| $$\mathbf{A}^b$$ | a.pow(b) | pow(a, b) | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×m}, \texttt{b}: ℝ^{λ}→ℝ) → (ℝ^{?}→ℝ^{m×m})$$ | |
| $$\sqrt{A}$$<br>$$\sqrt[3]{A}$$ | a.pow(1.0/2)<br>a.root(3) | sqrt(a)<br>cbrt(a) | a.sqrt()<br>a.cbrt() | $$(\texttt{a}: ℝ^{τ}→ℝ^{m×m}) → (ℝ^{τ}→ℝ^{m×m})$$ |
| $$\frac{da}{db},\frac{\partial{a}}{\partial{b}}$$ <br> $$D_b{a}$$ | a.d(b)<br>d(a) / d(b) | grad(a)[b] | $$(\texttt{a}: C(ℝ^{τ}→ℝ)^{*}, \texttt{b}: C(ℝ^{λ}→ℝ)) → (ℝ^{?}→ℝ)$$ | |
| $$\nabla{a}$$ | grad(a) | a.grad() | $$(\texttt{a}: C(ℝ^{τ}→ℝ)) → (ℝ^{τ}→ℝ^{τ})$$ | |
| $$\nabla_{\mathbf{B}}a$$ | a.d(b)<br>a.grad(b) | grad(a, b)<br>grad(a)[b] | $$(\texttt{a}: C(ℝ^{τ}→ ℝ^{π}), \texttt{b}: C(ℝ^{λ}→ℝ^{ω})) → (ℝ^{?}→ℝ^{π×ω})$$ | |
| $$\nabla\cdot{\mathbf{A}}$$ | divg(a) | a.divg() | $$(\texttt{a}: C(ℝ^{τ}→ℝ^{m})) → (ℝ^{τ}→ℝ)$$ | |
| $$\nabla\times{\mathbf{A}}$$ | curl(a) | a.curl() | $$(\texttt{a}: C(ℝ^{3}→ℝ^{3})) → (ℝ^{3}→ℝ^{3})$$ | |
| $$\mathcal{J}(\mathbf{A})$$ | grad(a) | a.grad() | $$(\texttt{a}: C(ℝ^{τ}→ℝ^{m})) → (ℝ^{τ}→ℝ^{m×τ})$$ | |
| $$\mathbf{H}(a)$$ | hess(a) | a.hess() | $$(\texttt{a}: C(ℝ^{τ}→ℝ)) → (ℝ^{τ}→ℝ^{τ×τ})$$ | |
| $$\Delta{a},\nabla^{2}a$$ | lapl(a) | a.lapl() | $$(\texttt{a}: C(ℝ^{τ}→ℝ)) → (ℝ^{τ}→ℝ^{τ})$$ |
ℝ can be a Double, Float or BigDecimal. Specialized operators are defined for subsets of ℝ, e.g., Int, Short or BigInteger for subsets of ℤ, however differentiation is only defined for continuously differentiable functions on ℝ.
<sup>†</sup> a and b are higher-order functions. These may be constants (e.g., 0, 1.0), variables (e.g., Var()) or expressions (e.g., x + 1, 2 * x + y).
<sup>‡</sup> For infix notation, . is optional. Parentheses are also optional depending on precedence.
<sup>§</sup> Matrix division is defined iff B is invertible, although it could be possible to redefine this operator using the Moore-Penrose inverse.
<sup>∗</sup> Where C(ℝ<sup>m</sup>) is the space of all continuous functions over ℝ. If the function is not over ℝ, it will fail at compile-time. If the function is over ℝ but not continuous differentiable at the point under consideration, it will fail at runtime.
<sup>?</sup> The input shape is tracked at runtime, but not at the type level. While it would be nice to infer a union type bound over the inputs of binary functions, it is likely impossible using the Kotlin type system without great effort. If the user desires type checking when invoking higher order functions with literal values, they will need to specify the combined input type explicitly or do so at runtime.
<sup>τ, λ, π, ω</sup> Arbitrary products.
Kotlin∇ supports derivatives between tensors of up to rank 2. The shape of a tensor derivative depends on (1) the shape of the function under differentiation and (2) the shape of the variable with respect to which we are differentiating.
| I/O Shape | $$ℝ^{?}→ℝ$$ | $$ℝ^{?}→ℝ^{m}$$ | $$ℝ^{?}→ℝ^{j×k}$$ |
|---|---|---|---|
| $$ℝ^{?}→ℝ$$ | $$ℝ^{?}→ℝ$$ | $$ℝ^{?}→ℝ^{m}$$ | $$ℝ^{?}→ℝ^{j×k}$$ |
| $$ℝ^{?}→ℝ^{n}$$ | $$ℝ^{?}→ℝ^{n}$$ |


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无 水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号