本文介绍了生成np.einsum评估图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划向同事讲授np.einsum,希望展示如何将其简化为乘法和求和.因此,我考虑使用字母字符代替数字数据.在数组中.

I was planning to teach np.einsum to colleagues, by hoping to show how it would be reduced to multiplications and summations.So, instead of numerical data, I thought to use alphabet chars. in the arrays.

说,我们将A(2X2)设置为[['a','b'],['c','d']],将B(2X1)设置为[['e'],['f' ]]我们可以使用einsum来创建矩阵C,例如:np.einsum('ab , bc -> ac', A, B).

Say, we have A (2X2) as [['a', 'b'], ['c', 'd']] and B (2X1) as [['e'], ['f']]We could use einsum to create a matrix C, say like: np.einsum('ab , bc -> ac', A, B).

我想看到的是:它返回计算图:诸如:a * c + ...等.

What I'd like to see is: it return the computation graph: something like: a*c + ..., etc.

当然,np.einsum需要数字数据,如果运行上述代码,则会给出错误.

Ofcourse, np.einsum expects numerical data and would give an error if given the above code to run.

推荐答案

tensordot的示例使用字符串作为其数组之一,同时利用了'a'*3 => 'aaa'的事实.但是einsum不能对字符串做任何事情(这是编译后的代码问题).

tensordot has an example using strings for one of its arrays, taking advantage of the fact that 'a'*3 => 'aaa'. But einsum can't do anything with strings (tha'ts a compiled code issue).

有时候,我写了一个类似Python的纯书,它解析'ij,jk->'字符串,并设置适当的乘积和.这包括额外的调试输出.那可能是您任务的起点.

Sometime ago I wrote a pure-python work alike, that parses the 'ij,jk->' string, and sets up the appropriate sum-of-products calculation. That includes extra debugging output. That might serve as a starting point for your task.

https://github.com/hpaulj/numpy-einsum

最新的einsum进行了一些优化,并提供了一些调试帮助. np.einsum_path对此提供了更多信息.

The latest einsum does some optimization, with some debugging help. np.einsum_path provides more information on that.

了解NumPy的einsum

https://en.wikipedia.org/wiki/Einstein_notation

这篇关于生成np.einsum评估图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 12:40