推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
bowenfu
V2EX  ›  Python

做了一个面向张量计算的语言,可从 Python 调用,支持显式索引和自动求导

  •  
  •   bowenfu · Apr 22 · 18399 views
    This topic created in 159 days ago, the information mentioned may be changed or developed.

    最近一直在做一个小项目Einlang( https://github.com/einlang/einlang ),出发点主要是希望代码本身像数学公式一样直观(但是要够精准无歧义,不需要动脑)。希望下面这几点

    • 张量怎么按索引算
    • 哪里要求和
    • 对谁求导

    都直接体现在代码里。比如平时常见写法可能是这样:

    np.einsum("ik,kj->ij", A, B)
    jax.grad(loss_fn)(W)
    

    在 Einlang 里可以这样写:

    let C[i, j] = sum[k](A[i, k] * B[k, j]);
    let dloss_dW = @loss / @W;
    

    语法上会有一点 Rust 的影子,比如 let 和分号。可以从 Python 调用,也可以直接走命令行。

    如果想试一下,可以执行

    
    python3 -m pip install "git+https://github.com/einlang/einlang.git"
    python3 -m einlang -c "let x = 1 + 1; print(x);"
    

    不知道这种实现在 AI 时代是否还有意义,希望能看到大家的点评和建议。

    除了矩阵运算和自动求导,Einlang 语法还支持递推。下面这段代码不需要先在外面写循环,再在循环里求导,而是可以直接把“每一步依赖前一步”的关系写出来:

    let alpha = 0.25;
    let x[0] = 8.0;
    
    let x[k in 1..6] = {
        let prev = x[k - 1];
        let loss = prev * prev;
        let g = @loss / @prev;
        prev - alpha * g
    };
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3288 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:28 · PVG 18:28 · LAX 03:28 · JFK 06:28
    ♥ Do have faith in what you're doing.