python - 在 tensorflow 中实现简单的线性代数运算

标签 python numpy tensorflow

给定一些具有列 x_1, ..., x_m 的 (n,m) 矩阵 X,我试图找到一个可以为我提供 3 模张量 [x_1 x_1^T, ..., x_m x_m ^T] 具有形状 (m, n,n) 或具有列 vec(x_1 x_1^T),...,vec(x_m x_m^T) 的 (n**2, m) 矩阵,其中 vec 是矩阵 x_i x_i^T。

换句话说,我试图概括

tf.tensordot(a,a,axes=0)

tf.tensordot(a,a,axes=0).reshape(-1,1)

从向量 a 到矩阵的列。有没有一种方法可以在不依赖循环的情况下完成此任务?

最佳答案

您可以通过以下方式做到这一点:

tf.expand_dims(a, 2) @ tf.expand_dims(a, 1)

或者使用tf.linalg.matmul如果您愿意,可以代替 @ 运算符。

关于python - 在 tensorflow 中实现简单的线性代数运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58837759/

相关文章:

python - Python readline()方法导致UnicodeDecodeError

python - 无法创建 Python 循环

python - 为什么我得到 'numpy.ndarray' 对象没有属性 'convert' ?

memory - 转置数组并实际重新排序内存

python - 如何在变量名中存储具有两个索引的数组?

json - tensorflow 服务 : Expects arg[0] to be float but string is provided

python - 异步 : Is it possible to release control of the event loop without using yield from?

python - 如何将 .value_counts() 用于数据框中的列表项

python - 值错误 : Cannot convert a partially known TensorShape to a Tensor: (? ,)

python - 在 TensorFlow 中提取子张量