如何用python生成身份张量?

2024-06-16 12:08:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道np.eye,它生成单位矩阵。我是说单位矩阵是

In linear algebra, the identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.

我知道我们可以在Numpy中用np.identity(3)创建它。在

But, I would like to know how can I have an identity Tensor in python.

我想在张量乘法中使用单位张量。如下所示:

其中G = Er ×1 U1 ×2 U2 ...×MUM是变换张量,Er ∈Rr×r×...×r是一个恒等张量(对角线元素是1,所有其他条目都是0)。我需要生成identity tensor的代码。在

提前谢谢你。在


Tags: oroftheinnpunitmatrixidentity
2条回答

使用np.identity代替tf.eye

tf.eye(2)
# [[1., 0.],
#  [0., 1.]]

像这样?在

def nd_id(n, d):
    out = np.zeros( (n,) * d )
    out[ tuple([np.arange(n)] * d) ] = 1
    return out

测试

^{pr2}$

相关问题 更多 >