简体   繁体   English

如何在不使用 for 循环的情况下对这两个 numpy 操作进行矢量化?

[英]How do I vectorized these two numpy operations without using for loop?

there is an operation in numpy I've found hard to implement without looping numpy 中有一个操作我发现没有循环很难实现

That operation is that I have two inputs: beta,x该操作是我有两个输入: beta,x

beta.shape = (M,N,K) and x.shape = (I,K) beta.shape = (M,N,K)x.shape = (I,K)

The operation I'm interested in can be done using for loop as follows我感兴趣的操作可以使用for循环完成如下

result = np.zeros((M,N,I,K)) # buffer to save my operation results
for m in range(M):
    for n in range(N):
         beta_ = beta[m][n] # has shape (K,)
         result[m][n] = x * beta_

Let tricks here that I can do without loops so that the whole operation can be computational efficient?让我可以在没有循环的情况下做一些技巧,以便整个操作可以高效计算?

You're interested in multiplying the elements across the common K dimension, and keeping the results along the remaining dimensions.您有兴趣在公共 K 维度上乘以元素,并将结果保持在其余维度上。

This means you can use np.einsum using the dimensions of beta, x, and the shape result you're interested in like 'mnk,ik->mnik' :这意味着您可以使用np.einsum使用 beta、x 的尺寸以及您感兴趣的形状结果,例如'mnk,ik->mnik'

import numpy as np

M = 4
N = 3
I = 7
K = 6

beta = np.arange(M*N*K).reshape(M,N,K)
x = np.arange(I*K).reshape(I,K)

result1 = np.zeros((M,N,I,K)) # buffer to save my operation results
for m in range(M):
    for n in range(N):
         beta_ = beta[m][n] # has shape (K,)
         result1[m][n] = x * beta_


result2 = np.einsum('mnk,ik->mnik', beta, x)

print (np.array_equal(result1,result2))
 True

Not part of the question but while talking about np.einsum ... If you want to sum across any of these dimensions, you can omit it from the end dimensions:不是问题的一部分,但在谈论np.einsum ...如果您想对这些维度中的任何一个求和,您可以从最终维度中省略它:

import numpy as np

M = 4
N = 3
I = 7
K = 6

beta = np.arange(M*N*K).reshape(M,N,K)
x = np.arange(I*K).reshape(I,K)

result1 = np.zeros((M,N,I,K)) # buffer to save my operation results
for m in range(M):
    for n in range(N):
         beta_ = beta[m][n] # has shape (K,)
         result1[m][n] = x * beta_
result1 = result1.sum(axis=1)


result2 = np.einsum('mnk,ik->mik', beta, x)

print (np.array_equal(result1,result2))
 True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用for循环的情况下将向量化函数映射到numpy数组? - How can I map a vectorized function to a numpy array without using a for loop? numpy 的基本操作是否矢量化,即它们是否使用 SIMD 操作? - Are numpy's basic operations vectorized, i.e. do they use SIMD operations? 如何使用矢量化操作从 pandas dataframe 创建特定的嵌套格式 JSON 或字典? - How do I create a specific nesting format JSON or dictionary from a pandas dataframe using vectorized operations? NumPy 中的向量化运算 - Vectorized operations in NumPy 在 NumPy 中是否有一种矢量化的方法来进行类似矩阵乘法的运算? - Is there a vectorized way to do matrix multiplication-like operations in NumPy? 如何在返回常量值的lambda函数上使用矢量化NumPy操作? - How to use vectorized NumPy operations on lambda functions that return constant values? Numpy:单循环矢量化代码比两循环迭代慢 - Numpy: Single loop vectorized code slow compared to two loop iteration 在python中,如何使用循环将行添加/添加到numpy数组中而不删除前一行? - In python, how do I append/add, using a loop, a row to a numpy array without deleting the previous row? 我如何在不使用numpy中使用循环的情况下使用两个2维数组形成一个3维数组? - How do i use two 2 dimentional arrays to form a 3 dimentional one without using loops in numpy? 如何在不使用 numpy 或 zip 的情况下找到两个列表之间的欧几里德距离? - How do I find the euclidean distance between two lists without using numpy or zip?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM