将einsum计算转换为在Theano中使用的点积 [英] Convert einsum computation to dot product to be used in Theano

查看:75
本文介绍了将einsum计算转换为在Theano中使用的点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才了解np.einsum,很快就迷上了它.但是theano似乎没有等效的功能,因此我需要以某种方式将numpy代码转换为theano.如何在theano中编写以下计算?

I have just recently learned about np.einsum and quickly became addicted to it. But it seems that theano doesn't have an equivalent function so I need to convert my numpy code to theano somehow. How can I write the following computation in theano?

IX=np.einsum('ijk,lj->ilk',p1,KX)

推荐答案

您只需要重新排列轴即可使其工作:

You only need to rearrange your axes to get this to work:

>>> import numpy as np
>>> a = np.random.rand(3, 4, 5)
>>> b = np.random.rand(5, 6)
>>> np.allclose(np.einsum('ikj,jl->ikl', a, b), np.dot(a, b))

因此请牢记:

>>> a = np.random.rand(3, 5, 4)
>>> b = np.random.rand(6, 5)
>>> out_ein = np.einsum('ijk,lj->ilk', a, b)
>>> out_dot = np.transpose(np.dot(np.transpose(a, (0, 2, 1)),
...                               np.transpose(b, (1, 0))),
...                        (0, 2, 1))
>>> np.allclose(out_ein, out_dot)

这篇关于将einsum计算转换为在Theano中使用的点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆